From bda9d88e84a1dccd4df978205a6ba1f141178b0c Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 19 May 2018 00:09:44 +0800 Subject: =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9E=82=E7=9B=B4=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/render/graphics.h | 1 + src/libjin/render/window.cpp | 28 +++++++++++++--------------- src/libjin/render/window.h | 11 ++++------- 3 files changed, 18 insertions(+), 22 deletions(-) (limited to 'src/libjin/render') diff --git a/src/libjin/render/graphics.h b/src/libjin/render/graphics.h index 242b19d..a3125c5 100644 --- a/src/libjin/render/graphics.h +++ b/src/libjin/render/graphics.h @@ -34,4 +34,5 @@ namespace render extern void polygon(RENDER_MODE mode, float* p, int count); } } + #endif diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 0d53654..0179877 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -32,40 +32,32 @@ namespace render { SDL_GL_DeleteContext(ctx); } - + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); 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_DOUBLEBUFFER, 1); - Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL ; - - int wx = SDL_WINDOWPOS_UNDEFINED, - wy = SDL_WINDOWPOS_UNDEFINED; + int wx = SDL_WINDOWPOS_UNDEFINED, + wy = SDL_WINDOWPOS_UNDEFINED; - /* Create window */ - wnd = SDL_CreateWindow(t, wx, wy, w, h, flags); - - // Create an opengl context + wnd = SDL_CreateWindow(t, wx, wy, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); ctx = SDL_GL_CreateContext(wnd); + // 开启垂直同步后降低GPU占用率8%->4% + SDL_GL_SetSwapInterval(1); SDL_GL_MakeCurrent(wnd, ctx); - - // Default clear color glClearColor(0.f, 0.f, 0.f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - - // Default render color glColor4f(1, 1, 1, 1); - /** * Set the viewport to top-left corner. * Bind to the default render buffer. */ Canvas::unbind(); - // Swap window buffer swapBuffers(); } @@ -80,5 +72,11 @@ namespace render return ctx; } + inline void Window::swapBuffers() + { + if (wnd) + SDL_GL_SwapWindow(wnd); + } + } } \ No newline at end of file diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h index f29c82d..515ffff 100644 --- a/src/libjin/render/window.h +++ b/src/libjin/render/window.h @@ -20,21 +20,18 @@ namespace render return (g_wnd ? g_wnd : (g_wnd = new Window())); } - inline int Window::getW() + inline int getW() { return w; } - inline int Window::getH() + inline int getH() { return h; } - inline void Window::swapBuffers() - { - if (wnd) - SDL_GL_SwapWindow(wnd); - } + inline void swapBuffers(); + private: Window(); -- cgit v1.1-26-g67d0