aboutsummaryrefslogtreecommitdiffstats
path: root/src/window.cpp
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2016-10-10 19:17:19 -0500
committerClyne Sullivan <tullivan99@gmail.com>2016-10-10 19:17:19 -0500
commitb19265bfa91e55c564b75aadcabd212ac89cf349 (patch)
treefab3a34fc96d52fc634ca0d507fdbaf5d3546b8e /src/window.cpp
parent1f762f82f929cfd21222739a627a32e6199c34a9 (diff)
the revival, entityx
Diffstat (limited to 'src/window.cpp')
-rw-r--r--src/window.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/window.cpp b/src/window.cpp
new file mode 100644
index 0000000..4a060de
--- /dev/null
+++ b/src/window.cpp
@@ -0,0 +1,71 @@
+#include <window.hpp>
+
+#include <config.hpp>
+
+#include <SDL2/SDL_image.h>
+#include <SDL2/SDL_mixer.h>
+
+constexpr const char* WINDOW_TITLE = "gamedev";
+
+WindowSystem::WindowSystem(void)
+{
+ // attempt to initialize SDL
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
+ std::cout << "SDL was not able to initialize! Error: " << SDL_GetError();
+ abort();
+ }
+ atexit(SDL_Quit);
+
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
+
+ // attempt to initialize SDL_image
+ if (!(IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG) & (IMG_INIT_PNG | IMG_INIT_JPG))) {
+ std::cout << "Could not init image libraries! Error: " << IMG_GetError();
+ abort();
+ }
+ atexit(IMG_Quit);
+
+ // attempt to initialize SDL_mixer
+ if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
+ std::cout << "SDL_mixer could not initialize! Error: " << Mix_GetError();
+ abort();
+ }
+ Mix_AllocateChannels(8);
+ atexit(Mix_Quit);
+
+ // create the SDL window object
+ window = SDL_CreateWindow(WINDOW_TITLE,
+ SDL_WINDOWPOS_UNDEFINED, // Spawn the window at random (undefined) x and y coordinates
+ SDL_WINDOWPOS_UNDEFINED, //
+ game::SCREEN_WIDTH,
+ game::SCREEN_HEIGHT,
+ SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
+ );
+
+ if (window == nullptr) {
+ std::cout << "The window failed to generate! SDL_Error: " << SDL_GetError();
+ abort();
+ }
+
+ // create the OpenGL object that SDL provides
+ if ((glContext = SDL_GL_CreateContext(window)) == nullptr) {
+ std::cout << "The OpenGL context failed to initialize! SDL_Error: " << SDL_GetError();
+ abort();
+ }
+}
+
+WindowSystem::~WindowSystem(void)
+{
+ SDL_GL_DeleteContext(glContext);
+ SDL_DestroyWindow(window);
+}
+
+void WindowSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
+{
+ (void)en;
+ (void)ev;
+ (void)dt;
+
+ SDL_GL_SwapWindow(window);
+}