SDL performance(my program uses a lot of resources)

Hey guys,

I’m new to SDL I’m following lazyfoos tutorials,any how I made a simple program that has a stickman and a background,the stickman just moves right when the right key is pressed, and left when the left key is pressed,there is two major problems here,first the stick man moves incredibly slow and is very laggy,

secondly it seems like the program is really putting a beating on my CPU,not only does my fan start to roar and my computer heat up but everytime I run the program my OS(windows) almost crashes(the program doesn’t crash),my code is below all thoughts and theories greatly appreciated,

thanks

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#define center SDL_WINDOWPOS_UNDEFINED

using namespace std;

SDL_Renderer* renderer;
SDL_Window* window;

bool init()
{

    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {

        cout << "error init" << endl;
        return false;
    }

    if(IMG_Init(IMG_INIT_PNG) < 0)
    {

        cout << "error init image" << endl;
        return false;
    }

    window = SDL_CreateWindow("practice",center,center,560,350,SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window,-1,0);
    return true;
}

void cleanUp()
{

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
}


class wTexture
{

public:
    int height;
    int width;
    SDL_Texture* texture;
    int rectH;
    int rectW;
    SDL_Rect rect;

    wTexture()
    {

        height = 0;
        width = 0;
        rectH = 240;
        rectW = 240;
        texture = NULL;
    }

    void loadImage(string str)
    {

        SDL_Surface* surface = NULL;
        SDL_Texture* tempTexture = NULL;
        surface = IMG_Load(str.c_str());
        SDL_SetColorKey(surface,SDL_TRUE,SDL_MapRGB(surface->format,255,255,255));

        width = surface->w;
        height = surface->h;

        tempTexture = SDL_CreateTextureFromSurface(renderer,surface);

        SDL_FreeSurface(surface);

        texture = tempTexture;
        tempTexture = NULL;

    }

    void render(int x,int y)
    {

        rect = {x,y,width,height};
        SDL_RenderCopy(renderer,texture,NULL,&rect);
    }
    void renderMoveRight()
    {

        rect.x++;
        SDL_RenderCopy(renderer,texture,NULL,&rect);
    }

    void renderMoveLeft()
    {

        rect.x--;
        SDL_RenderCopy(renderer,texture,NULL,&rect);
    }

    void dealloc()
    {

        SDL_DestroyTexture(texture);
    }
    ~wTexture()
    {

        dealloc();
    }
};

wTexture backGround;
wTexture sprite;

int main(int agrc,char* argv[])
{

    init();

    SDL_Event event;
    bool quit = false;
    backGround.loadImage("bg.png");
    sprite.loadImage("person.png");

    SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF);
    SDL_RenderClear(renderer);

    backGround.render(0,0);
    sprite.render(240,240);

    while(true)
    {
        
        while(SDL_PollEvent(&event))
        {

            if(event.type == SDL_QUIT)
                quit = true;

            if(event.type == SDL_KEYDOWN)
            {

                switch(event.key.keysym.sym)
                {

                case SDLK_RIGHT:
                    SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF);
                    SDL_RenderClear(renderer);

                    //backGround.render(0,0);
                    sprite.renderMoveRight();
                    backGround.render(0,0);
                    SDL_RenderPresent(renderer);
                    break;
                case SDLK_LEFT:
                    SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF);
                    SDL_RenderClear(renderer);

                    //backGround.render(0,0);
                    sprite.renderMoveLeft();
                    backGround.render(0,0);
                    SDL_RenderPresent(renderer);
                }
            }
        }

        SDL_RenderPresent(renderer);

    }
    cleanUp();
}

SDL is using a lot of resources because its allowed to run as fast as possible.
You can limit the amount of updates SDL does per second by enabling v-sync option.
This will set the amount of frames you update per second based off the refresh rate of the monitor. 60 fps is pretty common.

Other ways include using a timer and allowing SDL to sleep.

renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);