Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>
#define MAX_SPACE_IMAGES 3
#define SCREEN_WIDTH 920
#define SCREEN_HEIGHT 518
void SDL_errorexit( const char * error_message )
{
char buffer[BUFSIZ];
sprintf(buffer, "%s : %s", error_message, SDL_GetError());
fprintf(stderr, "%s", buffer);
exit(1);
}
void init_SDL( )
{
if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
SDL_errorexit("SDL_Init");
return;
}
SDL_Surface * load_image_opt( char * image_path, int is_transparent )
{
SDL_Surface * image = NULL;
SDL_Surface * temp_image = SDL_LoadBMP(image_path);
if ( !temp_image )
SDL_errorexit("SDL_LoadBMP");
if ( is_transparent )
SDL_SetColorKey( temp_image, SDL_SRCCOLORKEY, SDL_MapRGB(temp_image->format, 255, 255, 255) );
image = SDL_DisplayFormat( temp_image );
SDL_FreeSurface( temp_image );
return image;
}
SDL_Surface * set_video_mode( const char * window_title )
{
SDL_Surface * console = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 24, SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( window_title, "N/A" );
if (!console)
SDL_errorexit("SDL_SetVideoMode");
return console;
}
void game_intro( SDL_Surface ** space, SDL_Surface * title, SDL_Surface * console )
{
SDL_Rect title_rect;
int offset = 0;
int iteration;
title_rect.x = (console->w - title->w) / 2;
title_rect.y = (console->h - title->h) / 2;
title_rect.h = title->h;
title_rect.w = title->w;
for ( iteration = 0; iteration < 9; iteration++ )
{
offset = (offset > MAX_SPACE_IMAGES - 1 ? 0 : offset + 1);
SDL_FillRect(console, 0, SDL_MapRGB(console->format, 0, 0, 0)); /* Clear the screen */
SDL_BlitSurface(space[offset], 0, console, NULL );
SDL_BlitSurface(title, 0, console, &title_rect);
SDL_Flip(console); /* Update the screen */
SDL_Delay(250); /* Delay the loop */
}
return;
}
void game_start( SDL_Surface ** space, SDL_Surface * space_fighter, SDL_Surface * console )
{
SDL_Event event;
SDL_Rect fighter;
const int SPACE_UPDATE_FRAME = 60;
int frame = 0;
int offset = 0;
int quit = 0;
int x = 0;
fighter.x = (console->w - space_fighter->w ) / 2;
fighter.y = console->h - 100;
fighter.h = 75;
fighter.w = 75;
while ( !quit )
{
if ( frame == SPACE_UPDATE_FRAME )
offset = (offset > MAX_SPACE_IMAGES - 1 ? 0 : offset + 1);
/* While-loop was from SDL documentation : http://www.libsdl.org/release/SDL-1.2.15/docs/html/guideinputkeyboard.html */
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_QUIT :
quit = 1;
break;
/* Keydown event */
case SDL_KEYDOWN :
switch( event.key.keysym.sym )
{
case SDLK_LEFT :
x = -1;
break;
case SDLK_RIGHT :
x = 1;
break;
case SDLK_ESCAPE :
quit = 1;
break;
default : break;
}
break;
/* End keydown event */
/* Keyup event */
case SDL_KEYUP:
switch( event.key.keysym.sym )
{
case SDLK_LEFT :
if( x < 0 )
x = 0;
break;
case SDLK_RIGHT :
if( x > 0 )
x = 0;
break;
default : break;
}
/* End keyup event */
break;
default : break;
}
}
if ( fighter.x >= console->w - 75 )
{
x = 0;
fighter.x = console->w - 76;
}
fighter.x += x;
SDL_FillRect(console, 0, SDL_MapRGB(console->format, 0, 0, 0)); /* Clear the screen */
SDL_BlitSurface(space[offset], 0, console, NULL );
SDL_BlitSurface(space_fighter, 0, console, &fighter);
SDL_Flip(console); /* Update the screen */
frame = frame == SPACE_UPDATE_FRAME ? 0 : frame + 1;
}
return;
}
int main( int argc, char * argv[] )
{
/* Variable declarations */
SDL_Surface * space[MAX_SPACE_IMAGES] = {NULL};
SDL_Surface * title = NULL;
SDL_Surface * space_fighter = NULL;
SDL_Surface * console = NULL;
int offset;
/* Initializing SDL... */
init_SDL();
if (atexit(SDL_Quit))
{
perror("atexit");
exit(1);
}
/* Screen configurations */
console = set_video_mode( "Space Fighter" );
/* Image loading */
space[0] = load_image_opt( "space_dark.bmp", 0 );
space[1] = load_image_opt( "space_lighter.bmp", 0 );
space[2] = load_image_opt( "space_light.bmp", 0 );
title = load_image_opt( "space_fighter_intro.bmp", 1 );
space_fighter = load_image_opt( "space_fighter.bmp", 1 );
/* Start the game introduction */
game_intro( space, title, console );
SDL_FreeSurface(title); /* We no longer need the introduction title */
/* Start the game */
game_start( space, space_fighter, console );
/* Free allocated memory */
for (offset = 0; offset < MAX_SPACE_IMAGES - 1; offset++ )
SDL_FreeSurface(space[offset]);
SDL_FreeSurface(space_fighter);
return 0;
}
If someone needs me to include the bitmaps for testing purposes I can. Also, feel free to add suggestions for anything wrong in my code currently.