cpu usage

hi guys.

i was working on a pong game. and i thought we have a fps limit. so why not a loop per second limit. since it is a pong game no input is needed just mouse coordiantes. so no problem and i did it.

without limitation game loop per second is around 200,000. i limited it to 100.

and the part i m shocked is when i didnt limited loop, windows shows cpu usage around %4. but suprisingly when i limited it, windows shows cpu usage around %17.

what the hell. can someone tell me whatis going on.

here is my codes:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include "include/pongClass.h"

int main ( int argc, char** argv )
{
    pongClass pongG;

    if(pongG.Init() == false)return 1;

    pongG.mainLoop();

    pongG.Clean();

    return 0;
}


pongClass.h
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
#ifndef PONGCLASS_H
#define PONGCLASS_H

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <string>

class pongClass
{
    public:
        pongClass();
        void Clean();
        bool Init();
        void mainLoop();
        void updateGame(int X);
        void updateScreen();
    protected:
    private:
        int             width               = 800,
                        height              = 600,
                        lps                 = 100,
                        fps                 = 25,
                        aSec                = 1000;
        bool            quit                = false;

        Uint32          timeLastUS,
                        timeLastUG,
                        timeNow;
        SDL_Window      *pongWindow;
        SDL_Event       event;
        SDL_Renderer    *pongRenderer;
        SDL_Texture     *texPong;
        SDL_Texture     *texLimitsTop;
        SDL_Texture     *texLimitsBot;
        SDL_Surface     *surfPong;
        SDL_Surface     *surfLimitsTop;
        SDL_Surface     *surfLimitsBot;
        SDL_Rect        rectPong;
        SDL_Rect        rectLimitsTop;
        SDL_Rect        rectLimitsBot;
};

#endif // PONGCLASS_H 


pongClass.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
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
#include "../include/pongClass.h"

pongClass::pongClass()
{
        pongWindow          = NULL;
        pongRenderer        = NULL;
        texLimitsTop        = NULL;
        texLimitsBot        = NULL;
        texPong             = NULL;
        surfPong            = NULL;
        surfLimitsTop       = NULL;
        surfLimitsBot       = NULL;

        rectLimitsTop.h = 12;
        rectLimitsTop.w = width;
        rectLimitsTop.x = 0;
        rectLimitsTop.y = 0;

        rectLimitsBot.h = 12;
        rectLimitsBot.w = width;
        rectLimitsBot.x = 0;
        rectLimitsBot.y = height - rectLimitsBot.h;

        rectPong.h = 120;
        rectPong.w = 20;
        rectPong.x = 17;
        rectPong.y = height / 2 - rectPong.h / 2;
}

void pongClass::Clean()
{
    SDL_DestroyTexture(texLimitsBot);
    SDL_DestroyTexture(texLimitsTop);
    SDL_DestroyTexture(texPong);
    SDL_DestroyRenderer(pongRenderer);
    SDL_DestroyWindow(pongWindow);
    SDL_Quit();
}

bool pongClass::Init()
{
    if(SDL_Init(SDL_INIT_VIDEO) != 0)                                       return false;

    pongWindow = SDL_CreateWindow("Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                  width, height, SDL_WINDOW_SHOWN);
    if(pongWindow == NULL)                                                  return false;

    pongRenderer = SDL_CreateRenderer(pongWindow, -1,
                                      SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);
    if(pongRenderer == NULL)                                                return false;
    SDL_SetRenderDrawColor(pongRenderer, 175, 175, 75, 255);

    surfPong = IMG_Load("gfx\\pong.png");
    surfLimitsTop = IMG_Load("gfx\\limits.png");
    surfLimitsBot = IMG_Load("gfx\\limits.png");
    if(surfPong == NULL||surfLimitsTop == NULL||surfLimitsBot == NULL)      return false;

    texPong = SDL_CreateTextureFromSurface(pongRenderer, surfPong);
    texLimitsTop = SDL_CreateTextureFromSurface(pongRenderer, surfLimitsTop);
    texLimitsBot = SDL_CreateTextureFromSurface(pongRenderer, surfLimitsBot);
    if(texPong == NULL||texLimitsTop == NULL||texLimitsBot == NULL)         return false;
    SDL_FreeSurface(surfLimitsTop);
    SDL_FreeSurface(surfLimitsBot);
    SDL_FreeSurface(surfPong);


    return true;
}

void pongClass::mainLoop()
{
    timeLastUS = SDL_GetTicks();
    updateScreen();
    int a = 0;
    Uint32 timeCheck = SDL_GetTicks();
    while(!quit)
    {
        timeNow = SDL_GetTicks();
        if(timeNow - timeLastUG >= aSec / lps)
        {
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    quit = true;
                    break;
                case SDL_KEYDOWN:
                    if (event.key.keysym.sym == SDLK_ESCAPE)quit = true;
                    break;
                case SDL_MOUSEMOTION:
                    updateGame(event.motion.y);
                    break;
                default:
                    break;
                }
            }
            timeNow = SDL_GetTicks();
            if(timeNow - timeLastUS >= aSec / fps)
            {
                updateScreen();
            }

            timeLastUG = SDL_GetTicks();
        }
    }
}

void pongClass::updateGame(int X)
{
    if(rectLimitsTop.y + rectLimitsTop.h >= X - rectPong.h /2)
    {
        rectPong.y = rectLimitsTop.y + rectLimitsTop.h;
    }
    else if(rectLimitsBot.y <= X + rectPong.h /2)
    {
        rectPong.y = rectLimitsBot.y - rectPong.h;
    }
    else
    {
        rectPong.y = X - rectPong.h / 2;
    }
}

void pongClass::updateScreen()
{
    SDL_RenderClear(pongRenderer);
    SDL_RenderCopy(pongRenderer, texLimitsTop, NULL, &rectLimitsTop);
    SDL_RenderCopy(pongRenderer, texLimitsBot, NULL, &rectLimitsBot);
    SDL_RenderCopy(pongRenderer, texPong, NULL, &rectPong);
    SDL_RenderPresent(pongRenderer);
}
Last edited on
hmm. i will try to fix the situation. lets see what can i do
ok. i just used SDL_Delay() func. and it uses around %4 power of the cpu. but i doubt this tecnique will be useful in any other game than pong
Last edited on
Every program should sleep/wait/delay unless it needs 100% CPU time.

So yeah... SDL_Delay is the right thing to do. And you probably should be doing it in every game.
Topic archived. No new replies allowed.