Win32 Programming Question

Started by
14 comments, last by Adam Hamilton 17 years, 7 months ago
Hello, I've been programming for 5 months now, and Im starting to dive into some basic graphics programming. [smile] I'm going to be starting to learn Directx 9.0, and I've been looking at a myriad of tutorials on Directx 9, but In every example I find, they create a Window using Win32. I've recently started to learn SDL as well, overall, so far its been pretty easy to pick up. My question(s) is, when you guys create an OpenGL application or a Directx application, do you guys code making the window everytime? Do you guys remember every single little this that it takes to create a basic window with a white background, or do you copy and paste? or, do you use SDL to create the window? How did you guys learn/remember how to create a window? It seems like a plethora to remember! [sad] example:

#include <windows.h>
#include "resource.h"

const char ClassName[] = "MainWindowClass";

LRESULT CALLBACK WndProc( HWND    hWnd,
                          UINT    Msg,
                          WPARAM  wParam,          
                          LPARAM  lParam )
{
    switch (Msg)
    {
        case WM_CLOSE:
            DestroyWindow(hWnd);
        break;

        case WM_DESTROY:
            PostQuitMessage(0);
        break;

        default:
            return (DefWindowProc(hWnd, Msg, wParam, lParam));
    }

    return 0;
}

INT WINAPI WinMain( HINSTANCE  hInstance,
                    HINSTANCE  hPrevInstance,
                    LPSTR      lpCmdLine,
                    INT        nCmdShow )
{
    WNDCLASSEX    wc;

    wc.cbSize           = sizeof(WNDCLASSEX);
    wc.style            = 0;
    wc.lpfnWndProc      = (WNDPROC)WndProc;
    wc.cbClsExtra       = 0;
    wc.cbWndExtra       = 0;
    wc.hInstance        = hInstance;
    wc.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
    wc.hIconSm          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName     = NULL;
    wc.lpszClassName    = ClassName;

    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Failed To Register The Window Class.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    HWND    hWnd;

    hWnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    ClassName,
    "Win32 Tutorial No.1",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    240,
    120,
    NULL,
    NULL,
    hInstance,
    NULL);

    if (!hWnd)
    {
        MessageBox(NULL, "Window Creation Failed.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    MSG    Msg;

    while (GetMessage(&Msg, NULL, 0, 0))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    return Msg.wParam;
}

Advertisement
Don't retype code every time you start a project. Tedious tasks are what computers were designed for. Visual Studio has options to create templates so that all of those things will be done for you when you start a new project.

I actually use a pre-built engine, Irrlicht, because I got tired of debugging my bad OpenGL. I never did finish learning it OGL, but I have a lot more done now.

SDL was pretty good, but I even made a template for SDL projects in my IDE.

If you must use the Win32API, try building a class library for it.
XBox 360 gamertag: templewulf feel free to add me!
God, my brain doesnt have room to memorize 1/100th of what I need to program. I keep my references close at hand, bookmark sites that I find incredibly handy, keep all my previous code in directories so I can go back and cut and paste.

Plus, if I find myself doing something more then a couple times, I generally make my own class framework to handle it. That or adopt someone elses.
Similar to what templewulf stated. I have a series of source files that I start out with now, that have the majority of the functionality that I need.

But honestly, it only seems like a lot because you haven't done it often. When you work with it a lot, and understand what you're doing, creating a window will become second nature to you.

For me, I'm struggling to create stuff in Obj C & the whole NS architecture. Trying to get even a simple button on the screen programatically seems like a daunting task. On the other hand, I can get a button on a window in a matter of minutes using Win32. One I'm a complete beginner at, the other, I've had years of experience using.

*EDIT*

Although, I will admit, I have difficulty understanding some of the choices MS made in their API creation, such as cbSize. While I understand what its theoretically for, I've NEVER, not once, seen it initialized as anything other than the size of the structure.
Remember, any code you need to use again can be: A) Put in a library to be reusesd, B) Copied / pasted from your previous project, C) Put in a template / macro in the editor / IDE of your choice. I don't actually use custom templates / macros personally, but I do have a folder of various pieces of common starter code that I copy files out of when I need this skeleton stuff for each new project.

The beauty of computers, they perfectly remember everything you've ever done ... if you can jut remember where you put it :)

I highly recommend you grab something like subversion (svn) to just store little things you think you might want later. Its a lot cooler than using zipped up backups from a hundred different past experiences. I keep a sandbox area in my repository just for such little things, and I just go through and delete them when I'm certain they have no use for me anymore (just like email).
So I should create .cpp file, and everytime I need a window, just add that file to my project? Is that what you mean by template?
Raymond Chen on structure sizes

http://blogs.msdn.com/oldnewthing/archive/2003/12/12/56061.aspx
You could do that yes. There are various levels of sophistication and automation you could apply but that's more or less what it comes down to.

Another option is old fashioned copy-and-paste. I have a minimal Win32 app that does nothing except throw up a window and provide a menu with an exit option. When I start a new app I just copy my simple app and start building from there.

Of course experience counts for a lot. After you've been creating Win32 apps for 15 years you'll probably be able to write the skeleton code from memory too.
-Mike
I've recently been fiddling around with Win32 myself, before I start to seriously learn DirectX. It seems that no matter what you're going to do in DirectX (or OpenGL), you still need to set up a window. I've seen on the web several different ways to do this, usually in a reusable library as has been suggested previously.

That said, I'd be curious to see the reusable code other people use, just to learn about the different ways you can do the same thing (set up a window). Could be interesting to see how different people structure their basic window system.
Grant Palin
Just to let you know, you can use Direct3D with SDL.

This topic is closed to new replies.

Advertisement