hey everyone, ive been making some simple games recently with c++ and sdl, I use Dev-cpp as my compiler. I just was making a game where a missle follows after a planet, and it freezes after a few seconds of playing. When I debug it however it doesnt freeze. I'm totally perplexed, anyone know what might be the problem?

My only guess is that before it freezes the screen moves slightly, and i dont have anything built to allow the screen to move, which would freeze it but why is the screen moving?


I'm sure the problem isnt setup.h frameratecap.h or formatload.h
Main File

#include "Setup.h"
#include "FrameRateCap.h"
#include "C-Planet.cpp"
#include "C-Missle.cpp"

//remember to have sdl.dll in the folder with your game

int main(int argc, char *argv[])
{
    SDL_Surface *Screen;
    
    Screen = Setup(1000,1000,32);//sets up the screen and SDL
    Planet Earth(200,100,100,Screen);//initializes Planet Earth, its constructor function
    Missle Missle1(450,450,Screen); //constructor for missle, intializing Missle1 
    int Cap = 1; //the frame rate cap IMPORTANT variable, makes game smooth at 20, 30 is okay to
    Uint32 Last = SDL_GetTicks();// gets the basis for frame rate cap function, the function only caps it remember
    
    
    while(1)
    {
            Last = FrameRateCap(Cap, Last); //passes last into frame rate cap which caps the frame rate at cap and returns a value for last to use next time the while loop cycles
            Earth.Update();//see C-Planet class
            Missle1.Update(Earth.Dest.x,Earth.Dest.y);//gives the missle Earths position to track and has to update after Earth to be up to date on Earths exact coordinates
            if (GetAsyncKeyState(VK_SHIFT))//if shift is pressed it quits from SDl and the program
            {
                                           SDL_Quit();
                                           return 0;
                                           }
            SDL_Flip(Screen);//updates the screen with all objects preloaded at onceto prevent tearing from updating everything one at a time directly to the screen, this way there updated one at a time to a pre screen which then updates the screen
            }//end of while loop brace
    return 0;
}//end of program brace

Planet Class

#include "FormatLoad.h"
#include "SDL/SDL.h"
#include "Windows.h"
class Planet
{
      public:
             int VelocityY, VelocityX; //track velocity
             int Mass; //mass, used for gravity
             SDL_Surface *Earth, *Screen;
             SDL_Rect Source,Dest;
             Planet(int MassTemp, int PosX, int PosY, SDL_Surface *TempScreen)
             {
                   Mass = MassTemp;//sets the mass of the planet
                   VelocityY = 0;
                   VelocityX = 0;//sets velocity
                   Earth = FormatLoad("Earth.bmp");//loads planet into memory
                   Screen = TempScreen;
                   
                   Source.x = 0;
                   Source.y = 0;
                   Source.w = Earth->w;//width is the sdl_surface planets width
                   Source.h = Earth->h;
                   
                   Dest.x = PosX;//position is the position passed to the function
                   Dest.y = PosY;
                   Dest.w = Earth->w;
                   Dest.h = Earth->h;
                   SDL_BlitSurface(Earth,&Source,Screen,&Dest);//Blits planet to screen
                   return;
                   }
             int Update()
             {
                 if (GetAsyncKeyState(VK_UP))//these four check for key presses on the arrow keys and adjust velocity accordingly
                 {
                                           
                                           VelocityY++;
                                           }
                 if (GetAsyncKeyState(VK_DOWN))//these four check for key presses on the arrow keys and adjust velocity accordingly
                 {
                                            VelocityY--;
                                            }
                 if (GetAsyncKeyState(VK_RIGHT))//these four check for key presses on the arrow keys and adjust velocity accordingly
                 {
                                             VelocityX++;
                                             }
                 if (GetAsyncKeyState(VK_LEFT))
                 {
                                              VelocityX--;
                                              }
                 SDL_FillRect(Screen,&Dest,SDL_MapRGB(Screen->format,0,0,0));//fills in the planets old space with the color 0,0,0 (black)
                 Dest.x = Dest.x + VelocityX;//updates x position
                 Dest.y = Dest.y - VelocityY;//updates y position, y graph of positions is inverted with sdl so you subtract the velocity instead of adding,  
                 SDL_BlitSurface(Earth,&Source,Screen,&Dest);//Blits current planet to screen
                 }
                 };

And Finally the Missle Class, I think the problem is in the update function here.

#include "SDL/SDL.h"
#include "Windows.h"

class Missle
{
      public:
             int VelocityX, VelocityY;//velocity
             SDL_Surface *Tracker, *Screen;
             SDL_Rect Source,Dest;
             
             SDL_Surface* FormatAndLoad(const char *ImageToLoad)//multiple redefinitions blah blah, this is easier,formatANDload
             {
                     SDL_Surface *temp;
                     temp = SDL_LoadBMP(ImageToLoad);
                     SDL_Surface *Image;
                     Image = (SDL_DisplayFormat(temp));
                     SDL_FreeSurface(temp);
                     return Image;
                     }
             
             Missle(int PosX,int PosY, SDL_Surface *TempScreen)//constructor
             {
                   VelocityX = 0;//initializes variables, otherwise the comparisons (< or >) in update have nothing to compare to and the missle doesnt refresh and the program crashes
                   VelocityY = 0;
                   Screen = TempScreen;//gives the missle a pointer to the screen
                   
                   
                   
                   
                   Tracker = FormatAndLoad("Tracker.bmp");//loads the missle bitmap
                   
                   Dest.x = PosX;//initializes the position with the variables passed into the constructor
                   Dest.y = PosY;
                   Dest.w = Tracker->w;
                   Dest.h = Tracker->h;
                   
                   Source.x = 0;
                   Source.y = 0;
                   Source.w = Tracker->w;//in bitmapitializes width and height of tracker
                   Source.h = Tracker->h;
                   
                   SDL_BlitSurface(Tracker,&Source,Screen,&Dest);//Blits missle to the screen
                   return;//no return value for a constructor
                   }//end of missle function
             int Update(Sint16 PlanetX,Sint16 PlanetY)
             {
                             if (PlanetX > Dest.x)//exactly the same as y's below
                             {
                                         VelocityX++;
                                         }
                             if (PlanetX < Dest.x)
                             {
                                         VelocityX--;
                                         }
                             if (PlanetY < Dest.y)//if the Planet is higher than the missle 
                             {
                                         VelocityY++;//increse velocity
                                         }
                             if (PlanetY > Dest.y)//if the missle is higher than the planet
                             {
                                         VelocityY--;//decrease velcoity
                                         }
                             SDL_FillRect(Screen,&Dest,SDL_MapRGB(Screen->format,0,0,0));//fills in the missles old space with the color 0,0,0 (black)
                             Dest.x = Dest.x + VelocityX;//updates X position according to velocity X
                             Dest.y = Dest.y - VelocityY;//updates y position, y graph of positions is inverted with sdl so you subtract the velocity instead of adding, updates x position 
                             SDL_BlitSurface(Tracker,&Source,Screen,&Dest);
                             return 0;
                             }//end of update function
             };//end of class

sorry for the length, thanks for any help

Recommended Answers

All 2 Replies

Hi,
This is not compilable ...

Anyway, there's one method of debugging you could try: don't set any breakpoints, but run it in debug (just press F5) And wait for it to get stuck; then go to VS and from the debug menu press pause. Now you should be able to see where your threads are stuck.

If it doesn't get stuck this way, get it stuck the usual way and then attach the debugger to it and do the same stuff as above.

Debug run vs Release not run is quite often an initialization error. Memory typically isn't initialized in Release. Your constructor shold initialize your data members to a known good value. Before use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.