Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include "SDL/SDL_image.h"
- #include "SDL/SDL_ttf.h"
- #include "SDL/SDL_mixer.h"
- #include <string>
- #include <sstream>
- #include <fstream>
- using namespace std;
- //constants
- const int screenWidth = 1280;
- const int screenHeight = 720;
- const int screenBPP = 32;
- const int playerSpeed = 3;
- const int enemySpeed = 3;
- const int laserSpeed = 6;
- SDL_Event event;
- //surfaces
- SDL_Surface *spaceship = NULL;
- SDL_Surface *enemyBig = NULL;
- SDL_Surface *enemySmall = NULL;
- SDL_Surface *laserBlue = NULL;
- SDL_Surface *laserRed = NULL;
- SDL_Surface *menuBG = NULL;
- SDL_Surface *gameBG = NULL;
- SDL_Surface *screen = NULL;
- //music
- Mix_Music *gameMusic = NULL;
- Mix_Music *menuMusic = NULL;
- Mix_Music *deathMusic = NULL;
- Mix_Chunk *laser1 = NULL;
- Mix_Chunk *laser2 = NULL;
- //fonts
- TTF_Font *font = NULL;
- SDL_Color textColor = { 255, 255, 255 };
- //variables
- bool quit;
- int state = 1; // 0 = MENU, 1 = GAME, 2= DEATH
- std::ofstream logger( "log.txt" );
- //====================================================================
- //CLASSES
- //====================================================================
- class Laser
- {
- private:
- int x;
- int y;
- int xVel;
- int yVel;
- int width;
- int height;
- int countX;
- int countY;
- public:
- bool visible;
- Laser();
- void handleLogic(int playerX, int playerY, int playerWidth);
- void render();
- };
- class Player
- {
- private:
- int xVel;
- int yVel;
- int height;
- public:
- int x;
- int y;
- int width;
- Player();
- void handleInput(Laser laser);
- void handleLogic();
- void render();
- };
- //====================================================================
- //FUNCTIONS
- //====================================================================
- void log( std::string message )
- {
- //Write message to file
- logger << message << std::endl;
- //Flush the buffer
- logger.flush();
- }
- SDL_Surface *loadImage(std::string filename)
- {
- SDL_Surface* loadedImage = NULL;
- SDL_Surface* optimizedImage = NULL;
- loadedImage = IMG_Load(filename.c_str());
- if(loadedImage != NULL)
- {
- optimizedImage = SDL_DisplayFormatAlpha(loadedImage);
- SDL_FreeSurface(loadedImage);
- }
- log("Loaded image "+filename);
- return optimizedImage;
- }
- void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
- {
- SDL_Rect offset;
- offset.x = x;
- offset.y = y;
- SDL_BlitSurface(source, NULL, destination, &offset);
- }
- bool loadResources()
- {
- log("Loading Resources...");
- spaceship = loadImage("art/spaceship.png");
- enemyBig = loadImage("art/enemy_big.png");
- enemySmall = loadImage("art/enemy_small.png");
- laserRed = loadImage("art/laserRed.png");
- laserBlue = loadImage("art/laserBlue.png");
- menuBG = loadImage("art/menu_bg.png");
- gameBG = loadImage("art/space_bg.png");
- font = TTF_OpenFont("art/Fh_Space.ttf",25);
- if (font == NULL)
- {
- return false;
- }
- gameMusic = Mix_LoadMUS("sound/music_game.mp3");
- menuMusic = Mix_LoadMUS("sound/music_menu.ogg");
- deathMusic = Mix_LoadMUS("sound/music_death.mp3");
- laser1 = Mix_LoadWAV("sound/laser1.wav");
- laser2 = Mix_LoadWAV("sound/laser1.wav");
- log("Loaded Resources.");
- return true;
- }
- bool startUp()
- {
- log("Initializing everything...");
- if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
- {
- return false;
- }
- if (TTF_Init() == -1)
- {
- return false;
- }
- if (IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG|IMG_INIT_TIF) == -1)
- {
- return false;
- }
- screen = SDL_SetVideoMode(screenWidth,screenHeight,screenBPP,SDL_SWSURFACE);
- if (screen == NULL)
- {
- return false;
- }
- if (Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096 ) == -1)
- {
- return false;
- }
- SDL_WM_SetCaption("Space Norbos",NULL);
- if (loadResources() == false)
- {
- return false;
- }
- quit = false;
- log("Done initializing.");
- return true;
- }
- void cleanUp()
- {
- log("Cleaning up...");
- SDL_FreeSurface(gameBG);
- SDL_FreeSurface(menuBG);
- SDL_FreeSurface(spaceship);
- SDL_FreeSurface(enemyBig);
- SDL_FreeSurface(enemySmall);
- SDL_FreeSurface(laserBlue);
- SDL_FreeSurface(laserRed);
- TTF_CloseFont(font);
- Mix_FreeMusic(gameMusic);
- Mix_FreeMusic(menuMusic);
- Mix_FreeMusic(deathMusic);
- Mix_FreeChunk(laser1);
- Mix_FreeChunk(laser2);
- TTF_Quit();
- IMG_Quit();
- Mix_Quit();
- SDL_Quit();
- log("Cleaned up.");
- }
- void shoot(Laser laser)
- {
- log("Shooting!");
- laser.visible = true;
- }
- //====================================================================
- //CLASS FUNCTIONS
- //====================================================================
- //PLAYER======================================
- Player::Player()
- {
- x = (screenWidth/2)-(width/2);
- y = 656;
- xVel = 0;
- yVel = 0;
- width = 56;
- height = 54;
- }
- void Player::handleInput(Laser laser)
- {
- if (event.type == SDL_KEYDOWN)
- {
- switch (event.key.keysym.sym)
- {
- case SDLK_LEFT: xVel -= playerSpeed; break;
- case SDLK_RIGHT: xVel += playerSpeed; break;
- case SDLK_SPACE: laser.visible = true;
- }
- }
- else if( event.type == SDL_KEYUP )
- {
- //Adjust the velocity
- switch( event.key.keysym.sym )
- {
- case SDLK_LEFT: xVel += playerSpeed; break;
- case SDLK_RIGHT: xVel -= playerSpeed; break;
- }
- }
- }
- void Player::handleLogic()
- {
- x = x + xVel;
- if (x <= 0)
- {
- x = 0;
- }
- if (x + width > screenWidth)
- {
- x = screenWidth - width;
- }
- y = y + yVel;
- if (y <= 0)
- {
- y = 0;
- }
- if (y + height > screenHeight)
- {
- y = screenHeight - height;
- }
- }
- void Player::render()
- {
- applySurface(x,y,spaceship,screen);
- }
- //LASER======================================
- Laser::Laser()
- {
- x = 0;
- y = 0;
- xVel = 0;
- yVel = (laserSpeed);
- width = 10;
- height = 50;
- countX = 0;
- countY = 0;
- visible = false;
- }
- void Laser::handleLogic(int playerX, int playerY, int playerWidth)
- {
- if (visible == true)
- {
- if (countY == 0)
- {
- y = playerY - height - 2;
- countY += 1;
- }
- if (countX == 0)
- {
- x = playerX + (playerWidth/2)- (width/2);
- countX += 1;
- }
- y -= laserSpeed;
- if (y = 0)
- {
- visible = false;
- }
- }
- }
- void Laser::render()
- {
- if (visible == true)
- {
- applySurface(x,y,laserBlue,screen);
- }
- }
- //====================================================================
- //MAIN
- //====================================================================
- int main(int argc, char* args[])
- {
- if (startUp() == false)
- {
- return 1;
- }
- Player player;
- Laser laser;
- while (quit == false)
- {
- SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
- if (state == 1)
- {
- applySurface(0,0,gameBG,screen);
- while (SDL_PollEvent(&event))
- {
- player.handleInput(laser);
- switch (event.type)
- {
- case SDL_QUIT: quit = true; break;
- }
- }
- player.handleLogic();
- player.render();
- laser.handleLogic(player.x,player.x,player.width);
- laser.render();
- if (SDL_Flip(screen) == -1)
- {
- return 1;
- }
- }
- }
- cleanUp();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement