Need some help with SDL

I recently tried to learn some SDL and just started on a small program. The problem is that I'm having trouble receiving input when a button that's held down (it interprets it as one input). How do I make the program recognize a button being held down as multiple inputs?

the code is:
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//main.cpp
#include "Game.h"

using namespace std;

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;

SDL_Surface *coin = NULL;
SDL_Surface *block = NULL;

SDL_Event Event;

const int SCREEN_WIDTH = 672;
const int SCREEN_HEIGHT = 224;
const int SCREEN_BPP = 32;
const int LIMITED_FPS = 15;

int Frame_Time = SDL_GetTicks();

Player Mario;

bool done = false;

int main ( int argc, char** argv )
{
    init();
    Mario.Set_Sprite(Msprite);

    Coin test(100,SCREEN_HEIGHT-56);

    background = load_image("Sprites/background.png");

    while (!done)
    {
        while (SDL_PollEvent(&Event))
        {
            Mario.HandleInput(Event);
            switch(Event.type)
            {
                case SDL_QUIT:
                done = !done;
                break;

                case SDL_KEYDOWN:
                if (Event.key.keysym.sym == SDLK_ESCAPE)
                {
                    done = !done;
                }
                break;
            }

        }

        apply_surface(0,0,background,screen);

        Mario.Draw();
        test.Draw();

        SDL_Flip(screen);

        if ((SDL_GetTicks() - Frame_Time) < 1000 / LIMITED_FPS)
        {
            SDL_Delay( ( 1000 / LIMITED_FPS ) - (SDL_GetTicks() - Frame_Time) );
            //exit(5);
        }

        Frame_Time = SDL_GetTicks();
    }



    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}

//misc.h
#ifndef MISC_H_INCLUDED
#define MISC_H_INCLUDED

#include <SDL/SDL.h>
#include <SDL_image.h>
#include <SDL_TTF.h>
#include <iostream>
using namespace std;

extern const int SCREEN_BPP;
extern const int SCREEN_HEIGHT;
extern const int SCREEN_WIDTH;
extern SDL_Surface *screen;
extern SDL_Surface *coin;
extern SDL_Surface *block;

SDL_Surface *load_image( string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );

        //If the image was optimized just fine
        if( optimizedImage != NULL )
        {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0x55, 0x55, 0x55 );

            //Set all pixels of color R 0x55, G 0x55, B 0x55 to be transparent
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
        }
    }

    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return 1;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return 1;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Mario Game v0.5", NULL );

    coin = load_image("Sprites/coin.png"); //16x16 per frame with an empty line inbetween frames
    block = load_image("Sprites/block.png"); //16x16 per block with an empty line inbetween frames

    //If everything initialized fine
    return true;
}


#endif // MISC_H_INCLUDED

//game.h
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include "Misc.h"
#include <vector>
#include <math.h>

#define Position XY
#define Velocity XY

#define Msprite 0
#define Lsprite 1

class XY
{
    public:
    int X;
    int Y;

        XY(){}
        XY(int x, int y)
        {
            X = x;
            Y = y;
        }

        XY operator*(int scale) const
        {
            return XY(X * scale, Y * scale);
        }
		XY operator/(int scale) const
		{
		    return XY(X / scale, Y / scale);
		}
		XY operator+(const XY &other) const
		{
		    return XY(X + other.X, Y + other.Y);
		}
		XY operator-(const XY &other) const
		{
		    return XY(X - other.X, Y - other.Y);
		}
		XY operator!()
		{
		    return XY(X * -1,Y * -1);
		}

		const XY &operator*=(int scale)
		{
		    X *= scale;
		    Y *= scale;
		}
		const XY &operator/=(int scale)
		{
		    X /= scale;
		    Y /= scale;
		}
		const XY &operator+=(const XY &other)
		{
		    X += other.X;
		    Y += other.Y;
		}
		const XY &operator-=(const XY &other)
		{
		    X -= other.X;
		    Y -= other.Y;
		}
};

const Velocity Gravity(0,5);

class Player
{
    private:
        SDL_Surface *Sprite;
        SDL_Rect Clip; //16x32
        int frame;
        XY LengthHeight;
    public:
        Player()
        {
            LengthHeight = {16,32};
            frame = 1;
            Vel = {0,0};
            Pos = {0,SCREEN_HEIGHT - 56};
            Clip = {0,0,LengthHeight.X,LengthHeight.Y};
            Set_Collision_Rect();
        }
        void Set_Collision_Rect()
        {
            Collisions = {Pos.X,Pos.Y,Clip.w,Clip.h};
        }
        void Draw()
        {
            Clip.x = (fabs(frame)-1)*16;
            if (frame >= 0)
                Clip.y = 0;
            else
                Clip.y = LengthHeight.Y;
            apply_surface(Pos.X,Pos.Y,Sprite,screen,&Clip);
        }
        void Set_Sprite(int sprite)
        {
            switch(sprite)
            {
                case Msprite:
                Sprite = load_image("Sprites/Mario.png");
            }
        }
        void HandleInput(SDL_Event event)
        {
                if (event.type == SDL_KEYDOWN)
                {
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_RIGHT:
                        if (frame < 0)
                            frame = 1;
                        frame++;
                        if (frame == 5)
                            frame = 2;

                        Vel.X = 5;
                        Pos += Vel;
                        //exit(1);
                        break;

                        case SDLK_LEFT:
                        if (frame > 0)
                            frame = 0;
                        frame--;
                        if (frame == -4)
                            frame = -1;

                        Vel.X = -5;
                        Pos += Vel;
                        //exit(3);
                        break;
                    }
                }
                else if (event.type == SDL_KEYUP)
                {
                    Vel.X = 0;
                }
                Set_Collision_Rect();
        }

        Velocity Vel;
        Position Pos;
        SDL_Rect Collisions;
        bool IsJumping;
};

class Coin
{
    private:
        SDL_Rect Clip;
        SDL_Surface *sprite;
        int frame;
        XY LengthHeight;
    public:
        Coin(int x,int y)
        {
            LengthHeight = {16,16};
            Clip = {0,0,LengthHeight.X,LengthHeight.Y};
            frame = 1;
            sprite = coin;
            Pos = {x,y};
            Collisions = { Pos.X,Pos.Y,Clip.x,Clip.y };
        }
        void Draw()
        {
            Clip.y = (frame - 1)*16 + (frame - 1);
            apply_surface(Pos.X,Pos.Y,sprite,screen,&Clip);
            frame++;
            frame = frame%6;
            frame = (frame == 0) ? 6 : frame;
        }

        Position Pos;
        SDL_Rect Collisions;
};

#endif // GAME_H_INCLUDED

Last edited on
I can't help you, but i recommend lazyfoo's SDL tutorials.
That's the tutorial that I used to learn what I did. In one of his tutorials, he does have a moving dot that behaves the way I want this program to behave (in terms of holding down buttons); however, no matter how much I think about it and go over the code, I still don't see what he did that I didn't do that caused his to actually work that way.
bump
If you just want to check if a key is down or not you can use SDL_GetKeyState.
http://sdl.beuc.net/sdl.wiki/SDL_GetKeyState
Thanks you two. It works now
Topic archived. No new replies allowed.