SDL timer not initialized
This is my first post here so I'd like to say hello to everyone.
Now, to the description of my problem.
In file main.cpp in main() I create instance of class CGame which in it's constructor initializes SDL (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)), OpenGL & adds SDL timer which updates my fps & ups count. Works alright.
Afterwards I create instance of class CMap which is declared in map.h and defined in map.cpp which in turn creates instance of class CAnim declared in util.h and defined in util.cpp. In constuctor of CAnim I create SDL timer, however I get error saying "You must call SDL_Init(SDL_INIT_TIMER) first" and to overcome this I have to add SDL_InitSubSystem(SDL_INIT_TIMER).
What's wrong?
Global variables will be created before main() runs. The best solution is to not use a global variable, then you get to create it at a time of your choosing.
If you have a CMap at file scope in map.cpp, or a CAnim at file scope in util.cpp, they are globals and get created before you call SDL_Init in your game's constructor.
If that's not the situation you have, you should post more code to show the precise situation.
If that's not the situation you have, you should post more code to show the precise situation.
Thanks for reply
Here is a little bit of code so it should clear up any uncertainty.
As you can see CMap *p_map is declared before SDL_Init() call however it is created after CGame constructor has been run so SDL_AddTimer() in CMap isn't run before SDL_Init().
You can also take a look at the source itself ftp://grodzix.net/src.tgz
Here is a little bit of code so it should clear up any uncertainty.
from main.cpp#include <iostream>#include <GL/glu.h>#include <SDL/SDL.h>#include <SDL/SDL_mixer.h>#include "main.h"#include "map.h"CGame *p_game;CMap *p_map;//////////main()////////int main(int argc, char **argv){ p_game = new CGame(500, 500, false, 17.0, 5, 500, 32, 44100, AUDIO_S16LSB); SDL_InitSubSystem(SDL_INIT_TIMER); p_map = new CMap(p_game);[...]
CGame from main.cpp//////////////////CGame::CGame()////////////////CGame::CGame(int width, int height, bool fullscreen, float period, int max_frameskip, Uint32 fps_ups_timer_interval, int num_of_channels, int frequency, Uint16 format) :PERIOD(period), MAX_FRAMESKIP(max_frameskip), fps(0), ups(0), WIDTH(width), HEIGHT(height), frames(0), updates(0){ if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1){ std::cerr << SDL_GetError() << "\n"; this->state = STATE_FINISH; return; } //setup SDL for OpenGL SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //create window if(fullscreen){ if(SDL_SetVideoMode(this->WIDTH, this->HEIGHT, 0, SDL_OPENGL | SDL_DOUBLEBUF | SDL_FULLSCREEN) == NULL){ std::cerr << SDL_GetError() << "\n"; this->state = STATE_FINISH; return; } }else{ if(SDL_SetVideoMode(this->WIDTH, this->HEIGHT, 0, SDL_OPENGL | SDL_DOUBLEBUF) == NULL){ std::cerr << SDL_GetError() << "\n"; this->state = STATE_FINISH; return; } } SDL_WM_SetCaption(APP_NAME, NULL);[...]
As you can see CMap *p_map is declared before SDL_Init() call however it is created after CGame constructor has been run so SDL_AddTimer() in CMap isn't run before SDL_Init().
You can also take a look at the source itself ftp://grodzix.net/src.tgz
Quote:Original post by grodzix
Afterwards I create instance of class CMap which is declared in map.h and defined in map.cpp which in turn creates instance of class CAnim declared in util.h and defined in util.cpp.
Your mistake is here. You don't actually create an instance of CAnim inside a CMap. You create a CAnim as a global (map.cpp, line 19). This happens before main() is called, ie. before SDL_Init is called.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement