#include #include #include #include "app.hpp" #ifdef EMSCRIPTEN #include #endif #include "gl.hpp" static SDL_Window *s_window = nullptr; static SDL_GLContext s_context = nullptr; static bool s_quit = false; static std::unique_ptr s_app; static bool InitSDL() { if (SDL_Init(SDL_INIT_VIDEO) != 0) { std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl; return false; } s_window = SDL_CreateWindow("PortalGame", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_MAXIMIZED | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (!s_window) { std::cerr << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; SDL_Quit(); return false; } return true; } static bool InitGL() { #ifdef EMSCRIPTEN SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); #else SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); #endif s_context = SDL_GL_CreateContext(s_window); if (!s_context) { std::cerr << "SDL_GL_CreateContext Error: " << SDL_GetError() << std::endl; return false; } // Make context current if (SDL_GL_MakeCurrent(s_window, s_context) != 0) { std::cerr << "SDL_GL_MakeCurrent Error: " << SDL_GetError() << std::endl; SDL_GL_DeleteContext(s_context); return false; } #ifndef EMSCRIPTEN // Initialize GLAD if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) { std::cerr << "Failed to initialize GLAD" << std::endl; SDL_GL_DeleteContext(s_context); return false; } #endif return true; } static void ShutdownGL() { if (s_context) { SDL_GL_DeleteContext(s_context); s_context = nullptr; } } static void ShutdownSDL() { if (s_window) { SDL_DestroyWindow(s_window); s_window = nullptr; } SDL_Quit(); } static void PollEvents() { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { s_quit = true; return; } } } static void Frame() { Uint32 current_time = SDL_GetTicks(); s_app->SetTime(current_time / 1000.0f); // Set time in seconds PollEvents(); s_app->Frame(); SDL_GL_SwapWindow(s_window); } int main(int argc, char *argv[]) { if (!InitSDL()) { return 1; } if (!InitGL()) { ShutdownSDL(); return 1; } s_app = std::make_unique(); #ifdef EMSCRIPTEN emscripten_set_main_loop(Frame, 0, true); #else while (!s_quit) { Frame(); } s_app.reset(); ShutdownGL(); ShutdownSDL(); #endif return 0; }