• Advertisement
Sign in to follow this  

Want to start 2D game, finished basic C++

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

Heya. So I want to program a game, 3D one day but for now 2D to practice. I've finished learning basic C++, using books such as Accelerated C++ and Beginning C ++ Through Game Programming, Second Edition. Is that enough knowledge to start learning DirectX in order to start programming my first 2D game? Do I need to learn something *before* DirectX? I also was recommended on buying this book: http://www.amazon.ca/Beginning-DirectX-10-Game-Programming/dp/1598633619/ref=sr_1_5?ie=UTF8&s=books&qid=1215878324&sr=1-5 But a friend told me it's not a good book and that I need more than the knowledge I gained with Accelerated C++ and Beginning C++ Through Game Programming in order to program a simple 2D game. Any suggestions over here? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Yeah well, you need to have some idea about game design.
I'll tell you what I did and am doing.
Before I learned C++, i used to make 2d games with a program called gamemaker. which actually is great for a start. The most valuable thing i learned from it was how 2d games are actually organised. Sprites, sounds etc are all linked to objects which are placed in the game world.
Anyways I was learning C++ as I used gamemaker. Then, I got bored of such high level coding that gamemaker provided ( besides the drag and drop, ugh). I wanted to make a 2d game using C++.
So I found a 2d game library hge (Haaf's game engine, google it). I did the first two tutorials and I was like, this is easy man. Well, I didn't make a game actually. I didn't do much programming for quite a while.
Then when I found a book on Direct3d Programming, I downloaded the DirectX SDK. For about two months, I messed with DirectX, changing book from book. I didn't actually get far in any book. The fact is, despite being able to understand the books, i couldn't actually make something new on that level. So I would very much discourage starting using directx. Then, I restarted Hge, got a few people like me with just C++ training and started on an RPG. We're currently in the process of making it. So far it has given me valuable lessons on game design. Because game programming is more than just knowing the functions. HGE is quite easy to use to there was no problem with complex functions. Now I have also started directx in the background to a 2d level. I'm reading a book "Programming Role Playing Games with DirectX 2nd Ed." by Primier Press. Hopefully this will give me a sound knowledge of directx to a 2d level while also have an RPG with me. After this I plan to make the next version of my RPG with Directx while learning 3d directx in the background.


Hope this helps.

Share this post


Link to post
Share on other sites
Well I know pretty much how game mechanics work, the whole game loop, functions calling, input output, interface iteraction.
In theory anyways,
Now my problem is how to create it.

Share this post


Link to post
Share on other sites
Quote:
Original post by dhulli
Yeah well, you need to have some idea about game design.
I'll tell you what I did and am doing.
Before I learned C++, i used to make 2d games with a program called gamemaker. which actually is great for a start. The most valuable thing i learned from it was how 2d games are actually organised. Sprites, sounds etc are all linked to objects which are placed in the game world.


Gamemaker is a great place for anyone to start, i used to use it aswell and it has helped me so much while learning C++. I am forever greatful to Mark Overmars :)
I would recommend Gamemaker to anyone who wants to start game pogramming but has none or little programming knowledge. It's simple, fast gme development plus with a fntastic community to help you along your way. ;)


PS: Just had to say it, sorry lol

Share this post


Link to post
Share on other sites
You might want to learn some simpler game API first like SDL (libsld.org), but you can probably get into DirectX now. There are lots of books and tutorials out there that will make it as easy as possible. Just be aware it might be a rough ride, but that's true no matter your experience, unless you've already used a COM interface. If you're not afraid of banging your head on a few walls, go for DirectX.

Share this post


Link to post
Share on other sites
Quote:
Original post by Reegan
Gamemaker is a great place for anyone to start, i used to use it aswell and it has helped me so much while learning C++. I am forever greatful to Mark Overmars :)
I would recommend Gamemaker to anyone who wants to start game pogramming but has none or little programming knowledge. It's simple, fast gme development plus with a fntastic community to help you along your way. ;)


PS: Just had to say it, sorry lol


In that case, I think you'll be fine starting off with DirectX. ;) But if you want to start off with a simpler API, SDL is a good choice, also.

If you want to make a 2D game, just go ahead and do it! I recommend this book if you want to get into 3D programming with DirectX.

EDIT: Sorry, messed up. I thought I quoted the OP, my bad. :p

@Winterbreeze: But still, if you feel comfortable with C++ and you've learned the fundamentals, and you want to make 2D games, go right ahead. Maybe start out making a text-based game of tic-tac-toe or an adventure game if you haven't done so. SDL is simpler and a great 2D API, but if you want to start off with DirectX, that's fine as well. Good luck!

[Edited by - Moonshoe on July 12, 2008 4:58:57 PM]

Share this post


Link to post
Share on other sites
So, I started programming with DirectX for a game,
Here is what I have so far after short time, would like any comments on improving the code, suggestions are welcome.
So far all it does is load 2 gumps to be map borders, load a screen, for now it is only loading the same tile 19 times with size of 44x44 and load a sprite for a character which you are able to move with the keyboard.

