i made a program to move a dot around on the screen. i was basically just recreating lazyfoo sdl tutorial. the problem is my program eats memory. its memory usage is slowly increasing at like 200kb/s rate. it grows bigger overtime.
i tested it using visual leak detector, it says no memory leak. so what did i do wrong?? please help me. it's driving me crazy.
here is the code: credit to lazyfoo. if the code is too long or unclear i will edit it as necessary. thank you.
//Main Stage
int main(int argc, char* args[])
{
if (!init())
{
printf("failed to init! \n");
}
else
{
cTile *tileSet[TOTAL_TILES];
if (!loadMedia(tileSet))
{
printf("failed to load media! \n");
}
else
{
bool running = true;
SDL_Rect wall;
wall.x = 200;
wall.y = 200;
wall.h = 150;
wall.w = 70;
cDot Dot1(1270, 720);
cDot Dot2(SCREEN_HEIGHT / 4, SCREEN_WIDTH / 4);
cTimer timer;
SDL_Event e;
SDL_Color textColor = { 0, 0, 120, 120 };
std::vector<SDL_Rect> camera;
camera.resize(1);
camera[0] = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
Uint32 startTime = 0;
std::stringstream timeText;
int countedFrames = 0;
timer.start();
while (running)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
running = false;
}
Dot1.handleEvent(e);
for (int i = 0; i < TOTAL_BUTTONS; i++)
{
buttons[i].buttonEvent(&e);
}
}
float avgFPS = countedFrames / (timer.getTicks() / 1000.f);
if (avgFPS > 2000)
{
avgFPS = 0;
}
if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
{
printf("unable to render text ypoooooo");
}
timeText.str("");
timeText << "fps " << avgFPS;
Dot1.move(tileSet);
Dot1.setCamera(camera[0]);
/*camera.x = (Dot1.getPosX() + cDot::DOT_WIDTH / 2) - SCREEN_WIDTH / 2;
camera.y = (Dot1.getPosY() + cDot::DOT_HEIGHT / 2) - SCREEN_HEIGHT / 2;*/
/*if (camera.x < 0){ camera.x = 0; }
if (camera.y < 0){ camera.y = 0; }
if (camera.x > LEVEL_WIDTH - camera.w){ camera.x = LEVEL_WIDTH - camera.w; }
if (camera.y > LEVEL_HEIGHT - camera.h){ camera.y = LEVEL_HEIGHT - camera.h; }*/
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
//SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
//SDL_RenderDrawRect(renderer, &wall);
for (int i = 0; i < TOTAL_TILES; ++i)
{
tileSet[i]->render(camera);
}
//BGTexture.render(0, 0, &camera);
Dot1.render(camera[0]);
textTexture.render(250, 250);
timeTextTexture.render(250, 250 + textTexture.getWidth());
for (int i = 0; i < 2; i++)
{
//buttons[i].render();
}
//Dot2.render(camera.x, camera.y);
SDL_RenderPresent(renderer);
if (timer.started())
{
countedFrames++;
}
else if (!timer.started())
{
countedFrames = 0;
}
}
}
close(tileSet);
}
return 0;
}
EDIT:
solution is so simple THANKS TO PAUL AND WEATHER VANE !!!! apparently i put these lines of code inside a loop. where its main function is basically creating texture. and it loads texture over and over again because it's inside a loop. i just need to delete it. these lines:
if (!textTexture.loadTextFromFile(timeText.str().c_str(), textColor))
{
printf("unable to render text ypoooooo");
}
THANK YOU GUYS!!! :) sorry i can't vote up, apparently it needs more reputation to vote an answer up. :|