The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Simple program overloads CPU
Blaze


Joined: 05 Jul 2015
Posts: 11
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:

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;
}
Naith


Joined: 03 Jul 2014
Posts: 158
In the end of the mainloop of your application (after 'SDL_RenderPresent(renderer);'), add 'SDL_Delay(1);'.
This will allow the processor to take a break from your application and do something else, which will limit the processor usage on your application.

I couldn't see anything in your code that would cause high CPU-usage though.
Blaze


Joined: 05 Jul 2015
Posts: 11
Thank you, it now runs with <0% CPU usage. For the future, will setting a maximum framerate be a working alternative to using SDL_Delay()?
Naith


Joined: 03 Jul 2014
Posts: 158
Not sure I understand your question correctly.

Whenever you wanna do a frame-rate cap function, you normally set a maximum frame-rate (60 FPS for example) and then use SDL_Delay to limit the frame rate to be <= to 60 FPS.
If you don't wanna have such function, you can always create your program with VSync enabled but you won't be able to set the FPS you want and the highest FPS will instead be set to your monitor's refresh rate.

Both of this alternatives will indeed limit the processor usage on your program.
Blaze


Joined: 05 Jul 2015
Posts: 11
Okay, I understand. Thank you