Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
exiled pnoy

So, you want to be a game programmer...

2 posts in this topic

Hello, welcome to my game programming tutorial. Here, I'm going to tutor from the ground up. This tutorial will be for the Windows platform and done in Visual Studio or DirectX 9 because it's ancient and anyone who's ever used windows has heard of it. Let's skip the bullshit and get started, shall we? I assume you know the basics of C++.

 

Alright, after firing up Visual Studio, add in main.cpp, this is what we'll start with.

#include <Windows.h>INT WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR lpCmd, INT nCmdShow ){    return EXIT_SUCCESS;}

We're not in kindergarden anymore, so we won't be using a console project or the elementary int main(). Read msdn on the Windows Main function to understand the details. WINAPI is a #define'd function prototype. If you don't know what that is or about function pointers, leave now and come back when you actually know C++. And go learn bitwise operations while you're at it. For those still here, note how I don't have the previous instance either. Since we're not in the stone age, we won't be needing it.

Alright, now let's create and register a class so we can actually create a display window. We can't have DirectX draw our game when there's nothing to draw on.

 

Apparently, the class demands a window procedure function. So let's make one.

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){    if( uMsg == WM_DESTROY )    {        PostQuitMessage( 0 );        return 0;    }    return DefWindowProc( hWnd, uMsg, wParam, lParam );}

If you've done your homework, you'd know that the default windows procedure doesn't send the Quit message. That's why we went through the trouble of making our own that does. Let's pass that to our class and fill it in.

    WNDCLASS wc;    wc.style = CS_HREDRAW | CS_VREDRAW;    wc.lpfnWndProc = &WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = NULL;    wc.hCursor = NULL;    wc.hbrBackground = CreateSolidBrush( RGB(0,0,0) );    wc.lpszMenuName = NULL;    wc.lpszClassName = "Direct3D Engine";        if( !RegisterClass( &wc ) )        return EXIT_FAILURE;

Technically, we're supposed to have a Cursor resource, but the operating system fills in a default one if it isn't provided or if you've messed up linking the resources. Now that we have a class registered, we can finally create our window. It's done with a simple function.

    ULONG flags = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;    RECT rec = { 100, 100, 740, 580 };    AdjustWindowRect( &rec, flags, FALSE );    HWND hWnd = CreateWindow( "Direct3D Engine", "DirectX Engine", flags,        rec.left, rec.top, rec.right - rec.left, rec.bottom - rec.top, NULL, NULL, hInstance, NULL );    if( !hWnd )        return EXIT_FAILURE;

Why are we adjusting the window, you may ask? When the window is created, it's created at exactly that size. The client is only a fraction of the size of the window, so we're adjusting the window so that the client is the size we want (in this case, 640 x 480 ). Obviously, there will be some funky visual artifacts if we only estimate the client size and if we need to pass them as arguments to some structure or function for DirectX to process.

 

Alright, now we have our window. You may as well make the above code within some arbitrary QuickCreateWindow function that returns a HWND, a handle to the window (duh), instead of the windows main function because that's essentially the only resourceful thing we need here.

 

Next, let's work with the messages we'll get. Obviously, our game will have some form of input.

    MSG msg;    do    {        PeekMessage( &msg, NULL, NULL, NULL, PM_REMOVE );        TranslateMessage( &msg );        DispatchMessage( &msg );    }while( msg.message != WM_QUIT );

Notice, we're not using GetMessage, but PeekMessage. Why? Because GetMessage is retarded, that's why. It sleeps the program until it has input to process. Obviously, it'll be a problem even if the client is pressing a key or moving the mouse almost all of the time.

 

Well, this concludes the very first tutorial. It's a prerequisette to the the next tutorial where we actually draw something.

All of my tutorials will be mantained in this thread.

Edited by exiled pnoy
2 people like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.