Hi, SDL Community!
I ran into a problem in my game. When I leave the game idle for a good 45 minutes, this error appears in the terminal:
I am not sure what is causing this error but I do believe the cause is this:
//Init.cpp file
//load the sprites on the screen
bool Init::render_texture(int x, int y, SDL_Texture* image, SDL_Renderer* render, SDL_Rect* clip)
{
SDL_Rect renderQuad;
renderQuad.x = x;
renderQuad.y = y;
if (clip != NULL)
{
renderQuad.h = clip->h;
renderQuad.w = clip->w;
}
else
{
SDL_QueryTexture(image, NULL, NULL, &renderQuad.w, &renderQuad.h);
}
SDL_RenderCopy(render, image, clip, &renderQuad);
return true;
}
Or maybe it is my while loop:
//Init.cpp file
//render the world
bool Init::render_world(TTF_Font* fontTypes, SDL_Texture *image, SDL_Renderer* render)
{
//to count the frame rate
Timer fps;
//to cap the frame rate
Timer capFps;
//load the textures
ImageManager load;
//the stream for the timer
std::stringstream fpsFont;
//draw the world
World world;
//the timer for the fps
int frame = 0;
//the x and y position of the arrow selection sprite
int x = 315, y = 280;
//givee the font color
SDL_Color fontColor = { 255, 0, 0 };
std::string choices[] = { "Fargo Game Engine", "Start engine", "Exit engine" };
//boolean variable to accept user input for quit, start, and menu.
bool quit = false;
bool menu = true;
bool start = false;
fps.startClock();
//Main Loop:
while (!quit)
{
//handle events and menu selector!
while (SDL_PollEvent(&event))
{
//user request quit
if (event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE)
{
quit = true;
}
switch (menu)
{
case true:
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_w)
{
render_texture(x = 315, y = 280, image, render, &sprites.at(arrow));
}
else if (event.key.keysym.sym == SDLK_s)
{
render_texture(x = 315, y = 310, image, render, &sprites.at(arrow));
}
else if (event.key.keysym.sym == SDLK_RETURN)
{
if (x == 315 && y == 280)
{
start = true;
menu = false;
}
else if (x == 315 && y == 310)
{
quit = true;
}
}
}
break;
case false:
world.player_input(event);
break;
}
}
//Calculate and correct fps
float avgFPS = frame / (fps.fetch_ticks() / 1000.f);
if (avgFPS > 2000000)
{
avgFPS = 0;
}
fpsFont.str("");
fpsFont << "FPS:" << avgFPS;
//this will load the fps font images
fpsText = load.load_font(fpsFont.str().c_str(), fontTypes, fontColor, render);
SDL_RenderClear(render);
if (menu == true)
{
//the arrow selector texture being loaded
render_texture(x, y, image, render, &sprites.at(arrow));
//render the text of choices
render_texture(340, 250, load.load_font(choices[0], fontTypes, fontColor, render), render, NULL);
render_texture(340, 280, load.load_font(choices[1], fontTypes, fontColor, render), render, NULL);
render_texture(340, 310, load.load_font(choices[2], fontTypes, fontColor, render), render, NULL);
}
else if (start == true)
{
//render the world
world.load_tiles(image, render, sprites);
}
//render the fps font
render_texture(10, 580, fpsText, render, NULL);
SDL_RenderPresent(render);
frame++;
//If frame finished early
int frameTicks = capFps.fetch_ticks();
if (frameTicks < fps.screenTick)
{
//Wait remaining time
SDL_Delay(fps.screenTick - frameTicks);
}
}
close(fontTypes, image, render);
return quit;
}
Please, help me understand why is this happening. I think it can be a memory leak but the pointers are private and being reused.