Problem with game code.

This is a discussion on Problem with game code. within the C++ Programming forums, part of the General Programming Boards category; I'm not sure if this is exactly the right forum for this, so I apologise if it isn't. Im writing ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Problem with game code.

    I'm not sure if this is exactly the right forum for this, so I apologise if it isn't.

    Im writing game code for a basic slot machine. To test fuctionality I've written a basic prototype. When ever a certain area of teh screen is clicked, the pictures on display are radomised and redisplayed. The three possible images can come from a group of 6.

    The program compiles and links fine, however as soon as I open it it crashes, ive never come across a problem like this, however could it be caused by a memory managment mistake I'm not seeing?

    The problem should be within this code, as the game engine and all other include files come as example with a book I'm learning from, and have caused no problems in past. The code of the main program is included below.

    Thanks in advance for your help.

    Code:
    //-----------------------------------------------------------------
    // Include Files
    //-----------------------------------------------------------------
    #include "slot.h"
    
    //-----------------------------------------------------------------
    // Game Engine Functions
    //-----------------------------------------------------------------
    
    
    
    BOOL GameInitialize(HINSTANCE hInstance)
    {
      // Create the game engine
      g_pGame = new GameEngine(hInstance, TEXT("UFO"),
        TEXT("UFO"), NULL, NULL, 600, 600);
      if (g_pGame == NULL)
        return FALSE;
    
      // Set the frame rate
      g_pGame->SetFrameRate(0);
    
      // Store the instance handle
      g_hInstance = hInstance;
    
      return TRUE;
    }
    
    void GameStart(HWND hWindow)
    {
    
      // Create and load the background and saucer bitmaps
      int randselect;
    
      HDC hDC = GetDC(hWindow);
      g_pBackground = new Bitmap(hDC, IDB_BACKGROUND, g_hInstance);
      g_pTile[0] = new Bitmap(hDC, IDB_TILE1, g_hInstance);
      g_pTile[1] = new Bitmap(hDC, IDB_TILE2, g_hInstance);
      g_pTile[2] = new Bitmap(hDC, IDB_TILE3, g_hInstance);
      g_pTile[3] = new Bitmap(hDC, IDB_TILE4, g_hInstance);
      g_pTile[4] = new Bitmap(hDC, IDB_TILE5, g_hInstance);
      g_pTile[5] = new Bitmap(hDC, IDB_TILE6, g_hInstance);
    
      srand(GetTickCount());
    
      // Set the initial saucer position and speed
    
      randselect = rand() % 5;
      g_pTile1 = g_pTile[randselect];
      randselect = rand() % 5;
      g_pTile2 = g_pTile[randselect];
      randselect = rand() % 5;
      g_pTile3 = g_pTile[randselect];
    
      g_iTile1X = 50;
      g_iTile1Y = 100;
      g_iTile2X = 200;
      g_iTile2Y = 100;
      g_iTile3X = 400;
      g_iTile3Y = 100;
    
    
    
    }
    
    void GameEnd()
    {
      // Cleanup the background and saucer bitmaps
      delete g_pBackground;
      delete g_pTile[6];
      delete g_pTile1;
      delete g_pTile2;
      delete g_pTile3;
    
    
      // Cleanup the game engine
      delete g_pGame;
    }
    
    void GameActivate(HWND hWindow)
    {
    }
    
    void GameDeactivate(HWND hWindow)
    {
    }
    
    void GamePaint(HDC hDC)
    {
      // Draw the background and saucer bitmaps
      g_pBackground->Draw(hDC, 0, 0);
      g_pTile1->Draw(hDC, g_iTile1X, g_iTile1Y, TRUE);
      g_pTile2->Draw(hDC, g_iTile2X, g_iTile2Y, TRUE);
      g_pTile3->Draw(hDC, g_iTile3X, g_iTile3Y, TRUE);
    
    }
    
    void GameCycle()
    {
    }
    
    void HandleKeys()
    {
    }
    
    void MouseButtonDown(int x, int y, BOOL bLeft)
    {
      int randselect;
    
      if (bLeft)
      {
    
        if ((x >= 200 && x <= 400) &&  (y >= 500 && y <= 600 ))
        {
          randselect = rand() % 5;
          g_pTile1 = g_pTile[randselect];
          randselect = rand() % 5;
          g_pTile2 = g_pTile[randselect];
          randselect = rand() % 5;
          g_pTile3 = g_pTile[randselect];
    
          InvalidateRect(g_pGame->GetWindow(), NULL, FALSE);
        }
        else
        {
          InvalidateRect(g_pGame->GetWindow(), NULL, FALSE);
        }
    
      }
      else
      {
        InvalidateRect(g_pGame->GetWindow(), NULL, FALSE);
      }
    }
    
    void MouseButtonUp(int x, int y, BOOL bLeft)
    {
    }
    
    void MouseMove(int x, int y)
    {
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    How is g_pTile declared? You should specifically delete each thing that you specifically create with new. You don't use new with g_pTile1, g_pTile2, or g_pTile3, so you shouldn't call delete on those. Instead, you should set them to 0 when you delete the values in the g_pTile array (which they are pointing to). To delete the items in the g_pTile array, just delete them the same way you created them with new: delete g_pTile[0], delete g_pTile[1], etc.
    Last edited by Daved; 02-14-2006 at 12:44 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    I've changed it as following however it is still crashing, I tried to follow logically what you were saying, but I still may have missed something:

    Ive also included the header file incase the problem is there

    Code:
    void GameEnd()
    {
      // Cleanup the background and saucer bitmaps
      delete g_pBackground;
      delete g_pTile[0];
      delete g_pTile[1];
      delete g_pTile[2];
      delete g_pTile[3];
      delete g_pTile[4];
      delete g_pTile[5];
      g_pTile1 = 0;
      g_pTile2 = 0;
      g_pTile3 = 0;
    
    
      // Cleanup the game engine
      delete g_pGame;
    }
    Header file:

    Code:
    //-----------------------------------------------------------------
    
    #ifndef UFO_H
    #define UFO_H
    
    //-----------------------------------------------------------------
    // Include Files
    //-----------------------------------------------------------------
    #include <windows.h>
    #include "resource.h"
    #include "GameEngine.h"
    #include "Bitmap.h"
    
    //-----------------------------------------------------------------
    // Global Variables
    //-----------------------------------------------------------------
    HINSTANCE   g_hInstance;
    GameEngine* g_pGame;
    Bitmap*     g_pBackground;
    Bitmap*     g_pTile[6];
    Bitmap*     g_pTile1;
    Bitmap*     g_pTile2;
    Bitmap*     g_pTile3;
    int         g_iTile1X;
    int         g_iTile1Y;
    int         g_iTile2X;
    int         g_iTile2Y;
    int         g_iTile3X;
    int         g_iTile3Y;
    
    
    #endif

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    That deletion code looks better now.

    I can't see any specific problem in the posted code. It is possible that since the application crashes as soon as you open it, the problem would be in the initialization. Maybe you are passing the wrong parameters to the GameEngine constructor.

    If you have access to a debugger, you might be able to see exactly where it is crashing. You can also put cout statements to help you narrow down which code is causing the issue.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Edit:

    Just noticed you are using new to allocate memory -,-. Nevermind.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    21
    Many thanks, You were infact right,

    Code:
    g_pGame = new GameEngine(hInstance, TEXT("UFO"),
        TEXT("UFO"), NULL, NULL, 600, 600);
    The two null values are meant to be identifiers to icon files, I had set them to null as I hadnt designed icons yet, and forgot to change them before compiling. Obviously the GameEngine constructer doesn't like null values for those parameters.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed with game code
    By cakestler in forum C Programming
    Replies: 10
    Last Post: 03-13-2009, 04:28 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-25-2006, 11:14 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21