SDL game dev support

Hey guys, I have been trying SDL 2.0 lately and I have decided to make a Tetris game as my 1st game.

I have coded to load a square and move around while it is moving vertically down as usually happens in Tetris game.

My problem is I don't know how to make a square appear on the screen after my 1st square reaches the bottom of the screen.

This is my code
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <SDL.h>
#include <string>
#include <stdio.h>

SDL_Window *gameWindow = NULL; //game window
SDL_Texture *BGTexture = NULL; //SDL texture that holds the background
SDL_Texture *spriteTexture = NULL; // SDL texture that holds the game sprites
SDL_Renderer *gameRenderer = NULL; //game renderer
SDL_Event gameEvent; //main event

//SDL initializing function
bool Init()
{
    bool success = true;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL Initilizing error: ", SDL_GetError());
        success = false;
    }
    else
    {
        if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
        {
            printf("Warning: Linear texture filtering not enabled!");
        }
        gameWindow = SDL_CreateWindow("Tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
        if (gameWindow == NULL)
        {
            printf("Error in creating the game window: ", SDL_GetError());
            success = false;
        }
        else
        {
            gameRenderer = SDL_CreateRenderer(gameWindow, -1, SDL_RENDERER_ACCELERATED);
            if (gameRenderer == NULL)
            {
                printf("Error in creating the renderer: ", SDL_GetError());
                success = false;
            }
        }
    }
    return success;
}

//function that loads game sprites
bool loadMedia()
{
    bool success = true;
    SDL_Surface *imageSurf = SDL_LoadBMP("background.bmp");
    if (imageSurf == NULL)
    {
        printf("Error in loading the background", SDL_GetError());
        success = false;
    }
    else
    {
        BGTexture = SDL_CreateTextureFromSurface(gameRenderer, imageSurf);
        SDL_FreeSurface(imageSurf);
        if (BGTexture == NULL)
        {
            printf("Error in creating the background texture", SDL_GetError());
        }
    }

    imageSurf = SDL_LoadBMP("spritesheet.bmp");
    if (imageSurf == NULL)
    {
        printf("Error in loading the spritesheet", SDL_GetError());
        success = false;
    }
    else
    {
        spriteTexture = SDL_CreateTextureFromSurface(gameRenderer, imageSurf);
        SDL_FreeSurface(imageSurf);
        if (spriteTexture == NULL)
        {
            printf("Error in creating the sprite texture", SDL_GetError());
        }
    }
    return success;
}

//function that frees the memory which has been allocated by each SDL structures
void Close()
{
    SDL_DestroyTexture(BGTexture);
    SDL_DestroyTexture(spriteTexture);
    SDL_DestroyRenderer(gameRenderer);
    SDL_DestroyWindow(gameWindow);
    SDL_Quit();
}

//Square class for the game squares
class TSquare
{
private:
    // x, y, co-ordinates of the square
    int Xpos, Ypos;

public:
    //Square class constructor
    TSquare();
    //function that handles the user inputs to square objects
    void Input_handle();
    //function that moves the square
    void Move();
    //function that displays the square to the screen
    void Display();
};

TSquare::TSquare()
{
    Xpos = 0;
    Ypos = 0;
}

void TSquare::Input_handle()
{
    if (gameEvent.type == SDL_KEYDOWN)
    {
        switch(gameEvent.key.keysym.sym)
        {
            case SDLK_UP: Ypos -= 25; break;
            case SDLK_DOWN: Ypos += 25; break;
            case SDLK_LEFT: Xpos -= 25; break;
            case SDLK_RIGHT: Xpos += 25; break;
        }
    }
    else if (gameEvent.type == SDL_KEYUP)
    {
            switch(gameEvent.key.keysym.sym)
        {
            case SDLK_UP: Ypos += 0; break;
            case SDLK_DOWN: Ypos -= 0; break;
            case SDLK_LEFT: Xpos += 0; break;
            case SDLK_RIGHT: Xpos -= 0; break;
        }
    }
}

void TSquare::Move()
{
    Ypos += 1;
    //if the square moves to the bottom of the screen, the square will stop
    if (Ypos >= 450)
    {
        Ypos -= 1;
    }
}

void TSquare::Display()
{
    SDL_Rect SrcRect[3]; //rectangles for 4 square sprites
    SDL_Rect DestRect; //sprite rectangle that will be displayed

    SrcRect[0].x = 0;
    SrcRect[0].y = 0;
    SrcRect[0].w = 100;
    SrcRect[0].h = 100;

    SrcRect[1].x = 100;
    SrcRect[1].y = 0;
    SrcRect[1].w = 200;
    SrcRect[1].h = 100;

    SrcRect[2].x = 0;
    SrcRect[2].y = 100;
    SrcRect[2].w = 100;
    SrcRect[2].h = 200;

    SrcRect[3].x = 100;
    SrcRect[3].y = 100;
    SrcRect[3].w = 200;
    SrcRect[3].h = 200;

    DestRect.x = Xpos;
    DestRect.y = Ypos;
    DestRect.w = 25;
    DestRect.h = 25;

    SDL_RenderCopy(gameRenderer, spriteTexture, &SrcRect[1], &DestRect);
}

int main(int argc, char *args[])
 {
    //variable that holds the sdl ticks
    Uint32 loopStart = 0;

    Init();
    loadMedia();

    //1st object square in the game
    TSquare gameSquare;

    bool quit = false;
    while(!quit)
    {
        loopStart = SDL_GetTicks();

        while(SDL_PollEvent(&gameEvent))
        {
            gameSquare.Input_handle();

            if (gameEvent.type == SDL_QUIT)
            {
                quit = true;
            }
        }

        gameSquare.Move();

        SDL_RenderClear(gameRenderer);
        SDL_RenderCopy(gameRenderer, BGTexture, NULL, NULL);

        gameSquare.Display();

        SDL_RenderPresent(gameRenderer);

        //if the time difference at the end of the loop and the begining of
        //the loop is smaller than the time takes to run 60 frames,
        //the remaining time will be dlayed
        if ((SDL_GetTicks() - loopStart) < (1000/60))
        {
            SDL_Delay((1000/60) - (SDL_GetTicks() - loopStart));
        }
    }

    Close();

    return 0;
}
Topic archived. No new replies allowed.