Jump to content
  • Advertisement
Sign in to follow this  
NUCLEAR RABBIT

Win32 Programming Question

This topic is 4264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!