aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--Makefile2
-rw-r--r--src/main.cpp72
3 files changed, 56 insertions, 20 deletions
diff --git a/.gitignore b/.gitignore
index 3e258af..da93793 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,6 @@
main
out
+.*.swp
+.*.swo
*.o
diff --git a/Makefile b/Makefile
index b498fdd..d6a3d32 100644
--- 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
diff --git a/src/main.cpp b/src/main.cpp
index ef78737..5e5e278 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -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>
@@ -28,9 +28,52 @@
#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)