SDL Game Lag??[pong game]

This is really frustrating me...Why is it that the game runs smoothly sometimes but other times it lags so badly...Please help me.. Game runs fine if you set balls velocity to 500 instead of 300..Why would the delta time get all messed up depending on the balls velocity
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include "Window.h"
#include <SDL2_ttf/SDL_ttf.h>


using namespace std;




void gameLoop();
void update();
int main()
{
    gameLoop();
    
    SDL_Quit();
    
    
    return 0;
}

void gameLoop()
{
 
    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();
    
    Window win;

    
    SDL_Renderer *render = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    
    SDL_Surface *paddleSurf;
    
    paddleSurf = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(paddleSurf, NULL, SDL_MapRGB(paddleSurf->format, 255, 0, 0));
    
    SDL_Texture* paddleTex = SDL_CreateTextureFromSurface(render, paddleSurf);
    
    SDL_Event event;
    
  
    
    SDL_Rect paddle;
    
    paddle.x = 10;
    paddle.y = 400;
    
    //paddle dimensions
    paddle.w = 100;
    paddle.h = 20;
    
    
    
    SDL_Surface *ballSurf = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(ballSurf, NULL, SDL_MapRGB(ballSurf->format, 0xFF, 0xFF, 0xFF));
    SDL_Texture *ballTex = SDL_CreateTextureFromSurface(render, ballSurf);
    
    SDL_Surface *bgSurf = SDL_CreateRGBSurface(0, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(bgSurf, NULL, SDL_MapRGB(ballSurf->format, 0, 0, 255));
    SDL_Texture *bgTex  = SDL_CreateTextureFromSurface(render, bgSurf);
    
    SDL_Rect ball;
    
    ball.x = 640/2;
    ball.y = 480/2;
    
    //ball dimenions
    ball.w = 10;
    ball.h = 10;
    
    


    double deltaTime = 0.0f;
    double currentTime = 0;
    double lastTime = 0;
    
    
    double ballVel   = 300;
    double ballVely  = 300;
    
    double paddleVel = 600;
    bool wallhit = false;
    
    const Uint8 *state = SDL_GetKeyboardState(NULL);
    
    while (event.type != SDL_QUIT) {
        SDL_PollEvent(&event);
        
        
        currentTime = SDL_GetTicks();
        deltaTime =(currentTime - lastTime) / 1000;
        lastTime = currentTime;
        
        cout << deltaTime << endl;
        
        ball.x += ballVel  * deltaTime;
        ball.y += ballVely * deltaTime;
        
        if (ball.x > 640 || ball.x < 0) {
            ballVel *= -1;
            wallhit = true;
        }
        if (ball.y > 480 || ball.y < 0) {
            ballVely *= -1;
            wallhit = false;
        }
        
        if (state[SDL_SCANCODE_LEFT] && paddle.x > 0) {
            paddle.x-=paddleVel*deltaTime;
        }
        else if(state[SDL_SCANCODE_RIGHT] && paddle.x < 535)
        {
            paddle.x+=paddleVel*deltaTime;
        }
        
        if (state[SDL_SCANCODE_A] && paddle.x > 0) {
            paddle.x-=paddleVel*deltaTime;
        }
        else if(state[SDL_SCANCODE_D] && paddle.x < 535)
        {
            paddle.x+=paddleVel*deltaTime;
        }
        

        if (ball.x > paddle.x && ball.x < paddle.x+100 &&
            ball.y > paddle.y-10 &&  ball.y < paddle.y+10)  {
            ballVely *= -1;
        }
        
        
        SDL_SetRenderDrawColor(render, 25, 255, 10, 0);
        SDL_RenderClear(render);
       
        if (wallhit == true) {
            SDL_RenderCopy(render, bgTex, NULL, NULL);
        }
        
        SDL_RenderCopy(render, ballTex, NULL, &ball);
        SDL_RenderCopy(render, paddleTex, NULL, &paddle);
        SDL_RenderPresent(render);

    }
    
    SDL_FreeSurface(paddleSurf);
    SDL_FreeSurface(ballSurf);
    SDL_DestroyTexture(ballTex);
    SDL_DestroyTexture(paddleTex);
    SDL_DestroyRenderer(render);
    SDL_DestroyWindow(win);
}



Last edited on
Don't use cout in the game loop. It can slow things down quite a bit.

You are using integers to store the position of things so you will get rounding errors. Not sure it's enough for you to notice, it depend on what delta times you're getting.

It is often recommended to use a fixed time step, i.e. running the game logic with always the same value of delta time. If you have acceleration and other types of more advanced physics in the game the delta time can have big impact on the end result. In your game things seem to be moving in straight lines with fixed speeds so I don't think you will not notice the same kind of problem if you let the delta time vary but you probably still need to limit it somehow because if the delta time becomes too big the ball will move through walls and paddles.

You don't initialize the event variable so if you are unlucky event.type will be equal to SDL_QUIT the first time you arrive at the loop on line 89. Normally SDL_PollEvent is used in a loop. The way you have it now will not work if you decide to handle other types of events than SDL_QUIT. What I usually do is to handle the events in a loop and then set another variable to signal that the loop should end.
Last edited on
Topic archived. No new replies allowed.