|
| 1 | +// Knapecz, Adam - 1DAE11 |
| 2 | +#include "base.h" |
| 3 | +#include <iostream> |
| 4 | +#include <algorithm> |
| 5 | +#include <chrono> |
| 6 | +#include "BaseGame.h" |
| 7 | + |
| 8 | +BaseGame::BaseGame(const Window& window) |
| 9 | + : m_Window{window} |
| 10 | + , m_Viewport{0, 0, window.width, window.height} |
| 11 | + , m_pWindow{nullptr} |
| 12 | + , m_pContext{nullptr} |
| 13 | + , m_Initialized{false} |
| 14 | + , m_MaxElapsedSeconds{0.1f} |
| 15 | +{ |
| 16 | + InitializeGameEngine(); |
| 17 | +} |
| 18 | + |
| 19 | +BaseGame::~BaseGame() |
| 20 | +{ |
| 21 | + CleanupGameEngine(); |
| 22 | +} |
| 23 | + |
| 24 | +void BaseGame::InitializeGameEngine() |
| 25 | +{ |
| 26 | + // disable console close window button |
| 27 | +#ifdef _WIN32 |
| 28 | + HWND hwnd = GetConsoleWindow(); |
| 29 | + HMENU hmenu = GetSystemMenu(hwnd, FALSE); |
| 30 | + EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED); |
| 31 | +#endif |
| 32 | + |
| 33 | + // Initialize SDL |
| 34 | + if (SDL_Init(SDL_INIT_VIDEO /*| SDL_INIT_AUDIO*/) < 0) |
| 35 | + { |
| 36 | + std::cerr << "BaseGame::Initialize( ), error when calling SDL_Init: " << SDL_GetError() << std::endl; |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Use OpenGL 2.1 |
| 41 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
| 42 | + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); |
| 43 | + |
| 44 | + // Create window |
| 45 | + m_pWindow = SDL_CreateWindow( |
| 46 | + m_Window.title.c_str(), |
| 47 | + SDL_WINDOWPOS_CENTERED, |
| 48 | + SDL_WINDOWPOS_CENTERED, |
| 49 | + int(m_Window.width), |
| 50 | + int(m_Window.height), |
| 51 | + SDL_WINDOW_OPENGL); |
| 52 | + if (m_pWindow == nullptr) |
| 53 | + { |
| 54 | + std::cerr << "BaseGame::Initialize( ), error when calling SDL_CreateWindow: " << SDL_GetError() << std::endl; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // Create OpenGL context |
| 59 | + m_pContext = SDL_GL_CreateContext(m_pWindow); |
| 60 | + if (m_pContext == nullptr) |
| 61 | + { |
| 62 | + std::cerr << "BaseGame::Initialize( ), error when calling SDL_GL_CreateContext: " << SDL_GetError() << |
| 63 | + std::endl; |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Set the swap interval for the current OpenGL context, |
| 68 | + // synchronize it with the vertical retrace |
| 69 | + if (m_Window.isVSyncOn) |
| 70 | + { |
| 71 | + if (SDL_GL_SetSwapInterval(1) < 0) |
| 72 | + { |
| 73 | + std::cerr << "BaseGame::Initialize( ), error when calling SDL_GL_SetSwapInterval: " << SDL_GetError() << |
| 74 | + std::endl; |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + SDL_GL_SetSwapInterval(0); |
| 81 | + } |
| 82 | + |
| 83 | + // Set the Projection matrix to the identity matrix |
| 84 | + glMatrixMode(GL_PROJECTION); |
| 85 | + glLoadIdentity(); |
| 86 | + |
| 87 | + // Set up a two-dimensional orthographic viewing region. |
| 88 | + glOrtho(0, m_Window.width, 0, m_Window.height, -1, 1); // y from bottom to top |
| 89 | + |
| 90 | + // Set the viewport to the client window area |
| 91 | + // The viewport is the rectangular region of the window where the image is drawn. |
| 92 | + glViewport(0, 0, int(m_Window.width), int(m_Window.height)); |
| 93 | + |
| 94 | + // Set the Modelview matrix to the identity matrix |
| 95 | + glMatrixMode(GL_MODELVIEW); |
| 96 | + glLoadIdentity(); |
| 97 | + |
| 98 | + // Enable color blending and use alpha blending |
| 99 | + glEnable(GL_BLEND); |
| 100 | + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 101 | + |
| 102 | + // Initialize PNG loading |
| 103 | + |
| 104 | + int imgFlags = IMG_INIT_PNG; |
| 105 | + if (!(IMG_Init(imgFlags) & imgFlags)) |
| 106 | + { |
| 107 | + std::cerr << "BaseGame::Initialize( ), error when calling IMG_Init: " << IMG_GetError() << std::endl; |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + // Initialize SDL_ttf |
| 113 | + if (TTF_Init() == -1) |
| 114 | + { |
| 115 | + std::cerr << "BaseGame::Initialize( ), error when calling TTF_Init: " << TTF_GetError() << std::endl; |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + //Initialize SDL_mixer |
| 120 | + |
| 121 | + if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) |
| 122 | + { |
| 123 | + std::cerr << "BaseGame::Initialize( ), error when calling Mix_OpenAudio: " << Mix_GetError() << std::endl; |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + m_Initialized = true; |
| 129 | +} |
| 130 | + |
| 131 | +void BaseGame::Run() |
| 132 | +{ |
| 133 | + if (!m_Initialized) |
| 134 | + { |
| 135 | + std::cerr << "BaseGame::Run( ), BaseGame not correctly initialized, unable to run the BaseGame\n"; |
| 136 | + std::cin.get(); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + // Main loop flag |
| 141 | + bool quit{false}; |
| 142 | + |
| 143 | + // Set start time |
| 144 | + std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now(); |
| 145 | + |
| 146 | + //The event loop |
| 147 | + SDL_Event e{}; |
| 148 | + while (!quit) |
| 149 | + { |
| 150 | + // Poll next event from queue |
| 151 | + while (SDL_PollEvent(&e) != 0) |
| 152 | + { |
| 153 | + // Handle the polled event |
| 154 | + switch (e.type) |
| 155 | + { |
| 156 | + case SDL_QUIT: |
| 157 | + quit = true; |
| 158 | + break; |
| 159 | + case SDL_KEYDOWN: |
| 160 | + this->ProcessKeyDownEvent(e.key); |
| 161 | + break; |
| 162 | + case SDL_KEYUP: |
| 163 | + this->ProcessKeyUpEvent(e.key); |
| 164 | + break; |
| 165 | + case SDL_MOUSEMOTION: |
| 166 | + e.motion.y = int(m_Window.height) - e.motion.y; |
| 167 | + this->ProcessMouseMotionEvent(e.motion); |
| 168 | + break; |
| 169 | + case SDL_MOUSEBUTTONDOWN: |
| 170 | + e.button.y = int(m_Window.height) - e.button.y; |
| 171 | + this->ProcessMouseDownEvent(e.button); |
| 172 | + break; |
| 173 | + case SDL_MOUSEBUTTONUP: |
| 174 | + e.button.y = int(m_Window.height) - e.button.y; |
| 175 | + this->ProcessMouseUpEvent(e.button); |
| 176 | + break; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + if (!quit) |
| 181 | + { |
| 182 | + // Get current time |
| 183 | + std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now(); |
| 184 | + |
| 185 | + // Calculate elapsed time |
| 186 | + float elapsedSeconds = std::chrono::duration<float>(t2 - t1).count(); |
| 187 | + |
| 188 | + // Update current time |
| 189 | + t1 = t2; |
| 190 | + |
| 191 | + // Prevent jumps in time caused by break points |
| 192 | + elapsedSeconds = std::min(elapsedSeconds, m_MaxElapsedSeconds); |
| 193 | + |
| 194 | + // Call the BaseGame object 's Update function, using time in seconds (!) |
| 195 | + this->Update(elapsedSeconds); |
| 196 | + |
| 197 | + // Draw in the back buffer |
| 198 | + this->Draw(); |
| 199 | + |
| 200 | + // Update screen: swap back and front buffer |
| 201 | + SDL_GL_SwapWindow(m_pWindow); |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +void BaseGame::CleanupGameEngine() |
| 207 | +{ |
| 208 | + SDL_GL_DeleteContext(m_pContext); |
| 209 | + |
| 210 | + SDL_DestroyWindow(m_pWindow); |
| 211 | + m_pWindow = nullptr; |
| 212 | + |
| 213 | + //Quit SDL subsystems |
| 214 | + Mix_Quit(); |
| 215 | + TTF_Quit(); |
| 216 | + SDL_Quit(); |
| 217 | + |
| 218 | + // enable console close window button |
| 219 | +#ifdef _WIN32 |
| 220 | + HWND hwnd = GetConsoleWindow(); |
| 221 | + HMENU hmenu = GetSystemMenu(hwnd, FALSE); |
| 222 | + EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); |
| 223 | +#endif |
| 224 | +} |
0 commit comments