Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include "SDL_image.h"
- #include "SDL_mixer.h"
- #include "Timer.h"
- #include <string>
- using namespace::std;
- // Contant Variables
- const int FPS = 60;
- const int SpaceShip_w = 20;
- const int SpaceShip_h = 20;
- // Images
- SDL_Surface *spaceship = NULL;
- SDL_Surface *bullet_left = NULL;
- SDL_Surface *bullet_right = NULL;
- SDL_Surface *game_bg = NULL;
- SDL_Surface *screen = NULL;
- // Sounds
- Mix_Chunk *pass = NULL;
- // Events
- SDL_Event event;
- class SpaceShip {
- public:
- int SpaceShip_x, SpaceShip_y;
- int SpaceShip_xVel, SpaceShip_yVel;
- SpaceShip();
- void handle_input();
- void move();
- void show();
- };
- class Bullet {
- public:
- Bullet();
- void Update();
- void Fire(float startX, float startY, float xSpeed, float ySpeed);
- void Draw();
- private:
- bool alive;
- float Bullet_x, Bullet_y;
- float Bullet_xVel, Bullet_yVel;
- };
- SDL_Surface *load_image(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);
- }
- return optimizedImage;
- }
- void apply_surface(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 init()
- {
- if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
- return false;
- }
- const SDL_VideoInfo* myPointer = SDL_GetVideoInfo();
- screen = SDL_SetVideoMode(myPointer->current_w, myPointer->current_h, 32, SDL_FULLSCREEN | SDL_HWSURFACE);
- if(screen == NULL) {
- return false;
- }
- if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1) {
- return false;
- }
- SDL_WM_SetCaption("Galactic Wars v1", NULL);
- return true;
- }
- bool load_files() {
- spaceship = load_image("bin/spaceship.png");
- if(spaceship == NULL)
- {
- return false;
- }
- bullet_left = load_image("bin/bullet.png");
- if(bullet_left == NULL)
- {
- return false;
- }
- bullet_right = load_image("bin/bullet.png");
- if(bullet_right == NULL)
- {
- return false;
- }
- game_bg = load_image("bin/game_bg.jpg");
- if(game_bg == NULL) {
- return false;
- }
- pass = Mix_LoadWAV("bin/pass.wav");
- if(pass == NULL) {
- return false;
- }
- return true;
- }
- void clean_up() {
- SDL_FreeSurface(game_bg);
- SDL_FreeSurface(bullet_left);
- SDL_FreeSurface(bullet_right);
- Mix_FreeChunk(pass);
- SDL_Quit();
- }
- SpaceShip::SpaceShip() {
- SpaceShip_x = 880;
- SpaceShip_y = 900;
- SpaceShip_xVel = 0;
- SpaceShip_yVel = 0;
- }
- Bullet::Bullet():alive(false),Bullet_x(0),Bullet_y(0),Bullet_xVel(0),Bullet_yVel(0){}
- void SpaceShip::handle_input() {
- if(event.type == SDL_KEYDOWN) {
- switch(event.key.keysym.sym) {
- case SDLK_w: SpaceShip_yVel -= SpaceShip_h / 2; break;
- case SDLK_s: SpaceShip_yVel += SpaceShip_h / 2; break;
- case SDLK_a: SpaceShip_xVel -= SpaceShip_w / 2; break;
- case SDLK_d: SpaceShip_xVel += SpaceShip_w / 2; break;
- }
- }
- else if(event.type == SDL_KEYUP) {
- switch(event.key.keysym.sym) {
- case SDLK_w: SpaceShip_yVel += SpaceShip_h / 2; break;
- case SDLK_s: SpaceShip_yVel -= SpaceShip_h / 2; break;
- case SDLK_a: SpaceShip_xVel += SpaceShip_w / 2; break;
- case SDLK_d: SpaceShip_xVel -= SpaceShip_w / 2; break;
- }
- }
- }
- void SpaceShip::move() {
- const SDL_VideoInfo* myPointer = SDL_GetVideoInfo();
- SpaceShip_x += SpaceShip_xVel;
- if(SpaceShip_x + SpaceShip_w > myPointer->current_w) {
- SpaceShip_x = -60;
- Mix_PlayChannel(-1, pass, 0) == -1;
- }
- if(SpaceShip_x < -60) {
- SpaceShip_x = SpaceShip_x + SpaceShip_w + myPointer->current_w;
- Mix_PlayChannel(-1, pass, 0) == -1;
- }
- SpaceShip_y += SpaceShip_yVel;
- if(SpaceShip_y + SpaceShip_h > myPointer->current_h) {
- SpaceShip_y = -60;
- Mix_PlayChannel(-1, pass, 0) == -1;
- }
- if(SpaceShip_y < -60) {
- SpaceShip_y = SpaceShip_y + SpaceShip_h + myPointer->current_h;
- Mix_PlayChannel(-1, pass, 0) == -1;
- }
- }
- void SpaceShip::show() {
- apply_surface(SpaceShip_x, SpaceShip_y, spaceship, screen);
- }
- void Bullet::Update() {
- if(alive) {
- Bullet_x += Bullet_xVel;
- Bullet_y += Bullet_yVel;
- }
- }
- void Bullet::Fire(float startX, float startY, float xSpeed, float ySpeed) {
- Bullet_x = startX;
- Bullet_y = startY;
- Bullet_xVel = xSpeed;
- Bullet_yVel = ySpeed;
- alive = true;
- }
- void Bullet::Draw() {
- if(alive) {
- apply_surface(Bullet_x, Bullet_y, bullet_left, screen);
- }
- }
- int main(int argc, char* args[]) {
- bool quit = false;
- SpaceShip mySpaceShip;
- Bullet Left_Bullet;
- Timer fps;
- int bgX = 0, bgY = 0;
- if(init() == false) {
- return 1;
- }
- if(load_files() == false) {
- return 1;
- }
- if(SDL_Flip(screen) == -1) {
- return 1;
- }
- while(quit == false) {
- fps.start();
- while(SDL_PollEvent(&event)) {
- if(event.type == SDL_KEYDOWN) {
- switch(event.key.keysym.sym) {
- case SDLK_ESCAPE:
- quit = true;
- break;
- case SDLK_SPACE:
- Left_Bullet.Fire(SpaceShip_x, SpaceShip_y, 0, 1);
- break;
- }
- }
- mySpaceShip.handle_input();
- if(event.type == SDL_QUIT) {
- quit = true;
- }
- }
- bgY += 2;
- if(bgY >= game_bg->h) {
- bgY = 0;
- }
- mySpaceShip.move();
- Left_Bullet.Update();
- apply_surface(bgX, bgY, game_bg, screen);
- apply_surface(bgX, bgY-game_bg->h, game_bg, screen);
- mySpaceShip.show();
- Left_Bullet.Draw();
- if(SDL_Flip(screen) ==-1) {
- return 1;
- }
- if(fps.get_ticks() < 1000 / FPS) {
- SDL_Delay((1000 / FPS) - fps.get_ticks());
- }
- }
- clean_up();
- return 0;
- }
Add Comment
Please, Sign In to add comment