It is currently Mon Oct 15, 2018 9:04 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Wed Dec 20, 2017 3:02 pm 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 317
Location: Colorado USA
Another question, what's a handle? What benefits does it have from using a normal pointer or index?
of course, not a handle on a drawer, I'm talking about stuff like a handle to a palette or whatever


Last edited by DementedPurple on Wed Dec 20, 2017 3:12 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Wed Dec 20, 2017 3:12 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20656
Location: NE Indiana, USA (NTSC)
In theory, a handle lets the operating system's memory manager move the underlying data structure around in memory so that allocation and deallocation of data structures doesn't leave the heap fragmented. It's the same reason we refer to files by name* instead of disk sector number, so that data can still be located after the file system has moved it to another part of the same disk.

In classic Mac OS and its Apple II port GS/OS, a handle was a pointer to a data structure that was opaque other than that it began with a pointer to the actual data. In UNIX, MS-DOS 2 and later, and Windows, a file handle is an index into a kernel-managed list of open files or streams. A few file handles are predefined in UNIX: 0 is standard input, 1 is standard output, and 2 is standard error output.


* Or inode number on file systems that use that concept


Top
 Profile  
 
PostPosted: Wed Dec 20, 2017 3:16 pm 
Offline

Joined: Sun Mar 27, 2011 10:49 am
Posts: 265
Location: Seattle
That analogy doesn't sound right to me. Loosely, an object is a thing, like a specific folder, whereas a class is a kind of thing, as in the idea of a folder in general.

Are you familiar with the idea of structures in plain ol' C? Objects in C++ are very similar, but have more power of expression and abstraction. But in general you build a class whenever you want to express a new kind of idea in your program. If you were making a drawing program, you might have classes for points, lines, circles, and so on, because those are the things your program operates on. If you were designing a GUI library, you'd have classes for windows, buttons, and so on. In a game, you might have classes for maps, entities in the game world, images, and so on. And so on.

Your takeaway should be this: The idea is that you write your code in terms of the entities it deals with, rather than worrying about the underlying representation of those entities.

Also, don't confuse the WinAPI idea of a "window class" with a C++ class. Windows was originally designed to be coded against in plain C (and Pascal, I think). The idea is similar in that they both define a "kind" of thing, but they're totally different implementations of that idea.

A "handle" is just some way of referring to something. The point is that usually you don't care how that handle is represented: it could be a pointer, or it could be an index into a table, or a string that's looked up. One advantage of using a handle rather than just a pointer is that you can add indirection: you can move things around in memory without invalidating all your handles, but raw pointers would become invalid. Another advantage is that you could, say, expand from 16- to 32- to 64-bit pointers over the course of 30 years without anyone operating on your handles needing to make any changes.


Top
 Profile  
 
PostPosted: Wed Dec 20, 2017 3:18 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1705
The folder/file analogy is total nonsense.

The class is the type. The object is the variable.
That's it. That's the most straight-forward analogy.

If integer was a class and you declare int i;, then int would be the class and i your object.


The difference betwen classes and simple data types is this:


1. A class holds a whole collection of other data types. I.e. a class Person may hold two string variables FirstName and LastName and a date variable Birthdate.

In this way, a class is pretty similar to structs: A container that enables you to declare a whole bunch of predefined variables by simply writing the declaration of one variable.


2. A class can have functions. So, your object that holds values to all these variables can call functions of its class to manipulate its own variables or to output them or whatever.

I.e. a class called Player may have member variables for the x and y position. And it has the function MoveRight. This function checks whether the movement wouldn't end at a wall and then updates the x coordinate accordingly.

So, when you declare Player mario;, then you basically declare two integer variables put into one variable name called mario.
And when you call mario.MoveRight(), then, if you have programmed the function to do this, Mario's x variable would be incremented.


3. Classes have encapsulation: You can declare certain variables and functions as private, so that only the functions that belong to the class itself can call or update them.

For example, you don't want the programmer to call mario.X = 5 directly. So, you declare the x and y variable private and declare public class functions instead, so that the programmer has to call mario.SetXPosition(5) instead. This enables you to let the SetXPosition check the parameter first and to give an error if the value is invalid for your specific use.
This way, programmers who use your class cannot change the data arbitrarily, but only within the rules that you allow by writing functions for data manipulation.


4. Inheritance and polymorphism: Learn this when you have understood the basic properties of classes that I wrote about above.


About handles:

Handles are just a Microsoft way of calling pointers that point to Windows resources.
You create a window, so the pointer that references the window is called HWND (handle to a window).
You create a bitmap in memory, so the pointer that points to the bitmap is called HBITMAP.
It's just a naming convention.

That was probably done because you never actually work with the objects themselves.

I.e. declaring HBITMAP myBitmap doesn't mean that myBitmap[0] points to the first pixel and myBitmap[1] points to the next pixel.

The pointer points to something that you're not supposed to know exactly what it is. All you need to know is that it's somehow connected to a bitmap and that the WinAPI functions are able to use it for bitmap manipulation.
But you're not supposed to dereference the pointer and to directly access the contents of the memory location that the pointer points to.

That's why it's called a handle. Because PBITMAP (pointer to a bitmap) would mean that you can dereference the pointer and access the bitmap memory directly.

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Wed Dec 20, 2017 3:21 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20656
Location: NE Indiana, USA (NTSC)
If you want a better file system analogy for classes and objects: An object is to a file as a class is to a file format.


Top
 Profile  
 
PostPosted: Sun Dec 24, 2017 8:40 am 
Offline
User avatar

Joined: Tue Apr 05, 2016 5:25 pm
Posts: 186
If it's C++ GUI programming you're after, I would strongly recommend using Qt. It has a lot of useful infrastructure and has far less resource micromanagement, yet does more, and is more portable than, Win32/MFC. It's well-documented, and far easier to understand than anything on MSDN. It even has its own IDE, Qt Creator, which I've found to be quite a bit cleaner and easier to use than Visual Studio.

A quick tutorial I found: https://prognotes.net/2016/11/qt-5-hell ... t-creator/
There's also this Notepad tutorial in the official Qt documentation: http://doc.qt.io/qt-5/gettingstartedqt.html

_________________
SNES NTSC 2/1/3 1CHIP | serial number UN318588627


Top
 Profile  
 
PostPosted: Thu Dec 28, 2017 11:45 am 
Offline

Joined: Mon Jan 30, 2017 5:20 pm
Posts: 317
Location: Colorado USA
Yeah, but I can't use QT because considering I'm only 13 and I still make money by mowing my parents lawn, I simply don't have enough money to spend $295 dollars a month on a school project


Top
 Profile  
 
PostPosted: Thu Dec 28, 2017 11:47 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20656
Location: NE Indiana, USA (NTSC)
The GNU Lesser General Public License (GNU LGPL) version of Qt does not cost $295.


Top
 Profile  
 
PostPosted: Thu Dec 28, 2017 11:54 am 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1705
Is there anything specific you want to program in Windows?

For example, if your goal is to program a video game, then there are specific game libraries like SDL or SFML. They create the window for you and you don't need to deal with this at all and can start right away with the game-related stuff.

If you really need to program a GUI application for school, I would suggest not to use C++ and the WinAPI at all, but work with C# and WinForms: Much easier, less error-prone and a good GUI designer in even the most basic versions of Visual Studio. Also, C# is an overall nicer language than C++.

If it has to be C++, you can have a look at this:
https://en.wikipedia.org/wiki/List_of_p ... _libraries

Maybe wxWidgets or any other of the free libraries is something for you.
(But even Qt seems to have a free license, so maybe you don't need to pay for it at all in certain circumstances.)

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Tue Jan 30, 2018 11:51 am 
Offline
User avatar

Joined: Sat Feb 16, 2013 11:52 am
Posts: 320
You can also do Java and Swing, too. Most Java IDEs I know have a visual GUI editor so you don't have to code everything by hand. If you already have experience in Java or think you can learn it quickly enough to be worth it, you should try it. That could also be a nice way of learning Object Oriented Programming since using classes is definitely not optional here. :lol:

Here's something I'm developing for my game:

https://i.imgur.com/frySuJ6.png

Basically everything visual, ie. creating and positioning buttons, menus, etc. was done via drag and drop in an editor, you only have to code what happens when a button is pressed and stuff like that (events). Also Java's Graphics2D library is very simple to use and kinda resembles graphical BASIC, you can draw lines, solids, textures, etc. easily by simply overriding a JPanel's paintComponent() method. (that's what my middle panel uses).

For example... this is a simplified gridless version of my middle panel's drawing routines.

Code:
   
public class GrafxPanel extends javax.swing.JPanel {
    private final int dungeonSize = 16;
    private final int edgeOffsetX = 20;
    private final int edgeOffsetY = 20;
   
    Dungeon dungeon;

(...)

@Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        drawStuff(g);
    }

    private void drawStuff(Graphics g){
        Graphics2D gfx = (Graphics2D) g;
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
       
        int cellSize = calculateCellSize();
       
        g.setColor(Color.BLACK);
        g.fillRect(0,0, getWidth(), getHeight());
       
        for (int x = 0; x < dungeonSize; x++){
            for (int y = 0; y < dungeonSize; y++){
                Cell block = dungeon.getCell(x, y);
                if (block == null) break;
               
                if (block.getId() != 0x00){
                    g.setColor(Color.WHITE);
                    g.fillRect(edgeOffsetX + (x*cellSize), edgeOffsetY + (y*cellSize), cellSize, cellSize);

                }
            }
        }
        g.setColor(Color.WHITE);
        gfx.drawString("Huehehue", 20, 20);
    }
}


You can see that it's kinda simple even if you don't know Java. I can share the whole repository of my GUI project with you if it helps.

_________________
This is a block of text that can be added to posts you make. There is a 255 character limit.


Top
 Profile  
 
PostPosted: Sat Feb 10, 2018 9:14 am 
Offline
User avatar

Joined: Sun Sep 07, 2008 7:27 am
Posts: 494
Location: Seatlle, WA, USA
Minor example: Many years ago I wrote a MAP editor and save state editor for the NES game "Faria" in C++ on Win32. No MFC or anything funky. Just straight up Win32 + C++. It is on github: https://github.com/dennisjenkins75/FariaRomEditor

If I were to do the project over again now, I would use C++ / GTK++, so that I could do all of my development on Linux and occasionally compile for Windows as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2

All times are UTC - 7 hours


Who is online

Users browsing this forum: 93143, Dwedit and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group