Last active
August 29, 2015 14:25
-
-
Save gkbrk/3c852eb27a8aa7108d74 to your computer and use it in GitHub Desktop.
C++ Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <list> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_image.h> | |
#include "GameState.h" | |
#include "SplashScreen.cpp" | |
#include "AsteroidsGame.cpp" | |
SDL_Window *window = NULL; | |
SDL_Surface *surface = NULL; | |
bool init_sdl(){ | |
if (SDL_Init(SDL_INIT_VIDEO) != 0){ | |
return false; | |
}else{ | |
window = SDL_CreateWindow("Asteroids", 100, 100, 800, 600, SDL_WINDOW_SHOWN); | |
if (window == NULL){ | |
return false; | |
}else{ | |
surface = SDL_GetWindowSurface(window); | |
return true; | |
} | |
} | |
} | |
int main(){ | |
std::list<GameState*> gameStates; | |
gameStates.push_back(new SplashScreen()); | |
gameStates.push_back(new AsteroidsGame()); | |
if (init_sdl()){ | |
IMG_Init(IMG_INIT_PNG); | |
while (gameStates.size() > 0){ | |
GameState *state = gameStates.front(); | |
gameStates.pop_front(); | |
state->stateFinished = false; | |
state->window = window; | |
state->surface = surface; | |
state->InitState(); | |
while (!state->stateFinished){ | |
state->HandleEvents(); | |
state->Update(); | |
state->Draw(); | |
//std::cout << SDL_GetError() << std::endl; | |
} | |
} | |
SDL_DestroyWindow(window); | |
}else{ | |
std::cerr << "Failed to initialize SDL." << std::endl; | |
} | |
SDL_Quit(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <SDL2/SDL.h> | |
#include "GameState.h" | |
class AsteroidsGame: public GameState{ | |
public: | |
void Draw(){ | |
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 255, 0, 0)); | |
SDL_UpdateWindowSurface(window); | |
SDL_Delay(1234); | |
stateFinished = true; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "GameState.h" | |
class AsteroidsGame: public GameState{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL2/SDL.h> | |
#include <iostream> | |
class GameState{ | |
public: | |
virtual void InitState() {} | |
virtual void HandleEvents() {} | |
virtual void Update() {} | |
virtual void Draw() {} | |
bool stateFinished; | |
SDL_Window *window; | |
SDL_Surface *surface; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <SDL2/SDL.h> | |
class GameState{ | |
public: | |
virtual void InitState() {}; | |
virtual void HandleEvents() {}; | |
virtual void Update() {}; | |
virtual void Draw() {}; | |
bool stateFinished; | |
SDL_Window *window; | |
SDL_Surface *surface; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_image.h> | |
#include "GameState.h" | |
class SplashScreen: public GameState{ | |
public: | |
void InitState(){ | |
images.push_back(IMG_Load("SplashScreen/1.png")); | |
images.push_back(IMG_Load("SplashScreen/2.png")); | |
} | |
void Draw(){ | |
for (int i=0;i<images.size();i++){ | |
std::cout << "Blitting image " << i << std::endl; | |
SDL_BlitSurface(images[i], NULL, surface, NULL); | |
SDL_UpdateWindowSurface(window); | |
SDL_Delay(1500); | |
} | |
stateFinished = true; | |
} | |
std::vector<SDL_Surface*> images; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <vector> | |
#include "GameState.h" | |
class SplashScreen: public GameState{ | |
public: | |
std::vector<SDL_Surface*> images; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment