diff options
Diffstat (limited to 'src/libjin/render')
-rw-r--r-- | src/libjin/render/window.cpp | 41 | ||||
-rw-r--r-- | src/libjin/render/window.h | 23 |
2 files changed, 39 insertions, 25 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; diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h index 515ffff..8066f8a 100644 --- a/src/libjin/render/window.h +++ b/src/libjin/render/window.h @@ -1,6 +1,8 @@ #ifndef __JIN_RENDER_WINDOW #define __JIN_RENDER_WINDOW -#include "SDL2/SDL.h" +#include <SDL2/SDL.h> +#include "../utils/utils.h" + namespace jin { namespace render @@ -8,8 +10,15 @@ namespace render class Window { public: - - void init(int w, int h, const char* t); + + struct Setting + { + int width, height; // ڴС + bool vsync; // ֱͬ + const char* title; // + }; + + onlyonce void init(const Setting& setting); SDL_Window* getWnd(); @@ -22,12 +31,12 @@ namespace render inline int getW() { - return w; + return width; } inline int getH() { - return h; + return height; } inline void swapBuffers(); @@ -43,7 +52,9 @@ namespace render SDL_GLContext ctx; - int w, h; + int width, height; + + inline void _init(const Setting& setting); }; } } |