Changes to be made:
I am going to change the whole map system into classes.
Going to add scrolling ability for the map.
Use the map to read the tiles information from files.

Any suggestions on improving the code (especially the directx code as I am very new to directx and I believe the code is kinda crude) will be warmly taken.



// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

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

// define the screen resolution
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define GrassTile1 "GrassTile.png"

// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPD3DXSPRITE d3dspt; // the pointer to our Direct3D Sprite interface
LPDIRECT3DTEXTURE9 TileName;
LPDIRECT3DTEXTURE9 GumpName;
float enemyX = 60.0f, enemyY = 60.0f;




// My Variables!
float spritex = 400.f;
float spritey = 300.f;

// Map Loading Variables!
int MapTileGump[19][19];

// 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 MapLoadScreenTiles(void);
void LoadMap(void);
void DrawMap(void);
void LoadTile(LPDIRECT3DTEXTURE9* TileName, LPCTSTR TileFilename);
void DrawTile(LPDIRECT3DTEXTURE9 TileName, RECT texcoords, float x, float y, int a);
void LoadGump(LPDIRECT3DTEXTURE9* GumpName, LPCTSTR GumpFilename, float w, float h);
void DrawGump(LPDIRECT3DTEXTURE9 GumpName, RECT texcoords, float x, float y, int a);




//void RenderMap(void);

// sprite declarations
LPDIRECT3DTEXTURE9 sprite; // the pointer to the sprite
LPDIRECT3DTEXTURE9 Tile1; // the pointer to the sprite

// 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)
{
// the handle for the window, filled by a function
HWND hWnd;
// this struct holds information for the window class
WNDCLASSEX wc;

// clear out the window class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));

// fill in the struct with the needed information
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;
wc.lpszClassName = "WindowClass1";

// register the window class
RegisterClassEx(&wc);

// create the window and use the result as the handle
hWnd = CreateWindowEx(NULL,
"WindowClass1", // name of the window class
"Our First Windowed Program", // title of the window
WS_EX_TOPMOST | WS_POPUP, // fullscreen values // WS_OVERLAPPEDWINDOW, // window style
0, // x-position of the window
0, // y-position of the window
SCREEN_WIDTH, // width of the window
SCREEN_HEIGHT, // height of the window
NULL, // we have no parent window, NULL
NULL, // we aren't using menus, NULL
hInstance, // application handle
NULL); // used with multiple windows, NULL

// display the window on the screen
ShowWindow(hWnd, nCmdShow);

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

// enter the main loop:

// this struct holds Windows event messages
MSG msg;

// Enter the infinite message loop


//MapLoadScreenTiles();
//RenderMap();

while(TRUE)
{
// find out the starting time of each loop (This is to set the game timer)
DWORD starting_point = GetTickCount();

// Check to see if any messages are waiting in the queue
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// If the message is WM_QUIT, exit the while loop
if (msg.message == WM_QUIT)
break;

// Translate the message and dispatch it to WindowProc()
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Render the frames
render_frame();


// check the 'escape' key

if(KEY_DOWN(VK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);

if(KEY_DOWN(VK_RIGHT))
spritex +=1;

if(KEY_DOWN(VK_LEFT))
spritex -=1;

if(KEY_DOWN(VK_UP))
spritey -=1;

if(KEY_DOWN(VK_DOWN))
spritey +=1;


// Run game code here
// ...
// ...



// wait until 1/40th of a second has passed (Again, game timer)
while ((GetTickCount() - starting_point) < 25);
}
// clean up DirectX and COM
cleanD3D();

// return this part of the WM_QUIT message to Windows
return msg.wParam;
}

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch(message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;

/*case WM_MOUSEMOVE: // detects mouse movement
{
MouseXCoordinate = wParam;
MouseYCoordinate = lParam;
return 0;
} break;
*/

}

// Handle any messages the switch statement didn't
return DefWindowProc (hWnd, message, wParam, lParam);
}
// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface

D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = FALSE; // fullscreen, not window mode
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer
d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer

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


D3DXCreateSprite(d3ddev, &d3dspt); // create the Direct3D Sprite object

LoadMap();

D3DXCreateTextureFromFileEx(d3ddev, // the device pointer
"HumanKnight.png", // the new file name
34, //D3DX_DEFAULT, // default width
49, //D3DX_DEFAULT, // default height
D3DX_DEFAULT, // no mip mapping
NULL, // regular usage
D3DFMT_A8R8G8B8, // 32-bit pixels with alpha
D3DPOOL_MANAGED, // typical memory handling
D3DX_DEFAULT, // no filtering
D3DX_DEFAULT, // no mip filtering
D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key
NULL, // no image info struct
NULL, // not using 256 colors
&sprite); // load to sprite


