Skip to main content
gamedev.net
🔒 Locked

SDL

Started by Chocrates 2 replies 600+ views
Original Post
Chocrates
Chocrates
hey guys i did the 2d tutorials on aaroncox's website and am now attempting to write a 2d game of my own. i am using mario sprites (it should be ok since i don't plan on selling it, its just practice), and i am trying to get him to move. My trouble is this, i put the code i need for the sprite to move with the arrow keys in a function called red and it doesn't work. I know i need to feed the function an sdl thing but i don't know what it is. I think it might be something like SDL_KEYBOARD or whatever it was, i don't have my code in front of me. also how can i flip the background, then flip the new sprite of mario turning the other directon, it seems to refresh like crazy. If you guys want i will post my code, but it is long and i don't know if you want me to.
tstrimp
tstrimp
You should post the relevent bits in [source] tags.
cignox1
cignox1
I use the following code to read keyboard actions:
    bool run = true;    SDL_Event event;    while(run)    {        if(SDL_PollEvent(&event))        {            switch(event.type)            {                case SDL_QUIT: run = false; break;                case SDL_KEYDOWN:                {                    switch(event.key.keysym.sym)                    {                        case SDLK_ESCAPE: run = false; break;                        case SDLK_UP: //Start moving the sprite up                        case SDLK_DOWN: //Start moving the sprite down                        case SDLK_LEFT: //Start moving the sprite left                        case SDLK_RIGHT: //Start moving the sprite right                    }                    break;                }                case SDL_KEYUP:                {                    switch(event.key.keysym.sym)                    {                        case SDLK_UP: //Stop moving the sprite up                        case SDLK_DOWN: //Stop moving the sprite down                        case SDLK_LEFT: //Stop moving the sprite left                        case SDLK_RIGHT: //Stop moving the sprite right                    }                    break;                }            }        }        //Render the frame        Render();    }    SDL_Quit();	return 0;

When I press an arrow key, I set a flag on. Then in the Render() (or Update() or wherever you update your scene) the position of the sprite is moved following which flag is on. The flag is set off when I release the key.
I would really suggest you to start using opengl for your 2d games: not only it is faster (by far), but let you transform sprites in an easier way.
Once I started a simple game that I never finished because I was not able to get more than 30fps and the ball sprite was flickering. If you use a polygon for each sprite you can move millions of alpha blended, multi-textured and filtered (if it is the case) sprites.
Chocrates
Chocrates
ok heres my functions
void Red(SDL_Surface* bg, SDL_Surface* red, float red1x, float red1y){ SDL_Rect red1source; red1source.x = 162; red1source.y = 69; red1source.w = 16; red1source.h = 27; SDL_Rect reddestination; reddestination.x = red1x; reddestination.y = red1y; reddestination.w = 16; reddestination.h = 27;if (event.type == SDL_KEYDOWN)         {         SDLKey keyPressed = event.key.keysym.sym;               switch (keyPressed)         {         case SDLK_RIGHT:         red1x += .5;         SDL_FillRect(bg, NULL, SDL_MapRGB(bg->format, 142, 248, 255));          RedRight1(bg, red1, red1x, red1y);         SDL_Flip(bg);         break;         case SDLK_LEFT:         red1x -= .5;         SDL_FillRect(bg, NULL, SDL_MapRGB(bg->format, 142, 248, 255));          RedLeft1(bg, red1, red1x, red1y);         SDL_Flip(bg);         break;         }         }     }void RedRight1(SDL_Surface* bg, SDL_Surface* red1, float red1x, float red1y){ SDL_Rect red1source; red1source.x = 162; red1source.y = 69; red1source.w = 16; red1source.h = 27; SDL_Rect reddestination; reddestination.x = red1x; reddestination.y = red1y; reddestination.w = 16; reddestination.h = 27;  SDL_BlitSurface(red1, &red1source, bg, &reddestination); SDL_SetColorKey( red1, SDL_SRCCOLORKEY, SDL_MapRGB(red1->format, 255, 0, 238) );               }void RedLeft1(SDL_Surface* bg, SDL_Surface* red1, float red1x, float red1y){ SDL_Rect red1source; red1source.x = 121; red1source.y = 69; red1source.w = 16; red1source.h = 27; SDL_Rect reddestination; reddestination.x = red1x; reddestination.y = red1y; reddestination.w = 16; reddestination.h = 27;  SDL_BlitSurface(red1, &red1source, bg, &reddestination); SDL_SetColorKey( red1, SDL_SRCCOLORKEY, SDL_MapRGB(red1->format, 255, 0, 238) );               }

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.