]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
created a window class
authortcsullivan <tullivan99@gmail.com>
Mon, 26 Aug 2019 23:08:18 +0000 (19:08 -0400)
committertcsullivan <tullivan99@gmail.com>
Mon, 26 Aug 2019 23:08:18 +0000 (19:08 -0400)
.gitignore
Makefile
src/main.cpp

index 3e258af446b817a9b8da0d380f4d8952b2554907..da93793f1ce6b8801b354d5aa352b0f2a2b924fb 100644 (file)
@@ -1,4 +1,6 @@
 main
 out
+.*.swp
+.*.swo
 *.o
 
index b498fdd75304d006ba8cf164e67d325242fc16a9..d6a3d3237d0822c7b54f0495aa6f94d4591290ea 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,7 @@ LIBS = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -lluajit
 
 CXXFLAGS = -ggdb -std=c++17 \
        -Wall -Wextra -Werror -pedantic \
-       -Isrc -I$(LIBDIR)/LuaJIT -I$(LIBDIR)/entityx/entityx -I$(LIBDIR)/entityx
+       -Isrc -I$(LIBDIR)/LuaJIT/src -I$(LIBDIR)/entityx
 
 CXXSRCDIR = src
 CXXOUTDIR = out
index ef787374b921112dc58a960755c8da662d3bfdec..5e5e278f7fa08a9a8bda60846a53303551018bbc 100644 (file)
@@ -18,7 +18,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-#include <entityx.h>
+#include <entityx/entityx.h>
 
 #include <SDL2/SDL.h>
 
 #include <memory>
 #include <thread>
 
-constexpr const char *title = "gamedev2";
-constexpr int width = 640;
-constexpr int height = 480;
+class Window {
+private:
+       constexpr static const char *title = "gamedev2";
+       constexpr static int width = 640;
+       constexpr static int height = 480;
+
+       static std::unique_ptr<SDL_Window, void (*)(SDL_Window *)> window;
+       static SDL_GLContext context;
+
+       static void destroyWindow(SDL_Window *w) {
+               SDL_GL_DeleteContext(context);
+               SDL_DestroyWindow(w);
+       }
+
+public:
+       static int init(void) {
+               if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
+                       std::cerr << "SDL video failed to initialize: "
+                               << SDL_GetError() << std::endl;
+                       return -1;
+               }
+
+               window.reset(SDL_CreateWindow(title,
+                       SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+                       width, height,
+                       SDL_WINDOW_OPENGL));
+
+               if (window.get() == nullptr) {
+                       std::cerr << "SDL window creation failed: "
+                               << SDL_GetError() << std::endl;
+                       return -1;
+               }
+
+               context = SDL_GL_CreateContext(window.get());
+
+               return 0;
+       }
+
+       static void render(void) {
+               SDL_GL_SwapWindow(window.get());
+       }
+};
+
+std::unique_ptr<SDL_Window, void (*)(SDL_Window *)> Window::window (nullptr,
+       Window::destroyWindow);
+SDL_GLContext Window::context;
 
 std::atomic_bool shouldRun;
 
@@ -40,7 +83,7 @@ static void logicLoop(void);
 int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
 {
        // Initialize SDL
-       if (SDL_Init(SDL_INIT_VIDEO) != 0) {
+       if (SDL_Init(0) != 0) {
                std::cerr << "SDL failed to initialize: " << SDL_GetError()
                        << std::endl;
                return -1;
@@ -49,15 +92,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
        }
 
        // Create our window
-       std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)> window
-               (SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
-               SDL_WINDOWPOS_UNDEFINED, width, height, 0), SDL_DestroyWindow);
-
-       if (window.get() == nullptr) {
-               std::cerr << "SDL window creation failed: " << SDL_GetError()
-                       << std::endl;
-               return -1;
-       }
+       Window::init();
 
        // Start game
        shouldRun.store(true);
@@ -70,11 +105,10 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[])
 
 void renderLoop(void)
 {
-       using namespace std::chrono_literals;
-
-       // TODO render
-       while (shouldRun.load())
-               std::this_thread::sleep_for(100ms);
+       while (shouldRun.load()) {
+               Window::render();
+               std::this_thread::yield();
+       }
 }
 
 void logicLoop(void)