return;
/*
D3DXCreateTextureFromFileEx(d3ddev, // the device pointer
"GrassTile.png", // the new file name
44, //D3DX_DEFAULT, // default width
44, //D3DX_DEFAULT, // default height
D3DX_DEFAULT, // no mip mapping
NULL, // regular usage
D3DFMT_A8R8G8B8, // 32-bit pixels with alpha
D3DPOOL_MANAGED, // typical memory handling
D3DX_DEFAULT, // no filtering
D3DX_DEFAULT, // no mip filtering
D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key
NULL, // no image info struct
NULL, // not using 256 colors
&Tile1); // load to sprite

return;
*/

// return;


}

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

d3ddev->BeginScene(); // begins the 3D scene

d3dspt->Begin(D3DXSPRITE_ALPHABLEND); // begin sprite drawing

DrawMap();
// draw the sprite
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f); // center at the upper-left corner
D3DXVECTOR3 position(spritex, spritey, 0.0f); // position at 50, 50 with no depth
d3dspt->Draw(sprite, NULL, ¢er, &position, D3DCOLOR_ARGB(255, 255, 255, 255));

d3dspt->End(); // end sprite drawing

// do 3D rendering on the back buffer here

d3ddev->EndScene(); // ends the 3D scene

d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame

return;
}

// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{

sprite->Release();
// Tile1->Release();
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D

return;
}

// Map Loading Function!
void MapLoadScreenTiles(void) {
for (int i = 0; i < 19; i++ ) {
for (int i2 = 0; i2 < 19; i2++ ){
MapTileGump[i][i2] = *GrassTile1 ;
}
}
}
/*
void RenderMap(void) {
float Loadx;
float Loady;

for (int i = 0; i < 19; i++ ) {
for (int i2 = 0; i2 < 19; i2++ ){
Loadx = 44 * i;
Loady = 44 * i2;

d3ddev->BeginScene(); // begins the 3D scene
d3dspt->Begin(D3DXSPRITE_ALPHABLEND); // begin sprite drawing
D3DXVECTOR3 tilecenter(0.0f, 0.0f, 0.0f); // center at the upper-left corner
D3DXVECTOR3 tileposition(Loadx, Loady, 0.0f); // position at 50, 50 with no depth
d3dspt->Draw(Tile1, NULL, &tilecenter, &tileposition, D3DCOLOR_ARGB(255, 255, 255, 255));

d3dspt->End(); // end sprite drawing

// do 3D rendering on the back buffer here

d3ddev->EndScene(); // ends the 3D scene

d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame

return;

}
}
}*/

void LoadMap()
{
LoadTile(&TileName, "GrassTile.png");


return;
}

void DrawMap()
{
RECT Part;
SetRect(&Part, 0, 0, 10, 836);
LoadGump(&GumpName, "Border.png", 836, 10);
DrawGump(GumpName, Part, 836, 0, 255);

SetRect(&Part, 0, 0, 836, 10);
LoadGump(&GumpName, "Border2.png", 10, 836);
DrawGump(GumpName, Part, 0, 836, 255);

/* // DRAW THE RADAR
// display the backdrop
SetRect(&Part, 0, 0, 44, 44);
DrawTile(TileName, Part, 0, 0, 255);

// display the enemy
//SetRect(&Part, 341, 14, 344, 17);
//DrawTile(TileName, Part, enemyX, enemyY, 255);

// display the border
SetRect(&Part, 0, 0, 44, 44);
DrawTile(TileName, Part, 200, 200, 255);
*/
float x;
float y;
for (float i = 0; i < 19; i++ ) {
for (float i2 = 0; i2 < 19; i2++ ){
x = i * 44;
y = i2 *44;
SetRect(&Part, 0, 0, 44, 44);
DrawTile(TileName, Part, x, y, 255);
}
}
return;
}

void LoadTile(LPDIRECT3DTEXTURE9* TileName, LPCTSTR TileFilename)
{
D3DXCreateTextureFromFileEx(d3ddev, TileFilename, 44, 44,
D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, TileName);

return;
}

void LoadGump(LPDIRECT3DTEXTURE9* GumpName, LPCTSTR GumpFilename, float w, float h)
{
D3DXCreateTextureFromFileEx(d3ddev, GumpFilename, w, h,
D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), NULL, NULL, GumpName);

return;
}

void DrawTile(LPDIRECT3DTEXTURE9 TileName, RECT texcoords, float x, float y, int a)
{
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f), position(x, y, 0.0f);
d3dspt->Draw(TileName, &texcoords, ¢er, &position, D3DCOLOR_ARGB(a, 255, 255, 255));

return;
}

void DrawGump(LPDIRECT3DTEXTURE9 GumpName, RECT texcoords, float x, float y, int a)
{
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f), position(x, y, 0.0f);
d3dspt->Draw(GumpName, &texcoords, ¢er, &position, D3DCOLOR_ARGB(a, 255, 255, 255));

return;
}

Share this post


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

  • Advertisement