Help with C++ game glitch (im a beginner)

Started by
7 comments, last by Zahlman 16 years ago
Hi, I'm a beginner to computer programming i took a half credit school C++ course, and I'm making a 2D game using SDL. Whenever I run my game it's working perfectly, but if it's running for over 3 minutes it starts lagging, and if it runs for around 5 minutes it has to shut down. When I run a debug, after the five minutes my game always goes into a break, and the little arrow that points to the line at which the program stopped is at different places each time. If I run the game and dont press any of the controls, it stops at the same line every time, and its a line that makes a surface's colour transparent. If I do press the controls when the game starts, it stops on other random lines of my code. Also when I run the game for over 3 minutes not just the game lags but my entire computer does until I stop the program. The last time that I ran it for the full five minutes when the program closed a Windows message popped up that said Windows virtual memory minimum too low. If anyone knows what the problem could be related to please tell me. If you need me to send you a copy of my source code I can as well. Also, my entire code is in one 65kb .cpp file and its 2258 lines long. I don't know if thats too long but I never learned how to make a program with multiple .cpp files. Please, I'd really appreciate any help you could give, thanks! Russt17 russtaylor17@hotmail.com
Advertisement
Are you excessively allocating things? Are you ensuring everything you allocate gets deleted afterwards? This sounds like leaky memory. Make sure for every surface you create you destroy it as well.

Also do learn to separate your code into smaller modules, I know it's not a big deal now with small projects like this, but it's a habit to get into before your programs grow to crazy lengths.
how do I destroy a surface? currently when one of my surfaces is inactive I just dont blit it and I put its coordinates as x = 1000 and y = 1000 so that its off the screen.
SDL_FreeSurface
With respect to your slow game, you may wanna to check and make sure that you are correctly deleting all your pointers and you do not have any pointers leaking memory. I had a problem like that once and found that it was be because I was not deleting some of my pointers properly i.e.

pointer = 0;
delete pointer;

and hence I was leaking memory. With respect to using multiple cpp files. you may want to use cpp files together with a header files. The header files declare your prototype functions and the cpp files define them. You can then include your header file in the files in which you want to use the functions. take note of the include guards i.e.
#ifndef EXAMPLE_H_
#define EXAMPLE_H_

......

#endif

without them, you are going to get a lot of class redefinition errors. Hope this helps. Below is an example
e.g.


Header file

#ifndef EXAMPLE_H_#define EXAMPLE_H_#include <iostream>class Example{//Declare prototypes of functionspublic:       Example();      ~Example();      int exFunc();private:      void otherExFunc();};#endif/*EXAMPLE_H_*/


the cpp file


#include "Example.h"#include <iostream>//Define functions//Define constructorExample::Example(){                  }//Define destructorExample::~Example(){                  }//Define exFunc()int Example::exFunc(){                   int num =1;             cout<<"this is exFunc()"<<endl;              return num;                  }//Define otherExFunc()void Example::otherExFunc(){             cout<<"this is otherExFunc()i.e. Its private"<<endl;                  }


You could also have another cpp file to contain your main from where you can have an instance of your class and through it, you can calll your functions

e.g.

#include "Example.h"#include <iostream>int main (){//declare instance of calssExample *ex = new Example;//Call exFunc() via instance of classex->exFunc();// null terminate pointerex = 0;//Delete pointerdelete ex;return 0;}


ok thanks for all your guy's help but i think im too much of a beginner for this i need some sort of online tutorial because i hardly understand anything. does anybody know a really good online basic beginner tutorial for C++ / SDL?
I found the these helplful, cprogramming.com, cplusplus.com and also http://members.tripod.com/sundog97/tutorial_cpp/index.html. Hope they work for you.
If you're working on Linux, Valgrind is your friend. There are similar tools for Windows and Mac, but they all seem to cost money. IBM's Purify has a trial version though.
Quote:Original post by russt17
Whenever I run my game it's working perfectly, but if it's running for over 3 minutes it starts lagging, and if it runs for around 5 minutes it has to shut down. etc. etc.


Try taking stuff out until it doesn't happen any more; figure out where the problem is.

Quote:If I run the game and dont press any of the controls, it stops at the same line every time, and its a line that makes a surface's colour transparent.


You should only have to do this once per surface. Are you possibly re-creating the surface inside a loop? That's bad. Get your images ready ahead of time.

Quote:The last time that I ran it for the full five minutes when the program closed a Windows message popped up that said Windows virtual memory minimum too low.


Yep, that sounds like a memory leak all right. What's happening is that you keep re-loading the image from disk, putting more copies of it into memory, and abandoning old copies. You need to use SDL_FreeSurface() to tell SDL that a surface is no longer in use; but avoid doing this more than necessary - i.e., don't keep reloading the thing!

Quote:Also, my entire code is in one 65kb .cpp file and its 2258 lines long. I don't know if thats too long but I never learned how to make a program with multiple .cpp files.


Then learn.

This topic is closed to new replies.

Advertisement