My test program runs fine but when I check it in Task Manager it takes up about 30-40% of my CPU. For comparison another SDL game takes about 0-1% of the CPU on the menu. Can someone help me find what is causing this? Below is the entirety of the code:
//Main menu test
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
//Screen width and height
const int SCREEN_WIDTH = 320;
const int SCREEN_HEIGHT = 224;
//The levels the player could be in
enum levels
{
MAIN_MENU,
LEVEL_1,
LEVEL_2,
LEVEL_3,
LEVEL_4,
LEVEL_5,
LEVEL_6,
LEVEL_7,
LEVEL_8,
LEVEL_9,
LEVEL_10,
LEVEL_11,
LEVEL_12,
LEVEL_EXTRA,
CREDITS
};
//The level the player is actually in
levels currentLevel = MAIN_MENU;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
//Current texture
SDL_Texture* currentTexture = NULL;
//Rect for right side panel
SDL_Rect rightPanelRect;
SDL_Texture* loadTexture( std::string path )
{
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
if( loadedSurface == NULL )
{
printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
}
else
{
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
if( newTexture == NULL )
{
printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
}
return newTexture;
}
//Function for loading images
bool loadImages()
{
//Image loading success flag
bool success = true;
//Switch determines which images must be loaded for each level
switch (currentLevel)
{
case MAIN_MENU:
currentTexture = loadTexture("rightPanel.png");
if( currentTexture == NULL )
{
printf( "Failed to load texture image!\n" );
success = false;
}
break;
case LEVEL_1:
break;
case LEVEL_2:
break;
case LEVEL_3:
break;
case LEVEL_4:
break;
case LEVEL_5:
break;
case LEVEL_6:
break;
case LEVEL_7:
break;
case LEVEL_8:
break;
case LEVEL_9:
break;
case LEVEL_10:
break;
case LEVEL_11:
break;
case LEVEL_12:
break;
case LEVEL_EXTRA:
break;
case CREDITS:
break;
}
return success;
}
//Initialize SDL
bool init()
{
bool success = true;
//Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO)<0)
{
printf("SDL could not initialize! %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1") )
{
printf( "Warning! Linear texture filtering not enabled!");
}
//Create window
window = SDL_CreateWindow("Main menu test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(window == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(renderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success == false;
}
else
{
//Initialize renderer draw color
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
success = false;
}
}
}
}
return success;
}
void close()
{
SDL_DestroyTexture(currentTexture);
currentTexture = NULL;
SDL_DestroyRenderer(renderer);
renderer = NULL;
SDL_DestroyWindow(window);
window = NULL;
IMG_Quit();
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if(!init() )
{
printf("Failed to initialize!\n");
}
else
{
bool quit = false;
SDL_Event e;
rightPanelRect.x = 256;
rightPanelRect.y = 0;
rightPanelRect.w = 64;
rightPanelRect.h = 224;
loadImages();
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}
SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, currentTexture, NULL, &rightPanelRect);
SDL_RenderPresent(renderer);
}
}
close();
return 0;
}