Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL movement issues

Started by GameDeveloper933 May 5, 2012 at 12:11 AM 0 replies 900+ views
Original Post
GameDeveloper933
GameDeveloper933
I am currently fooling around with SDL and for the lights of me cant get my sprite to move heres what i have so far.


#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif
//Set up some surfaces
SDL_Surface * screen = NULL;
SDL_Surface * map = NULL;
SDL_Surface * tank = NULL;
//Set up const
int const SCREEN_WIDTH = 640;
int const SCREEN_HEIGHT = 480;
int const SCREEN_BPP = 32;
int const TANK_WIDTH = 32;
int const TANK_HEIGHT = 32;
//Integers
int tankX = 100;
int tankY = 100;
/**
*Load event handler
**/
SDL_Event event;
/**
*Check if game is running or not
**/
bool GameIsRunning = true;
/**
*Create a new function for applying a surface
**/
void apply_surface(int x,int y,SDL_Surface * source,SDL_Surface * destination){
SDL_Rect rect;
rect.x = x;
rect.y = y;
SDL_BlitSurface(source,NULL,destination,▭);
}
/**
*Create a function for drawing our sprite
**/
void SpriteDraw(SDL_Surface * ImageSurface,SDL_Surface * ScreenSurface,int srcX,int srcY,int dstX,int dstY
,int SpriteWidth,int SpriteHeight
){
SDL_Rect SrcRect;
SrcRect.x = srcX;
SrcRect.y = srcY;
SrcRect.w = SpriteWidth;
SrcRect.h = SpriteHeight;
SDL_Rect DstRect;
DstRect.x = dstX;
DstRect.y = dstY;
DstRect.w = SpriteWidth;
DstRect.h = SpriteHeight;
SDL_BlitSurface(ImageSurface,&SrcRect,ScreenSurface,&DstRect);
}
int main ( int argc, char** argv )
{
/**
*INIT SDL
**/
SDL_Init(SDL_INIT_EVERYTHING);
/**
*Set the caption of our game
**/
SDL_WM_SetCaption("Tank Wars v1.0.0--Development by Steve",NULL);
/**
*Set up main screen
**/
screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE|SDL_DOUBLEBUF);
//Set up images needed
map = SDL_LoadBMP("level_1.bmp");
tank = SDL_LoadBMP("tank.bmp");
/**
*Check if our images cant be loaded
**/
if(map == NULL){
printf("%s","We have failed to load the image level_1.bmp");
SDL_Quit();
}
/**
*Start our main game loop
**/
while(GameIsRunning){
if(SDL_PollEvent(&event)){
/**
*Check if a key was pressed down
**/
if(event.type == SDL_KEYDOWN){
/**
*Check if we want to end the game by pressing escape, if we do
we set GameIsRunning to false
**/
if(event.key.keysym.sym == SDLK_ESCAPE){
GameIsRunning = false;
}
if(event.key.keysym.sym == SDLK_LEFT){
tankX -= 1;
}
}
//Apply surfaces
apply_surface( 0, 0, map, screen );
apply_surface( 300, 200, tank, screen );
SpriteDraw(tank,screen,300,200,tankX,tankY,TANK_WIDTH,TANK_HEIGHT);
/* update the screen */
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
//Update our screen
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}

Topic Locked

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

Sign in to reply to this topic.