Jump to content
  • Advertisement
Sign in to follow this  
GameDeveloper933

SDL movement issues

This topic is 2359 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.


#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;
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!