├── 01 ├── 001 │ ├── compile.sh │ └── main.cpp ├── 002 │ ├── compile.sh │ └── main.cpp └── 003 │ ├── Game.cpp │ ├── Game.h │ ├── compile.sh │ └── main.cpp ├── 02 ├── 001 │ ├── Game.cpp │ ├── Game.h │ ├── compile.sh │ └── main.cpp ├── 002 │ ├── Game.cpp │ ├── Game.h │ ├── compile.sh │ └── main.cpp ├── 003 │ ├── Game.cpp │ ├── Game.h │ ├── compile.sh │ └── main.cpp ├── 004 │ ├── Game.cpp │ ├── Game.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── compile.sh │ └── main.cpp └── 005 │ ├── Game.cpp │ ├── Game.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── compile.sh │ └── main.cpp ├── 03 ├── 001 │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.cpp │ ├── GameObject.h │ ├── Player.cpp │ ├── Player.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── compile.sh │ └── main.cpp ├── 002 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.cpp │ ├── GameObject.h │ ├── Player.cpp │ ├── Player.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── compile.sh │ └── main.cpp └── 003 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── LoaderParams.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── compile.sh │ └── main.cpp ├── 04 ├── 001 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── LoaderParams.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── Vector2D.h │ ├── compile.sh │ └── main.cpp └── 002 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── LoaderParams.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── Vector2D.h │ ├── compile.sh │ └── main.cpp ├── 05 ├── 001 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── GameState.h │ ├── GameStateMachine.cpp │ ├── GameStateMachine.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── LoaderParams.h │ ├── MenuState.cpp │ ├── MenuState.h │ ├── PlayState.cpp │ ├── PlayState.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── Vector2D.h │ ├── compile.sh │ └── main.cpp ├── 002 │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── GameState.h │ ├── GameStateMachine.cpp │ ├── GameStateMachine.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── LoaderParams.h │ ├── MenuButton.cpp │ ├── MenuButton.h │ ├── MenuState.cpp │ ├── MenuState.h │ ├── PlayState.cpp │ ├── PlayState.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── Vector2D.h │ ├── compile.sh │ └── main.cpp └── 003 │ ├── AnimatedGraphic.cpp │ ├── AnimatedGraphic.h │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── GameOverState.cpp │ ├── GameOverState.h │ ├── GameState.h │ ├── GameStateMachine.cpp │ ├── GameStateMachine.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── LoaderParams.h │ ├── MenuButton.cpp │ ├── MenuButton.h │ ├── MenuState.cpp │ ├── MenuState.h │ ├── PauseState.cpp │ ├── PauseState.h │ ├── PlayState.cpp │ ├── PlayState.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── Vector2D.h │ ├── compile.sh │ └── main.cpp ├── 06 └── 001 │ ├── AnimatedGraphic.cpp │ ├── AnimatedGraphic.h │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── GameObjectFactory.cpp │ ├── GameObjectFactory.h │ ├── GameOverState.cpp │ ├── GameOverState.h │ ├── GameState.h │ ├── GameStateMachine.cpp │ ├── GameStateMachine.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── LoaderParams.h │ ├── MainMenuState.cpp │ ├── MainMenuState.h │ ├── MenuButton.cpp │ ├── MenuButton.h │ ├── MenuState.h │ ├── PauseState.cpp │ ├── PauseState.h │ ├── PlayState.cpp │ ├── PlayState.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── StateParser.cpp │ ├── StateParser.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── Vector2D.h │ ├── compile.sh │ ├── main.cpp │ └── test.xml ├── 07 ├── 001 │ ├── AnimatedGraphic.cpp │ ├── AnimatedGraphic.h │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── GameObjectFactory.cpp │ ├── GameObjectFactory.h │ ├── GameOverState.cpp │ ├── GameOverState.h │ ├── GameState.h │ ├── GameStateMachine.cpp │ ├── GameStateMachine.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── Layer.h │ ├── Level.cpp │ ├── Level.h │ ├── LevelParser.cpp │ ├── LevelParser.h │ ├── LoaderParams.h │ ├── MainMenuState.cpp │ ├── MainMenuState.h │ ├── MenuButton.cpp │ ├── MenuButton.h │ ├── MenuState.h │ ├── PauseState.cpp │ ├── PauseState.h │ ├── PlayState.cpp │ ├── PlayState.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── StateParser.cpp │ ├── StateParser.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── TileLayer.cpp │ ├── TileLayer.h │ ├── Vector2D.h │ ├── compile.sh │ ├── hola.txt │ ├── main.cpp │ ├── map1.tmx │ └── test.xml └── 002 │ ├── AnimatedGraphic.cpp │ ├── AnimatedGraphic.h │ ├── Enemy.cpp │ ├── Enemy.h │ ├── Game.cpp │ ├── Game.h │ ├── GameObject.h │ ├── GameObjectFactory.cpp │ ├── GameObjectFactory.h │ ├── GameOverState.cpp │ ├── GameOverState.h │ ├── GameState.h │ ├── GameStateMachine.cpp │ ├── GameStateMachine.h │ ├── InputHandler.cpp │ ├── InputHandler.h │ ├── Layer.h │ ├── Level.cpp │ ├── Level.h │ ├── LevelParser.cpp │ ├── LevelParser.h │ ├── LoaderParams.h │ ├── MainMenuState.cpp │ ├── MainMenuState.h │ ├── MenuButton.cpp │ ├── MenuButton.h │ ├── MenuState.h │ ├── ObjectLayer.cpp │ ├── ObjectLayer.h │ ├── PauseState.cpp │ ├── PauseState.h │ ├── PlayState.cpp │ ├── PlayState.h │ ├── Player.cpp │ ├── Player.h │ ├── SDLGameObject.cpp │ ├── SDLGameObject.h │ ├── StateParser.cpp │ ├── StateParser.h │ ├── TextureManager.cpp │ ├── TextureManager.h │ ├── TileLayer.cpp │ ├── TileLayer.h │ ├── Vector2D.h │ ├── base64.cpp │ ├── base64.h │ ├── compile.sh │ ├── hola.txt │ ├── main.cpp │ ├── map1.tmx │ └── test.xml └── README.md /01/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp -o main `sdl2-config --cflags --libs` 2 | -------------------------------------------------------------------------------- /01/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | SDL_Window* g_pWindow = 0; 4 | SDL_Renderer* g_pRenderer = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | // initialize SDL 9 | if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) 10 | { 11 | // if succeeded create our window 12 | g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN); 13 | 14 | // if the window creation suceeded create our renderer 15 | if(g_pWindow != 0) 16 | { 17 | g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0); 18 | } 19 | } 20 | else 21 | { 22 | return 1; // sdl could not initialize 23 | } 24 | 25 | // everything succeeded lets draw the window 26 | 27 | // set to black 28 | // Alpha as color values 29 | SDL_SetRenderDrawColor(g_pRenderer, 255, 0, 0, 255); 30 | 31 | // clear the window to black 32 | SDL_RenderClear(g_pRenderer); 33 | 34 | // show the window 35 | SDL_RenderPresent(g_pRenderer); 36 | 37 | // set a delay before quitting 38 | SDL_Delay(5000); 39 | 40 | // clean up SDL 41 | SDL_Quit(); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /01/002/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp -o main `sdl2-config --cflags --libs` 2 | -------------------------------------------------------------------------------- /01/002/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | SDL_Window* g_pWindow = 0; 4 | SDL_Renderer* g_pRenderer = 0; 5 | bool g_bRunning = false; // this will create a loop 6 | 7 | bool init(const char* title, int xpos, int ypos, int height, int width, int flags) 8 | { 9 | // initialize SDL 10 | if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) 11 | { 12 | // if succeeded create our window 13 | g_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags); 14 | 15 | // if the window createion succeeded create our renderer 16 | if(g_pWindow != 0) 17 | { 18 | g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0); 19 | } 20 | } 21 | else 22 | { 23 | return false; // sdl could not initialize 24 | } 25 | 26 | return true; 27 | } 28 | 29 | void render() 30 | { 31 | // set to black 32 | SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255); 33 | 34 | // clear the window to black 35 | SDL_RenderClear(g_pRenderer); 36 | 37 | // show the window 38 | SDL_RenderPresent(g_pRenderer); 39 | } 40 | 41 | 42 | int main(int argc, char* args[]) 43 | { 44 | if(init("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN)) 45 | { 46 | g_bRunning = true; 47 | } 48 | else 49 | { 50 | return 1; // something's wrong 51 | } 52 | 53 | while(g_bRunning) 54 | { 55 | render(); 56 | } 57 | 58 | // clean up SDL 59 | SDL_Quit(); 60 | 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /01/003/Game.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) 5 | { 6 | // attempt to initialize SDL 7 | if(SDL_Init(SDL_INIT_EVERYTHING) == 0) 8 | { 9 | int flags = 0; 10 | if(fullscreen) 11 | { 12 | flags = SDL_WINDOW_FULLSCREEN; 13 | } 14 | 15 | std::cout << "SDL init success\n"; 16 | // init the window 17 | m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags); 18 | 19 | if(m_pWindow != 0) // window init success 20 | { 21 | std::cout << "window creation success\n"; 22 | m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0); 23 | 24 | if(m_pRenderer != 0) // renderer init success 25 | { 26 | std::cout << "renderer creation success\n"; 27 | SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255); 28 | } 29 | else 30 | { 31 | std::cout << "renderer init fail\n"; 32 | return false; // renderer init fail 33 | } 34 | } 35 | else 36 | { 37 | std::cout << "window init fail\n"; 38 | return false; // window init fail 39 | } 40 | } 41 | else 42 | { 43 | std::cout << "SDL init fail\n"; 44 | return false; // SDL init fail 45 | } 46 | 47 | std::cout << "init success\n"; 48 | m_bRunning = true; // everything inited successfully, start the main loop 49 | 50 | return true; 51 | } 52 | 53 | void Game::render() 54 | { 55 | SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color 56 | 57 | SDL_RenderPresent(m_pRenderer); // draw to the screen 58 | } 59 | 60 | void Game::clean() 61 | { 62 | std::cout << "cleaning game\n"; 63 | SDL_DestroyWindow(m_pWindow); 64 | SDL_DestroyRenderer(m_pRenderer); 65 | SDL_Quit(); 66 | } 67 | 68 | void Game::handleEvents() 69 | { 70 | SDL_Event event; 71 | if(SDL_PollEvent(&event)) 72 | { 73 | switch(event.type) 74 | { 75 | case SDL_QUIT: 76 | m_bRunning = false; 77 | break; 78 | 79 | default: 80 | break; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /01/003/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | 6 | class Game 7 | { 8 | public: 9 | 10 | Game() {} 11 | ~Game() {} 12 | 13 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 14 | void render(); 15 | void update() {} 16 | void handleEvents(); 17 | void clean(); 18 | 19 | bool running() { return m_bRunning; } 20 | 21 | private: 22 | 23 | SDL_Window* m_pWindow; 24 | SDL_Renderer* m_pRenderer; 25 | 26 | bool m_bRunning; 27 | }; 28 | 29 | #endif // __Game__ 30 | -------------------------------------------------------------------------------- /01/003/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp -o main `sdl2-config --cflags --libs` 2 | -------------------------------------------------------------------------------- /01/003/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | } 18 | g_game->clean(); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /02/001/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | 6 | class Game 7 | { 8 | public: 9 | 10 | Game() {} 11 | ~Game() {} 12 | 13 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 14 | void render(); 15 | void update() {} 16 | void handleEvents(); 17 | void clean(); 18 | 19 | bool running() { return m_bRunning; } 20 | 21 | private: 22 | 23 | SDL_Window* m_pWindow; 24 | SDL_Renderer* m_pRenderer; 25 | SDL_Texture* m_pTexture; // the new SDL_Texture variable 26 | SDL_Rect m_sourceRectangle; // the first rectangle 27 | SDL_Rect m_destinationRectangle; // another rectangle 28 | 29 | bool m_bRunning; 30 | }; 31 | 32 | #endif // __Game__ 33 | -------------------------------------------------------------------------------- /02/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp -o main `sdl2-config --cflags --libs` 2 | -------------------------------------------------------------------------------- /02/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | } 18 | g_game->clean(); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /02/002/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | 6 | class Game 7 | { 8 | public: 9 | 10 | Game() {} 11 | ~Game() {} 12 | 13 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 14 | void render(); 15 | void update(); 16 | void handleEvents(); 17 | void clean(); 18 | 19 | bool running() { return m_bRunning; } 20 | 21 | private: 22 | 23 | SDL_Window* m_pWindow; 24 | SDL_Renderer* m_pRenderer; 25 | SDL_Texture* m_pTexture; // the new SDL_Texture variable 26 | SDL_Rect m_sourceRectangle; // the first rectangle 27 | SDL_Rect m_destinationRectangle; // another rectangle 28 | 29 | bool m_bRunning; 30 | }; 31 | 32 | #endif // __Game__ 33 | -------------------------------------------------------------------------------- /02/002/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp -o main `sdl2-config --cflags --libs` 2 | -------------------------------------------------------------------------------- /02/002/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | } 18 | g_game->clean(); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /02/003/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | 6 | class Game 7 | { 8 | public: 9 | 10 | Game() {} 11 | ~Game() {} 12 | 13 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 14 | void render(); 15 | void update(); 16 | void handleEvents(); 17 | void clean(); 18 | 19 | bool running() { return m_bRunning; } 20 | 21 | private: 22 | 23 | SDL_Window* m_pWindow; 24 | SDL_Renderer* m_pRenderer; 25 | SDL_Texture* m_pTexture; // the new SDL_Texture variable 26 | SDL_Rect m_sourceRectangle; // the first rectangle 27 | SDL_Rect m_destinationRectangle; // another rectangle 28 | 29 | bool m_bRunning; 30 | }; 31 | 32 | #endif // __Game__ 33 | -------------------------------------------------------------------------------- /02/003/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /02/003/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | } 18 | g_game->clean(); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /02/004/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | 7 | class Game 8 | { 9 | public: 10 | 11 | Game() {} 12 | ~Game() {} 13 | 14 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 15 | void render(); 16 | void update(); 17 | void handleEvents(); 18 | void clean(); 19 | 20 | bool running() { return m_bRunning; } 21 | 22 | private: 23 | 24 | SDL_Window* m_pWindow; 25 | SDL_Renderer* m_pRenderer; 26 | SDL_Texture* m_pTexture; 27 | SDL_Rect m_sourceRectangle; 28 | SDL_Rect m_destinationRectangle; 29 | 30 | bool m_bRunning; 31 | 32 | int m_currentFrame; 33 | TextureManager m_textureManager; 34 | }; 35 | 36 | #endif // __Game__ 37 | -------------------------------------------------------------------------------- /02/004/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 5 | { 6 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 7 | 8 | if(pTempSurface == 0) 9 | { 10 | return false; 11 | } 12 | 13 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 14 | 15 | SDL_FreeSurface(pTempSurface); 16 | 17 | if(pTexture != 0) 18 | { 19 | m_textureMap[id] = pTexture; 20 | return true; 21 | } 22 | 23 | return false; 24 | } 25 | 26 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 27 | { 28 | SDL_Rect srcRect; 29 | SDL_Rect destRect; 30 | 31 | srcRect.x = 0; 32 | srcRect.y = 0; 33 | srcRect.w = destRect.w = width; 34 | srcRect.h = destRect.h = height; 35 | destRect.x = x; 36 | destRect.y = y; 37 | 38 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 39 | } 40 | 41 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 42 | { 43 | SDL_Rect srcRect; 44 | SDL_Rect destRect; 45 | srcRect.x = width * currentFrame; 46 | srcRect.y = height * (currentRow - 1); 47 | srcRect.w = destRect.w = width; 48 | srcRect.h = destRect.h = height; 49 | destRect.x = x; 50 | destRect.y = y; 51 | 52 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 53 | } 54 | -------------------------------------------------------------------------------- /02/004/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | private: 17 | 18 | std::map m_textureMap; 19 | }; 20 | 21 | #endif // __TextureManager__ 22 | -------------------------------------------------------------------------------- /02/004/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /02/004/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | } 18 | g_game->clean(); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /02/005/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | 7 | class Game 8 | { 9 | public: 10 | 11 | Game() {} 12 | ~Game() {} 13 | 14 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 15 | void render(); 16 | void update(); 17 | void handleEvents(); 18 | void clean(); 19 | 20 | bool running() { return m_bRunning; } 21 | 22 | private: 23 | 24 | SDL_Window* m_pWindow; 25 | SDL_Renderer* m_pRenderer; 26 | SDL_Texture* m_pTexture; 27 | SDL_Rect m_sourceRectangle; 28 | SDL_Rect m_destinationRectangle; 29 | 30 | bool m_bRunning; 31 | 32 | int m_currentFrame; 33 | }; 34 | 35 | #endif // __Game__ 36 | -------------------------------------------------------------------------------- /02/005/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /02/005/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /02/005/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /02/005/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | } 18 | g_game->clean(); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /03/001/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | 9 | class Game 10 | { 11 | public: 12 | 13 | Game() {} 14 | ~Game() {} 15 | 16 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 17 | void render(); 18 | void update(); 19 | void handleEvents(); 20 | void clean(); 21 | 22 | bool running() { return m_bRunning; } 23 | 24 | private: 25 | 26 | SDL_Window* m_pWindow; 27 | SDL_Renderer* m_pRenderer; 28 | SDL_Texture* m_pTexture; 29 | SDL_Rect m_sourceRectangle; 30 | SDL_Rect m_destinationRectangle; 31 | 32 | bool m_bRunning; 33 | int m_currentFrame; 34 | 35 | GameObject m_go; 36 | Player m_player; 37 | }; 38 | 39 | #endif // __Game__ 40 | -------------------------------------------------------------------------------- /03/001/GameObject.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "GameObject.h" 3 | #include "TextureManager.h" 4 | 5 | void GameObject::load(int x, int y, int width, int height, std::string textureID) 6 | { 7 | m_x = x; 8 | m_y = y; 9 | m_width = width; 10 | m_height = height; 11 | m_textureID = textureID; 12 | 13 | m_currentRow = 1; 14 | m_currentFrame = 1; 15 | } 16 | 17 | void GameObject::draw(SDL_Renderer* pRenderer) 18 | { 19 | TextureManager::Instance()->drawFrame(m_textureID, m_x, m_y, m_width, m_height, m_currentRow, m_currentFrame, pRenderer); 20 | } 21 | 22 | void GameObject::update() 23 | { 24 | m_x += 1; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /03/001/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | #include "SDL.h" 6 | 7 | class GameObject 8 | { 9 | public: 10 | 11 | void load(int x, int y, int width, int height, std::string textureID); 12 | void draw(SDL_Renderer* pRenderer); 13 | void update(); 14 | void clean(); 15 | 16 | protected: 17 | 18 | std::string m_textureID; 19 | 20 | int m_currentFrame; 21 | int m_currentRow; 22 | 23 | int m_x; 24 | int m_y; 25 | 26 | int m_width; 27 | int m_height; 28 | }; 29 | 30 | #endif // __GameObject__ 31 | -------------------------------------------------------------------------------- /03/001/Player.cpp: -------------------------------------------------------------------------------- 1 | #include "Player.h" 2 | #include 3 | 4 | void Player::load(int x, int y, int width, int height, std::string textureID) 5 | { 6 | GameObject::load(x, y, width, height, textureID); 7 | } 8 | 9 | void Player::draw(SDL_Renderer* pRenderer) 10 | { 11 | GameObject::draw(pRenderer); 12 | } 13 | 14 | void Player::update() 15 | { 16 | m_x -= 1; 17 | } 18 | -------------------------------------------------------------------------------- /03/001/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include "GameObject.h" 5 | 6 | class Player : public GameObject 7 | { 8 | public: 9 | 10 | void load(int x, int y, int width, int height, std::string textureID); 11 | void draw(SDL_Renderer* pRenderer); 12 | void update(); 13 | void clean(); 14 | }; 15 | 16 | #endif // __Player__ 17 | -------------------------------------------------------------------------------- /03/001/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /03/001/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /03/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp GameObject.cpp Player.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /03/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | 18 | SDL_Delay(10); 19 | } 20 | g_game->clean(); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /03/002/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include "Enemy.h" 2 | #include 3 | 4 | void Enemy::load(int x, int y, int width, int height, std::string textureID) 5 | { 6 | GameObject::load(x, y, width, height, textureID); 7 | } 8 | 9 | void Enemy::draw(SDL_Renderer* pRenderer) 10 | { 11 | GameObject::draw(pRenderer); 12 | } 13 | 14 | void Enemy::update() 15 | { 16 | m_x += 1; 17 | m_y += 1; 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 19 | } 20 | -------------------------------------------------------------------------------- /03/002/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | 8 | class Enemy : public GameObject 9 | { 10 | public: 11 | 12 | void load(int x, int y, int width, int height, std::string textureID); 13 | void draw(SDL_Renderer* pRenderer); 14 | void update(); 15 | void clean(); 16 | }; 17 | 18 | #endif // __Enemy__ 19 | -------------------------------------------------------------------------------- /03/002/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include 5 | 6 | #include "SDL.h" 7 | #include "TextureManager.h" 8 | #include "GameObject.h" 9 | #include "Player.h" 10 | #include "Enemy.h" 11 | 12 | class Game 13 | { 14 | public: 15 | 16 | Game() {} 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | 25 | bool running() { return m_bRunning; } 26 | 27 | private: 28 | 29 | SDL_Window* m_pWindow; 30 | SDL_Renderer* m_pRenderer; 31 | SDL_Texture* m_pTexture; 32 | SDL_Rect m_sourceRectangle; 33 | SDL_Rect m_destinationRectangle; 34 | 35 | bool m_bRunning; 36 | int m_currentFrame; 37 | 38 | GameObject* m_go; 39 | GameObject* m_player; 40 | GameObject* m_enemy; 41 | std::vector m_gameObjects; 42 | 43 | }; 44 | 45 | #endif // __Game__ 46 | -------------------------------------------------------------------------------- /03/002/GameObject.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "GameObject.h" 3 | #include "TextureManager.h" 4 | 5 | void GameObject::load(int x, int y, int width, int height, std::string textureID) 6 | { 7 | m_x = x; 8 | m_y = y; 9 | m_width = width; 10 | m_height = height; 11 | m_textureID = textureID; 12 | 13 | m_currentRow = 1; 14 | m_currentFrame = 1; 15 | } 16 | 17 | void GameObject::draw(SDL_Renderer* pRenderer) 18 | { 19 | TextureManager::Instance()->drawFrame(m_textureID, m_x, m_y, m_width, m_height, m_currentRow, m_currentFrame, pRenderer); 20 | } 21 | 22 | void GameObject::update() 23 | { 24 | m_x += 1; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /03/002/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | #include "SDL.h" 6 | 7 | class GameObject 8 | { 9 | public: 10 | 11 | virtual void load(int x, int y, int width, int height, std::string textureID); 12 | void draw(SDL_Renderer* pRenderer); 13 | virtual void update(); 14 | void clean(); 15 | 16 | protected: 17 | 18 | std::string m_textureID; 19 | 20 | int m_currentFrame; 21 | int m_currentRow; 22 | 23 | int m_x; 24 | int m_y; 25 | 26 | int m_width; 27 | int m_height; 28 | }; 29 | 30 | #endif // __GameObject__ 31 | -------------------------------------------------------------------------------- /03/002/Player.cpp: -------------------------------------------------------------------------------- 1 | #include "Player.h" 2 | #include 3 | 4 | void Player::load(int x, int y, int width, int height, std::string textureID) 5 | { 6 | GameObject::load(x, y, width, height, textureID); 7 | } 8 | 9 | void Player::draw(SDL_Renderer* pRenderer) 10 | { 11 | GameObject::draw(pRenderer); 12 | } 13 | 14 | void Player::update() 15 | { 16 | m_x -= 1; 17 | } 18 | -------------------------------------------------------------------------------- /03/002/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | 8 | class Player : public GameObject 9 | { 10 | public: 11 | 12 | void load(int x, int y, int width, int height, std::string textureID); 13 | void draw(SDL_Renderer* pRenderer); 14 | void update(); 15 | void clean(); 16 | }; 17 | 18 | #endif // __Player__ 19 | -------------------------------------------------------------------------------- /03/002/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /03/002/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /03/002/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp GameObject.cpp Player.cpp Enemy.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /03/002/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | // our Game object 4 | Game* g_game = 0; 5 | 6 | int main(int argc, char* args[]) 7 | { 8 | g_game = new Game(); 9 | 10 | g_game->init("Chapter 1", 100, 100, 640, 480, false); 11 | 12 | while(g_game->running()) 13 | { 14 | g_game->handleEvents(); 15 | g_game->update(); 16 | g_game->render(); 17 | 18 | SDL_Delay(10); 19 | } 20 | g_game->clean(); 21 | 22 | return 0; 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /03/003/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Enemy::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Enemy::update() 14 | { 15 | m_x += 1; 16 | m_y += 1; 17 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 18 | } 19 | 20 | void Enemy::clean() {} 21 | -------------------------------------------------------------------------------- /03/003/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Enemy : public SDLGameObject 9 | { 10 | public: 11 | 12 | Enemy(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Enemy__ 20 | -------------------------------------------------------------------------------- /03/003/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include 5 | 6 | #include "SDL.h" 7 | #include "TextureManager.h" 8 | #include "GameObject.h" 9 | #include "Player.h" 10 | #include "Enemy.h" 11 | 12 | class Game 13 | { 14 | public: 15 | 16 | ~Game() {} 17 | 18 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 19 | void render(); 20 | void update(); 21 | void handleEvents(); 22 | void clean(); 23 | 24 | bool running() { return m_bRunning; } 25 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 26 | 27 | static Game* Instance() 28 | { 29 | if(s_pInstance == 0) 30 | { 31 | s_pInstance = new Game(); 32 | return s_pInstance; 33 | } 34 | } 35 | 36 | private: 37 | 38 | Game() {}; 39 | 40 | SDL_Window* m_pWindow; 41 | SDL_Renderer* m_pRenderer; 42 | SDL_Texture* m_pTexture; 43 | SDL_Rect m_sourceRectangle; 44 | SDL_Rect m_destinationRectangle; 45 | 46 | bool m_bRunning; 47 | int m_currentFrame; 48 | 49 | std::vector m_gameObjects; 50 | 51 | static Game* s_pInstance; 52 | }; 53 | 54 | typedef Game TheGame; 55 | #endif // __Game__ 56 | -------------------------------------------------------------------------------- /03/003/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | 16 | protected: 17 | 18 | GameObject(const LoaderParams* pParams) {} 19 | virtual ~GameObject() {} 20 | }; 21 | 22 | #endif // __GameObject__ 23 | -------------------------------------------------------------------------------- /03/003/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) 11 | { } 12 | 13 | int getX() const { return m_x; } 14 | int getY() const { return m_y; } 15 | int getWidth() const { return m_width; } 16 | int getHeight() const { return m_height; } 17 | std::string getTextureID() const { return m_textureID; } 18 | 19 | private: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | std::string m_textureID; 28 | }; 29 | 30 | #endif // __LoaderParams__ 31 | -------------------------------------------------------------------------------- /03/003/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | 6 | Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Player::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Player::update() 14 | { 15 | m_x -= 1; 16 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 17 | } 18 | 19 | void Player::clean() {} 20 | -------------------------------------------------------------------------------- /03/003/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Player : public SDLGameObject 9 | { 10 | public: 11 | 12 | Player(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Player__ 20 | -------------------------------------------------------------------------------- /03/003/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | 5 | SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams) 6 | { 7 | m_x = pParams->getX(); 8 | m_y = pParams->getY(); 9 | m_width = pParams->getWidth(); 10 | m_height = pParams->getHeight(); 11 | m_textureID = pParams->getTextureID(); 12 | 13 | m_currentRow = 1; 14 | m_currentFrame = 1; 15 | } 16 | 17 | void SDLGameObject::draw() 18 | { 19 | TextureManager::Instance()->drawFrame(m_textureID, m_x, m_y, m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 20 | } 21 | -------------------------------------------------------------------------------- /03/003/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | 9 | class SDLGameObject : public GameObject 10 | { 11 | public: 12 | 13 | SDLGameObject(const LoaderParams* pParams); 14 | 15 | virtual void draw(); 16 | virtual void update() {}; 17 | virtual void clean() {}; 18 | 19 | protected: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | int m_currentRow; 28 | int m_currentFrame; 29 | 30 | std::string m_textureID; 31 | }; 32 | 33 | #endif // __SDLGameObject__ 34 | -------------------------------------------------------------------------------- /03/003/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /03/003/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /03/003/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /03/003/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | 7 | int main(int argc, char* args[]) 8 | { 9 | std::cout << "game init attempt...\n"; 10 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 11 | { 12 | std::cout << "game init success!\n"; 13 | while(TheGame::Instance()->running()) 14 | { 15 | TheGame::Instance()->handleEvents(); 16 | TheGame::Instance()->update(); 17 | TheGame::Instance()->render(); 18 | 19 | SDL_Delay(10); 20 | } 21 | } 22 | else 23 | { 24 | std::cout << "game init failure - " << SDL_GetError() << "\n"; 25 | return -1; 26 | } 27 | 28 | std::cout << "game closing...\n"; 29 | TheGame::Instance()->clean(); 30 | 31 | return 0; 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /04/001/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Enemy::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Enemy::update() 14 | { 15 | m_position.setX(m_position.getX() + 1); 16 | m_position.setY(m_position.getY() + 1); 17 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 18 | } 19 | 20 | void Enemy::clean() {} 21 | -------------------------------------------------------------------------------- /04/001/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Enemy : public SDLGameObject 9 | { 10 | public: 11 | 12 | Enemy(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Enemy__ 20 | -------------------------------------------------------------------------------- /04/001/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include 5 | 6 | #include "SDL.h" 7 | #include "TextureManager.h" 8 | #include "GameObject.h" 9 | #include "Player.h" 10 | #include "Enemy.h" 11 | 12 | class Game 13 | { 14 | public: 15 | 16 | ~Game() {} 17 | 18 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 19 | void render(); 20 | void update(); 21 | void handleEvents(); 22 | void clean(); 23 | 24 | bool running() { return m_bRunning; } 25 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 26 | 27 | static Game* Instance() 28 | { 29 | if(s_pInstance == 0) 30 | { 31 | s_pInstance = new Game(); 32 | return s_pInstance; 33 | } 34 | } 35 | 36 | private: 37 | 38 | Game() {}; 39 | 40 | SDL_Window* m_pWindow; 41 | SDL_Renderer* m_pRenderer; 42 | SDL_Texture* m_pTexture; 43 | SDL_Rect m_sourceRectangle; 44 | SDL_Rect m_destinationRectangle; 45 | 46 | bool m_bRunning; 47 | int m_currentFrame; 48 | 49 | std::vector m_gameObjects; 50 | 51 | static Game* s_pInstance; 52 | }; 53 | 54 | typedef Game TheGame; 55 | #endif // __Game__ 56 | -------------------------------------------------------------------------------- /04/001/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | 16 | protected: 17 | 18 | GameObject(const LoaderParams* pParams) {} 19 | virtual ~GameObject() {} 20 | }; 21 | 22 | #endif // __GameObject__ 23 | -------------------------------------------------------------------------------- /04/001/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) 11 | { } 12 | 13 | int getX() const { return m_x; } 14 | int getY() const { return m_y; } 15 | int getWidth() const { return m_width; } 16 | int getHeight() const { return m_height; } 17 | std::string getTextureID() const { return m_textureID; } 18 | 19 | private: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | std::string m_textureID; 28 | }; 29 | 30 | #endif // __LoaderParams__ 31 | -------------------------------------------------------------------------------- /04/001/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | 6 | Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Player::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Player::update() 14 | { 15 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 16 | m_acceleration.setX(1); 17 | SDLGameObject::update(); 18 | } 19 | 20 | void Player::clean() {} 21 | -------------------------------------------------------------------------------- /04/001/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Player : public SDLGameObject 9 | { 10 | public: 11 | 12 | Player(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Player__ 20 | -------------------------------------------------------------------------------- /04/001/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | 5 | SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams), m_position(pParams->getX(), pParams->getY()), m_velocity(0, 0), m_acceleration(0, 0) 6 | { 7 | m_width = pParams->getWidth(); 8 | m_height = pParams->getHeight(); 9 | m_textureID = pParams->getTextureID(); 10 | 11 | m_currentRow = 1; 12 | m_currentFrame = 1; 13 | } 14 | 15 | void SDLGameObject::draw() 16 | { 17 | TextureManager::Instance()->drawFrame(m_textureID, (int)m_position.getX(), (int)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 18 | } 19 | 20 | void SDLGameObject::update() 21 | { 22 | m_velocity += m_acceleration; 23 | m_position += m_velocity; 24 | } 25 | -------------------------------------------------------------------------------- /04/001/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(const LoaderParams* pParams); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | 20 | protected: 21 | 22 | Vector2D m_position; 23 | Vector2D m_velocity; 24 | Vector2D m_acceleration; 25 | 26 | int m_width; 27 | int m_height; 28 | 29 | int m_currentRow; 30 | int m_currentFrame; 31 | 32 | std::string m_textureID; 33 | }; 34 | 35 | #endif // __SDLGameObject__ 36 | -------------------------------------------------------------------------------- /04/001/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /04/001/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /04/001/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /04/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /04/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt...\n"; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << "\n"; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing...\n"; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /04/002/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Enemy::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Enemy::update() 14 | { 15 | m_position.setX(m_position.getX() + 1); 16 | m_position.setY(m_position.getY() + 1); 17 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 18 | } 19 | 20 | void Enemy::clean() {} 21 | -------------------------------------------------------------------------------- /04/002/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Enemy : public SDLGameObject 9 | { 10 | public: 11 | 12 | Enemy(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Enemy__ 20 | -------------------------------------------------------------------------------- /04/002/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include 5 | 6 | #include "SDL.h" 7 | #include "TextureManager.h" 8 | #include "GameObject.h" 9 | #include "Player.h" 10 | #include "Enemy.h" 11 | 12 | class Game 13 | { 14 | public: 15 | 16 | ~Game() {} 17 | 18 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 19 | void render(); 20 | void update(); 21 | void handleEvents(); 22 | void clean(); 23 | void quit() { m_bRunning = false; } 24 | 25 | bool running() { return m_bRunning; } 26 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 27 | 28 | static Game* Instance() 29 | { 30 | if(s_pInstance == 0) 31 | { 32 | s_pInstance = new Game(); 33 | return s_pInstance; 34 | } 35 | } 36 | 37 | private: 38 | 39 | Game() {}; 40 | 41 | SDL_Window* m_pWindow; 42 | SDL_Renderer* m_pRenderer; 43 | SDL_Texture* m_pTexture; 44 | SDL_Rect m_sourceRectangle; 45 | SDL_Rect m_destinationRectangle; 46 | 47 | bool m_bRunning; 48 | int m_currentFrame; 49 | 50 | std::vector m_gameObjects; 51 | 52 | static Game* s_pInstance; 53 | }; 54 | 55 | typedef Game TheGame; 56 | #endif // __Game__ 57 | -------------------------------------------------------------------------------- /04/002/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | 16 | protected: 17 | 18 | GameObject(const LoaderParams* pParams) {} 19 | virtual ~GameObject() {} 20 | }; 21 | 22 | #endif // __GameObject__ 23 | -------------------------------------------------------------------------------- /04/002/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) 11 | { } 12 | 13 | int getX() const { return m_x; } 14 | int getY() const { return m_y; } 15 | int getWidth() const { return m_width; } 16 | int getHeight() const { return m_height; } 17 | std::string getTextureID() const { return m_textureID; } 18 | 19 | private: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | std::string m_textureID; 28 | }; 29 | 30 | #endif // __LoaderParams__ 31 | -------------------------------------------------------------------------------- /04/002/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 17 | 18 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RIGHT)) 19 | { 20 | m_velocity.setX(2); 21 | } 22 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LEFT)) 23 | { 24 | m_velocity.setX(-2); 25 | } 26 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_UP)) 27 | { 28 | m_velocity.setY(-2); 29 | } 30 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_DOWN)) 31 | { 32 | m_velocity.setY(2); 33 | } 34 | 35 | SDLGameObject::update(); 36 | } 37 | 38 | void Player::clean() {} 39 | -------------------------------------------------------------------------------- /04/002/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Player : public SDLGameObject 9 | { 10 | public: 11 | 12 | Player(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Player__ 20 | -------------------------------------------------------------------------------- /04/002/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | 5 | SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams), m_position(pParams->getX(), pParams->getY()), m_velocity(0, 0), m_acceleration(0, 0) 6 | { 7 | m_width = pParams->getWidth(); 8 | m_height = pParams->getHeight(); 9 | m_textureID = pParams->getTextureID(); 10 | 11 | m_currentRow = 1; 12 | m_currentFrame = 1; 13 | } 14 | 15 | void SDLGameObject::draw() 16 | { 17 | TextureManager::Instance()->drawFrame(m_textureID, (int)m_position.getX(), (int)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 18 | } 19 | 20 | void SDLGameObject::update() 21 | { 22 | m_velocity += m_acceleration; 23 | m_position += m_velocity; 24 | } 25 | -------------------------------------------------------------------------------- /04/002/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(const LoaderParams* pParams); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | 20 | protected: 21 | 22 | Vector2D m_position; 23 | Vector2D m_velocity; 24 | Vector2D m_acceleration; 25 | 26 | int m_width; 27 | int m_height; 28 | 29 | int m_currentRow; 30 | int m_currentFrame; 31 | 32 | std::string m_textureID; 33 | }; 34 | 35 | #endif // __SDLGameObject__ 36 | -------------------------------------------------------------------------------- /04/002/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /04/002/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /04/002/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /04/002/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /04/002/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt...\n"; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << "\n"; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing...\n"; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /05/001/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Enemy::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Enemy::update() 14 | { 15 | m_position.setX(m_position.getX() + 1); 16 | m_position.setY(m_position.getY() + 1); 17 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 18 | } 19 | 20 | void Enemy::clean() {} 21 | -------------------------------------------------------------------------------- /05/001/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Enemy : public SDLGameObject 9 | { 10 | public: 11 | 12 | Enemy(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Enemy__ 20 | -------------------------------------------------------------------------------- /05/001/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | #include "Enemy.h" 9 | #include "GameStateMachine.h" 10 | #include "MenuState.h" 11 | #include "PlayState.h" 12 | 13 | class Game 14 | { 15 | public: 16 | 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | void quit() { m_bRunning = false; } 25 | 26 | bool running() { return m_bRunning; } 27 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 28 | 29 | static Game* Instance() 30 | { 31 | if(s_pInstance == 0) 32 | { 33 | s_pInstance = new Game(); 34 | return s_pInstance; 35 | } 36 | } 37 | 38 | private: 39 | 40 | Game() {}; 41 | 42 | SDL_Window* m_pWindow; 43 | SDL_Renderer* m_pRenderer; 44 | SDL_Texture* m_pTexture; 45 | SDL_Rect m_sourceRectangle; 46 | SDL_Rect m_destinationRectangle; 47 | 48 | bool m_bRunning; 49 | int m_currentFrame; 50 | 51 | GameStateMachine* m_pGameStateMachine; 52 | 53 | static Game* s_pInstance; 54 | }; 55 | 56 | typedef Game TheGame; 57 | #endif // __Game__ 58 | -------------------------------------------------------------------------------- /05/001/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | 16 | protected: 17 | 18 | GameObject(const LoaderParams* pParams) {} 19 | virtual ~GameObject() {} 20 | }; 21 | 22 | #endif // __GameObject__ 23 | -------------------------------------------------------------------------------- /05/001/GameState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameState__ 2 | #define __GameState__ 3 | 4 | #include 5 | 6 | class GameState 7 | { 8 | public: 9 | virtual void update() = 0; 10 | virtual void render() = 0; 11 | 12 | virtual bool onEnter() = 0; 13 | virtual bool onExit() = 0; 14 | 15 | virtual std::string getStateID() const = 0; 16 | }; 17 | 18 | #endif // __GameState__ 19 | 20 | -------------------------------------------------------------------------------- /05/001/GameStateMachine.cpp: -------------------------------------------------------------------------------- 1 | #include "GameStateMachine.h" 2 | 3 | void GameStateMachine::pushState(GameState *pState) 4 | { 5 | m_gameStates.push_back(pState); 6 | m_gameStates.back()->onEnter(); 7 | } 8 | 9 | void GameStateMachine::popState() 10 | { 11 | if(!m_gameStates.empty()) 12 | { 13 | if(m_gameStates.back()->onExit()) 14 | { 15 | delete m_gameStates.back(); 16 | m_gameStates.pop_back(); 17 | } 18 | } 19 | } 20 | 21 | void GameStateMachine::changeState(GameState* pState) 22 | { 23 | if(!m_gameStates.empty()) 24 | { 25 | if(m_gameStates.back()->getStateID() == pState->getStateID()) 26 | { 27 | return; // do nothing 28 | } 29 | 30 | if(m_gameStates.back()->onExit()) 31 | { 32 | delete m_gameStates.back(); 33 | m_gameStates.pop_back(); 34 | } 35 | } 36 | 37 | // push back our new state 38 | m_gameStates.push_back(pState); 39 | 40 | // initialise it 41 | m_gameStates.back()->onEnter(); 42 | } 43 | 44 | void GameStateMachine::update() 45 | { 46 | if(!m_gameStates.empty()) 47 | { 48 | m_gameStates.back()->update(); 49 | } 50 | } 51 | 52 | void GameStateMachine::render() 53 | { 54 | if(!m_gameStates.empty()) 55 | { 56 | m_gameStates.back()->render(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /05/001/GameStateMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameStateMachine__ 2 | #define __GameStateMachine__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameStateMachine 9 | { 10 | public: 11 | 12 | void pushState(GameState* pState); 13 | void changeState(GameState* pState); 14 | void popState(); 15 | 16 | void update(); 17 | void render(); 18 | 19 | private: 20 | std::vector m_gameStates; 21 | }; 22 | 23 | #endif // __GameStateMachine__ 24 | -------------------------------------------------------------------------------- /05/001/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) 11 | { } 12 | 13 | int getX() const { return m_x; } 14 | int getY() const { return m_y; } 15 | int getWidth() const { return m_width; } 16 | int getHeight() const { return m_height; } 17 | std::string getTextureID() const { return m_textureID; } 18 | 19 | private: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | std::string m_textureID; 28 | }; 29 | 30 | #endif // __LoaderParams__ 31 | -------------------------------------------------------------------------------- /05/001/MenuState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "MenuState.h" 5 | 6 | const std::string MenuState::s_menuID = "MENU"; 7 | 8 | void MenuState::update() 9 | { 10 | } 11 | 12 | void MenuState::render() 13 | { 14 | } 15 | 16 | bool MenuState::onEnter() 17 | { 18 | std::cout << "entering MenuState\n"; 19 | return true; 20 | } 21 | 22 | bool MenuState::onExit() 23 | { 24 | std::cout << "exiting MenuState\n"; 25 | return true; 26 | } 27 | -------------------------------------------------------------------------------- /05/001/MenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuState__ 2 | #define __MenuState__ 3 | 4 | #include "GameState.h" 5 | 6 | class MenuState : public GameState 7 | { 8 | public: 9 | 10 | virtual void update(); 11 | virtual void render(); 12 | 13 | virtual bool onEnter(); 14 | virtual bool onExit(); 15 | 16 | virtual std::string getStateID() const { return s_menuID; } 17 | 18 | private: 19 | 20 | static const std::string s_menuID; 21 | }; 22 | 23 | #endif // __MenuState__ 24 | -------------------------------------------------------------------------------- /05/001/PlayState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "PlayState.h" 5 | 6 | const std::string PlayState::s_playID = "PLAY"; 7 | 8 | void PlayState::update() 9 | { 10 | } 11 | 12 | void PlayState::render() 13 | {} 14 | 15 | bool PlayState::onEnter() 16 | { 17 | std::cout << "entering PlayState\n"; 18 | return true; 19 | } 20 | 21 | bool PlayState::onExit() 22 | { 23 | std::cout << "exiting PlayState\n"; 24 | return true; 25 | } 26 | -------------------------------------------------------------------------------- /05/001/PlayState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PlayState__ 2 | #define __PlayState__ 3 | 4 | #include "GameState.h" 5 | 6 | class PlayState : public GameState 7 | { 8 | public: 9 | 10 | virtual void update(); 11 | virtual void render(); 12 | 13 | virtual bool onEnter(); 14 | virtual bool onExit(); 15 | 16 | virtual std::string getStateID() const { return s_playID; } 17 | 18 | private: 19 | 20 | static const std::string s_playID; 21 | }; 22 | 23 | #endif // __PlayState__ 24 | -------------------------------------------------------------------------------- /05/001/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 17 | 18 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RIGHT)) 19 | { 20 | m_velocity.setX(2); 21 | } 22 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LEFT)) 23 | { 24 | m_velocity.setX(-2); 25 | } 26 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_UP)) 27 | { 28 | m_velocity.setY(-2); 29 | } 30 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_DOWN)) 31 | { 32 | m_velocity.setY(2); 33 | } 34 | 35 | SDLGameObject::update(); 36 | } 37 | 38 | void Player::clean() {} 39 | -------------------------------------------------------------------------------- /05/001/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Player : public SDLGameObject 9 | { 10 | public: 11 | 12 | Player(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Player__ 20 | -------------------------------------------------------------------------------- /05/001/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | 5 | SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams), m_position(pParams->getX(), pParams->getY()), m_velocity(0, 0), m_acceleration(0, 0) 6 | { 7 | m_width = pParams->getWidth(); 8 | m_height = pParams->getHeight(); 9 | m_textureID = pParams->getTextureID(); 10 | 11 | m_currentRow = 1; 12 | m_currentFrame = 1; 13 | } 14 | 15 | void SDLGameObject::draw() 16 | { 17 | TextureManager::Instance()->drawFrame(m_textureID, (int)m_position.getX(), (int)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 18 | } 19 | 20 | void SDLGameObject::update() 21 | { 22 | m_velocity += m_acceleration; 23 | m_position += m_velocity; 24 | } 25 | -------------------------------------------------------------------------------- /05/001/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(const LoaderParams* pParams); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | 20 | protected: 21 | 22 | Vector2D m_position; 23 | Vector2D m_velocity; 24 | Vector2D m_acceleration; 25 | 26 | int m_width; 27 | int m_height; 28 | 29 | int m_currentRow; 30 | int m_currentFrame; 31 | 32 | std::string m_textureID; 33 | }; 34 | 35 | #endif // __SDLGameObject__ 36 | -------------------------------------------------------------------------------- /05/001/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | -------------------------------------------------------------------------------- /05/001/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | 16 | static TextureManager* Instance() 17 | { 18 | if(s_pInstance == 0) 19 | { 20 | s_pInstance = new TextureManager(); 21 | return s_pInstance; 22 | } 23 | return s_pInstance; 24 | } 25 | 26 | private: 27 | 28 | TextureManager() {} 29 | 30 | std::map m_textureMap; 31 | static TextureManager* s_pInstance; 32 | 33 | }; 34 | 35 | typedef TextureManager TheTextureManager; 36 | 37 | #endif // __TextureManager__ 38 | -------------------------------------------------------------------------------- /05/001/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /05/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp MenuState.cpp PlayState.cpp GameStateMachine.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /05/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt...\n"; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << "\n"; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing...\n"; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /05/002/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) {} 7 | 8 | void Enemy::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void Enemy::update() 14 | { 15 | m_position.setX(m_position.getX() + 1); 16 | m_position.setY(m_position.getY() + 1); 17 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 18 | } 19 | 20 | void Enemy::clean() {} 21 | -------------------------------------------------------------------------------- /05/002/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Enemy : public SDLGameObject 9 | { 10 | public: 11 | 12 | Enemy(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Enemy__ 20 | -------------------------------------------------------------------------------- /05/002/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | #include "Enemy.h" 9 | #include "GameStateMachine.h" 10 | #include "MenuState.h" 11 | #include "PlayState.h" 12 | 13 | class Game 14 | { 15 | public: 16 | 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | void quit() { m_bRunning = false; } 25 | 26 | bool running() { return m_bRunning; } 27 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 28 | 29 | static Game* Instance() 30 | { 31 | if(s_pInstance == 0) 32 | { 33 | s_pInstance = new Game(); 34 | return s_pInstance; 35 | } 36 | } 37 | 38 | private: 39 | 40 | Game() {}; 41 | 42 | SDL_Window* m_pWindow; 43 | SDL_Renderer* m_pRenderer; 44 | SDL_Texture* m_pTexture; 45 | SDL_Rect m_sourceRectangle; 46 | SDL_Rect m_destinationRectangle; 47 | 48 | bool m_bRunning; 49 | int m_currentFrame; 50 | 51 | GameStateMachine* m_pGameStateMachine; 52 | 53 | static Game* s_pInstance; 54 | }; 55 | 56 | typedef Game TheGame; 57 | #endif // __Game__ 58 | -------------------------------------------------------------------------------- /05/002/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | 16 | protected: 17 | 18 | GameObject(const LoaderParams* pParams) {} 19 | virtual ~GameObject() {} 20 | }; 21 | 22 | #endif // __GameObject__ 23 | -------------------------------------------------------------------------------- /05/002/GameState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameState__ 2 | #define __GameState__ 3 | 4 | #include 5 | 6 | class GameState 7 | { 8 | public: 9 | virtual void update() = 0; 10 | virtual void render() = 0; 11 | 12 | virtual bool onEnter() = 0; 13 | virtual bool onExit() = 0; 14 | 15 | virtual std::string getStateID() const = 0; 16 | }; 17 | 18 | #endif // __GameState__ 19 | 20 | -------------------------------------------------------------------------------- /05/002/GameStateMachine.cpp: -------------------------------------------------------------------------------- 1 | #include "GameStateMachine.h" 2 | 3 | void GameStateMachine::pushState(GameState *pState) 4 | { 5 | m_gameStates.push_back(pState); 6 | m_gameStates.back()->onEnter(); 7 | } 8 | 9 | void GameStateMachine::popState() 10 | { 11 | if(!m_gameStates.empty()) 12 | { 13 | if(m_gameStates.back()->onExit()) 14 | { 15 | delete m_gameStates.back(); 16 | m_gameStates.pop_back(); 17 | } 18 | } 19 | } 20 | 21 | void GameStateMachine::changeState(GameState* pState) 22 | { 23 | if(!m_gameStates.empty()) 24 | { 25 | if(m_gameStates.back()->getStateID() == pState->getStateID()) 26 | { 27 | return; // do nothing 28 | } 29 | 30 | if(m_gameStates.back()->onExit()) 31 | { 32 | delete m_gameStates.back(); 33 | m_gameStates.pop_back(); 34 | } 35 | } 36 | 37 | // push back our new state 38 | m_gameStates.push_back(pState); 39 | 40 | // initialise it 41 | m_gameStates.back()->onEnter(); 42 | } 43 | 44 | void GameStateMachine::update() 45 | { 46 | if(!m_gameStates.empty()) 47 | { 48 | m_gameStates.back()->update(); 49 | } 50 | } 51 | 52 | void GameStateMachine::render() 53 | { 54 | if(!m_gameStates.empty()) 55 | { 56 | m_gameStates.back()->render(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /05/002/GameStateMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameStateMachine__ 2 | #define __GameStateMachine__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameStateMachine 9 | { 10 | public: 11 | 12 | void pushState(GameState* pState); 13 | void changeState(GameState* pState); 14 | void popState(); 15 | 16 | void update(); 17 | void render(); 18 | 19 | private: 20 | std::vector m_gameStates; 21 | }; 22 | 23 | #endif // __GameStateMachine__ 24 | -------------------------------------------------------------------------------- /05/002/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) 11 | { } 12 | 13 | int getX() const { return m_x; } 14 | int getY() const { return m_y; } 15 | int getWidth() const { return m_width; } 16 | int getHeight() const { return m_height; } 17 | std::string getTextureID() const { return m_textureID; } 18 | 19 | private: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | std::string m_textureID; 28 | }; 29 | 30 | #endif // __LoaderParams__ 31 | -------------------------------------------------------------------------------- /05/002/MenuButton.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuButton.h" 2 | #include "Vector2D.h" 3 | #include "InputHandler.h" 4 | 5 | MenuButton::MenuButton(const LoaderParams* pParams) : SDLGameObject(pParams) 6 | { 7 | m_currentFrame = MOUSE_OUT; // start at frame 0 8 | } 9 | 10 | void MenuButton::draw() 11 | { 12 | SDLGameObject::draw(); // use the base class drawing 13 | } 14 | 15 | void MenuButton::update() 16 | { 17 | Vector2D* pMousePos = TheInputHandler::Instance()->getMousePosition(); 18 | 19 | if(pMousePos->getX() < (m_position.getX() + m_width) 20 | && pMousePos->getX() > m_position.getX() 21 | && pMousePos->getY() < (m_position.getY() + m_height) 22 | && pMousePos->getY() > m_position.getY()) 23 | { 24 | m_currentFrame = MOUSE_OVER; 25 | 26 | if(TheInputHandler::Instance()->getMouseButtonState(LEFT)) 27 | { 28 | m_currentFrame = CLICKED; 29 | } 30 | } 31 | else 32 | { 33 | m_currentFrame = MOUSE_OUT; 34 | } 35 | } 36 | 37 | void MenuButton::clean() 38 | { 39 | SDLGameObject::clean(); 40 | } 41 | -------------------------------------------------------------------------------- /05/002/MenuButton.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuButton__ 2 | #define __MenuButton__ 3 | 4 | #include "LoaderParams.h" 5 | #include "SDLGameObject.h" 6 | 7 | class MenuButton : public SDLGameObject 8 | { 9 | public: 10 | 11 | MenuButton(const LoaderParams* pParams); 12 | 13 | virtual void draw(); 14 | virtual void update(); 15 | virtual void clean(); 16 | 17 | private: 18 | enum button_state 19 | { 20 | MOUSE_OUT = 0, 21 | MOUSE_OVER = 1, 22 | CLICKED = 2 23 | }; 24 | }; 25 | 26 | #endif // __MenuButton__ 27 | -------------------------------------------------------------------------------- /05/002/MenuState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "MenuState.h" 5 | #include "MenuButton.h" 6 | #include "TextureManager.h" 7 | #include "Game.h" 8 | 9 | const std::string MenuState::s_menuID = "MENU"; 10 | 11 | void MenuState::update() 12 | { 13 | for(int i = 0; i < m_gameObjects.size(); i++) 14 | { 15 | m_gameObjects[i]->update(); 16 | } 17 | } 18 | 19 | void MenuState::render() 20 | { 21 | for(int i = 0; i < m_gameObjects.size(); i++) 22 | { 23 | m_gameObjects[i]->draw(); 24 | } 25 | } 26 | 27 | bool MenuState::onEnter() 28 | { 29 | if(!TheTextureManager::Instance()->load("assets/button.png", "playbutton", TheGame::Instance()->getRenderer())) 30 | { 31 | return false; 32 | } 33 | 34 | if(!TheTextureManager::Instance()->load("assets/exit.png", "exitbutton", TheGame::Instance()->getRenderer())) 35 | { 36 | return false; 37 | } 38 | 39 | GameObject* button1 = new MenuButton(new LoaderParams(100, 100, 400, 100, "playbutton")); 40 | GameObject* button2 = new MenuButton(new LoaderParams(100, 300, 400, 100, "exitbutton")); 41 | 42 | m_gameObjects.push_back(button1); 43 | m_gameObjects.push_back(button2); 44 | 45 | std::cout << "entering MenuState\n"; 46 | return true; 47 | } 48 | 49 | bool MenuState::onExit() 50 | { 51 | for(int i = 0; i < m_gameObjects.size(); i++) 52 | { 53 | m_gameObjects[i]-> clean(); 54 | } 55 | m_gameObjects.clear(); 56 | TheTextureManager::Instance()->clearFromTextureMap("playbutton"); 57 | TheTextureManager::Instance()->clearFromTextureMap("exitbutton"); 58 | 59 | std::cout << "exiting MenuState\n"; 60 | return true; 61 | } 62 | -------------------------------------------------------------------------------- /05/002/MenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuState__ 2 | #define __MenuState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "GameObject.h" 8 | 9 | class MenuState : public GameState 10 | { 11 | public: 12 | 13 | virtual void update(); 14 | virtual void render(); 15 | 16 | virtual bool onEnter(); 17 | virtual bool onExit(); 18 | 19 | virtual std::string getStateID() const { return s_menuID; } 20 | 21 | private: 22 | 23 | static const std::string s_menuID; 24 | std::vector m_gameObjects; 25 | }; 26 | 27 | #endif // __MenuState__ 28 | -------------------------------------------------------------------------------- /05/002/PlayState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "PlayState.h" 5 | 6 | const std::string PlayState::s_playID = "PLAY"; 7 | 8 | void PlayState::update() 9 | { 10 | } 11 | 12 | void PlayState::render() 13 | {} 14 | 15 | bool PlayState::onEnter() 16 | { 17 | std::cout << "entering PlayState\n"; 18 | return true; 19 | } 20 | 21 | bool PlayState::onExit() 22 | { 23 | std::cout << "exiting PlayState\n"; 24 | return true; 25 | } 26 | -------------------------------------------------------------------------------- /05/002/PlayState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PlayState__ 2 | #define __PlayState__ 3 | 4 | #include "GameState.h" 5 | 6 | class PlayState : public GameState 7 | { 8 | public: 9 | 10 | virtual void update(); 11 | virtual void render(); 12 | 13 | virtual bool onEnter(); 14 | virtual bool onExit(); 15 | 16 | virtual std::string getStateID() const { return s_playID; } 17 | 18 | private: 19 | 20 | static const std::string s_playID; 21 | }; 22 | 23 | #endif // __PlayState__ 24 | -------------------------------------------------------------------------------- /05/002/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | m_currentFrame = int(((SDL_GetTicks() / 100) % 6)); 17 | 18 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RIGHT)) 19 | { 20 | m_velocity.setX(2); 21 | } 22 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_LEFT)) 23 | { 24 | m_velocity.setX(-2); 25 | } 26 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_UP)) 27 | { 28 | m_velocity.setY(-2); 29 | } 30 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_DOWN)) 31 | { 32 | m_velocity.setY(2); 33 | } 34 | 35 | SDLGameObject::update(); 36 | } 37 | 38 | void Player::clean() {} 39 | -------------------------------------------------------------------------------- /05/002/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Player : public SDLGameObject 9 | { 10 | public: 11 | 12 | Player(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Player__ 20 | -------------------------------------------------------------------------------- /05/002/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | 5 | SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams), m_position(pParams->getX(), pParams->getY()), m_velocity(0, 0), m_acceleration(0, 0) 6 | { 7 | m_width = pParams->getWidth(); 8 | m_height = pParams->getHeight(); 9 | m_textureID = pParams->getTextureID(); 10 | 11 | m_currentRow = 1; 12 | m_currentFrame = 1; 13 | } 14 | 15 | void SDLGameObject::draw() 16 | { 17 | TextureManager::Instance()->drawFrame(m_textureID, (int)m_position.getX(), (int)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 18 | } 19 | 20 | void SDLGameObject::update() 21 | { 22 | m_velocity += m_acceleration; 23 | m_position += m_velocity; 24 | } 25 | -------------------------------------------------------------------------------- /05/002/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(const LoaderParams* pParams); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | 20 | protected: 21 | 22 | Vector2D m_position; 23 | Vector2D m_velocity; 24 | Vector2D m_acceleration; 25 | 26 | int m_width; 27 | int m_height; 28 | 29 | int m_currentRow; 30 | int m_currentFrame; 31 | 32 | std::string m_textureID; 33 | }; 34 | 35 | #endif // __SDLGameObject__ 36 | -------------------------------------------------------------------------------- /05/002/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | 57 | void TextureManager::clearFromTextureMap(std::string id) 58 | { 59 | m_textureMap.erase(id); 60 | } 61 | -------------------------------------------------------------------------------- /05/002/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | void clearFromTextureMap(std::string id); 16 | 17 | static TextureManager* Instance() 18 | { 19 | if(s_pInstance == 0) 20 | { 21 | s_pInstance = new TextureManager(); 22 | return s_pInstance; 23 | } 24 | return s_pInstance; 25 | } 26 | 27 | private: 28 | 29 | TextureManager() {} 30 | 31 | std::map m_textureMap; 32 | static TextureManager* s_pInstance; 33 | 34 | }; 35 | 36 | typedef TextureManager TheTextureManager; 37 | 38 | #endif // __TextureManager__ 39 | -------------------------------------------------------------------------------- /05/002/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /05/002/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -O0 -g -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp MenuState.cpp PlayState.cpp GameStateMachine.cpp MenuButton.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /05/002/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt...\n"; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << "\n"; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing...\n"; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /05/003/AnimatedGraphic.cpp: -------------------------------------------------------------------------------- 1 | #include "SDL.h" 2 | #include "AnimatedGraphic.h" 3 | 4 | AnimatedGraphic::AnimatedGraphic(const LoaderParams *pParams, int animSpeed) : SDLGameObject(pParams), m_animSpeed(animSpeed) 5 | { 6 | } 7 | 8 | void AnimatedGraphic::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void AnimatedGraphic::update() 14 | { 15 | m_currentFrame = int(((SDL_GetTicks() / (1000 / m_animSpeed)) % 2)); 16 | } 17 | 18 | void AnimatedGraphic::clean() 19 | { 20 | } 21 | -------------------------------------------------------------------------------- /05/003/AnimatedGraphic.h: -------------------------------------------------------------------------------- 1 | #ifndef __AnimatedGraphic__ 2 | #define __AnimatedGraphic__ 3 | 4 | #include "SDLGameObject.h" 5 | #include "LoaderParams.h" 6 | 7 | class AnimatedGraphic : public SDLGameObject 8 | { 9 | public: 10 | 11 | AnimatedGraphic(const LoaderParams *pParams, int animSpeed); 12 | 13 | virtual void draw(); 14 | virtual void update(); 15 | virtual void clean(); 16 | 17 | private: 18 | int m_animSpeed; 19 | }; 20 | 21 | #endif /* defined(__AnimatedGraphic__) */ 22 | -------------------------------------------------------------------------------- /05/003/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy(const LoaderParams* pParams) : SDLGameObject(pParams) 7 | { 8 | m_velocity.setY(2); 9 | m_velocity.setX(0.001); 10 | } 11 | 12 | void Enemy::draw() 13 | { 14 | SDLGameObject::draw(); 15 | } 16 | 17 | void Enemy::update() 18 | { 19 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 20 | 21 | if(m_position.getY() < 0) 22 | { 23 | m_velocity.setY(2); 24 | } 25 | else if(m_position.getY() > 400) 26 | { 27 | m_velocity.setY(-2); 28 | } 29 | 30 | SDLGameObject::update(); 31 | } 32 | 33 | void Enemy::clean() {} 34 | -------------------------------------------------------------------------------- /05/003/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Enemy : public SDLGameObject 9 | { 10 | public: 11 | 12 | Enemy(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | }; 18 | 19 | #endif // __Enemy__ 20 | -------------------------------------------------------------------------------- /05/003/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | #include "Enemy.h" 9 | #include "GameStateMachine.h" 10 | #include "MenuState.h" 11 | #include "PlayState.h" 12 | 13 | class Game 14 | { 15 | public: 16 | 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | void quit() { m_bRunning = false; } 25 | GameStateMachine* getStateMachine() { return m_pGameStateMachine; } 26 | 27 | bool running() { return m_bRunning; } 28 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 29 | 30 | static Game* Instance() 31 | { 32 | if(s_pInstance == 0) 33 | { 34 | s_pInstance = new Game(); 35 | return s_pInstance; 36 | } 37 | } 38 | 39 | private: 40 | 41 | Game() {}; 42 | 43 | SDL_Window* m_pWindow; 44 | SDL_Renderer* m_pRenderer; 45 | SDL_Texture* m_pTexture; 46 | SDL_Rect m_sourceRectangle; 47 | SDL_Rect m_destinationRectangle; 48 | 49 | bool m_bRunning; 50 | int m_currentFrame; 51 | 52 | GameStateMachine* m_pGameStateMachine; 53 | 54 | static Game* s_pInstance; 55 | }; 56 | 57 | typedef Game TheGame; 58 | #endif // __Game__ 59 | -------------------------------------------------------------------------------- /05/003/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | 16 | protected: 17 | 18 | GameObject(const LoaderParams* pParams) {} 19 | virtual ~GameObject() {} 20 | }; 21 | 22 | #endif // __GameObject__ 23 | -------------------------------------------------------------------------------- /05/003/GameOverState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameOverState__ 2 | #define __GameOverState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "GameState.h" 8 | 9 | class GameObject; 10 | 11 | class GameOverState : public GameState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_gameOverID; } 21 | 22 | private: 23 | static void s_gameOverToMain(); 24 | static void s_restartPlay(); 25 | 26 | static const std::string s_gameOverID; 27 | 28 | std::vector m_gameObjects; 29 | }; 30 | 31 | #endif // __GameOverState__ 32 | -------------------------------------------------------------------------------- /05/003/GameState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameState__ 2 | #define __GameState__ 3 | 4 | #include 5 | 6 | class GameState 7 | { 8 | public: 9 | virtual void update() = 0; 10 | virtual void render() = 0; 11 | 12 | virtual bool onEnter() = 0; 13 | virtual bool onExit() = 0; 14 | 15 | virtual std::string getStateID() const = 0; 16 | }; 17 | 18 | #endif // __GameState__ 19 | 20 | -------------------------------------------------------------------------------- /05/003/GameStateMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameStateMachine__ 2 | #define __GameStateMachine__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameStateMachine 9 | { 10 | public: 11 | 12 | GameStateMachine() : newState(nullptr), popStateFlag(false), changeStateFlag(false) {} 13 | void pushState(GameState* pState); 14 | void changeState(GameState* pState); 15 | void popState(); 16 | 17 | void update(); 18 | void render(); 19 | 20 | private: 21 | std::vector m_gameStates; 22 | void popStatePrivate(); 23 | void changeStatePrivate(GameState* pState); 24 | GameState* newState; 25 | bool popStateFlag; 26 | bool changeStateFlag; 27 | }; 28 | 29 | #endif // __GameStateMachine__ 30 | -------------------------------------------------------------------------------- /05/003/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) 11 | { } 12 | 13 | int getX() const { return m_x; } 14 | int getY() const { return m_y; } 15 | int getWidth() const { return m_width; } 16 | int getHeight() const { return m_height; } 17 | std::string getTextureID() const { return m_textureID; } 18 | 19 | private: 20 | 21 | int m_x; 22 | int m_y; 23 | 24 | int m_width; 25 | int m_height; 26 | 27 | std::string m_textureID; 28 | }; 29 | 30 | #endif // __LoaderParams__ 31 | -------------------------------------------------------------------------------- /05/003/MenuButton.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuButton.h" 2 | #include "Vector2D.h" 3 | #include "InputHandler.h" 4 | 5 | MenuButton::MenuButton(const LoaderParams* pParams, void(*callback)()) : SDLGameObject(pParams), m_callback(callback) 6 | { 7 | m_currentFrame = MOUSE_OUT; // start at frame 0 8 | } 9 | 10 | void MenuButton::draw() 11 | { 12 | SDLGameObject::draw(); // use the base class drawing 13 | } 14 | 15 | void MenuButton::update() 16 | { 17 | Vector2D* pMousePos = TheInputHandler::Instance()->getMousePosition(); 18 | 19 | if(pMousePos->getX() < (m_position.getX() + m_width) 20 | && pMousePos->getX() > m_position.getX() 21 | && pMousePos->getY() < (m_position.getY() + m_height) 22 | && pMousePos->getY() > m_position.getY()) 23 | { 24 | if(TheInputHandler::Instance()->getMouseButtonState(LEFT) && m_bReleased) 25 | { 26 | m_currentFrame = CLICKED; 27 | m_callback(); // call our callback function 28 | m_bReleased = false; 29 | } 30 | else if(!TheInputHandler::Instance()->getMouseButtonState(LEFT)) 31 | { 32 | m_bReleased = true; 33 | m_currentFrame = MOUSE_OVER; 34 | } 35 | } 36 | else 37 | { 38 | m_currentFrame = MOUSE_OUT; 39 | } 40 | } 41 | 42 | void MenuButton::clean() 43 | { 44 | SDLGameObject::clean(); 45 | } 46 | -------------------------------------------------------------------------------- /05/003/MenuButton.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuButton__ 2 | #define __MenuButton__ 3 | 4 | #include "LoaderParams.h" 5 | #include "SDLGameObject.h" 6 | 7 | class MenuButton : public SDLGameObject 8 | { 9 | public: 10 | 11 | MenuButton(const LoaderParams* pParams, void (*callback)()); 12 | 13 | virtual void draw(); 14 | virtual void update(); 15 | virtual void clean(); 16 | 17 | private: 18 | enum button_state 19 | { 20 | MOUSE_OUT = 0, 21 | MOUSE_OVER = 1, 22 | CLICKED = 2 23 | }; 24 | 25 | void(*m_callback)(); 26 | bool m_bReleased; 27 | }; 28 | 29 | #endif // __MenuButton__ 30 | -------------------------------------------------------------------------------- /05/003/MenuState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "MenuState.h" 5 | #include "MenuButton.h" 6 | #include "TextureManager.h" 7 | #include "Game.h" 8 | 9 | const std::string MenuState::s_menuID = "MENU"; 10 | 11 | void MenuState::update() 12 | { 13 | for(int i = 0; i < m_gameObjects.size(); i++) 14 | { 15 | m_gameObjects[i]->update(); 16 | } 17 | } 18 | 19 | void MenuState::render() 20 | { 21 | for(int i = 0; i < m_gameObjects.size(); i++) 22 | { 23 | m_gameObjects[i]->draw(); 24 | } 25 | } 26 | 27 | bool MenuState::onEnter() 28 | { 29 | if(!TheTextureManager::Instance()->load("assets/button.png", "playbutton", TheGame::Instance()->getRenderer())) 30 | { 31 | return false; 32 | } 33 | 34 | if(!TheTextureManager::Instance()->load("assets/exit.png", "exitbutton", TheGame::Instance()->getRenderer())) 35 | { 36 | return false; 37 | } 38 | 39 | GameObject* button1 = new MenuButton(new LoaderParams(100, 100, 400, 100, "playbutton"), s_menuToPlay); 40 | GameObject* button2 = new MenuButton(new LoaderParams(100, 300, 400, 100, "exitbutton"), s_exitFromMenu); 41 | 42 | m_gameObjects.push_back(button1); 43 | m_gameObjects.push_back(button2); 44 | 45 | std::cout << "entering MenuState" << std::endl; 46 | return true; 47 | } 48 | 49 | bool MenuState::onExit() 50 | { 51 | for(int i = 0; i < m_gameObjects.size(); i++) 52 | { 53 | m_gameObjects[i]-> clean(); 54 | } 55 | m_gameObjects.clear(); 56 | TheTextureManager::Instance()->clearFromTextureMap("playbutton"); 57 | TheTextureManager::Instance()->clearFromTextureMap("exitbutton"); 58 | 59 | std::cout << "exiting MenuState" << std::endl; 60 | return true; 61 | } 62 | 63 | void MenuState::s_menuToPlay() 64 | { 65 | std::cout << "Play button clicked" << std::endl; 66 | TheGame::Instance()->getStateMachine()->changeState(new PlayState()); 67 | } 68 | 69 | void MenuState::s_exitFromMenu() 70 | { 71 | std::cout << "Exit button clicked" << std::endl; 72 | TheGame::Instance()->quit(); 73 | } 74 | -------------------------------------------------------------------------------- /05/003/MenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuState__ 2 | #define __MenuState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "GameObject.h" 8 | 9 | class MenuState : public GameState 10 | { 11 | public: 12 | 13 | virtual void update(); 14 | virtual void render(); 15 | 16 | virtual bool onEnter(); 17 | virtual bool onExit(); 18 | 19 | virtual std::string getStateID() const { return s_menuID; } 20 | 21 | private: 22 | 23 | static const std::string s_menuID; 24 | std::vector m_gameObjects; 25 | static void s_menuToPlay(); 26 | static void s_exitFromMenu(); 27 | }; 28 | 29 | #endif // __MenuState__ 30 | -------------------------------------------------------------------------------- /05/003/PauseState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PauseState__ 2 | #define __PauseState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "GameState.h" 8 | 9 | class GameObject; 10 | 11 | class PauseState : public GameState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_pauseID; } 21 | 22 | private: 23 | static void s_pauseToMain(); 24 | static void s_resumePlay(); 25 | 26 | static const std::string s_pauseID; 27 | 28 | std::vector m_gameObjects; 29 | }; 30 | 31 | #endif // __PauseState__ 32 | -------------------------------------------------------------------------------- /05/003/PlayState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PlayState__ 2 | #define __PlayState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameObject; 9 | class SDLGameObject; 10 | 11 | class PlayState : public GameState 12 | { 13 | public: 14 | 15 | virtual void update(); 16 | virtual void render(); 17 | 18 | virtual bool onEnter(); 19 | virtual bool onExit(); 20 | 21 | virtual std::string getStateID() const { return s_playID; } 22 | 23 | private: 24 | 25 | static const std::string s_playID; 26 | std::vector m_gameObjects; 27 | 28 | bool checkCollision(SDLGameObject* p1, SDLGameObject* p2); 29 | }; 30 | 31 | #endif // __PlayState__ 32 | -------------------------------------------------------------------------------- /05/003/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams) {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | handleInput(); 17 | 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | SDLGameObject::update(); 21 | } 22 | 23 | void Player::clean() {} 24 | 25 | void Player::handleInput() 26 | { 27 | Vector2D* target = TheInputHandler::Instance()->getMousePosition(); 28 | m_velocity = *target - m_position; 29 | m_velocity /= 50; 30 | } 31 | -------------------------------------------------------------------------------- /05/003/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | 8 | class Player : public SDLGameObject 9 | { 10 | public: 11 | 12 | Player(const LoaderParams* pParams); 13 | 14 | void draw(); 15 | void update(); 16 | void clean(); 17 | void handleInput(); 18 | }; 19 | 20 | #endif // __Player__ 21 | -------------------------------------------------------------------------------- /05/003/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | 5 | SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams), m_position(pParams->getX(), pParams->getY()), m_velocity(0, 0), m_acceleration(0, 0) 6 | { 7 | m_width = pParams->getWidth(); 8 | m_height = pParams->getHeight(); 9 | m_textureID = pParams->getTextureID(); 10 | 11 | m_currentRow = 1; 12 | m_currentFrame = 1; 13 | } 14 | 15 | void SDLGameObject::draw() 16 | { 17 | if(m_velocity.getX() > 0) 18 | { 19 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer(), SDL_FLIP_HORIZONTAL); 20 | } 21 | else 22 | { 23 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 24 | } 25 | } 26 | 27 | void SDLGameObject::update() 28 | { 29 | m_velocity += m_acceleration; 30 | m_position += m_velocity; 31 | } 32 | -------------------------------------------------------------------------------- /05/003/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(const LoaderParams* pParams); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | 20 | Vector2D& getPosition() { return m_position; } 21 | int getWidth() { return m_width; } 22 | int getHeight() { return m_height; } 23 | 24 | protected: 25 | 26 | Vector2D m_position; 27 | Vector2D m_velocity; 28 | Vector2D m_acceleration; 29 | 30 | int m_width; 31 | int m_height; 32 | 33 | int m_currentRow; 34 | int m_currentFrame; 35 | 36 | std::string m_textureID; 37 | }; 38 | 39 | #endif // __SDLGameObject__ 40 | -------------------------------------------------------------------------------- /05/003/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | 57 | void TextureManager::clearFromTextureMap(std::string id) 58 | { 59 | m_textureMap.erase(id); 60 | } 61 | -------------------------------------------------------------------------------- /05/003/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | void clearFromTextureMap(std::string id); 16 | 17 | static TextureManager* Instance() 18 | { 19 | if(s_pInstance == 0) 20 | { 21 | s_pInstance = new TextureManager(); 22 | return s_pInstance; 23 | } 24 | return s_pInstance; 25 | } 26 | 27 | private: 28 | 29 | TextureManager() {} 30 | 31 | std::map m_textureMap; 32 | static TextureManager* s_pInstance; 33 | 34 | }; 35 | 36 | typedef TextureManager TheTextureManager; 37 | 38 | #endif // __TextureManager__ 39 | -------------------------------------------------------------------------------- /05/003/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /05/003/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -O0 -g -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp MenuState.cpp PlayState.cpp GameStateMachine.cpp MenuButton.cpp PauseState.cpp AnimatedGraphic.cpp GameOverState.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /05/003/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt..." << std::endl; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << std::endl; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing..." << std::endl; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /06/001/AnimatedGraphic.cpp: -------------------------------------------------------------------------------- 1 | #include "SDL.h" 2 | #include "AnimatedGraphic.h" 3 | 4 | AnimatedGraphic::AnimatedGraphic() : SDLGameObject() 5 | { 6 | } 7 | 8 | void AnimatedGraphic::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void AnimatedGraphic::update() 14 | { 15 | m_currentFrame = int(((SDL_GetTicks() / (1000 / m_animSpeed)) % 2)); 16 | } 17 | 18 | void AnimatedGraphic::clean() 19 | { 20 | } 21 | 22 | void AnimatedGraphic::load(const LoaderParams* pParams) 23 | { 24 | SDLGameObject::load(pParams); 25 | m_animSpeed = pParams->getAnimSpeed(); 26 | } 27 | -------------------------------------------------------------------------------- /06/001/AnimatedGraphic.h: -------------------------------------------------------------------------------- 1 | #ifndef __AnimatedGraphic__ 2 | #define __AnimatedGraphic__ 3 | 4 | #include "SDLGameObject.h" 5 | #include "LoaderParams.h" 6 | #include "GameObjectFactory.h" 7 | 8 | class AnimatedGraphic : public SDLGameObject 9 | { 10 | public: 11 | 12 | AnimatedGraphic(); 13 | 14 | virtual void draw(); 15 | virtual void update(); 16 | virtual void clean(); 17 | virtual void load(const LoaderParams* pParams); 18 | 19 | private: 20 | int m_animSpeed; 21 | }; 22 | 23 | class AnimatedGraphicCreator : public BaseCreator 24 | { 25 | public: 26 | GameObject* createGameObject() const 27 | { 28 | return new AnimatedGraphic(); 29 | } 30 | }; 31 | 32 | #endif /* defined(__AnimatedGraphic__) */ 33 | -------------------------------------------------------------------------------- /06/001/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy() : SDLGameObject() 7 | { 8 | 9 | } 10 | 11 | void Enemy::draw() 12 | { 13 | SDLGameObject::draw(); 14 | } 15 | 16 | void Enemy::update() 17 | { 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | if(m_position.getY() < 0) 21 | { 22 | m_velocity.setY(2); 23 | } 24 | else if(m_position.getY() > 400) 25 | { 26 | m_velocity.setY(-2); 27 | } 28 | 29 | SDLGameObject::update(); 30 | } 31 | 32 | void Enemy::clean() {} 33 | 34 | void Enemy::load(const LoaderParams *pParams) 35 | { 36 | SDLGameObject::load(pParams); 37 | m_velocity.setY(2); 38 | } 39 | -------------------------------------------------------------------------------- /06/001/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | #include "GameObjectFactory.h" 8 | 9 | class Enemy : public SDLGameObject 10 | { 11 | public: 12 | 13 | Enemy(); 14 | 15 | void draw(); 16 | void update(); 17 | void clean(); 18 | virtual void load(const LoaderParams* pParams); 19 | }; 20 | 21 | class EnemyCreator : public BaseCreator 22 | { 23 | GameObject* createGameObject() const 24 | { 25 | return new Enemy(); 26 | } 27 | }; 28 | 29 | #endif // __Enemy__ 30 | -------------------------------------------------------------------------------- /06/001/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | #include "Enemy.h" 9 | #include "GameStateMachine.h" 10 | #include "MenuState.h" 11 | #include "PlayState.h" 12 | 13 | class Game 14 | { 15 | public: 16 | 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | void quit() { m_bRunning = false; } 25 | GameStateMachine* getStateMachine() { return m_pGameStateMachine; } 26 | 27 | bool running() { return m_bRunning; } 28 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 29 | 30 | static Game* Instance() 31 | { 32 | if(s_pInstance == 0) 33 | { 34 | s_pInstance = new Game(); 35 | return s_pInstance; 36 | } 37 | } 38 | 39 | private: 40 | 41 | Game() {}; 42 | 43 | SDL_Window* m_pWindow; 44 | SDL_Renderer* m_pRenderer; 45 | SDL_Texture* m_pTexture; 46 | SDL_Rect m_sourceRectangle; 47 | SDL_Rect m_destinationRectangle; 48 | 49 | bool m_bRunning; 50 | int m_currentFrame; 51 | 52 | GameStateMachine* m_pGameStateMachine; 53 | 54 | static Game* s_pInstance; 55 | }; 56 | 57 | typedef Game TheGame; 58 | #endif // __Game__ 59 | -------------------------------------------------------------------------------- /06/001/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | virtual void load(const LoaderParams* pParams) = 0; 16 | 17 | protected: 18 | 19 | GameObject() {} 20 | virtual ~GameObject() {} 21 | }; 22 | 23 | #endif // __GameObject__ 24 | -------------------------------------------------------------------------------- /06/001/GameObjectFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "GameObjectFactory.h" 2 | 3 | GameObjectFactory* GameObjectFactory::pInstance = 0; 4 | -------------------------------------------------------------------------------- /06/001/GameObjectFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObjectFactory__ 2 | #define __GameObjectFactory__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "GameObject.h" 9 | 10 | class BaseCreator 11 | { 12 | public: 13 | virtual GameObject* createGameObject() const = 0; 14 | virtual ~BaseCreator() {} 15 | }; 16 | 17 | class GameObjectFactory 18 | { 19 | public: 20 | 21 | static GameObjectFactory* Instance() 22 | { 23 | if(pInstance == 0) 24 | { 25 | pInstance = new GameObjectFactory(); 26 | } 27 | 28 | return pInstance; 29 | } 30 | 31 | bool registerType(std::string typeID, BaseCreator* pCreator) 32 | { 33 | std::map::iterator it = m_creators.find(typeID); 34 | 35 | // if the type is alreade registered, do nothing 36 | if(it != m_creators.end()) 37 | { 38 | delete pCreator; 39 | return false; 40 | } 41 | 42 | m_creators[typeID] = pCreator; 43 | 44 | return true; 45 | } 46 | 47 | GameObject* create(std::string typeID) 48 | { 49 | std::map::iterator it = m_creators.find(typeID); 50 | 51 | if(it == m_creators.end()) 52 | { 53 | std::cout << "could not find type: " << typeID << std::endl; 54 | return NULL; 55 | } 56 | 57 | BaseCreator* pCreator = (*it).second; 58 | return pCreator->createGameObject(); 59 | } 60 | 61 | private: 62 | 63 | static GameObjectFactory* pInstance; 64 | std::map m_creators; 65 | 66 | }; 67 | 68 | typedef GameObjectFactory TheGameObjectFactory; 69 | 70 | #endif // __GameObjectFactory__ 71 | -------------------------------------------------------------------------------- /06/001/GameOverState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameOverState__ 2 | #define __GameOverState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "MenuState.h" 8 | 9 | class GameObject; 10 | 11 | class GameOverState : public MenuState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_gameOverID; } 21 | 22 | private: 23 | 24 | virtual void setCallbacks(const std::vector& callbacks); 25 | 26 | static void s_gameOverToMain(); 27 | static void s_restartPlay(); 28 | 29 | static const std::string s_gameOverID; 30 | 31 | std::vector m_gameObjects; 32 | }; 33 | 34 | #endif // __GameOverState__ 35 | -------------------------------------------------------------------------------- /06/001/GameState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameState__ 2 | #define __GameState__ 3 | 4 | #include 5 | 6 | class GameState 7 | { 8 | public: 9 | 10 | virtual void update() = 0; 11 | virtual void render() = 0; 12 | 13 | virtual bool onEnter() = 0; 14 | virtual bool onExit() = 0; 15 | 16 | virtual std::string getStateID() const = 0; 17 | 18 | protected: 19 | std::vector m_textureIDList; 20 | }; 21 | 22 | #endif // __GameState__ 23 | 24 | -------------------------------------------------------------------------------- /06/001/GameStateMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameStateMachine__ 2 | #define __GameStateMachine__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameStateMachine 9 | { 10 | public: 11 | 12 | GameStateMachine() : newState(nullptr), popStateFlag(false), changeStateFlag(false) {} 13 | void pushState(GameState* pState); 14 | void changeState(GameState* pState); 15 | void popState(); 16 | 17 | void update(); 18 | void render(); 19 | 20 | private: 21 | std::vector m_gameStates; 22 | void popStatePrivate(); 23 | void changeStatePrivate(GameState* pState); 24 | GameState* newState; 25 | bool popStateFlag; 26 | bool changeStateFlag; 27 | }; 28 | 29 | #endif // __GameStateMachine__ 30 | -------------------------------------------------------------------------------- /06/001/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID, int numFrames, int callbackID = 0, int animSpeed = 0) : 11 | m_x(x), 12 | m_y(y), 13 | m_width(width), 14 | m_height(height), 15 | m_textureID(textureID), 16 | m_numFrames(numFrames), 17 | m_callbackID(callbackID), 18 | m_animSpeed(animSpeed) 19 | { 20 | 21 | } 22 | 23 | int getX() const { return m_x; } 24 | int getY() const { return m_y; } 25 | int getWidth() const { return m_width; } 26 | int getHeight() const { return m_height; } 27 | int getAnimSpeed() const { return m_animSpeed; } 28 | int getCallbackID() const { return m_callbackID; } 29 | int getNumFrames() const { return m_numFrames; } 30 | std::string getTextureID() const { return m_textureID; } 31 | 32 | private: 33 | 34 | int m_x; 35 | int m_y; 36 | 37 | int m_width; 38 | int m_height; 39 | 40 | std::string m_textureID; 41 | 42 | int m_numFrames; 43 | int m_animSpeed; 44 | int m_callbackID; 45 | }; 46 | 47 | #endif // __LoaderParams__ 48 | -------------------------------------------------------------------------------- /06/001/MainMenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MainMenuState__ 2 | #define __MainMenuState__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "MenuState.h" 8 | 9 | class MainMenuState : public MenuState 10 | { 11 | public: 12 | 13 | virtual void update(); 14 | virtual void render(); 15 | 16 | virtual bool onEnter(); 17 | virtual bool onExit(); 18 | 19 | virtual std::string getStateID() const { return s_menuID; } 20 | 21 | private: 22 | 23 | virtual void setCallbacks(const std::vector& callbacks); 24 | 25 | // callback functions for menu items 26 | static void s_menuToPlay(); 27 | static void s_exitFromMenu(); 28 | 29 | static const std::string s_menuID; 30 | std::vector m_gameObjects; 31 | }; 32 | 33 | #endif // __MainMenuState__ 34 | -------------------------------------------------------------------------------- /06/001/MenuButton.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuButton.h" 2 | #include "Vector2D.h" 3 | #include "InputHandler.h" 4 | 5 | MenuButton::MenuButton() : SDLGameObject() 6 | { 7 | m_currentFrame = MOUSE_OUT; // start at frame 0 8 | } 9 | 10 | void MenuButton::draw() 11 | { 12 | SDLGameObject::draw(); // use the base class drawing 13 | } 14 | 15 | void MenuButton::update() 16 | { 17 | Vector2D* pMousePos = TheInputHandler::Instance()->getMousePosition(); 18 | 19 | if(pMousePos->getX() < (m_position.getX() + m_width) 20 | && pMousePos->getX() > m_position.getX() 21 | && pMousePos->getY() < (m_position.getY() + m_height) 22 | && pMousePos->getY() > m_position.getY()) 23 | { 24 | if(TheInputHandler::Instance()->getMouseButtonState(LEFT) && m_bReleased) 25 | { 26 | m_currentFrame = CLICKED; 27 | m_callback(); // call our callback function 28 | m_bReleased = false; 29 | } 30 | else if(!TheInputHandler::Instance()->getMouseButtonState(LEFT)) 31 | { 32 | m_bReleased = true; 33 | m_currentFrame = MOUSE_OVER; 34 | } 35 | } 36 | else 37 | { 38 | m_currentFrame = MOUSE_OUT; 39 | } 40 | } 41 | 42 | void MenuButton::clean() 43 | { 44 | SDLGameObject::clean(); 45 | } 46 | 47 | void MenuButton::load(const LoaderParams *pParams) 48 | { 49 | SDLGameObject::load(pParams); 50 | m_callbackID = pParams->getCallbackID(); 51 | m_currentFrame = MOUSE_OUT; 52 | } 53 | -------------------------------------------------------------------------------- /06/001/MenuButton.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuButton__ 2 | #define __MenuButton__ 3 | 4 | #include "LoaderParams.h" 5 | #include "SDLGameObject.h" 6 | #include "GameObjectFactory.h" 7 | 8 | class MenuButton : public SDLGameObject 9 | { 10 | public: 11 | 12 | MenuButton(); 13 | 14 | virtual void draw(); 15 | virtual void update(); 16 | virtual void clean(); 17 | virtual void load(const LoaderParams* pParams); 18 | void setCallback(void(*callback)()) { m_callback = callback; } 19 | int getCallbackID() { return m_callbackID; } 20 | 21 | private: 22 | enum button_state 23 | { 24 | MOUSE_OUT = 0, 25 | MOUSE_OVER = 1, 26 | CLICKED = 2 27 | }; 28 | 29 | void(*m_callback)(); 30 | bool m_bReleased; 31 | int m_callbackID; 32 | }; 33 | 34 | class MenuButtonCreator : public BaseCreator 35 | { 36 | GameObject* createGameObject() const 37 | { 38 | return new MenuButton(); 39 | } 40 | }; 41 | 42 | #endif // __MenuButton__ 43 | -------------------------------------------------------------------------------- /06/001/MenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuState__ 2 | #define __MenuState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "GameObject.h" 8 | 9 | class MenuState : public GameState 10 | { 11 | protected: 12 | 13 | typedef void(*Callback)(); 14 | virtual void setCallbacks(const std::vector& callbacks) = 0; 15 | 16 | std::vector m_callbacks; 17 | }; 18 | 19 | #endif // __MenuState__ 20 | -------------------------------------------------------------------------------- /06/001/PauseState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PauseState__ 2 | #define __PauseState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "MenuState.h" 8 | 9 | class GameObject; 10 | 11 | class PauseState : public MenuState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_pauseID; } 21 | 22 | private: 23 | 24 | virtual void setCallbacks(const std::vector& callbacks); 25 | 26 | static void s_pauseToMain(); 27 | static void s_resumePlay(); 28 | 29 | static const std::string s_pauseID; 30 | 31 | std::vector m_gameObjects; 32 | }; 33 | 34 | #endif // __PauseState__ 35 | -------------------------------------------------------------------------------- /06/001/PlayState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PlayState__ 2 | #define __PlayState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameObject; 9 | class SDLGameObject; 10 | 11 | class PlayState : public GameState 12 | { 13 | public: 14 | 15 | virtual void update(); 16 | virtual void render(); 17 | 18 | virtual bool onEnter(); 19 | virtual bool onExit(); 20 | 21 | virtual std::string getStateID() const { return s_playID; } 22 | 23 | private: 24 | 25 | static const std::string s_playID; 26 | std::vector m_gameObjects; 27 | 28 | bool checkCollision(SDLGameObject* p1, SDLGameObject* p2); 29 | }; 30 | 31 | #endif // __PlayState__ 32 | -------------------------------------------------------------------------------- /06/001/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player() : SDLGameObject() {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | handleInput(); 17 | 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | SDLGameObject::update(); 21 | } 22 | 23 | void Player::clean() {} 24 | 25 | void Player::handleInput() 26 | { 27 | Vector2D* target = TheInputHandler::Instance()->getMousePosition(); 28 | m_velocity = *target - m_position; 29 | m_velocity /= 50; 30 | } 31 | 32 | void Player::load(const LoaderParams *pParams) 33 | { 34 | SDLGameObject::load(pParams); 35 | } 36 | -------------------------------------------------------------------------------- /06/001/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | #include "GameObjectFactory.h" 8 | 9 | class Player : public SDLGameObject 10 | { 11 | public: 12 | 13 | Player(); 14 | 15 | void draw(); 16 | void update(); 17 | void clean(); 18 | void load(const LoaderParams* pParams); 19 | 20 | private: 21 | void handleInput(); 22 | 23 | }; 24 | 25 | class PlayerCreator : public BaseCreator 26 | { 27 | public: 28 | GameObject* createGameObject() const 29 | { 30 | return new Player(); 31 | } 32 | }; 33 | 34 | #endif // __Player__ 35 | -------------------------------------------------------------------------------- /06/001/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | #include "LoaderParams.h" 5 | #include "Vector2D.h" 6 | 7 | SDLGameObject::SDLGameObject() : GameObject(), m_position(0,0), m_velocity(0,0), m_acceleration(0,0) 8 | {} 9 | 10 | void SDLGameObject::load(const LoaderParams *pParams) 11 | { 12 | m_position = Vector2D(pParams->getX(), pParams->getY()); 13 | m_velocity = Vector2D(0, 0); 14 | m_acceleration = Vector2D(0, 0); 15 | m_width = pParams->getWidth(); 16 | m_height = pParams->getHeight(); 17 | m_textureID = pParams->getTextureID(); 18 | m_currentRow = 1; 19 | m_currentFrame = 1; 20 | m_numFrames = pParams->getNumFrames(); 21 | } 22 | 23 | void SDLGameObject::draw() 24 | { 25 | if(m_velocity.getX() > 0) 26 | { 27 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer(), SDL_FLIP_HORIZONTAL); 28 | } 29 | else 30 | { 31 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 32 | } 33 | } 34 | 35 | void SDLGameObject::update() 36 | { 37 | m_velocity += m_acceleration; 38 | m_position += m_velocity; 39 | } 40 | -------------------------------------------------------------------------------- /06/001/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | virtual void load(const LoaderParams *pParams); 20 | 21 | Vector2D& getPosition() { return m_position; } 22 | int getWidth() { return m_width; } 23 | int getHeight() { return m_height; } 24 | 25 | protected: 26 | 27 | Vector2D m_position; 28 | Vector2D m_velocity; 29 | Vector2D m_acceleration; 30 | 31 | int m_width; 32 | int m_height; 33 | 34 | int m_currentRow; 35 | int m_currentFrame; 36 | int m_numFrames; 37 | 38 | std::string m_textureID; 39 | }; 40 | 41 | #endif // __SDLGameObject__ 42 | -------------------------------------------------------------------------------- /06/001/StateParser.h: -------------------------------------------------------------------------------- 1 | #ifndef __StateParser__ 2 | #define __StateParser__ 3 | 4 | #include 5 | #include 6 | #include "tinyxml.h" 7 | 8 | class GameObject; 9 | 10 | class StateParser 11 | { 12 | public: 13 | bool parseState(const char* stateFile, std::string stateID, std::vector *pObjects, std::vector *pTextureIDs); 14 | 15 | private: 16 | void parseObjects(TiXmlElement* pStateRoot, std::vector *pObjects); 17 | void parseTextures(TiXmlElement* psStateRoot, std::vector *pTextureIDs); 18 | }; 19 | 20 | #endif /* defined(__StateParser__) */ 21 | 22 | -------------------------------------------------------------------------------- /06/001/TextureManager.cpp: -------------------------------------------------------------------------------- 1 | #include "TextureManager.h" 2 | #include "SDL_image.h" 3 | 4 | TextureManager* TextureManager::s_pInstance = 0; 5 | 6 | bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) 7 | { 8 | SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); 9 | 10 | if(pTempSurface == 0) 11 | { 12 | return false; 13 | } 14 | 15 | SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); 16 | 17 | SDL_FreeSurface(pTempSurface); 18 | 19 | if(pTexture != 0) 20 | { 21 | m_textureMap[id] = pTexture; 22 | return true; 23 | } 24 | 25 | return false; 26 | } 27 | 28 | void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) 29 | { 30 | SDL_Rect srcRect; 31 | SDL_Rect destRect; 32 | 33 | srcRect.x = 0; 34 | srcRect.y = 0; 35 | srcRect.w = destRect.w = width; 36 | srcRect.h = destRect.h = height; 37 | destRect.x = x; 38 | destRect.y = y; 39 | 40 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 41 | } 42 | 43 | void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) 44 | { 45 | SDL_Rect srcRect; 46 | SDL_Rect destRect; 47 | srcRect.x = width * currentFrame; 48 | srcRect.y = height * (currentRow - 1); 49 | srcRect.w = destRect.w = width; 50 | srcRect.h = destRect.h = height; 51 | destRect.x = x; 52 | destRect.y = y; 53 | 54 | SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); 55 | } 56 | 57 | void TextureManager::clearFromTextureMap(std::string id) 58 | { 59 | m_textureMap.erase(id); 60 | } 61 | -------------------------------------------------------------------------------- /06/001/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | void clearFromTextureMap(std::string id); 16 | 17 | static TextureManager* Instance() 18 | { 19 | if(s_pInstance == 0) 20 | { 21 | s_pInstance = new TextureManager(); 22 | return s_pInstance; 23 | } 24 | return s_pInstance; 25 | } 26 | 27 | private: 28 | 29 | TextureManager() {} 30 | 31 | std::map m_textureMap; 32 | static TextureManager* s_pInstance; 33 | 34 | }; 35 | 36 | typedef TextureManager TheTextureManager; 37 | 38 | #endif // __TextureManager__ 39 | -------------------------------------------------------------------------------- /06/001/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /06/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -O0 -g -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp PlayState.cpp GameStateMachine.cpp MenuButton.cpp PauseState.cpp AnimatedGraphic.cpp GameOverState.cpp GameObjectFactory.cpp StateParser.cpp MainMenuState.cpp tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /06/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt..." << std::endl; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << std::endl; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing..." << std::endl; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /07/001/AnimatedGraphic.cpp: -------------------------------------------------------------------------------- 1 | #include "SDL.h" 2 | #include "AnimatedGraphic.h" 3 | 4 | AnimatedGraphic::AnimatedGraphic() : SDLGameObject() 5 | { 6 | } 7 | 8 | void AnimatedGraphic::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void AnimatedGraphic::update() 14 | { 15 | m_currentFrame = int(((SDL_GetTicks() / (1000 / m_animSpeed)) % 2)); 16 | } 17 | 18 | void AnimatedGraphic::clean() 19 | { 20 | } 21 | 22 | void AnimatedGraphic::load(const LoaderParams* pParams) 23 | { 24 | SDLGameObject::load(pParams); 25 | m_animSpeed = pParams->getAnimSpeed(); 26 | } 27 | -------------------------------------------------------------------------------- /07/001/AnimatedGraphic.h: -------------------------------------------------------------------------------- 1 | #ifndef __AnimatedGraphic__ 2 | #define __AnimatedGraphic__ 3 | 4 | #include "SDLGameObject.h" 5 | #include "LoaderParams.h" 6 | #include "GameObjectFactory.h" 7 | 8 | class AnimatedGraphic : public SDLGameObject 9 | { 10 | public: 11 | 12 | AnimatedGraphic(); 13 | 14 | virtual void draw(); 15 | virtual void update(); 16 | virtual void clean(); 17 | virtual void load(const LoaderParams* pParams); 18 | 19 | private: 20 | int m_animSpeed; 21 | }; 22 | 23 | class AnimatedGraphicCreator : public BaseCreator 24 | { 25 | public: 26 | GameObject* createGameObject() const 27 | { 28 | return new AnimatedGraphic(); 29 | } 30 | }; 31 | 32 | #endif /* defined(__AnimatedGraphic__) */ 33 | -------------------------------------------------------------------------------- /07/001/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy() : SDLGameObject() 7 | { 8 | 9 | } 10 | 11 | void Enemy::draw() 12 | { 13 | SDLGameObject::draw(); 14 | } 15 | 16 | void Enemy::update() 17 | { 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | if(m_position.getY() < 0) 21 | { 22 | m_velocity.setY(2); 23 | } 24 | else if(m_position.getY() > 400) 25 | { 26 | m_velocity.setY(-2); 27 | } 28 | 29 | SDLGameObject::update(); 30 | } 31 | 32 | void Enemy::clean() {} 33 | 34 | void Enemy::load(const LoaderParams *pParams) 35 | { 36 | SDLGameObject::load(pParams); 37 | m_velocity.setY(2); 38 | } 39 | -------------------------------------------------------------------------------- /07/001/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | #include "GameObjectFactory.h" 8 | 9 | class Enemy : public SDLGameObject 10 | { 11 | public: 12 | 13 | Enemy(); 14 | 15 | void draw(); 16 | void update(); 17 | void clean(); 18 | virtual void load(const LoaderParams* pParams); 19 | }; 20 | 21 | class EnemyCreator : public BaseCreator 22 | { 23 | GameObject* createGameObject() const 24 | { 25 | return new Enemy(); 26 | } 27 | }; 28 | 29 | #endif // __Enemy__ 30 | -------------------------------------------------------------------------------- /07/001/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | #include "Enemy.h" 9 | #include "GameStateMachine.h" 10 | #include "MenuState.h" 11 | #include "PlayState.h" 12 | 13 | class Game 14 | { 15 | public: 16 | 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | void quit() { m_bRunning = false; } 25 | GameStateMachine* getStateMachine() { return m_pGameStateMachine; } 26 | 27 | bool running() { return m_bRunning; } 28 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 29 | 30 | static Game* Instance() 31 | { 32 | if(s_pInstance == 0) 33 | { 34 | s_pInstance = new Game(); 35 | return s_pInstance; 36 | } 37 | } 38 | 39 | int getGameWidth() const 40 | { 41 | return m_gameWidth; 42 | } 43 | 44 | int getGameHeight() const 45 | { 46 | return m_gameHeight; 47 | } 48 | 49 | private: 50 | 51 | Game() {}; 52 | 53 | SDL_Window* m_pWindow; 54 | SDL_Renderer* m_pRenderer; 55 | SDL_Texture* m_pTexture; 56 | SDL_Rect m_sourceRectangle; 57 | SDL_Rect m_destinationRectangle; 58 | 59 | bool m_bRunning; 60 | int m_currentFrame; 61 | int m_gameWidth; 62 | int m_gameHeight; 63 | 64 | GameStateMachine* m_pGameStateMachine; 65 | 66 | static Game* s_pInstance; 67 | }; 68 | 69 | typedef Game TheGame; 70 | #endif // __Game__ 71 | -------------------------------------------------------------------------------- /07/001/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | virtual void load(const LoaderParams* pParams) = 0; 16 | 17 | protected: 18 | 19 | GameObject() {} 20 | virtual ~GameObject() {} 21 | }; 22 | 23 | #endif // __GameObject__ 24 | -------------------------------------------------------------------------------- /07/001/GameObjectFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "GameObjectFactory.h" 2 | 3 | GameObjectFactory* GameObjectFactory::pInstance = 0; 4 | -------------------------------------------------------------------------------- /07/001/GameObjectFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObjectFactory__ 2 | #define __GameObjectFactory__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "GameObject.h" 9 | 10 | class BaseCreator 11 | { 12 | public: 13 | virtual GameObject* createGameObject() const = 0; 14 | virtual ~BaseCreator() {} 15 | }; 16 | 17 | class GameObjectFactory 18 | { 19 | public: 20 | 21 | static GameObjectFactory* Instance() 22 | { 23 | if(pInstance == 0) 24 | { 25 | pInstance = new GameObjectFactory(); 26 | } 27 | 28 | return pInstance; 29 | } 30 | 31 | bool registerType(std::string typeID, BaseCreator* pCreator) 32 | { 33 | std::map::iterator it = m_creators.find(typeID); 34 | 35 | // if the type is alreade registered, do nothing 36 | if(it != m_creators.end()) 37 | { 38 | delete pCreator; 39 | return false; 40 | } 41 | 42 | m_creators[typeID] = pCreator; 43 | 44 | return true; 45 | } 46 | 47 | GameObject* create(std::string typeID) 48 | { 49 | std::map::iterator it = m_creators.find(typeID); 50 | 51 | if(it == m_creators.end()) 52 | { 53 | std::cout << "could not find type: " << typeID << std::endl; 54 | return NULL; 55 | } 56 | 57 | BaseCreator* pCreator = (*it).second; 58 | return pCreator->createGameObject(); 59 | } 60 | 61 | private: 62 | 63 | static GameObjectFactory* pInstance; 64 | std::map m_creators; 65 | 66 | }; 67 | 68 | typedef GameObjectFactory TheGameObjectFactory; 69 | 70 | #endif // __GameObjectFactory__ 71 | -------------------------------------------------------------------------------- /07/001/GameOverState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameOverState__ 2 | #define __GameOverState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "MenuState.h" 8 | 9 | class GameObject; 10 | 11 | class GameOverState : public MenuState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_gameOverID; } 21 | 22 | private: 23 | 24 | virtual void setCallbacks(const std::vector& callbacks); 25 | 26 | static void s_gameOverToMain(); 27 | static void s_restartPlay(); 28 | 29 | static const std::string s_gameOverID; 30 | 31 | std::vector m_gameObjects; 32 | }; 33 | 34 | #endif // __GameOverState__ 35 | -------------------------------------------------------------------------------- /07/001/GameState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameState__ 2 | #define __GameState__ 3 | 4 | #include 5 | 6 | class GameState 7 | { 8 | public: 9 | 10 | virtual void update() = 0; 11 | virtual void render() = 0; 12 | 13 | virtual bool onEnter() = 0; 14 | virtual bool onExit() = 0; 15 | 16 | virtual std::string getStateID() const = 0; 17 | 18 | protected: 19 | std::vector m_textureIDList; 20 | }; 21 | 22 | #endif // __GameState__ 23 | 24 | -------------------------------------------------------------------------------- /07/001/GameStateMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameStateMachine__ 2 | #define __GameStateMachine__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameStateMachine 9 | { 10 | public: 11 | 12 | GameStateMachine() : newState(nullptr), popStateFlag(false), changeStateFlag(false) {} 13 | void pushState(GameState* pState); 14 | void changeState(GameState* pState); 15 | void popState(); 16 | 17 | void update(); 18 | void render(); 19 | 20 | private: 21 | std::vector m_gameStates; 22 | void popStatePrivate(); 23 | void changeStatePrivate(GameState* pState); 24 | GameState* newState; 25 | bool popStateFlag; 26 | bool changeStateFlag; 27 | }; 28 | 29 | #endif // __GameStateMachine__ 30 | -------------------------------------------------------------------------------- /07/001/Layer.h: -------------------------------------------------------------------------------- 1 | #ifndef __Layer__ 2 | #define __Layer__ 3 | 4 | class Layer 5 | { 6 | public: 7 | 8 | virtual void render() = 0; 9 | virtual void update() = 0; 10 | 11 | protected: 12 | 13 | virtual ~Layer() {} 14 | 15 | }; 16 | 17 | #endif // __Layer__ 18 | -------------------------------------------------------------------------------- /07/001/Level.cpp: -------------------------------------------------------------------------------- 1 | #include "Level.h" 2 | 3 | void Level::render() 4 | { 5 | for(int i = 0; i < m_layers.size(); i++) 6 | { 7 | m_layers[i]->render(); 8 | } 9 | } 10 | 11 | void Level::update() 12 | { 13 | for(int i = 0; i < m_layers.size(); i++) 14 | { 15 | m_layers[i]->update(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /07/001/Level.h: -------------------------------------------------------------------------------- 1 | #ifndef __Level__ 2 | #define __Level__ 3 | 4 | #include 5 | #include 6 | 7 | #include "Layer.h" 8 | 9 | struct Tileset 10 | { 11 | int firstGridID; 12 | int tileWidth; 13 | int tileHeight; 14 | int spacing; 15 | int margin; 16 | int width; 17 | int height; 18 | int numColumns; 19 | std::string name; 20 | }; 21 | 22 | class Level 23 | { 24 | public: 25 | 26 | void update(); 27 | void render(); 28 | 29 | std::vector* getTilesets() 30 | { 31 | return &m_tilesets; 32 | } 33 | 34 | std::vector* getLayers() 35 | { 36 | return &m_layers; 37 | } 38 | 39 | private: 40 | 41 | friend class LevelParser; 42 | Level() {} 43 | 44 | std::vector m_tilesets; 45 | std::vector m_layers; 46 | }; 47 | 48 | #endif // __Level__ 49 | -------------------------------------------------------------------------------- /07/001/LevelParser.h: -------------------------------------------------------------------------------- 1 | #ifndef __LevelParser__ 2 | #define __LevelParser__ 3 | 4 | #include 5 | 6 | #include "tinyxml.h" 7 | #include "Level.h" 8 | 9 | class Level; 10 | class Layer; 11 | 12 | class LevelParser 13 | { 14 | public: 15 | 16 | Level* parseLevel(const char* levelFile); 17 | 18 | private: 19 | 20 | void parseTilesets(TiXmlElement* pTilesetRoot, std::vector* pTilesets); 21 | 22 | void parseTileLayer(TiXmlElement* pTileElement, std::vector* pLayers, const std::vector* pTilesets); 23 | 24 | int m_tileSize; 25 | int m_width; 26 | int m_height; 27 | }; 28 | 29 | 30 | #endif // __LevelParser__ 31 | -------------------------------------------------------------------------------- /07/001/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID, int numFrames, int callbackID = 0, int animSpeed = 0) : 11 | m_x(x), 12 | m_y(y), 13 | m_width(width), 14 | m_height(height), 15 | m_textureID(textureID), 16 | m_numFrames(numFrames), 17 | m_callbackID(callbackID), 18 | m_animSpeed(animSpeed) 19 | { 20 | 21 | } 22 | 23 | int getX() const { return m_x; } 24 | int getY() const { return m_y; } 25 | int getWidth() const { return m_width; } 26 | int getHeight() const { return m_height; } 27 | int getAnimSpeed() const { return m_animSpeed; } 28 | int getCallbackID() const { return m_callbackID; } 29 | int getNumFrames() const { return m_numFrames; } 30 | std::string getTextureID() const { return m_textureID; } 31 | 32 | private: 33 | 34 | int m_x; 35 | int m_y; 36 | 37 | int m_width; 38 | int m_height; 39 | 40 | std::string m_textureID; 41 | 42 | int m_numFrames; 43 | int m_animSpeed; 44 | int m_callbackID; 45 | }; 46 | 47 | #endif // __LoaderParams__ 48 | -------------------------------------------------------------------------------- /07/001/MainMenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MainMenuState__ 2 | #define __MainMenuState__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "MenuState.h" 8 | 9 | class MainMenuState : public MenuState 10 | { 11 | public: 12 | 13 | virtual void update(); 14 | virtual void render(); 15 | 16 | virtual bool onEnter(); 17 | virtual bool onExit(); 18 | 19 | virtual std::string getStateID() const { return s_menuID; } 20 | 21 | private: 22 | 23 | virtual void setCallbacks(const std::vector& callbacks); 24 | 25 | // callback functions for menu items 26 | static void s_menuToPlay(); 27 | static void s_exitFromMenu(); 28 | 29 | static const std::string s_menuID; 30 | std::vector m_gameObjects; 31 | }; 32 | 33 | #endif // __MainMenuState__ 34 | -------------------------------------------------------------------------------- /07/001/MenuButton.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuButton.h" 2 | #include "Vector2D.h" 3 | #include "InputHandler.h" 4 | 5 | MenuButton::MenuButton() : SDLGameObject() 6 | { 7 | m_currentFrame = MOUSE_OUT; // start at frame 0 8 | } 9 | 10 | void MenuButton::draw() 11 | { 12 | SDLGameObject::draw(); // use the base class drawing 13 | } 14 | 15 | void MenuButton::update() 16 | { 17 | Vector2D* pMousePos = TheInputHandler::Instance()->getMousePosition(); 18 | 19 | if(pMousePos->getX() < (m_position.getX() + m_width) 20 | && pMousePos->getX() > m_position.getX() 21 | && pMousePos->getY() < (m_position.getY() + m_height) 22 | && pMousePos->getY() > m_position.getY()) 23 | { 24 | if(TheInputHandler::Instance()->getMouseButtonState(LEFT) && m_bReleased) 25 | { 26 | m_currentFrame = CLICKED; 27 | m_callback(); // call our callback function 28 | m_bReleased = false; 29 | } 30 | else if(!TheInputHandler::Instance()->getMouseButtonState(LEFT)) 31 | { 32 | m_bReleased = true; 33 | m_currentFrame = MOUSE_OVER; 34 | } 35 | } 36 | else 37 | { 38 | m_currentFrame = MOUSE_OUT; 39 | } 40 | } 41 | 42 | void MenuButton::clean() 43 | { 44 | SDLGameObject::clean(); 45 | } 46 | 47 | void MenuButton::load(const LoaderParams *pParams) 48 | { 49 | SDLGameObject::load(pParams); 50 | m_callbackID = pParams->getCallbackID(); 51 | m_currentFrame = MOUSE_OUT; 52 | } 53 | -------------------------------------------------------------------------------- /07/001/MenuButton.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuButton__ 2 | #define __MenuButton__ 3 | 4 | #include "LoaderParams.h" 5 | #include "SDLGameObject.h" 6 | #include "GameObjectFactory.h" 7 | 8 | class MenuButton : public SDLGameObject 9 | { 10 | public: 11 | 12 | MenuButton(); 13 | 14 | virtual void draw(); 15 | virtual void update(); 16 | virtual void clean(); 17 | virtual void load(const LoaderParams* pParams); 18 | void setCallback(void(*callback)()) { m_callback = callback; } 19 | int getCallbackID() { return m_callbackID; } 20 | 21 | private: 22 | enum button_state 23 | { 24 | MOUSE_OUT = 0, 25 | MOUSE_OVER = 1, 26 | CLICKED = 2 27 | }; 28 | 29 | void(*m_callback)(); 30 | bool m_bReleased; 31 | int m_callbackID; 32 | }; 33 | 34 | class MenuButtonCreator : public BaseCreator 35 | { 36 | GameObject* createGameObject() const 37 | { 38 | return new MenuButton(); 39 | } 40 | }; 41 | 42 | #endif // __MenuButton__ 43 | -------------------------------------------------------------------------------- /07/001/MenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuState__ 2 | #define __MenuState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "GameObject.h" 8 | 9 | class MenuState : public GameState 10 | { 11 | protected: 12 | 13 | typedef void(*Callback)(); 14 | virtual void setCallbacks(const std::vector& callbacks) = 0; 15 | 16 | std::vector m_callbacks; 17 | }; 18 | 19 | #endif // __MenuState__ 20 | -------------------------------------------------------------------------------- /07/001/PauseState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PauseState__ 2 | #define __PauseState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "MenuState.h" 8 | 9 | class GameObject; 10 | 11 | class PauseState : public MenuState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_pauseID; } 21 | 22 | private: 23 | 24 | virtual void setCallbacks(const std::vector& callbacks); 25 | 26 | static void s_pauseToMain(); 27 | static void s_resumePlay(); 28 | 29 | static const std::string s_pauseID; 30 | 31 | std::vector m_gameObjects; 32 | }; 33 | 34 | #endif // __PauseState__ 35 | -------------------------------------------------------------------------------- /07/001/PlayState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PlayState__ 2 | #define __PlayState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "Level.h" 8 | 9 | class GameObject; 10 | class SDLGameObject; 11 | 12 | class PlayState : public GameState 13 | { 14 | public: 15 | 16 | virtual void update(); 17 | virtual void render(); 18 | 19 | virtual bool onEnter(); 20 | virtual bool onExit(); 21 | 22 | virtual std::string getStateID() const { return s_playID; } 23 | 24 | private: 25 | 26 | static const std::string s_playID; 27 | std::vector m_gameObjects; 28 | Level* pLevel; 29 | 30 | bool checkCollision(SDLGameObject* p1, SDLGameObject* p2); 31 | }; 32 | 33 | #endif // __PlayState__ 34 | -------------------------------------------------------------------------------- /07/001/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player() : SDLGameObject() {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | handleInput(); 17 | 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | SDLGameObject::update(); 21 | } 22 | 23 | void Player::clean() {} 24 | 25 | void Player::handleInput() 26 | { 27 | Vector2D* target = TheInputHandler::Instance()->getMousePosition(); 28 | m_velocity = *target - m_position; 29 | m_velocity /= 50; 30 | } 31 | 32 | void Player::load(const LoaderParams *pParams) 33 | { 34 | SDLGameObject::load(pParams); 35 | } 36 | -------------------------------------------------------------------------------- /07/001/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | #include "GameObjectFactory.h" 8 | 9 | class Player : public SDLGameObject 10 | { 11 | public: 12 | 13 | Player(); 14 | 15 | void draw(); 16 | void update(); 17 | void clean(); 18 | void load(const LoaderParams* pParams); 19 | 20 | private: 21 | void handleInput(); 22 | 23 | }; 24 | 25 | class PlayerCreator : public BaseCreator 26 | { 27 | public: 28 | GameObject* createGameObject() const 29 | { 30 | return new Player(); 31 | } 32 | }; 33 | 34 | #endif // __Player__ 35 | -------------------------------------------------------------------------------- /07/001/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | #include "LoaderParams.h" 5 | #include "Vector2D.h" 6 | 7 | SDLGameObject::SDLGameObject() : GameObject(), m_position(0,0), m_velocity(0,0), m_acceleration(0,0) 8 | {} 9 | 10 | void SDLGameObject::load(const LoaderParams *pParams) 11 | { 12 | m_position = Vector2D(pParams->getX(), pParams->getY()); 13 | m_velocity = Vector2D(0, 0); 14 | m_acceleration = Vector2D(0, 0); 15 | m_width = pParams->getWidth(); 16 | m_height = pParams->getHeight(); 17 | m_textureID = pParams->getTextureID(); 18 | m_currentRow = 1; 19 | m_currentFrame = 1; 20 | m_numFrames = pParams->getNumFrames(); 21 | } 22 | 23 | void SDLGameObject::draw() 24 | { 25 | if(m_velocity.getX() > 0) 26 | { 27 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer(), SDL_FLIP_HORIZONTAL); 28 | } 29 | else 30 | { 31 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 32 | } 33 | } 34 | 35 | void SDLGameObject::update() 36 | { 37 | m_velocity += m_acceleration; 38 | m_position += m_velocity; 39 | } 40 | -------------------------------------------------------------------------------- /07/001/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | virtual void load(const LoaderParams *pParams); 20 | 21 | Vector2D& getPosition() { return m_position; } 22 | int getWidth() { return m_width; } 23 | int getHeight() { return m_height; } 24 | 25 | protected: 26 | 27 | Vector2D m_position; 28 | Vector2D m_velocity; 29 | Vector2D m_acceleration; 30 | 31 | int m_width; 32 | int m_height; 33 | 34 | int m_currentRow; 35 | int m_currentFrame; 36 | int m_numFrames; 37 | 38 | std::string m_textureID; 39 | }; 40 | 41 | #endif // __SDLGameObject__ 42 | -------------------------------------------------------------------------------- /07/001/StateParser.h: -------------------------------------------------------------------------------- 1 | #ifndef __StateParser__ 2 | #define __StateParser__ 3 | 4 | #include 5 | #include 6 | #include "tinyxml.h" 7 | 8 | class GameObject; 9 | 10 | class StateParser 11 | { 12 | public: 13 | bool parseState(const char* stateFile, std::string stateID, std::vector *pObjects, std::vector *pTextureIDs); 14 | 15 | private: 16 | void parseObjects(TiXmlElement* pStateRoot, std::vector *pObjects); 17 | void parseTextures(TiXmlElement* psStateRoot, std::vector *pTextureIDs); 18 | }; 19 | 20 | #endif /* defined(__StateParser__) */ 21 | 22 | -------------------------------------------------------------------------------- /07/001/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | void clearFromTextureMap(std::string id); 16 | 17 | static TextureManager* Instance() 18 | { 19 | if(s_pInstance == 0) 20 | { 21 | s_pInstance = new TextureManager(); 22 | return s_pInstance; 23 | } 24 | return s_pInstance; 25 | } 26 | 27 | void drawTile(std::string id, int margin, int spacing, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer); 28 | 29 | private: 30 | 31 | TextureManager() {} 32 | 33 | std::map m_textureMap; 34 | static TextureManager* s_pInstance; 35 | 36 | }; 37 | 38 | typedef TextureManager TheTextureManager; 39 | 40 | #endif // __TextureManager__ 41 | -------------------------------------------------------------------------------- /07/001/TileLayer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "TileLayer.h" 5 | #include "Game.h" 6 | #include "TextureManager.h" 7 | 8 | TileLayer::TileLayer(int tileSize, const std::vector& tilesets) : m_tileSize(tileSize), m_tilesets(tilesets), m_position(0, 0), m_velocity(0, 0) 9 | { 10 | m_numColumns = (TheGame::Instance()->getGameWidth() / m_tileSize); 11 | m_numRows = (TheGame::Instance()->getGameHeight() / m_tileSize); 12 | } 13 | 14 | void TileLayer::update() 15 | { 16 | m_position += m_velocity; 17 | } 18 | 19 | void TileLayer::render() 20 | { 21 | int x, y, x2, y2 = 0; 22 | 23 | x = m_position.getX() / m_tileSize; 24 | y = m_position.getY() / m_tileSize; 25 | 26 | x2 = int(m_position.getX()) % m_tileSize; 27 | y2 = int(m_position.getY()) % m_tileSize; 28 | 29 | for(int i = 0; i < m_numRows; i++) 30 | { 31 | for(int j = 0; j < m_numColumns; j++) 32 | { 33 | int id = m_tileIDs[i][j + x]; 34 | 35 | if(id == 0) 36 | { 37 | continue; 38 | } 39 | 40 | Tileset tileset = getTilesetByID(id); 41 | 42 | id--; 43 | 44 | TheTextureManager::Instance()->drawTile( 45 | tileset.name, 2, 2, 46 | (j * m_tileSize) - x2, (i * m_tileSize) - y2, 47 | m_tileSize, m_tileSize, 48 | (id - (tileset.firstGridID - 1)) / tileset.numColumns, 49 | (id - (tileset.firstGridID - 1 )) % tileset.numColumns, 50 | TheGame::Instance()->getRenderer()); 51 | } 52 | } 53 | } 54 | 55 | Tileset TileLayer::getTilesetByID(int tileID) 56 | { 57 | for(int i = 0; i < m_tilesets.size(); i++) 58 | { 59 | if(i + 1 <= m_tilesets.size() - 1) 60 | { 61 | if(tileID >= m_tilesets[i].firstGridID && tileID < m_tilesets[i + 1].firstGridID) 62 | { 63 | return m_tilesets[i]; 64 | } 65 | } 66 | else 67 | { 68 | return m_tilesets[i]; 69 | } 70 | } 71 | 72 | std::cout << "did not find tileset, returning empy tileset" << std::endl; 73 | Tileset t; 74 | return t; 75 | } 76 | -------------------------------------------------------------------------------- /07/001/TileLayer.h: -------------------------------------------------------------------------------- 1 | #ifndef __TileLayer__ 2 | #define __TileLayer__ 3 | 4 | #include 5 | 6 | #include "Layer.h" 7 | #include "Level.h" 8 | #include "Vector2D.h" 9 | 10 | class TileLayer : public Layer 11 | { 12 | public: 13 | 14 | TileLayer(int tileSize, const std::vector &tilesets); 15 | 16 | virtual void update(); 17 | virtual void render(); 18 | 19 | void setTileIDs(const std::vector>& data) 20 | { 21 | m_tileIDs = data; 22 | } 23 | 24 | void setTileSize(int tileSize) 25 | { 26 | m_tileSize = tileSize; 27 | } 28 | 29 | Tileset getTilesetByID(int tileID); 30 | 31 | private: 32 | 33 | int m_numColumns; 34 | int m_numRows; 35 | int m_tileSize; 36 | 37 | Vector2D m_position; 38 | Vector2D m_velocity; 39 | 40 | const std::vector &m_tilesets; 41 | 42 | std::vector> m_tileIDs; 43 | }; 44 | 45 | #endif // __TileLayer__ 46 | -------------------------------------------------------------------------------- /07/001/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /07/001/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -O0 -g -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp PlayState.cpp GameStateMachine.cpp MenuButton.cpp PauseState.cpp AnimatedGraphic.cpp GameOverState.cpp GameObjectFactory.cpp StateParser.cpp MainMenuState.cpp tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp Level.cpp TileLayer.cpp LevelParser.cpp base64.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /07/001/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt..." << std::endl; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << std::endl; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing..." << std::endl; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /07/002/AnimatedGraphic.cpp: -------------------------------------------------------------------------------- 1 | #include "SDL.h" 2 | #include "AnimatedGraphic.h" 3 | 4 | AnimatedGraphic::AnimatedGraphic() : SDLGameObject() 5 | { 6 | } 7 | 8 | void AnimatedGraphic::draw() 9 | { 10 | SDLGameObject::draw(); 11 | } 12 | 13 | void AnimatedGraphic::update() 14 | { 15 | m_currentFrame = int(((SDL_GetTicks() / (1000 / m_animSpeed)) % 2)); 16 | } 17 | 18 | void AnimatedGraphic::clean() 19 | { 20 | } 21 | 22 | void AnimatedGraphic::load(const LoaderParams* pParams) 23 | { 24 | SDLGameObject::load(pParams); 25 | m_animSpeed = pParams->getAnimSpeed(); 26 | } 27 | -------------------------------------------------------------------------------- /07/002/AnimatedGraphic.h: -------------------------------------------------------------------------------- 1 | #ifndef __AnimatedGraphic__ 2 | #define __AnimatedGraphic__ 3 | 4 | #include "SDLGameObject.h" 5 | #include "LoaderParams.h" 6 | #include "GameObjectFactory.h" 7 | 8 | class AnimatedGraphic : public SDLGameObject 9 | { 10 | public: 11 | 12 | AnimatedGraphic(); 13 | 14 | virtual void draw(); 15 | virtual void update(); 16 | virtual void clean(); 17 | virtual void load(const LoaderParams* pParams); 18 | 19 | private: 20 | int m_animSpeed; 21 | }; 22 | 23 | class AnimatedGraphicCreator : public BaseCreator 24 | { 25 | public: 26 | GameObject* createGameObject() const 27 | { 28 | return new AnimatedGraphic(); 29 | } 30 | }; 31 | 32 | #endif /* defined(__AnimatedGraphic__) */ 33 | -------------------------------------------------------------------------------- /07/002/Enemy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Enemy.h" 5 | 6 | Enemy::Enemy() : SDLGameObject() 7 | { 8 | 9 | } 10 | 11 | void Enemy::draw() 12 | { 13 | SDLGameObject::draw(); 14 | } 15 | 16 | void Enemy::update() 17 | { 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | if(m_position.getY() < 0) 21 | { 22 | m_velocity.setY(2); 23 | } 24 | else if(m_position.getY() > 400) 25 | { 26 | m_velocity.setY(-2); 27 | } 28 | 29 | SDLGameObject::update(); 30 | } 31 | 32 | void Enemy::clean() {} 33 | 34 | void Enemy::load(const LoaderParams *pParams) 35 | { 36 | SDLGameObject::load(pParams); 37 | m_velocity.setY(2); 38 | } 39 | -------------------------------------------------------------------------------- /07/002/Enemy.h: -------------------------------------------------------------------------------- 1 | #ifndef __Enemy__ 2 | #define __Enemy__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | #include "GameObjectFactory.h" 8 | 9 | class Enemy : public SDLGameObject 10 | { 11 | public: 12 | 13 | Enemy(); 14 | 15 | void draw(); 16 | void update(); 17 | void clean(); 18 | virtual void load(const LoaderParams* pParams); 19 | }; 20 | 21 | class EnemyCreator : public BaseCreator 22 | { 23 | GameObject* createGameObject() const 24 | { 25 | return new Enemy(); 26 | } 27 | }; 28 | 29 | #endif // __Enemy__ 30 | -------------------------------------------------------------------------------- /07/002/Game.h: -------------------------------------------------------------------------------- 1 | #ifndef __Game__ 2 | #define __Game__ 3 | 4 | #include "SDL.h" 5 | #include "TextureManager.h" 6 | #include "GameObject.h" 7 | #include "Player.h" 8 | #include "Enemy.h" 9 | #include "GameStateMachine.h" 10 | #include "MenuState.h" 11 | #include "PlayState.h" 12 | 13 | class Game 14 | { 15 | public: 16 | 17 | ~Game() {} 18 | 19 | bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); 20 | void render(); 21 | void update(); 22 | void handleEvents(); 23 | void clean(); 24 | void quit() { m_bRunning = false; } 25 | GameStateMachine* getStateMachine() { return m_pGameStateMachine; } 26 | 27 | bool running() { return m_bRunning; } 28 | SDL_Renderer* getRenderer() const { return m_pRenderer; } 29 | 30 | static Game* Instance() 31 | { 32 | if(s_pInstance == 0) 33 | { 34 | s_pInstance = new Game(); 35 | return s_pInstance; 36 | } 37 | } 38 | 39 | int getGameWidth() const 40 | { 41 | return m_gameWidth; 42 | } 43 | 44 | int getGameHeight() const 45 | { 46 | return m_gameHeight; 47 | } 48 | 49 | private: 50 | 51 | Game() {}; 52 | 53 | SDL_Window* m_pWindow; 54 | SDL_Renderer* m_pRenderer; 55 | SDL_Texture* m_pTexture; 56 | SDL_Rect m_sourceRectangle; 57 | SDL_Rect m_destinationRectangle; 58 | 59 | bool m_bRunning; 60 | int m_currentFrame; 61 | int m_gameWidth; 62 | int m_gameHeight; 63 | 64 | GameStateMachine* m_pGameStateMachine; 65 | 66 | static Game* s_pInstance; 67 | }; 68 | 69 | typedef Game TheGame; 70 | #endif // __Game__ 71 | -------------------------------------------------------------------------------- /07/002/GameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObject__ 2 | #define __GameObject__ 3 | 4 | #include 5 | 6 | #include "LoaderParams.h" 7 | 8 | class GameObject 9 | { 10 | public: 11 | 12 | virtual void draw() = 0; 13 | virtual void update() = 0; 14 | virtual void clean() = 0; 15 | virtual void load(const LoaderParams* pParams) = 0; 16 | 17 | protected: 18 | 19 | GameObject() {} 20 | virtual ~GameObject() {} 21 | }; 22 | 23 | #endif // __GameObject__ 24 | -------------------------------------------------------------------------------- /07/002/GameObjectFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "GameObjectFactory.h" 2 | 3 | GameObjectFactory* GameObjectFactory::pInstance = 0; 4 | -------------------------------------------------------------------------------- /07/002/GameObjectFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameObjectFactory__ 2 | #define __GameObjectFactory__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "GameObject.h" 9 | 10 | class BaseCreator 11 | { 12 | public: 13 | virtual GameObject* createGameObject() const = 0; 14 | virtual ~BaseCreator() {} 15 | }; 16 | 17 | class GameObjectFactory 18 | { 19 | public: 20 | 21 | static GameObjectFactory* Instance() 22 | { 23 | if(pInstance == 0) 24 | { 25 | pInstance = new GameObjectFactory(); 26 | } 27 | 28 | return pInstance; 29 | } 30 | 31 | bool registerType(std::string typeID, BaseCreator* pCreator) 32 | { 33 | std::map::iterator it = m_creators.find(typeID); 34 | 35 | // if the type is alreade registered, do nothing 36 | if(it != m_creators.end()) 37 | { 38 | delete pCreator; 39 | return false; 40 | } 41 | 42 | m_creators[typeID] = pCreator; 43 | 44 | return true; 45 | } 46 | 47 | GameObject* create(std::string typeID) 48 | { 49 | std::map::iterator it = m_creators.find(typeID); 50 | 51 | if(it == m_creators.end()) 52 | { 53 | std::cout << "could not find type: " << typeID << std::endl; 54 | return NULL; 55 | } 56 | 57 | BaseCreator* pCreator = (*it).second; 58 | return pCreator->createGameObject(); 59 | } 60 | 61 | private: 62 | 63 | static GameObjectFactory* pInstance; 64 | std::map m_creators; 65 | 66 | }; 67 | 68 | typedef GameObjectFactory TheGameObjectFactory; 69 | 70 | #endif // __GameObjectFactory__ 71 | -------------------------------------------------------------------------------- /07/002/GameOverState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameOverState__ 2 | #define __GameOverState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "MenuState.h" 8 | 9 | class GameObject; 10 | 11 | class GameOverState : public MenuState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_gameOverID; } 21 | 22 | private: 23 | 24 | virtual void setCallbacks(const std::vector& callbacks); 25 | 26 | static void s_gameOverToMain(); 27 | static void s_restartPlay(); 28 | 29 | static const std::string s_gameOverID; 30 | 31 | std::vector m_gameObjects; 32 | }; 33 | 34 | #endif // __GameOverState__ 35 | -------------------------------------------------------------------------------- /07/002/GameState.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameState__ 2 | #define __GameState__ 3 | 4 | #include 5 | 6 | class GameState 7 | { 8 | public: 9 | 10 | virtual void update() = 0; 11 | virtual void render() = 0; 12 | 13 | virtual bool onEnter() = 0; 14 | virtual bool onExit() = 0; 15 | 16 | virtual std::string getStateID() const = 0; 17 | 18 | protected: 19 | std::vector m_textureIDList; 20 | }; 21 | 22 | #endif // __GameState__ 23 | 24 | -------------------------------------------------------------------------------- /07/002/GameStateMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef __GameStateMachine__ 2 | #define __GameStateMachine__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | 8 | class GameStateMachine 9 | { 10 | public: 11 | 12 | GameStateMachine() : newState(nullptr), popStateFlag(false), changeStateFlag(false) {} 13 | void pushState(GameState* pState); 14 | void changeState(GameState* pState); 15 | void popState(); 16 | 17 | void update(); 18 | void render(); 19 | 20 | private: 21 | std::vector m_gameStates; 22 | void popStatePrivate(); 23 | void changeStatePrivate(GameState* pState); 24 | GameState* newState; 25 | bool popStateFlag; 26 | bool changeStateFlag; 27 | }; 28 | 29 | #endif // __GameStateMachine__ 30 | -------------------------------------------------------------------------------- /07/002/Layer.h: -------------------------------------------------------------------------------- 1 | #ifndef __Layer__ 2 | #define __Layer__ 3 | 4 | class Layer 5 | { 6 | public: 7 | 8 | virtual void render() = 0; 9 | virtual void update() = 0; 10 | 11 | protected: 12 | 13 | virtual ~Layer() {} 14 | 15 | }; 16 | 17 | #endif // __Layer__ 18 | -------------------------------------------------------------------------------- /07/002/Level.cpp: -------------------------------------------------------------------------------- 1 | #include "Level.h" 2 | 3 | void Level::render() 4 | { 5 | for(int i = 0; i < m_layers.size(); i++) 6 | { 7 | m_layers[i]->render(); 8 | } 9 | } 10 | 11 | void Level::update() 12 | { 13 | for(int i = 0; i < m_layers.size(); i++) 14 | { 15 | m_layers[i]->update(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /07/002/Level.h: -------------------------------------------------------------------------------- 1 | #ifndef __Level__ 2 | #define __Level__ 3 | 4 | #include 5 | #include 6 | 7 | #include "Layer.h" 8 | 9 | struct Tileset 10 | { 11 | int firstGridID; 12 | int tileWidth; 13 | int tileHeight; 14 | int spacing; 15 | int margin; 16 | int width; 17 | int height; 18 | int numColumns; 19 | std::string name; 20 | }; 21 | 22 | class Level 23 | { 24 | public: 25 | 26 | void update(); 27 | void render(); 28 | 29 | std::vector* getTilesets() 30 | { 31 | return &m_tilesets; 32 | } 33 | 34 | std::vector* getLayers() 35 | { 36 | return &m_layers; 37 | } 38 | 39 | private: 40 | 41 | friend class LevelParser; 42 | Level() {} 43 | 44 | std::vector m_tilesets; 45 | std::vector m_layers; 46 | }; 47 | 48 | #endif // __Level__ 49 | -------------------------------------------------------------------------------- /07/002/LevelParser.h: -------------------------------------------------------------------------------- 1 | #ifndef __LevelParser__ 2 | #define __LevelParser__ 3 | 4 | #include 5 | 6 | #include "tinyxml.h" 7 | #include "Level.h" 8 | 9 | class Level; 10 | class Layer; 11 | 12 | class LevelParser 13 | { 14 | public: 15 | 16 | Level* parseLevel(const char* levelFile); 17 | 18 | private: 19 | 20 | void parseTilesets(TiXmlElement* pTilesetRoot, std::vector* pTilesets); 21 | void parseTileLayer(TiXmlElement* pTileElement, std::vector* pLayers, const std::vector* pTilesets); 22 | void parseTextures(TiXmlElement* pTextureRoot); 23 | void parseObjectLayer(TiXmlElement* pObjectElement, std::vector *pLayers); 24 | 25 | int m_tileSize; 26 | int m_width; 27 | int m_height; 28 | }; 29 | 30 | 31 | #endif // __LevelParser__ 32 | -------------------------------------------------------------------------------- /07/002/LoaderParams.h: -------------------------------------------------------------------------------- 1 | #ifndef __LoaderParams__ 2 | #define __LoaderParams__ 3 | 4 | #include 5 | 6 | class LoaderParams 7 | { 8 | public: 9 | 10 | LoaderParams(int x, int y, int width, int height, std::string textureID, int numFrames, int callbackID = 0, int animSpeed = 0) : 11 | m_x(x), 12 | m_y(y), 13 | m_width(width), 14 | m_height(height), 15 | m_textureID(textureID), 16 | m_numFrames(numFrames), 17 | m_callbackID(callbackID), 18 | m_animSpeed(animSpeed) 19 | { 20 | 21 | } 22 | 23 | int getX() const { return m_x; } 24 | int getY() const { return m_y; } 25 | int getWidth() const { return m_width; } 26 | int getHeight() const { return m_height; } 27 | int getAnimSpeed() const { return m_animSpeed; } 28 | int getCallbackID() const { return m_callbackID; } 29 | int getNumFrames() const { return m_numFrames; } 30 | std::string getTextureID() const { return m_textureID; } 31 | 32 | private: 33 | 34 | int m_x; 35 | int m_y; 36 | 37 | int m_width; 38 | int m_height; 39 | 40 | std::string m_textureID; 41 | 42 | int m_numFrames; 43 | int m_animSpeed; 44 | int m_callbackID; 45 | }; 46 | 47 | #endif // __LoaderParams__ 48 | -------------------------------------------------------------------------------- /07/002/MainMenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MainMenuState__ 2 | #define __MainMenuState__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "MenuState.h" 8 | 9 | class MainMenuState : public MenuState 10 | { 11 | public: 12 | 13 | virtual void update(); 14 | virtual void render(); 15 | 16 | virtual bool onEnter(); 17 | virtual bool onExit(); 18 | 19 | virtual std::string getStateID() const { return s_menuID; } 20 | 21 | private: 22 | 23 | virtual void setCallbacks(const std::vector& callbacks); 24 | 25 | // callback functions for menu items 26 | static void s_menuToPlay(); 27 | static void s_exitFromMenu(); 28 | 29 | static const std::string s_menuID; 30 | std::vector m_gameObjects; 31 | }; 32 | 33 | #endif // __MainMenuState__ 34 | -------------------------------------------------------------------------------- /07/002/MenuButton.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuButton.h" 2 | #include "Vector2D.h" 3 | #include "InputHandler.h" 4 | 5 | MenuButton::MenuButton() : SDLGameObject() 6 | { 7 | m_currentFrame = MOUSE_OUT; // start at frame 0 8 | } 9 | 10 | void MenuButton::draw() 11 | { 12 | SDLGameObject::draw(); // use the base class drawing 13 | } 14 | 15 | void MenuButton::update() 16 | { 17 | Vector2D* pMousePos = TheInputHandler::Instance()->getMousePosition(); 18 | 19 | if(pMousePos->getX() < (m_position.getX() + m_width) 20 | && pMousePos->getX() > m_position.getX() 21 | && pMousePos->getY() < (m_position.getY() + m_height) 22 | && pMousePos->getY() > m_position.getY()) 23 | { 24 | if(TheInputHandler::Instance()->getMouseButtonState(LEFT) && m_bReleased) 25 | { 26 | m_currentFrame = CLICKED; 27 | m_callback(); // call our callback function 28 | m_bReleased = false; 29 | } 30 | else if(!TheInputHandler::Instance()->getMouseButtonState(LEFT)) 31 | { 32 | m_bReleased = true; 33 | m_currentFrame = MOUSE_OVER; 34 | } 35 | } 36 | else 37 | { 38 | m_currentFrame = MOUSE_OUT; 39 | } 40 | } 41 | 42 | void MenuButton::clean() 43 | { 44 | SDLGameObject::clean(); 45 | } 46 | 47 | void MenuButton::load(const LoaderParams *pParams) 48 | { 49 | SDLGameObject::load(pParams); 50 | m_callbackID = pParams->getCallbackID(); 51 | m_currentFrame = MOUSE_OUT; 52 | } 53 | -------------------------------------------------------------------------------- /07/002/MenuButton.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuButton__ 2 | #define __MenuButton__ 3 | 4 | #include "LoaderParams.h" 5 | #include "SDLGameObject.h" 6 | #include "GameObjectFactory.h" 7 | 8 | class MenuButton : public SDLGameObject 9 | { 10 | public: 11 | 12 | MenuButton(); 13 | 14 | virtual void draw(); 15 | virtual void update(); 16 | virtual void clean(); 17 | virtual void load(const LoaderParams* pParams); 18 | void setCallback(void(*callback)()) { m_callback = callback; } 19 | int getCallbackID() { return m_callbackID; } 20 | 21 | private: 22 | enum button_state 23 | { 24 | MOUSE_OUT = 0, 25 | MOUSE_OVER = 1, 26 | CLICKED = 2 27 | }; 28 | 29 | void(*m_callback)(); 30 | bool m_bReleased; 31 | int m_callbackID; 32 | }; 33 | 34 | class MenuButtonCreator : public BaseCreator 35 | { 36 | GameObject* createGameObject() const 37 | { 38 | return new MenuButton(); 39 | } 40 | }; 41 | 42 | #endif // __MenuButton__ 43 | -------------------------------------------------------------------------------- /07/002/MenuState.h: -------------------------------------------------------------------------------- 1 | #ifndef __MenuState__ 2 | #define __MenuState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "GameObject.h" 8 | 9 | class MenuState : public GameState 10 | { 11 | protected: 12 | 13 | typedef void(*Callback)(); 14 | virtual void setCallbacks(const std::vector& callbacks) = 0; 15 | 16 | std::vector m_callbacks; 17 | }; 18 | 19 | #endif // __MenuState__ 20 | -------------------------------------------------------------------------------- /07/002/ObjectLayer.cpp: -------------------------------------------------------------------------------- 1 | #include "ObjectLayer.h" 2 | 3 | void ObjectLayer::update() 4 | { 5 | for(int i = 0; i < m_gameObjects.size(); i++) 6 | { 7 | m_gameObjects[i]->update(); 8 | } 9 | } 10 | 11 | void ObjectLayer::render() 12 | { 13 | for(int i = 0; i < m_gameObjects.size(); i++) 14 | { 15 | m_gameObjects[i]->draw(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /07/002/ObjectLayer.h: -------------------------------------------------------------------------------- 1 | #ifndef __ObjectLayer__ 2 | #define __ObjectLayer__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "Layer.h" 8 | 9 | class ObjectLayer : public Layer 10 | { 11 | public: 12 | virtual void update(); 13 | virtual void render(); 14 | 15 | std::vector* getGameObjects() 16 | { 17 | return &m_gameObjects; 18 | } 19 | 20 | private: 21 | std::vector m_gameObjects; 22 | }; 23 | 24 | #endif // __ObjectLayer__ 25 | -------------------------------------------------------------------------------- /07/002/PauseState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PauseState__ 2 | #define __PauseState__ 3 | 4 | #include 5 | #include 6 | 7 | #include "MenuState.h" 8 | 9 | class GameObject; 10 | 11 | class PauseState : public MenuState 12 | { 13 | public: 14 | virtual void update(); 15 | virtual void render(); 16 | 17 | virtual bool onEnter(); 18 | virtual bool onExit(); 19 | 20 | virtual std::string getStateID() const { return s_pauseID; } 21 | 22 | private: 23 | 24 | virtual void setCallbacks(const std::vector& callbacks); 25 | 26 | static void s_pauseToMain(); 27 | static void s_resumePlay(); 28 | 29 | static const std::string s_pauseID; 30 | 31 | std::vector m_gameObjects; 32 | }; 33 | 34 | #endif // __PauseState__ 35 | -------------------------------------------------------------------------------- /07/002/PlayState.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "PlayState.h" 5 | #include "TextureManager.h" 6 | #include "Game.h" 7 | #include "GameObject.h" 8 | #include "InputHandler.h" 9 | #include "PauseState.h" 10 | #include "GameOverState.h" 11 | #include "LoaderParams.h" 12 | #include "StateParser.h" 13 | #include "LevelParser.h" 14 | #include "Level.h" 15 | 16 | const std::string PlayState::s_playID = "PLAY"; 17 | 18 | void PlayState::update() 19 | { 20 | if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_ESCAPE)) 21 | { 22 | TheGame::Instance()->getStateMachine()->pushState(new PauseState()); 23 | } 24 | pLevel->update(); 25 | } 26 | 27 | void PlayState::render() 28 | { 29 | pLevel->render(); 30 | } 31 | 32 | bool PlayState::onEnter() 33 | { 34 | LevelParser levelParser; 35 | pLevel = levelParser.parseLevel("map1.tmx"); 36 | 37 | std::cout << "entering PlayState" << std::endl; 38 | return true; 39 | } 40 | 41 | bool PlayState::onExit() 42 | { 43 | std::cout << "exiting PlayState" << std::endl; 44 | return true; 45 | } 46 | 47 | bool PlayState::checkCollision(SDLGameObject* p1, SDLGameObject* p2) 48 | { 49 | int leftA, leftB; 50 | int rightA, rightB; 51 | int topA, topB; 52 | int bottomA, bottomB; 53 | 54 | leftA = p1->getPosition().getX(); 55 | rightA = p1->getPosition().getX() + p1->getWidth(); 56 | topA = p1->getPosition().getY(); 57 | bottomA = p1->getPosition().getY() + p1->getHeight(); 58 | 59 | // Calculate the sides of rect B 60 | leftB = p2->getPosition().getX(); 61 | rightB = p2->getPosition().getX() + p2->getWidth(); 62 | topB = p2->getPosition().getY(); 63 | bottomB = p2->getPosition().getY() + p2->getHeight(); 64 | 65 | // If any of the sides from A are outside of B 66 | if(bottomA <= topB) { return false; } 67 | if(topA >= bottomB) { return false; } 68 | if(rightA <= leftB) { return false; } 69 | if(leftA >= rightB) { return false; } 70 | 71 | return true; 72 | } 73 | -------------------------------------------------------------------------------- /07/002/PlayState.h: -------------------------------------------------------------------------------- 1 | #ifndef __PlayState__ 2 | #define __PlayState__ 3 | 4 | #include 5 | 6 | #include "GameState.h" 7 | #include "Level.h" 8 | 9 | class GameObject; 10 | class SDLGameObject; 11 | 12 | class PlayState : public GameState 13 | { 14 | public: 15 | 16 | virtual void update(); 17 | virtual void render(); 18 | 19 | virtual bool onEnter(); 20 | virtual bool onExit(); 21 | 22 | virtual std::string getStateID() const { return s_playID; } 23 | 24 | private: 25 | 26 | static const std::string s_playID; 27 | std::vector m_gameObjects; 28 | Level* pLevel; 29 | 30 | bool checkCollision(SDLGameObject* p1, SDLGameObject* p2); 31 | }; 32 | 33 | #endif // __PlayState__ 34 | -------------------------------------------------------------------------------- /07/002/Player.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SDL.h" 3 | 4 | #include "Player.h" 5 | #include "InputHandler.h" 6 | 7 | Player::Player() : SDLGameObject() {} 8 | 9 | void Player::draw() 10 | { 11 | SDLGameObject::draw(); 12 | } 13 | 14 | void Player::update() 15 | { 16 | handleInput(); 17 | 18 | m_currentFrame = int(((SDL_GetTicks() / 100) % 5)); 19 | 20 | SDLGameObject::update(); 21 | } 22 | 23 | void Player::clean() {} 24 | 25 | void Player::handleInput() 26 | { 27 | Vector2D* target = TheInputHandler::Instance()->getMousePosition(); 28 | m_velocity = *target - m_position; 29 | m_velocity /= 50; 30 | } 31 | 32 | void Player::load(const LoaderParams *pParams) 33 | { 34 | SDLGameObject::load(pParams); 35 | } 36 | -------------------------------------------------------------------------------- /07/002/Player.h: -------------------------------------------------------------------------------- 1 | #ifndef __Player__ 2 | #define __Player__ 3 | 4 | #include 5 | 6 | #include "SDLGameObject.h" 7 | #include "GameObjectFactory.h" 8 | 9 | class Player : public SDLGameObject 10 | { 11 | public: 12 | 13 | Player(); 14 | 15 | void draw(); 16 | void update(); 17 | void clean(); 18 | void load(const LoaderParams* pParams); 19 | 20 | private: 21 | void handleInput(); 22 | 23 | }; 24 | 25 | class PlayerCreator : public BaseCreator 26 | { 27 | public: 28 | GameObject* createGameObject() const 29 | { 30 | return new Player(); 31 | } 32 | }; 33 | 34 | #endif // __Player__ 35 | -------------------------------------------------------------------------------- /07/002/SDLGameObject.cpp: -------------------------------------------------------------------------------- 1 | #include "SDLGameObject.h" 2 | #include "TextureManager.h" 3 | #include "Game.h" 4 | #include "LoaderParams.h" 5 | #include "Vector2D.h" 6 | 7 | SDLGameObject::SDLGameObject() : GameObject(), m_position(0,0), m_velocity(0,0), m_acceleration(0,0) 8 | {} 9 | 10 | void SDLGameObject::load(const LoaderParams *pParams) 11 | { 12 | m_position = Vector2D(pParams->getX(), pParams->getY()); 13 | m_velocity = Vector2D(0, 0); 14 | m_acceleration = Vector2D(0, 0); 15 | m_width = pParams->getWidth(); 16 | m_height = pParams->getHeight(); 17 | m_textureID = pParams->getTextureID(); 18 | m_currentRow = 1; 19 | m_currentFrame = 1; 20 | m_numFrames = pParams->getNumFrames(); 21 | } 22 | 23 | void SDLGameObject::draw() 24 | { 25 | if(m_velocity.getX() > 0) 26 | { 27 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer(), SDL_FLIP_HORIZONTAL); 28 | } 29 | else 30 | { 31 | TextureManager::Instance()->drawFrame(m_textureID, (Uint32)m_position.getX(), (Uint32)m_position.getY(), m_width, m_height, m_currentRow, m_currentFrame, TheGame::Instance()->getRenderer()); 32 | } 33 | } 34 | 35 | void SDLGameObject::update() 36 | { 37 | m_velocity += m_acceleration; 38 | m_position += m_velocity; 39 | } 40 | -------------------------------------------------------------------------------- /07/002/SDLGameObject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SDLGameObject__ 2 | #define __SDLGameObject__ 3 | 4 | #include 5 | 6 | #include "GameObject.h" 7 | #include "LoaderParams.h" 8 | #include "Vector2D.h" 9 | 10 | class SDLGameObject : public GameObject 11 | { 12 | public: 13 | 14 | SDLGameObject(); 15 | 16 | virtual void draw(); 17 | virtual void update(); 18 | virtual void clean() {}; 19 | virtual void load(const LoaderParams *pParams); 20 | 21 | Vector2D& getPosition() { return m_position; } 22 | int getWidth() { return m_width; } 23 | int getHeight() { return m_height; } 24 | 25 | protected: 26 | 27 | Vector2D m_position; 28 | Vector2D m_velocity; 29 | Vector2D m_acceleration; 30 | 31 | int m_width; 32 | int m_height; 33 | 34 | int m_currentRow; 35 | int m_currentFrame; 36 | int m_numFrames; 37 | 38 | std::string m_textureID; 39 | }; 40 | 41 | #endif // __SDLGameObject__ 42 | -------------------------------------------------------------------------------- /07/002/StateParser.h: -------------------------------------------------------------------------------- 1 | #ifndef __StateParser__ 2 | #define __StateParser__ 3 | 4 | #include 5 | #include 6 | #include "tinyxml.h" 7 | 8 | class GameObject; 9 | 10 | class StateParser 11 | { 12 | public: 13 | bool parseState(const char* stateFile, std::string stateID, std::vector *pObjects, std::vector *pTextureIDs); 14 | 15 | private: 16 | void parseObjects(TiXmlElement* pStateRoot, std::vector *pObjects); 17 | void parseTextures(TiXmlElement* psStateRoot, std::vector *pTextureIDs); 18 | }; 19 | 20 | #endif /* defined(__StateParser__) */ 21 | 22 | -------------------------------------------------------------------------------- /07/002/TextureManager.h: -------------------------------------------------------------------------------- 1 | #ifndef __TextureManager__ 2 | #define __TextureManager__ 3 | 4 | #include 5 | #include 6 | #include "SDL.h" 7 | 8 | class TextureManager 9 | { 10 | public: 11 | 12 | bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); 13 | void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 14 | void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); 15 | void clearFromTextureMap(std::string id); 16 | 17 | static TextureManager* Instance() 18 | { 19 | if(s_pInstance == 0) 20 | { 21 | s_pInstance = new TextureManager(); 22 | return s_pInstance; 23 | } 24 | return s_pInstance; 25 | } 26 | 27 | void drawTile(std::string id, int margin, int spacing, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer); 28 | 29 | private: 30 | 31 | TextureManager() {} 32 | 33 | std::map m_textureMap; 34 | static TextureManager* s_pInstance; 35 | 36 | }; 37 | 38 | typedef TextureManager TheTextureManager; 39 | 40 | #endif // __TextureManager__ 41 | -------------------------------------------------------------------------------- /07/002/TileLayer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "TileLayer.h" 5 | #include "Game.h" 6 | #include "TextureManager.h" 7 | 8 | TileLayer::TileLayer(int tileSize, const std::vector& tilesets) : m_tileSize(tileSize), m_tilesets(tilesets), m_position(0, 0), m_velocity(0, 0) 9 | { 10 | m_numColumns = (TheGame::Instance()->getGameWidth() / m_tileSize); 11 | m_numRows = (TheGame::Instance()->getGameHeight() / m_tileSize); 12 | } 13 | 14 | void TileLayer::update() 15 | { 16 | m_position += m_velocity; 17 | m_velocity.setX(1); 18 | } 19 | 20 | void TileLayer::render() 21 | { 22 | int x, y, x2, y2 = 0; 23 | 24 | x = m_position.getX() / m_tileSize; 25 | y = m_position.getY() / m_tileSize; 26 | 27 | x2 = int(m_position.getX()) % m_tileSize; 28 | y2 = int(m_position.getY()) % m_tileSize; 29 | 30 | for(int i = 0; i < m_numRows; i++) 31 | { 32 | for(int j = 0; j < m_numColumns + 1; j++) 33 | { 34 | int id = m_tileIDs[i][j + x]; 35 | 36 | if(id == 0) 37 | { 38 | continue; 39 | } 40 | 41 | Tileset tileset = getTilesetByID(id); 42 | 43 | id--; 44 | 45 | TheTextureManager::Instance()->drawTile( 46 | tileset.name, 2, 2, 47 | (j * m_tileSize) - x2, (i * m_tileSize) - y2, 48 | m_tileSize, m_tileSize, 49 | (id - (tileset.firstGridID - 1)) / tileset.numColumns, 50 | (id - (tileset.firstGridID - 1 )) % tileset.numColumns, 51 | TheGame::Instance()->getRenderer()); 52 | } 53 | } 54 | } 55 | 56 | Tileset TileLayer::getTilesetByID(int tileID) 57 | { 58 | for(int i = 0; i < m_tilesets.size(); i++) 59 | { 60 | if(i + 1 <= m_tilesets.size() - 1) 61 | { 62 | if(tileID >= m_tilesets[i].firstGridID && tileID < m_tilesets[i + 1].firstGridID) 63 | { 64 | return m_tilesets[i]; 65 | } 66 | } 67 | else 68 | { 69 | return m_tilesets[i]; 70 | } 71 | } 72 | 73 | std::cout << "did not find tileset, returning empy tileset" << std::endl; 74 | Tileset t; 75 | return t; 76 | } 77 | -------------------------------------------------------------------------------- /07/002/TileLayer.h: -------------------------------------------------------------------------------- 1 | #ifndef __TileLayer__ 2 | #define __TileLayer__ 3 | 4 | #include 5 | 6 | #include "Layer.h" 7 | #include "Level.h" 8 | #include "Vector2D.h" 9 | 10 | class TileLayer : public Layer 11 | { 12 | public: 13 | 14 | TileLayer(int tileSize, const std::vector &tilesets); 15 | 16 | virtual void update(); 17 | virtual void render(); 18 | 19 | void setTileIDs(const std::vector>& data) 20 | { 21 | m_tileIDs = data; 22 | } 23 | 24 | void setTileSize(int tileSize) 25 | { 26 | m_tileSize = tileSize; 27 | } 28 | 29 | Tileset getTilesetByID(int tileID); 30 | 31 | private: 32 | 33 | int m_numColumns; 34 | int m_numRows; 35 | int m_tileSize; 36 | 37 | Vector2D m_position; 38 | Vector2D m_velocity; 39 | 40 | const std::vector &m_tilesets; 41 | 42 | std::vector> m_tileIDs; 43 | }; 44 | 45 | #endif // __TileLayer__ 46 | -------------------------------------------------------------------------------- /07/002/Vector2D.h: -------------------------------------------------------------------------------- 1 | #ifndef __Vector__ 2 | #define __Vector__ 3 | 4 | #include 5 | 6 | class Vector2D 7 | { 8 | public: 9 | 10 | Vector2D(float x, float y): m_x(x), m_y(y) {} 11 | 12 | float getX() { return m_x; } 13 | float getY() { return m_y; } 14 | 15 | void setX(float x) { m_x = x; } 16 | void setY(float y) { m_y = y; } 17 | 18 | float length() { return sqrt(m_x * m_x + m_y * m_y); } 19 | 20 | Vector2D operator+(const Vector2D& v2) const 21 | { 22 | return Vector2D(m_x + v2.m_x, m_y + v2.m_y); 23 | } 24 | 25 | Vector2D& operator+=(const Vector2D& v2) 26 | { 27 | m_x += v2.m_x; 28 | m_y += v2.m_y; 29 | 30 | return *this; 31 | } 32 | 33 | Vector2D operator-(const Vector2D& v2) const 34 | { 35 | return Vector2D(m_x - v2.m_x, m_y - v2.m_y); 36 | } 37 | 38 | Vector2D& operator-=(const Vector2D& v2) 39 | { 40 | m_x -= v2.m_x; 41 | m_y -= v2.m_y; 42 | 43 | return *this; 44 | } 45 | 46 | Vector2D operator*(float scalar) 47 | { 48 | return Vector2D(m_x * scalar, m_y * scalar); 49 | } 50 | 51 | Vector2D& operator*=(float scalar) 52 | { 53 | m_x *= scalar; 54 | m_y *= scalar; 55 | 56 | return *this; 57 | } 58 | 59 | Vector2D operator/(float scalar) 60 | { 61 | return Vector2D(m_x / scalar, m_y / scalar); 62 | } 63 | 64 | Vector2D& operator/=(float scalar) 65 | { 66 | m_x /= scalar; 67 | m_y /= scalar; 68 | 69 | return *this; 70 | } 71 | 72 | void normalize() 73 | { 74 | float l = length(); 75 | if(l > 0) 76 | { 77 | (*this) *= 1 / l; 78 | } 79 | } 80 | private: 81 | 82 | float m_x; 83 | float m_y; 84 | }; 85 | 86 | #endif // __Vector__ 87 | -------------------------------------------------------------------------------- /07/002/base64.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kirbyboy/SDL-game-development-source-code/875bb7ccb0f817ec5f8e2237d2f4d5acc7f9fd62/07/002/base64.cpp -------------------------------------------------------------------------------- /07/002/base64.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | std::string base64_encode(unsigned char const* , unsigned int len); 5 | std::string base64_decode(std::string const& s); 6 | 7 | -------------------------------------------------------------------------------- /07/002/compile.sh: -------------------------------------------------------------------------------- 1 | g++ -O0 -g -std=c++11 main.cpp Game.cpp TextureManager.cpp Player.cpp Enemy.cpp SDLGameObject.cpp InputHandler.cpp PlayState.cpp GameStateMachine.cpp MenuButton.cpp PauseState.cpp AnimatedGraphic.cpp GameOverState.cpp GameObjectFactory.cpp StateParser.cpp MainMenuState.cpp tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp Level.cpp TileLayer.cpp LevelParser.cpp base64.cpp ObjectLayer.cpp -o main `sdl2-config --cflags --libs` -lSDL2_image 2 | -------------------------------------------------------------------------------- /07/002/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Game.h" 3 | 4 | // our Game object 5 | Game* g_game = 0; 6 | const int FPS = 60; 7 | const int DELAY_TIME = 1000.0f / FPS; 8 | 9 | int main() 10 | { 11 | Uint32 frameStart, frameTime; 12 | 13 | std::cout << "game init attempt..." << std::endl; 14 | if(TheGame::Instance()->init("Chapter 1", 100, 100, 640, 480, false)) 15 | { 16 | std::cout << "game init success!\n"; 17 | while(TheGame::Instance()->running()) 18 | { 19 | frameStart = SDL_GetTicks(); 20 | 21 | TheGame::Instance()->handleEvents(); 22 | TheGame::Instance()->update(); 23 | TheGame::Instance()->render(); 24 | 25 | frameTime = SDL_GetTicks() - frameStart; 26 | if(frameTime < DELAY_TIME) 27 | { 28 | SDL_Delay((int)(DELAY_TIME - frameTime)); 29 | } 30 | } 31 | } 32 | else 33 | { 34 | std::cout << "game init failure - " << SDL_GetError() << std::endl; 35 | return -1; 36 | } 37 | 38 | std::cout << "game closing..." << std::endl; 39 | TheGame::Instance()->clean(); 40 | 41 | return 0; 42 | } 43 | 44 | 45 | --------------------------------------------------------------------------------