1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
const int SCREEN_WIDTH = 1290;
const int SCREEN_HEIGHT = 700;
const int SCREEN_BPP = 32;
SDL_Event event;
SDL_Rect backgroundrect;
bool init(){
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) return false;
if( (screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE )) == NULL ) return false;
SDL_WM_SetCaption( "Blablabla", NULL );
if( TTF_Init() == -1 ) return false;
return true;
}
void clean_up(){
SDL_FreeSurface( background );
SDL_FreeSurface ( manleft );
SDL_FreeSurface( manright );
SDL_FreeSurface( bullet );
SDL_FreeSurface( explosion );
SDL_FreeSurface( monsters );
SDL_FreeSurface( lifeindicator );
SDL_FreeSurface( wallpic );
SDL_Quit();
}
int main( int argc, char* args[] ){
bool quit = false, moveright = false, moveleft = false, right = true;
backgroundrect.w = 1290; backgroundrect.h = 700; backgroundrect.x = 0; backgroundrect.y = 0;
uint16_t mousex = 0, mousey = 0;
int xpos = 500, ypos = 300;
if( init() == false ) return 1;
if( load_files() == false ) return 1;
apply_surface( 0, 0, background, screen, &backgroundrect );
apply_surface( xpos, ypos, manright, screen );
while( quit == false ){//this is the loop the program somehow manages to escape from
while( SDL_PollEvent( &event ) ){//I doubt there's a problem in events
if( event.type == SDL_KEYDOWN){
if(event.key.keysym.sym == SDLK_d){ moveright = true; }
if(event.key.keysym.sym == SDLK_a){ moveleft = true; }
}
if(event.type == SDL_KEYUP){
if(event.key.keysym.sym == SDLK_d){ moveright = false; }
if(event.key.keysym.sym == SDLK_a){ moveleft = false; }
}
if(event.type == SDL_MOUSEMOTION){
mousex = event.motion.x; mousey = event.motion.y; // this is the only place i can think where the problem would be
}
if( event.type == SDL_QUIT || event.key.keysym.sym == SDLK_ESCAPE) quit = true;
}
///MOVEMENT AND BACKGROUND SCROLLING
if(moveright == true){
xpos += 9;
}
else if(moveleft == true) xpos += -9;
else xpos += 0;
if( mousex > xpos + 14 ) right = true;//sets the direction of the character depending on where the mouse is.
else right = false;
///BLITTING
apply_surface( 0, 0, background, screen, &backgroundrect );
if(right == true){//prints the character facing either left or right
apply_surface( xpos, ypos, manright, screen );
}
else apply_surface( xpos, ypos, manleft, screen );
if( SDL_Flip( screen ) == -1 ) return 12;
}
clean_up();
return 0;
}
| |