Skip to content

Instantly share code, notes, and snippets.

@gkbrk
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkbrk/3c852eb27a8aa7108d74 to your computer and use it in GitHub Desktop.
Save gkbrk/3c852eb27a8aa7108d74 to your computer and use it in GitHub Desktop.
C++ Game
#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();
}
#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;
}
};
#pragma once
#include "GameState.h"
class AsteroidsGame: public GameState{
}
#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;
};
#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;
};
#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;
};
#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