Sign in to follow this  

Game programming tutorials?

This topic is 4065 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

I would really like to make a small 3D Game with DirectX and have taken many tutorials that explained many things in DirectX from 2d primitives to 3D Translations, rotations and scaling to lighting to texturing but I just can't even come close to turning all of that information into a game. For example I've had it explained to me how to make a large terrain or a tiled floor but I haven't been able to completely understand it without seeing code that created it, it would be best if there was some sort of tutorial in which you could make a small 3D game or something like that so that I would understand better and be able to make 3D games of my own. So if anyone knows of any such tutorials could you please post a link to it.

Share this post


Link to post
Share on other sites
To be honest - tutorials are great but they are no substitute for sheer hard work. You don't say how experienced you are at coding but my advice - steer clear of the harder topics and don't be too concerned about your engine - just sit down and code a very simple game.

Start from the basics. Get a square block moving about the screen using user input.

Add some 'enemies' (more square blocks - differently coloured if you are feeling ambitous) and get them to move around.. very simple movement is okay.

Now try allowing the user to 'fire' at the enemies.

Try to detect that the enemies have bumped into the users square block and end the game if they have.

The real skill of a programmer is not in the knowledge of techniques but in the application of those techniques to solve problems. (start flame war).

In other words - just dive in there and have a go!

Share this post


Link to post
Share on other sites
Quote:
Original post by deathknight2005
I would really like to make a small 3D Game with DirectX and have taken many tutorials that explained many things in DirectX from 2d primitives to 3D Translations, rotations and scaling to lighting to texturing but I just can't even come close to turning all of that information into a game. For example I've had it explained to me how to make a large terrain or a tiled floor but I haven't been able to completely understand it without seeing code that created it, it would be best if there was some sort of tutorial in which you could make a small 3D game or something like that so that I would understand better and be able to make 3D games of my own. So if anyone knows of any such tutorials could you please post a link to it.


Forget the tutorials. Try to make a simple tetris or a breakout game in the begining. Making a game (or coding for a game) is more about getting everything to work right, like input, graphics, sprite movements. Don't aim too high.

Tutorials are there so that you understand how to do a particular thing. Believe me it is easy to write a Dot3 bumpmapping and per-pixel lighting tutorial than it is to integrate it into your game and make it look right.

There are books on how to start here

Share this post


Link to post
Share on other sites
Go to http://www.drunkenhyena.com/

He has fantastic DirectX 8 and 9 tutorials, that take you right from the basics of setting up a window aplication, to plotting points and lines, to 3d models, textures, and cameras.
He goes through all of his tutorials with both heavily commented code, and an in depth explaination on the website.

http://andypike.com is another good sight that also covers 2d billboarding, fonts, sound, direct input, etc. It's for DirectX 8 though.

Where you go from there depends on what kind of game you want. If you apply gravity to objects by accelerating them down every frame unless they're on the ground, and detect collisions by seeing if two objects are closer than the sum of their radii then return them to their original positions to separate them, then you've got the foundations of a large number of game possibilities sorted out.

Share this post


Link to post
Share on other sites
Something else you may want to look into is getting a directx book or even a game programming book that focuses on directx. Good ones will even walk you through the basics of creating a game and then have you modifying their code so that you can see what works and what doesn't.

Share this post


Link to post
Share on other sites
Yes I would like to get a book like that, I've been looking for a while. So far since my last reply I have made a small 2D game and I am in the process of adding enemies now. Unfortunately I have now run into a problem on my game!
Here is the source code (so far it is just a moving triangle and a triangle that just stays in the same place):
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

// define the screen resolution and keyboard macros
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL; // the pointer to the vertex buffer
LPDIRECT3DSURFACE9 z_buffer = NULL; // the pointer to the z-buffer
LPDIRECT3DVERTEXBUFFER9 enemy_buffer = NULL;
float position[2];
float newpos[2];
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void init_graphics(void); // 3D declarations

struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR;};
#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
position[0] = 1.25f;
position[1] = -1.5f;
position[2] = 0.0f;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
// wc.hbrBackground = (HBRUSH)COLOR_WINDOW; // not needed any more
wc.lpszClassName = L"WindowClass1";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
L"WindowClass1",
L"Our Direct3D Program",
WS_EX_TOPMOST | WS_POPUP, // fullscreen values
0, 0, // the starting x and y positions should be 0
SCREEN_WIDTH, SCREEN_HEIGHT, // set the window to 640 x 480
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);

// set up and initialize Direct3D
initD3D(hWnd);

// enter the main loop:

MSG msg;

while(TRUE)
{
DWORD starting_point = GetTickCount();

if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

render_frame();

// check the 'escape' key
if(KEY_DOWN(VK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);

while ((GetTickCount() - starting_point) < 25);
}

// clean up DirectX and COM
cleanD3D();

return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc (hWnd, message, wParam, lParam);
}


// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.EnableAutoDepthStencil = TRUE; // automatically run the z-buffer for us
d3dpp.AutoDepthStencilFormat = D3DFMT_D16; // 16-bit pixel format for the z-buffer

// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

d3ddev->CreateDepthStencilSurface(SCREEN_WIDTH, // the z-buffer width
SCREEN_HEIGHT, // the z-buffer height
D3DFMT_D16, // 16-bit pixel format
D3DMULTISAMPLE_NONE, // no multisampling
0, // no multisampling quality
TRUE, // discard old buffer data
&z_buffer, // the address of the buffer
NULL); // reserved...we don't use this.

init_graphics(); // call the function to initialize the triangle

d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE); // turn off the 3D lighting
d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE); // turn on the z-buffer

return;
}


// this is the function used to render a single frame
void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);

// SET UP THE VIEW AND PROJECTION TRANSFORMS

D3DXMATRIX matView; // the view transform matrix
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, 15.0f), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
SCREEN_WIDTH / SCREEN_HEIGHT, // the aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection


// select the vertex buffer to display
d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

D3DXMATRIX matTranslateA; // a matrix to store the translation for triangle A
// build MULTIPLE matrices to rotate and translate the model


if(KEY_DOWN(VK_UP))
{
newpos[1] = position[1] + 0.25f;
D3DXMatrixTranslation(&matTranslateA, position[0], newpos[1], position[2]);
d3ddev->SetTransform(D3DTS_WORLD, &matTranslateA);
position[1] = newpos[1];
}
if(KEY_DOWN(VK_RIGHT))
{
newpos[0] = position[0] - 0.25f;
D3DXMatrixTranslation(&matTranslateA, newpos[0], position[1], position[2]);
d3ddev->SetTransform(D3DTS_WORLD, &matTranslateA);
position[0] = newpos[0];
}
if(KEY_DOWN(VK_LEFT))
{
newpos[0] = position[0] + 0.25f;
D3DXMatrixTranslation(&matTranslateA, newpos[0], position[1], position[2]);
d3ddev->SetTransform(D3DTS_WORLD, &matTranslateA);
position[0] = newpos[0];
}
if(KEY_DOWN(VK_DOWN))
{
newpos[1] = position[1] - 0.25f;
D3DXMatrixTranslation(&matTranslateA, position[0], newpos[1], position[2]);
d3ddev->SetTransform(D3DTS_WORLD, &matTranslateA);
position[1] = newpos[1];
}
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

d3ddev->SetStreamSource(0, enemy_buffer, 0, sizeof(CUSTOMVERTEX));
D3DXMATRIX enemyTranslate;
D3DXMatrixTranslation(&enemyTranslate, 3.0, 3.0, 0.0);
d3ddev->SetTransform(D3DTS_WORLD, &enemyTranslate);
d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D

return;
}


// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX t_vert[] =
{

{ 2.5f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 1.25f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 0.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(255, 0, 0), },
};

// create a vertex buffer interface called t_buffer
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&t_buffer,
NULL);

VOID* pVoid; // a void pointer

// lock t_buffer and load the vertices into it
t_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, t_vert, sizeof(t_vert));
t_buffer->Unlock();

CUSTOMVERTEX enemy_vert[] =
{

{ 2.5f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 1.25f, 0.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 0.0f, -3.0f, 0.0f, D3DCOLOR_XRGB(0, 255, 0), },
};

// create a vertex buffer interface called t_buffer
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&enemy_buffer,
NULL);

VOID* pVoid2; // a void pointer

// lock t_buffer and load the vertices into it
enemy_buffer->Lock(0, 0, (void**)&pVoid2, 0);
memcpy(pVoid2, enemy_vert, sizeof(enemy_vert));
enemy_buffer->Unlock();

return;
}

And for some reason when I am not pressing an arrow key, the triangle that cannot move is seen but not the other triangle. When I press an arrow key the moving triangle suddenly appears and starts moving but then the other triangle disappears! Then when I release the arrow key it goes back to how it started and the triangle that moves disappears and the other one appears! How do I fix this problem?

[Edited by - deathknight2005 on October 20, 2006 7:32:27 PM]

Share this post


Link to post
Share on other sites
I'm no expert, but I think this is happening:

You're only calling draw primative once. When you press a key, it sets the player triangle as the source and renders it. It then sets the enemy triangle as the source, but doesn't render it, because you haven't called draw primative again. One call, one primative.

However, if you don't press a key, it doesn't set the player triangle as the source. It is still set as the enemy from the last loop. So this time, it renders the enemy triangle.

If you put a drawprimative after d3ddev->SetTransform(D3DTS_WORLD, &enemyTranslate); then it should always render the enemy triangle.

To render the player triangle, take the transforms and such out of the functions called by the keydowns. Have those functions just change the player poly position. Then, only call one transform, outside of any keydown functions, once the change in position has been determined. Call it even if the change in position is 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathknight2005
I would really like to make a small 3D Game with DirectX and have taken many tutorials that explained many things in DirectX from 2d primitives to 3D Translations, rotations and scaling to lighting to texturing but I just can't even come close to turning all of that information into a game. For example I've had it explained to me how to make a large terrain or a tiled floor but I haven't been able to completely understand it without seeing code that created it, it would be best if there was some sort of tutorial in which you could make a small 3D game or something like that so that I would understand better and be able to make 3D games of my own. So if anyone knows of any such tutorials could you please post a link to it.


Dude, thx for making this thread.
I have the exact same problem.
I have read so many good directx tutorial out there, but for some reason i still dont feel like im ready to make a game.
I think im gonna stick to Byron's idea, to make a small block game, and adding features to it.

Share this post


Link to post
Share on other sites

This topic is 4065 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.

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