   Creating 2D Game in DirectX9
  I'm sure that some of these questions have been answered, but my search results keep coming up with answers that are older than I'd hoped.So, I'm working on creating a little 2D game using C++ and DirectX9 in Visual Studio 2010.  I have successfully drawn a few sprites and made them recognize collisions and I can even control my character with my keyboard (although my animations are awful).I'm wondering where to go next.  I would like to implement a title screen and perhaps a pause menu, and I'd also really like to learn how to implement basic physics that make my character appear to be standing on the ground, held there by some invisible force such as gravity or maybe I'd settle for a ghost.I'm unsure where to put logic that would give my player life (health points, I'm not Frankenstein), or how to put a working life bar on the screen. I want to make badguys that will die, and cutscenes where badguys talk before they die.  Maybe a townsperson that repeats a phrase over and over.   I'd like to add a second or third level to my game.My problem seems to be that I'm unfamiliar with the logic structure of DirectX. It seems like most of what I would call gameplay happens between BeginScene and EndScene, but games don't generally load their entire content at all times, right? I mean, how do I tell the game what level to draw? Let's say that he reaches the edge of the screen, do I End Scene and call another function that contains a scene? I'm not afraid of doing a ridiculous amount of work, but I'm afraid that my performance is going to suffer if I continue loading everything into a single scene.  Where do I go from here?   
  If you're just starting in, you've mentioned about six months development working fulltime. My advice would be to pick one thing from your list and work on that.Quote:my search results keep coming up with answers that are older than I'd hoped.Search again. There are hundreds of posts on collision detection, for example, all posted within the last year or two. Physics and collision calculations don't change with age.Quote:It seems like most of what I would call gameplay happens between BeginScene and EndSceneActually, BeginScene/EndScene is just the rendering portion of your game. All the physics, getting and processing user-input, sound-playing, collision calculations, health point counting, etc., normally happens elsewhere.As mentioned above, your best approach for the time being should be to pick one thing, try to get it to work and ask questions when you run into problems.Go, Bucks!   ;  I guess I should've rephrased my question. I knew I put too much information in that first post. I already have code for all of that stuff, I just don't know where it goes.I feel like an idiot not being able to explain my dilemma.  I have a function called game_run() that replaces the majority of the Win32 message loop and basically (at least, as I understand it) just runs over and over again to create my game.The Game_Run() function looks like this:(caveman and caveman2 are just sprites)void Game_Run(HWND hwnd){    //make sure the Direct3D device is valid    if (d3ddev == NULL)   return;Poll_Mouse();Poll_Keyboard();   //after short delay, ready for next frame?   //this keeps the game running at a steady frame rate   if (GetTickCount() - start >= 30)   {  //reset timing  start = GetTickCount();  //move the sprite  caveman.x += caveman.movex;  caveman.y += caveman.movey;if (Key_Down(DIK_LEFT)){caveman2.x -= 5;  if (++caveman2.animcount > caveman2.animdelay){//reset countercaveman2.animcount = 0;//animate the spriteif (++caveman2.curframe > caveman2.lastframe)caveman2.curframe = 0;}}if (Key_Down(DIK_RIGHT)){caveman2.x += 5;if (++caveman2.animcount > caveman2.animdelay){//reset countercaveman2.animcount = 0;//animate the spriteif (++caveman2.curframe > caveman2.lastframe)caveman2.curframe = 0;}}if (Key_Down(DIK_UP)){caveman2.y -= 5;if (++caveman2.animcount > caveman2.animdelay){//reset countercaveman2.animcount = 0;//animate the spriteif (++caveman2.curframe > caveman2.lastframe)caveman2.curframe = 0;}}if (Key_Down(DIK_DOWN)){caveman2.y += 5;if (++caveman2.animcount > caveman2.animdelay){//reset countercaveman2.animcount = 0;//animate the spriteif (++caveman2.curframe > caveman2.lastframe)caveman2.curframe = 0;}}  //"warp" the sprite at screen edges  if (caveman.x > SCREEN_WIDTH - caveman.width) caveman.x = 0;  if (caveman.x < 0) caveman.x = SCREEN_WIDTH - caveman.width;if (caveman2.x > SCREEN_WIDTH - caveman2.width)caveman2.x = SCREEN_WIDTH - caveman2.width;if (caveman2.x < 0)caveman2.x = 0;if (caveman2.y > SCREEN_HEIGHT - caveman2.height)caveman2.y = SCREEN_HEIGHT - caveman2.height;if (caveman2.y < 0)caveman2.y = 0;    //has animation delay reached threshold?  if (++caveman.animcount > caveman.animdelay)  { //reset counter caveman.animcount = 0; //animate the sprite if (++caveman.curframe > caveman.lastframe)caveman.curframe = 0;  }  }   //start rendering   if (d3ddev->BeginScene())   {  //erase the entire background  d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);//start sprite handlersprite_handler->Begin(D3DXSPRITE_ALPHABLEND);//create vector to update sprite positionD3DXVECTOR3 position((float)caveman.x, (float)caveman.y, 0);D3DXVECTOR3 position2((float)caveman2.x, (float)caveman2.y, 0);//configure the rect for the source fileRECT srcRect;int columns = 8;srcRect.left = (caveman.curframe % columns) * caveman.width;srcRect.top = (caveman.curframe / columns) * caveman.height;srcRect.right = srcRect.left + caveman.width;srcRect.bottom = srcRect.top + caveman.height;RECT srcRect2;int columns2 = 8;srcRect2.left = (caveman2.curframe % columns) * caveman2.width;srcRect2.top = (caveman2.curframe / columns) * caveman2.height;srcRect2.right = srcRect2.left + caveman2.width;srcRect2.bottom = srcRect2.top + caveman2.height;//draw the spritesprite_handler->Draw(caveman_image,&srcRect,NULL,&position,D3DCOLOR_XRGB(255, 255, 255));sprite_handler->Draw(caveman_image2,&srcRect2,NULL,&position2,D3DCOLOR_XRGB(255, 255, 255));if(Collision(caveman, caveman2)){caveman.x = caveman.x - 10;}//stop drawingsprite_handler->End();//stop rendering   d3ddev->EndScene();    }    //display the back buffer on the screen    d3ddev->Present(NULL, NULL, NULL, NULL);    //check for escape key (to exit program)    if (Key_Down(DIK_ESCAPE))   PostMessage(hwnd, WM_DESTROY, 0, 0);}Now, can I split this function up somehow to encompass these other features? I have been reading game programming books left and right but nothing's quite clear on how to break this up.  I don't mean to sound ungrateful for a response, and I'd certainly only focus on one thing at a time, but I don't know how to even begin working on the next part.   ;Quote:I knew I put too much information in that first post.Yeah. Ask a general question, get a general answer.I understand that you're looking for information on organizing your code. However, ensuring your program is properly coded should be your first priority. I.e., organizing poor code isn't what it's about at the moment. Some basics that you should address first may change the way you approach your run-loop.Checking for errors: You should get in the habit right now of properly checking for errors. Every DirectX function gives some indication of success or failure. The tutorial examples in the SDK are a good place to start to see how to do that, primarily the use of the SUCCEEDED and FAILED macros. Those examples will also provide with a better (more efficient and useful) way to clear the backbuffer.With regard to error checking:if (d3ddev->BeginScene()) ...BeginScene and most DirectX functions return a value indicating success or failure, not true/false values. If there's a problem with the device, you don't handle it.Due to the fact that your program is sharing the computer with other applications, frequently testing the status of the device should be a requirement in your code to avoid nasty situations. It's common to check the status once each frame and handle each error (if any) explicitly. dev->TestCooperativeLevel() is a good function for that testing.With regard to general game- and run-loop organization, this article has some good general organization information. The idea is to encapsulate unrelated activities into separate routines. Those routines (functions) are then called (in order) within the loop.Quote:I have a function called game_run() that replaces the majority of the Win32 message loopYou may want to consider using a message loop for getting user input, and eliminate the complications of DirectInput. For a lot of applications, getting user input through the window procedure is much simpler, more than adequate and avoids the complications of creating, managing and shutting down DirectInput (or XInput).Using the example from the article cited above, I'd modify it for DirectX something like the following:while( user doesn't exit )  check for user input // a PeekMessage loop, for instance  CheckDeviceStatus  if( device is ok )    run AI    move enemies    resolve collisions    draw graphics    play sounds  else handle other conditions    such as device lost, device unavailable, etc.end while[Edited by - Buckeye on November 19, 2010 12:10:21 PM];  For some reason I can't click on the link that you posted (it turns blue, but it doesn't actually do anything).Is checking device status on every loop iteration going to be resource intensive?The return types from those functions actually will set off that if statement if anything other than success is returned on that function. I will add additional handling for specific issues, but that one checks for failure of that function. If the device fails, that function will fail, and my loop will close. I know this because it's happened.This particular project is rather shoddy because I'm just poking things in to see if they'll work. I'm not all that interested in creating a performance-intensive project right now, because I can honestly say that I'm still missing a few of the basics.   ;  RE: the link. It's fixed now. I didn't check my own syntax. [embarrass]Quote:Is checking device status on every loop iteration going to be resource intensive?Nope.Quote:This particular project is rather shoddy because I'm just poking things in to see if they'll work. I'm not all that interested in creating a performance-intensive project right now, because I can honestly say that I'm still missing a few of the basics.I understand the interest in getting something working, and the comments I've made have nothing to do with making things performance-intensive.   