Original Post
Hello, i'm starting a game in C and i'm using DirectX (ok so its not entirely C, the DirectX part is C++
) It's a 2D rpg (kind of like the super nintendo final fantasy game series).
I've read a few tutorials but i'm not really sure how good what i'm doing is (i think everyone gets that on their first big project
) so i'd like a little advice... maybe pointers in the right direction and all that, mostly on how i'm rendering graphics with DirectDraw.
Here's how i do things in my main...
) one that says what part of the game is running (loading stuff, title screen, map, shop, menu, battle...) and the other that says what it's doing right now (for example character 1's turn, enemy 1's turn, using an item... etc...) ERROR is a function that takes the return code and if it's non-0 then it displays an error message otherwise it returns no problem.
Is this a good idea? Cutting it up in parts like that and all...
* Here's the big part i'm worried about: how i draw things. I have the following things...
- An array of ~40 surfaces on which i load bitmaps on the fly (enemy pics, tiles, character sprites, animations... each has a couple of spare buffers so i can have multiple, say, animations loaded at once)
- A buffer surface i use when i need to preserve parts of my screen (for instance when there's a cursor when the user can select options in a menu. I'm doing this kind of like with dirty rectangles).
- A screen surface.
- My primary surface and the backbuffer.
And here's how i render stuff...
- Game draws on the screen surface.
- After it's done handling the current frame, game fastblts the whole screen surface into the back buffer (this is what i don't like...)
- I flip the surfaces.
Like i said i really, really don't like how i'm doing this. But i don't know how else to do it because otherwise it'll draw to the backbuffer, flip, then on the next flip unless i redraw the whole screen (which means working out some system where i store every letter drawn on screen (custom bitmap font engine), every window, every sprite, tile... and which would require parsing and executing, say, the entire draw window routine, draw text, etc every frame) it would be blank or just have the updates from the current "game frame"... I know i'm doing something wrong here.
If you're wondering, SETUP_BLT just sets up a RECT (i just use the SetRect function) for blitting purposes. DD_Scrn is my screen surface and DD_SurfB is my backbuffer (DD_SurfA is the priamry surface).
* In CleanUp, i just destroy every created ressource and post a quit message.
* Finally, i'm not 100% certain how to handle input yet...
Thanks for any advice you may have.
[edited by - havenmyst on December 29, 2003 4:47:26 AM]
while(1)
{
TickCount = GetTickCount();
if(PeekMessage(&hMsg, NULL, 0, 0, PM_NOREMOVE))
{
if(GetMessage(&hMsg, NULL, 0, 0) == FALSE) return hMsg.wParam;
TranslateMessage(&hMsg);
DispatchMessage (&hMsg);
}
if(f_State == STA_LOAD ) if(ERROR(HandleLoad ())) f_KillApp = 1;
if(f_State == STA_TITLE) if(ERROR(HandleTitle())) f_KillApp = 1;
SETUP_BLT(0, 0, 640, 480);
DD_SurfB->BltFast(0, 0, DD_Scrn, &BltRect, DDBLTFAST_NOCOLORKEY);
while(1)
{
HRESULT Res = DD_SurfA->Flip(NULL, 0);
if(Res == DD_OK ) break;
if(Res == DDERR_SURFACELOST ) if(NOT_OK DD_SurfA->Restore()) break;
if(Res != DDERR_WASSTILLDRAWING) break;
}
while((GetTickCount() - TickCount) < 8) Sleep(1);
if(f_KillApp) { CleanUp(); PostQuitMessage(0); }
}
Ok...
* On TickCount... I use it to give the game a stable FPS. Maybe if it were 3d i'd consider just like moving stuff around by how much time it took to render the scene (y'know what i mean right?) but this is a 2d tilebased game so... but is GetTickCount a good idea or should i use something more performant, like QueryPerformanceCounter? Am i doing all this right or is the while(...) sleep(1) line a bad idea?
* Message queue, nothing out of the ordinary there (i think?)
* Now here comes how i handle things... i have two global variables (yes i know, yuck, yuck