Archived

This topic is now archived and is closed to further replies.

jmg5

SDL - getting the image to move

Recommended Posts

jmg5    100
Been reading up on SDL for awhile, and finally got a bitmap to load on the screen. Now, I want to move it up and down with the arrow keys. I got a variable YPOS, and I update it in the game loop when a key is pressed. But how do I tell SDL to know I want the image to go up and down? How do I assign YPOS to the image?
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

SDL_Surface *screen;
SDL_Surface *image;
SDL_Event event;
int ypos=0;

void display_bmp(SDL_Surface *image)
{
    image = SDL_LoadBMP("paddle.bmp");
    if (image == NULL) {
        printf("Could Not Load Bitmap: %s\n", SDL_GetError());
        return;
    }

    if(SDL_BlitSurface(image, NULL, screen, NULL) < 0)
        printf("BlitSurface Error: %s\n", SDL_GetError());

    SDL_UpdateRect(screen, 0, 0, image->w, image->h);
    SDL_FreeSurface(image);
}

int main(int argc, char *argv[])
{   
    Uint8* keys;

    if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)
    {
        printf("Could Not Initialize SDL: %s", SDL_GetError());
        exit(1);
    }
    atexit(SDL_Quit);
    
    screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
    if (screen == NULL)
    {
        printf("Could Not Use 800x600 Video Mode: %s", SDL_GetError());
        exit(1);
    }
 
    int done=1;
    while(done)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT) {done = 0;}

            if (event.type == SDL_KEYDOWN)
            {
                keys = SDL_GetKeyState(NULL);
                if (keys[SDLK_UP]) {ypos -= 1;}
                if (keys[SDLK_DOWN]) {ypos += 1;}
            }
        }
        display_bmp(image);
    }
    return 0;
}
That''s what I have so far. I can''t figure out a way to make the image move. I think it may have something to do with the SDL_UpdateRect function. All help is appreciated.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
this simple program can help

#include "SDL.h"
#include <math.h>

int main(int argc, char *argv[])
{
SDL_Surface *Display;
SDL_Surface *bmpGoylel;
SDL_Event Event;
SDL_Rect SrcRect, DstRect;
SDL_Rect Velocity;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

SDL_Init(SDL_INIT_VIDEO);

Display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16,
SDL_FULLSCREEN|SDL_DOUBLEBUF);
bmpGoylel = SDL_LoadBMP("goylel.bmp");

SrcRect.w = DstRect.w = bmpGoylel->w;
SrcRect.h = DstRect.h = bmpGoylel->h;
SrcRect.x = SrcRect.y = DstRect.x = DstRect.y = Velocity.x = Velocity.y = 0;

for(;;)
{
if(SDL_PollEvent(&Event) == 0)
{
DstRect.x += Velocity.x;
DstRect.y += Velocity.y;

//collision detection on the screen

if(DstRect.x <= 0)
Velocity.x = abs(Velocity.x);
if(DstRect.y <= 0)
Velocity.y = abs(Velocity.y);
if(DstRect.x >= SCREEN_WIDTH - DstRect.w)
Velocity.x = -abs(Velocity.x);
if(DstRect.y >= SCREEN_HEIGHT - DstRect.h)
Velocity.y = -abs(Velocity.y);

SDL_FillRect(Display, NULL, 0);
SDL_BlitSurface(bmpGoylel, &SrcRect, Display, &DstRect);

SDL_Flip(Display);
}
else
{
if(Event.type == SDL_KEYDOWN)
{
//movement actions

switch(Event.key.keysym.sym)
{
case SDLK_UP:
Velocity.y--;
break;
case SDLK_DOWN:
Velocity.y++;
break;
case SDLK_RIGHT:
Velocity.x++;
break;
case SDLK_LEFT:
Velocity.x--;
break;
case SDLK_SPACE:
Velocity.x = Velocity.y = 0;
break;
}

if(Event.key.keysym.sym == SDLK_ESCAPE)
break;//break out of loop

}

if(Event.type == SDL_QUIT)
break;
}
}
return 0;
}

Share this post


Link to post
Share on other sites