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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include "Sprite.h"
#include <string>
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface* screen = NULL;
SDL_Surface* background = NULL;
SDL_Surface* spriteObject = NULL;
SDL_Surface* asteroids = NULL;
SDL_Surface* message = NULL;
SDL_Surface* lives = NULL;
//Stores event data to be used.
SDL_Event event;
//The portions of the sprite map to be blitted
//Only use incase of sprite sheets.
SDL_Rect astro[16];
//The fonts thats gonna be used.
TTF_Font* font = NULL;
TTF_Font* lfont = NULL;
//The color of the font.
SDL_Color textColor = { 255, 255, 255 };
//Sounds
//Music
Mix_Music* music = NULL;
//Sound Effects
Mix_Chunk* laser = NULL;
Mix_Chunk* explosion = NULL;
Mix_Chunk* GameOver = NULL;
[...]
SDL_Surface* load_image(std::string filename) {
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
/*
//If the optimized image was done just fine.
if( optimizedImage != NULL )
{
//Map the color key
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
//Sets all the pixels of color R 0, G 0xFF to be
//transparent
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
*/
//returns the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* astro = NULL )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, astro, destination, &offset );
}
//Here is the initialization function. This function starts up SDL, sets up the window,
//sets the caption and returns false if there are any errors.
bool init()
{
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//check if screen loads correctly
if( screen == NULL )
{
return false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
///Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Space Invaders", NULL );
return true;
}
bool load_files()
{
//Load images
spriteObject = load_image( "Images\\spaceship.png" );
background = load_image( "Images\\stars-1.jpeg" );
asteroids = load_image( "Images\\asteroid-spread-sheet.png" );
//Open the font
font = TTF_OpenFont( "Fonts\\SLANT.ttf", 20 );
lfont = TTF_OpenFont( "Fonts\\SLANT.ttf", 22 );
//If there was any error loading the images.
if( spriteObject == NULL || background == NULL
|| asteroids == NULL )
{
return false;
}
//If there was any error loading the fonts.
if( font == NULL || lfont == NULL )
{
return false;
}
//load music
music = Mix_LoadMUS( "Sounds\\earlycarengine.wav" );
//If there was any error loading the music
if( music == NULL )
{
return false;
}
//Load sound effects
laser = Mix_LoadWAV( "Sounds\\ray-gun" );
//If there was any error loading the sound effects.
if( laser == NULL )
{
return false;
}
//If everything loaded fine.
return true;
}
void clean_up()
{
//Free's the loaded images
SDL_FreeSurface( spriteObject );
SDL_FreeSurface( background );
SDL_FreeSurface( message );
SDL_FreeSurface( asteroids );
//Close the font that was used
TTF_CloseFont( font );
TTF_CloseFont( lfont );
//Free the music files
Mix_FreeMusic( music );
//Free the sound effects
Mix_FreeChunk( laser );
//Quit SDL_mixer
Mix_CloseAudio();
//Quit SDL_ttf
TTF_Quit();
//Quit SDL
SDL_Quit();
}
[...]
int main( int argc, char* args[] )
{
/*********************************************************************/
//Initialize the sprites
//Sprite s1("spaceship", "spaceship-sprite.gif", spriteObject);
/*********************************************************************/
//Make sure the program waits for quit.
bool quit = false;
//Calls the init and file loading methods.
if( init() == false )
return 1;
if( load_files() == false )
return 1;
//Render the fonts
message = TTF_RenderText_Solid( font, "Score:", textColor );
lives = TTF_RenderText_Solid( lfont, "Lives:", textColor );
//If there was an error rendering the text
if( message == NULL || lives == NULL )
{
return 1;
}
/*************************************************************************/
Sprite s1("spaceship", "spaceship.png", spriteObject);
/************************************************************************/
[...]
//Update Screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Main loop- will keep going until the user sets quit to true.
while( quit == false )
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
switch(event.type)
{
case SDL_QUIT://If the user presses X ( close ).
//Quits the program
quit = true;
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_UP:
s1.setSpeed( 0.0, -1.0 );
break;
case SDLK_DOWN:
s1.setSpeed( 0.0, 1.0 );
break;
case SDLK_LEFT:
s1.setSpeed( -1.0, 0.0 );
break;
case SDLK_RIGHT:
s1.setSpeed( 1.0, 0.0 );
break;
case SDLK_SPACE:
{
//Play laser sound
if( Mix_PlayChannel( -1, laser, 0 ) == -1 )
return 1;
}
}
}
}
}
//Update Screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
clean_up();
return 0;
}
| |