   My first finished game!
  Hey everyone I just finished -- key word finished, my first game! It's pong and it's very primitive. The only thing I don't like about it, is that it eats CPU usage. Oh well, here's my code take a look give me pointers and such =).#include <windows.h>#include <iostream>#include <gl/gl.h>#include <cstdlib>#include <ctime>#define pwidth 0.16f#define pheight 0.06f#define bwidth 0.03f#define bheight 0.03fLRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);void gameInit();void gameRun(HDC hDC);void movePaddle(int dir);typedef struct {    float x, y, x2, y2;}paddle;typedef struct{    float x, y, x2, y2;}ball;enum direction {N = 0, S = 1, E = 2, W = 3, NW = 4, NE = 5, SE = 6, SW = 7};paddle PlayerPaddle;paddle AIPaddle;ball GameBall;direction BALLDIR;int pscore, aiscore;int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int iCmdShow){    pscore = 0;    aiscore = 0;    srand(time(NULL));    WNDCLASS wc;    HWND hWnd;    HDC hDC;    HGLRC hRC;  MSG msg;    BOOL bQuit = FALSE;    wc.style = CS_OWNDC;    wc.lpfnWndProc = WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wc.hCursor = LoadCursor (NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);    wc.lpszMenuName = NULL;    wc.lpszClassName = "PongAPP";    RegisterClass (&wc);    hWnd = CreateWindow ( "PongAPP", "Pong", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 800, 800, NULL, NULL, hInstance, NULL);    if(hWnd == NULL)    {   MessageBox(NULL, "Hwnd is null", "WinMain", MB_ICONEXCLAMATION);   return 0;    }    EnableOpenGL (hWnd, &hDC, &hRC);    gameInit();    while (!bQuit)    {   if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))   {  if (msg.message == WM_QUIT)  { bQuit = TRUE;  }  else  { TranslateMessage (&msg); DispatchMessage (&msg);  }   }   else   {  gameRun(hDC);   }    }    DisableOpenGL (hWnd, hDC, hRC);    DestroyWindow (hWnd);    return msg.wParam;}LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message)    {    case WM_CREATE:   return 0;    case WM_CLOSE:   PostQuitMessage (0);   return 0;    case WM_DESTROY:   return 0;    case WM_KEYDOWN:   switch (wParam)   {   case VK_ESCAPE:  PostQuitMessage(0);  return 0;   case VK_LEFT:  movePaddle(1);  return 0;   case VK_RIGHT:  movePaddle(-1);  return 0;   }   return 0;    default:   return DefWindowProc (hWnd, message, wParam, lParam);    }}void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){    PIXELFORMATDESCRIPTOR pfd;    int iFormat;    *hDC = GetDC (hWnd);    ZeroMemory (&pfd, sizeof (pfd));    pfd.nSize = sizeof (pfd);    pfd.nVersion = 1;    pfd.dwFlags = PFD_DRAW_TO_WINDOW |  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;    pfd.iPixelType = PFD_TYPE_RGBA;    pfd.cColorBits = 24;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;    iFormat = ChoosePixelFormat (*hDC, &pfd);    SetPixelFormat (*hDC, iFormat, &pfd);    *hRC = wglCreateContext( *hDC );    wglMakeCurrent( *hDC, *hRC );}void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){    wglMakeCurrent (NULL, NULL);    wglDeleteContext (hRC);    ReleaseDC (hWnd, hDC);}void movePaddle(int dir){    switch(dir)    {   case 1:  PlayerPaddle.x -=0.03f;  PlayerPaddle.x2 -=0.03f;  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  glClear(GL_COLOR_BUFFER_BIT);  break;   case -1:  PlayerPaddle.x +=0.03f;  PlayerPaddle.x2 +=0.03f;  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  glClear(GL_COLOR_BUFFER_BIT);  break;    }}void moveAIPaddle(){    if(GameBall.y >= 0.0f)    {   if(GameBall.x > AIPaddle.x + pheight)   {  AIPaddle.x += 0.01f;  AIPaddle.x2 += 0.01f;   }   if(GameBall.x < AIPaddle.x)   {  AIPaddle.x -= 0.01f;  AIPaddle.x2 -= 0.01f;   }    }}void ballMove(){    switch(BALLDIR)    {   case N:  GameBall.y += 0.0015;  GameBall.y2 += 0.0015;  break;   case S:  GameBall.y -= 0.0015;  GameBall.y2 -= 0.0015;  break;   case E:  GameBall.x += 0.0015;  GameBall.x2 += 0.0015;  break;   case W:  GameBall.x -= 0.0015;  GameBall.x2 -= 0.0015;  break;   case NW:  GameBall.y += 0.0015;  GameBall.y2 += 0.0015;  GameBall.x -= 0.0015;  GameBall.x2 -= 0.0015;  break;   case NE:  GameBall.y += 0.0015;  GameBall.y2 += 0.0015;  GameBall.x += 0.0015;  GameBall.x2 += 0.0015;  break;   case SW:  GameBall.y -= 0.0015;  GameBall.y2 -= 0.0015;  GameBall.x -= 0.0015;  GameBall.x2 -= 0.0015;  break;   case SE:  GameBall.y -= 0.0015;  GameBall.y2 -= 0.0015;  GameBall.x += 0.0015;  GameBall.x2 += 0.0015;  break;    }}void checkBallCollision(){    if(GameBall.x <= -1.0f)    {   switch(BALLDIR)   {  case W: BALLDIR = E; break;  case NW: BALLDIR = NE; break;  case SW: BALLDIR = SE; break;   }    }    if(GameBall.x >= 1.0f)    {   switch(BALLDIR)   {  case E: BALLDIR = W; break;  case NE: BALLDIR = NW; break;  case SE: BALLDIR = SW; break;   }    }    if(GameBall.y >= 1.0f)    {   switch(BALLDIR)   {  case N: BALLDIR = S; break;  case NE: BALLDIR = SE; break;  case NW: BALLDIR = SW; break;   }    }    if(GameBall.y <= PlayerPaddle.y + pheight && GameBall.y >= PlayerPaddle.y)    {   if(GameBall.x >= PlayerPaddle.x && GameBall.x <= PlayerPaddle.x2)   {  if(GameBall.x + bwidth >= PlayerPaddle.x && GameBall.x <= PlayerPaddle.x + 0.04)  { BALLDIR = NW;  }  if(GameBall.x >= PlayerPaddle.x + 0.04 && GameBall.x <= PlayerPaddle.x + 0.12)  { BALLDIR = N;  }  if(GameBall.x >= PlayerPaddle.x + 0.12 && GameBall.x <= PlayerPaddle.x + 0.16)  { BALLDIR = NE;  }   }    }    if(GameBall.y + bheight >= AIPaddle.y && GameBall.y <= AIPaddle.y + pheight)    {   if(GameBall.x >= AIPaddle.x && GameBall.x <= AIPaddle.x2)   {  if(GameBall.x + bwidth >= AIPaddle.x && GameBall.x <= AIPaddle.x + 0.04)  { BALLDIR = SW;  }  if(GameBall.x >= AIPaddle.x + 0.04 && GameBall.x <= AIPaddle.x + 0.12)  { BALLDIR = S;  }  if(GameBall.x >= AIPaddle.x + 0.12 && GameBall.x <= AIPaddle.x + 0.16)  { BALLDIR = SE;  }   }    }    if(GameBall.y + bheight < PlayerPaddle.y)    {   aiscore++;   MessageBox(NULL, "Bleh", "Pong", MB_ICONEXCLAMATION);   gameInit();    }    if(GameBall.y > AIPaddle.y)    {   pscore++;   MessageBox(NULL, "Bleh", "Pong", MB_ICONEXCLAMATION);   gameInit();    }}void gameInit(){    int direction;    PlayerPaddle.x = -0.13f;    PlayerPaddle.y = -0.95f;    PlayerPaddle.x2 = PlayerPaddle.x + pwidth;    PlayerPaddle.y2 = PlayerPaddle.y + pheight;    AIPaddle.x = -0.13f;    AIPaddle.y = 0.95f;    AIPaddle.x2 = AIPaddle.x + pwidth;    AIPaddle.y2 = AIPaddle.y - pheight;    GameBall.x = 0.0f;    GameBall.y = 0.0f;    GameBall.x2 = GameBall.x + bwidth;    GameBall.y2 = GameBall.y + bheight;    direction = rand()%6;    switch(direction)    {   case 0:  BALLDIR = N;  break;   case 1:  BALLDIR = S;  break;   case 2:  BALLDIR = NW;  break;   case 3:  BALLDIR = NE;  break;   case 4:  BALLDIR = SW;  break;   case 5:  BALLDIR = SE;  break;    }}void gameRun(HDC hDC){    static int count = 0;    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glPushMatrix();    glBegin(GL_QUADS);   glColor3f(1.0f, 1.0f, 1.0f);   glVertex2f(PlayerPaddle.x, PlayerPaddle.y);   glVertex2f(PlayerPaddle.x, PlayerPaddle.y2);   glVertex2f(PlayerPaddle.x2, PlayerPaddle.y2);   glVertex2f(PlayerPaddle.x2, PlayerPaddle.y);    glEnd();    glBegin(GL_QUADS);   glColor3f(1.0f, 1.0f, 1.0f);   glVertex2f(AIPaddle.x, AIPaddle.y);   glVertex2f(AIPaddle.x, AIPaddle.y2);   glVertex2f(AIPaddle.x2, AIPaddle.y2);   glVertex2f(AIPaddle.x2, AIPaddle.y);    glEnd();    ballMove();    if(count%7 == 0)   moveAIPaddle();    checkBallCollision();    glBegin(GL_QUADS);   glColor3f(1.0f, 1.0f, 1.0f);   glVertex2f(GameBall.x, GameBall.y);   glVertex2f(GameBall.x, GameBall.y2);   glVertex2f(GameBall.x2, GameBall.y2);   glVertex2f(GameBall.x2, GameBall.y);    glEnd();    glPopMatrix();    SwapBuffers(hDC);    count++;    if(aiscore >= 15)    {   MessageBox(NULL, "You Lose!", "Pong", MB_ICONEXCLAMATION);   PostQuitMessage(0);    }    if(pscore >= 15)    {   MessageBox(NULL, "You Win!", "Pong", MB_ICONEXCLAMATION);   PostQuitMessage(0);    }}Yep, there it is 423 lines of goodness! Thanks.   
  Congratulations, Niddles!You've accomplished what many have not, and you've learned an important lesson in the process.  Being a game developer is about 50% skill, and 50% will.  It's not enough to want to be a game programmer, you've got to want to program games...that's an important distinction which many people miss.Again, congratulations...I'll look forward to Breakout next.  [smile] Rating++ for finishing a game, and being an example for all.Cheers!   ;  Very nice! Btw. You might want to consider inserting a Sleep (1) in your main loop to avoid eating up all the CPU resources.   ;  Alright, Thanks alot guys!   ;  Congratulations Niddles   ;  Good job!  And about the Sleep(1), I wouldn't bother.  Real-time games will always eat 100% of your CPU, no matter how primitive they are.  If they're complex, they'll use 100% of the CPU to render 50 FPS.  Yours is simple and probably uses 100% of the CPU to render 4000 FPS.  It's not like you're going to be doing other things on your computer while playing pong.   ;  Hmm.What about other apps like a virus scanner or bittorrent client in the back? What when the game is minimized?There's an alternative to "sleep(1)", though, I'm not sure but there's a "yield()" in most thread implementations that says: "I'm done for now, let the others have some processing time, but let me have some more as soon as possible".sleep(int) doesn't guarantee the sleep duration, but it can, for simple games, still be used to "request" a certain update rate of, say, 50 fps, simply by choosing appropriate parameters other than 0 and 1 :)   ;  Really nice, I felt great when i finished my pong a couple of weeks back, and I moved to tetris and now i'm making a breakout... You should do the same, start a new game project, not a mmorpg or anything like that :P make I simple game that you are gonna be able to finish and most important, FINISH IT!!! :)Keep up the good work man.   ;  Congrats!!! Finished games are rare!You really need to comment you code tho...   ;  good good! :)   