diff --git a/Game.cpp b/Game.cpp index 0822ff2e51131d77cf4afbd05f24d0047a2fd7bc..bb778ff6544596c0dac67f313e474bedeb43004c 100644 --- a/Game.cpp +++ b/Game.cpp @@ -1,7 +1,14 @@ #include "Game.h" - -bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags) +#include <iostream> +#include "SDL2/SDL.h" +bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) { + int flags = 0; + if(fullscreen) + { + flags = SDL_WINDOW_FULLSCREEN; + } + // attempt to initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) == 0) { @@ -34,8 +41,12 @@ bool Game::init(const char* title, int xpos, int ypos, int width, int height, in std::cout << "SDL init fail\n"; return false; // SDL init fail } + std::cout << "init success\n"; m_bRunning = true; // everything inited successfully, start the main loop + + m_textureManager.load("gfx/yolo.png", "animate", m_pRenderer); + return true; } @@ -44,3 +55,27 @@ void Game::render() SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color SDL_RenderPresent(m_pRenderer); // draw to the screen } + +void Game::clean() +{ + std::cout << "cleaning game\n"; + SDL_DestroyWindow(m_pWindow); + SDL_DestroyRenderer(m_pRenderer); + SDL_Quit(); +} + +void Game::handleEvents() +{ + SDL_Event event; + if(SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: + m_bRunning = false; + break; + default: + break; + } + } +} diff --git a/Game.h b/Game.h index 68666f133d63967eaa51ec791c520be8d982d757..0b652496e43c6a42d62ba3a8c0d2658f52589b5e 100644 --- a/Game.h +++ b/Game.h @@ -1,13 +1,14 @@ -#ifndef __Game__ -#define __Game__ +#ifndef GAME_H_INCLUDED +#define GAME_H_INCLUDED #include "SDL.h" +#include "TextureManager.h" class Game { public: - Game(); - ~Game(); - bool init(const char* title, int xpos, int ypos, int width, int height, int flags); + //Game(); + //~Game(); + bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen); void render(); void update(); void handleEvents(); @@ -16,7 +17,8 @@ public: private: SDL_Window* m_pWindow; SDL_Renderer* m_pRenderer; - + int m_currentFrame; + TextureManager m_textureManager; bool m_bRunning; }; #endif /* defined(__Game__) */ diff --git a/TextureManager.cpp b/TextureManager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97763d4b37c6de83ff50a13c6d01efa609144746 --- /dev/null +++ b/TextureManager.cpp @@ -0,0 +1,48 @@ +#include "TextureManager.h" +#include <iostream> +#include "SDL2/SDL.h" +#include "SDL2/SDL_image.h" +bool TextureManager::load(std::string fileName, std::string id, SDL_Renderer* pRenderer) +{ + SDL_Surface* pTempSurface = IMG_Load(fileName.c_str()); + if(pTempSurface == 0) + { + return false; + } + SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface); + SDL_FreeSurface(pTempSurface); + // everything went ok, add the texture to our list + if(pTexture != 0) + { + m_textureMap[id] = pTexture; + return true; + } + // reaching here means something went wrong + return false; +} + +void TextureManager::draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip) +{ + SDL_Rect srcRect; + SDL_Rect destRect; + srcRect.x = 0; + srcRect.y = 0; + srcRect.w = destRect.w = width; + srcRect.h = destRect.h = height; + destRect.x = x; + destRect.y = y; + SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); +} + +void TextureManager::drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer *pRenderer, SDL_RendererFlip flip) +{ + SDL_Rect srcRect; + SDL_Rect destRect; + srcRect.x = width * currentFrame; + srcRect.y = height * currentRow; + srcRect.w = destRect.w = width; + srcRect.h = destRect.h = height; + destRect.x = x; + destRect.y = y; + SDL_RenderCopyEx(pRenderer, m_textureMap[id], &srcRect, &destRect, 0, 0, flip); +} diff --git a/TextureManager.h b/TextureManager.h new file mode 100644 index 0000000000000000000000000000000000000000..8759521d0b423d01a0c251119e47d9cc3592f1a5 --- /dev/null +++ b/TextureManager.h @@ -0,0 +1,19 @@ +#ifndef TEXTUREMANAGER_H_INCLUDED +#define TEXTUREMANAGER_H_INCLUDED +#include <iostream> +#include <map> +#include "SDL2/SDL.h" +class TextureManager +{ +public: + bool load(std::string fileName, std::string id, SDL_Renderer* pRenderer); + void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE); + 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); +private: + std::map<std::string, SDL_Texture*> m_textureMap; +}; + + + + +#endif // TEXTUREMANAGER_H_INCLUDED diff --git a/gfx/arc1.png b/gfx/arc1.png new file mode 100644 index 0000000000000000000000000000000000000000..511f64c319e3e9ffe9c31fa90ebd65cdd713514e Binary files /dev/null and b/gfx/arc1.png differ diff --git a/gfx/blocks1.png b/gfx/blocks1.png new file mode 100644 index 0000000000000000000000000000000000000000..40044e1f885c6878ba870b1879e62472e40869ae Binary files /dev/null and b/gfx/blocks1.png differ diff --git a/gfx/yolo.png b/gfx/yolo.png new file mode 100644 index 0000000000000000000000000000000000000000..b76e6fbb9eac7b380ce72b21f407dbe40bcb7ff7 Binary files /dev/null and b/gfx/yolo.png differ diff --git a/main.cpp b/main.cpp index c644e15f9009340672b8d96c20696b9bd6a9bf35..61d8002b4c62d1c8c6ab0e3745a393ed4c09df41 100644 --- a/main.cpp +++ b/main.cpp @@ -1,37 +1,19 @@ -#include<SDL.h> -SDL_Window* g_pWindow = 0; -SDL_Renderer* g_pRenderer = 0; -int main(int argc, char* args[]) +#include "Game.h" + +// our Game object +Game* g_game = 0; + +int main(int argc, char* argv[]) { - // initialize SDL - if(SDL_Init(SDL_INIT_EVERYTHING) >= 0) - { - // if succeeded create our window - g_pWindow = SDL_CreateWindow("Chapter 1: Setting up SDL", - SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, - 640, 480, - SDL_WINDOW_SHOWN); - // if the window creation succeeded create our renderer - if(g_pWindow != 0) - { - g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0); - } - } - else - { - return 1; // sdl could not initialize - } - // everything succeeded lets draw the window - // set to black // This function expects Red, Green, Blue and - // Alpha as color values - SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255); - // clear the window to black - SDL_RenderClear(g_pRenderer); - // show the window - SDL_RenderPresent(g_pRenderer); - // set a delay before quitting - SDL_Delay(5000); - // clean up SDL - SDL_Quit(); - return 0; + g_game = new Game(); + g_game->init("Chapter 1", 100, 100, 640, 580, false); + while(g_game->running()) + { + g_game->handleEvents(); + //g_game->update(); + g_game->render(); + } + g_game->clean(); + delete g_game; + return 0; }