Sign in to follow this  

SDL movement issues

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

[CODE]
#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,&rect);
}
/**
*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;
}
[/CODE]

Share this post


Link to post
Share on other sites

This topic is 2050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this