Player movement feels laggy (SDL)

I'm trying to make a simple 2D rpg game, and I have some problems. I have created a player sprite that I could move around the screen. But the sprite movement feels laggy and if I draw fewer tiles the player sprite starts suddenly to move smoother. The memory and the cpu usage are low so I think it must be a problem with the way I render things.

some parts of init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bool initialize::initWindow()
{

    bool succsess = true;

    if (SDL_Init(SDL_INIT_EVERYTHING)<0)
    {
        succsess = false;
        std::cout << "Could not initialize SDL: " << SDL_GetError();
    }
    else
    {

    SDL_DisplayMode current;
    SDL_GetCurrentDisplayMode(0, &current);

    window = SDL_CreateWindow("SDL TEST",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              current.w,
                              current.h,
                              SDL_WINDOW_SHOWN);
    }
    if (window == 0)
    {
        succsess = false;
        std::cout << "Could not create window: " << SDL_GetError();
    }
    else
    {
         renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    }

    return succsess;
}


some parts of main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
int main(int argc, char* argv[])
{
    bool quit = false;
    uint32_t FrameStartTimeMs = 0;

    initialize *init = new initialize();
    close *freeMem = new close();
    readMapFromFile *readTileMap = new readMapFromFile();
    playerMovement *movePlayer = new playerMovement();
    drawPlayer *drawPly = new drawPlayer();

    ///Initialize Window
    init->initWindow();

    ///Import Map
    readTileMap->readTileMap();

    tile MAP[TILES_X][TILES_Y];

    while(!quit)
    {
    SDL_GetMouseState(&mouseX,&mouseY);
    FrameStartTimeMs = SDL_GetTicks();

    ///Wait for Inputs
    while (SDL_PollEvent(&event)!= 0)
    {
        if (event.type == SDL_QUIT)
        {
            quit = true;
        }
        if (event.type == SDL_KEYDOWN)
        {
            movePlayer->playerKeyDown();
        }
        else if (event.type == SDL_KEYUP)
        {
            movePlayer->playerKeyUp();
        }
    }

    movePlayer->movePlayer();

    ///Draw Tiles
    for(int y = 0; y < TILES_Y; ++y)
    {
        for(int x = 0; x < TILES_X; ++x)
        {
        Vector2<int> position;

        position.x = x*tile::TILESIZE_X;
        position.y = y*tile::TILESIZE_Y;

        switch(tileMap[y][x])
        {
           case 1:
           MAP[x][y].render(position.x, position.y,1);
           break;

           case 2:
           MAP[x][y].render(position.x, position.y,2);
           break;

           case 3:
           MAP[x][y].render(position.x, position.y,3);
           break;
        }
        }
    }

    drawPly->drawPlayerSprite(playerMoveX,playerMoveY);

    ///Update Screen
    SDL_RenderPresent(renderer);

    ///60 FPS
    while(SDL_GetTicks() - FrameStartTimeMs < 1000/FPS);
    }

    ///Free Memory
    freeMem->freeMemory();
    return 0;
}


some parts of tile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void tile::render(int PositionX, int PositionY, int type)
{
    tileTexture = SDL_CreateTextureFromSurface(renderer, tileSurface);

    switch(type)
    {
    case 1:
    tileRectDst.x = PositionX;
    tileRectDst.y = PositionY;
    SDL_RenderCopy(renderer,tileTexture,&tileRectSrc[WALL],&tileRectDst);
    break;

    case 2:
    tileRectDst.x = PositionX;
    tileRectDst.y = PositionY;
    SDL_RenderCopy(renderer,tileTexture,&tileRectSrc[GRASS],&tileRectDst);
    break;

    case 3:
    tileRectDst.x = PositionX;
    tileRectDst.y = PositionY;
    SDL_RenderCopy(renderer,tileTexture,&tileRectSrc[WATER],&tileRectDst);
    break;
    }
    SDL_DestroyTexture(tileTexture);
}


draw.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SDL_Rect playerDst;
SDL_Rect playerSrc;
SDL_Surface *playerSurface = SDL_LoadBMP("player.bmp");
SDL_Texture *playerTexture;

void drawPlayer::drawPlayerSprite(int playerX, int playerY)
{
    playerTexture = SDL_CreateTextureFromSurface(renderer, playerSurface);
    playerDst.x = playerX;
    playerDst.y = playerY;
    playerDst.w = 50;
    playerDst.h = 50;
    SDL_RenderCopy(renderer,playerTexture,NULL,&playerDst);
    SDL_DestroyTexture(playerTexture);
}

Last edited on
Creating textures are slow. You should create the texture and then reuse the same texture each time the draw/render function is called.
Topic archived. No new replies allowed.