diff options
Diffstat (limited to 'src/libjin/render/window.cpp')
-rw-r--r-- | src/libjin/render/window.cpp | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 0179877..d481793 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -1,7 +1,8 @@ #include "window.h" #include "3rdparty/GLee/GLee.h" #include "canvas.h" -#include "../utils/macros.h" +#include "../utils/utils.h" + namespace jin { namespace render @@ -16,18 +17,25 @@ namespace render Window::~Window() { } - - void Window::init(int pw, int ph, const char* t) + + onlyonce void Window::init(const Window::Setting& setting) + { + CallOnce(_init(setting)); + } + + inline void Window::_init(const Setting& setting) { - w = pw; - h = ph; + width = setting.width; + height = setting.height; + const char* title = setting.title; + bool vsync = setting.vsync; if (wnd) { - SDL_DestroyWindow(wnd); + SDL_DestroyWindow(wnd); SDL_FlushEvent(SDL_WINDOWEVENT); } - + if (ctx) { SDL_GL_DeleteContext(ctx); @@ -39,29 +47,24 @@ namespace render SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - int wx = SDL_WINDOWPOS_UNDEFINED, - wy = SDL_WINDOWPOS_UNDEFINED; + int wx = SDL_WINDOWPOS_UNDEFINED, + wy = SDL_WINDOWPOS_UNDEFINED; - wnd = SDL_CreateWindow(t, wx, wy, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); + wnd = SDL_CreateWindow(title, wx, wy, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); ctx = SDL_GL_CreateContext(wnd); - // ֱͬGPUռ8%->4% - SDL_GL_SetSwapInterval(1); + SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_MakeCurrent(wnd, ctx); glClearColor(0.f, 0.f, 0.f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glColor4f(1, 1, 1, 1); - /** - * Set the viewport to top-left corner. - * Bind to the default render buffer. - */ + Canvas::unbind(); - // Swap window buffer swapBuffers(); } - + SDL_Window* Window::getWnd() { return wnd; |