From adfda73e1810973a40b7bedd9a8edc3e7ab89e3c Mon Sep 17 00:00:00 2001 From: chai Date: Sat, 19 May 2018 12:05:57 +0800 Subject: =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=90=E7=B3=BB=E7=BB=9F=E5=9F=BA?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/render/drawable.cpp | 2 +- src/libjin/render/drawable.h | 22 ++++----- src/libjin/render/jsl.cpp | 100 ++++++++++++++++++++--------------------- src/libjin/render/window.cpp | 33 ++++++++------ src/libjin/render/window.h | 27 ++++++----- 5 files changed, 97 insertions(+), 87 deletions(-) (limited to 'src/libjin/render') diff --git a/src/libjin/render/drawable.cpp b/src/libjin/render/drawable.cpp index c0d40a1..4ac4f03 100644 --- a/src/libjin/render/drawable.cpp +++ b/src/libjin/render/drawable.cpp @@ -60,7 +60,7 @@ namespace render glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); - // pop the model matrix + // pop the model matrix glPopMatrix(); // bind texture to default screen diff --git a/src/libjin/render/drawable.h b/src/libjin/render/drawable.h index b8e985a..0cf6d4b 100644 --- a/src/libjin/render/drawable.h +++ b/src/libjin/render/drawable.h @@ -15,20 +15,20 @@ namespace render void draw(int x, int y, float sx, float sy, float r); - inline int Drawable::getWidth() const - { - return width; - } + inline int Drawable::getWidth() const + { + return width; + } - inline int Drawable::getHeight() const - { - return height; - } + inline int Drawable::getHeight() const + { + return height; + } inline GLuint getTexture() const - { - return texture; - }; + { + return texture; + }; protected: #define DRAWABLE_V_SIZE 8 diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp index 58b9a15..3702e14 100644 --- a/src/libjin/render/jsl.cpp +++ b/src/libjin/render/jsl.cpp @@ -10,7 +10,7 @@ namespace render "#define number float \n" "#define Image sampler2D \n" "#define Canvas sampler2D \n" - "#define Color vec4 \n" + "#define Color vec4 \n" "#define Texel texture2D \n" "#define extern uniform \n" "uniform Image _tex0_; \n" @@ -19,10 +19,10 @@ namespace render "gl_FragColor = effect(gl_Color, _tex0_, gl_TexCoord[0].xy, gl_FragCoord.xy);\n" "}\0"; - shared GLint JSLProgram::currentTextureUnit = 0; - shared GLint JSLProgram::maxTextureUnits = -1; + shared GLint JSLProgram::currentTextureUnit = 0; + shared GLint JSLProgram::maxTextureUnits = -1; - shared JSLProgram* JSLProgram::currentJSLProgram = nullptr; + shared JSLProgram* JSLProgram::currentJSLProgram = nullptr; JSLProgram::JSLProgram(const char* program) { @@ -61,17 +61,17 @@ namespace render std::map::iterator texture_unit = texturePool.find(name); if (texture_unit != texturePool.end()) return texture_unit->second; - if (++currentTextureUnit >= maxTextureUnits) - return 0; + if (++currentTextureUnit >= maxTextureUnits) + return 0; texturePool[name] = currentTextureUnit; - return currentTextureUnit; + return currentTextureUnit; } #define checkJSL() if (currentJSLProgram != this) return void JSLProgram::sendFloat(const char* variable, float number) { - checkJSL(); + checkJSL(); int loc = glGetUniformLocation(pid, variable); glUniform1f(loc, number); @@ -79,12 +79,12 @@ namespace render void JSLProgram::sendImage(const char* variable, const Image* image) { - checkJSL(); + checkJSL(); GLint texture_unit = JSLProgram::getTextureUnit(variable); GLint location = glGetUniformLocation(pid, variable); - glUniform1i(location, texture_unit); + glUniform1i(location, texture_unit); glActiveTexture(GL_TEXTURE0 + texture_unit); glBindTexture(GL_TEXTURE_2D, image->getTexture()); @@ -92,57 +92,57 @@ namespace render glActiveTexture(GL_TEXTURE0); } - void JSLProgram::sendCanvas(const char* variable, const Canvas* canvas) - { - checkJSL(); + void JSLProgram::sendCanvas(const char* variable, const Canvas* canvas) + { + checkJSL(); - GLint texture_unit = getTextureUnit(variable); + GLint texture_unit = getTextureUnit(variable); - GLint location = glGetUniformLocation(pid, variable); - glUniform1i(location, texture_unit); + GLint location = glGetUniformLocation(pid, variable); + glUniform1i(location, texture_unit); - glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(GL_TEXTURE_2D, canvas->getTexture()); + glActiveTexture(GL_TEXTURE0 + texture_unit); + glBindTexture(GL_TEXTURE_2D, canvas->getTexture()); - glActiveTexture(GL_TEXTURE0); - } + glActiveTexture(GL_TEXTURE0); + } - void JSLProgram::sendVec2(const char* name, float x, float y) - { - checkJSL(); + void JSLProgram::sendVec2(const char* name, float x, float y) + { + checkJSL(); - int loc = glGetUniformLocation(pid, name); - glUniform2f(loc, x, y); - } + int loc = glGetUniformLocation(pid, name); + glUniform2f(loc, x, y); + } - void JSLProgram::sendVec3(const char* name, float x, float y, float z) - { - checkJSL(); + void JSLProgram::sendVec3(const char* name, float x, float y, float z) + { + checkJSL(); - int loc = glGetUniformLocation(pid, name); - glUniform3f(loc, x, y, z); - } + int loc = glGetUniformLocation(pid, name); + glUniform3f(loc, x, y, z); + } - void JSLProgram::sendVec4(const char* name, float x, float y, float z, float w) - { - checkJSL(); + void JSLProgram::sendVec4(const char* name, float x, float y, float z, float w) + { + checkJSL(); - int loc = glGetUniformLocation(pid, name); - glUniform4f(loc, x, y, z, w); - } + int loc = glGetUniformLocation(pid, name); + glUniform4f(loc, x, y, z, w); + } - void JSLProgram::sendColor(const char* name, const color* col) - { - checkJSL(); - - int loc = glGetUniformLocation(pid, name); - glUniform4f(loc, - col->rgba.r / 255.f, - col->rgba.g / 255.f, - col->rgba.b / 255.f, - col->rgba.a / 255.f - ); - } + void JSLProgram::sendColor(const char* name, const color* col) + { + checkJSL(); + + int loc = glGetUniformLocation(pid, name); + glUniform4f(loc, + col->rgba.r / 255.f, + col->rgba.g / 255.f, + col->rgba.b / 255.f, + col->rgba.a / 255.f + ); + } } } diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 70e7e2d..1f55be8 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -18,17 +18,17 @@ namespace render { } - onlyonce void Window::init(const Window::Setting& setting) + onlyonce bool Window::_init(const SettingBase* s) { - CallOnce(_init(setting)); - } + if (SDL_Init(SDL_INIT_VIDEO) < 0) + return false; - inline void Window::_init(const Setting& setting) - { - width = setting.width; - height = setting.height; - const char* title = setting.title; - bool vsync = setting.vsync; + WindowSetting* setting = (WindowSetting*)s; + + width = setting->width; + height = setting->height; + bool vsync = setting->vsync; + const char* title = setting->title; if (wnd) { @@ -54,7 +54,11 @@ namespace render wy = SDL_WINDOWPOS_UNDEFINED; wnd = SDL_CreateWindow(title, wx, wy, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); + if (wnd == NULL) + return false; ctx = SDL_GL_CreateContext(wnd); + if (ctx == NULL) + return false; SDL_GL_SetSwapInterval(vsync ? 1 : 0); SDL_GL_MakeCurrent(wnd, ctx); glClearColor(0.f, 0.f, 0.f, 1.f); @@ -66,6 +70,7 @@ namespace render Canvas::unbind(); swapBuffers(); + return true; } SDL_Window* Window::getWnd() @@ -78,11 +83,11 @@ namespace render return ctx; } - inline void Window::swapBuffers() - { - if (wnd) - SDL_GL_SwapWindow(wnd); - } + 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 8066f8a..1064c36 100644 --- a/src/libjin/render/window.h +++ b/src/libjin/render/window.h @@ -2,24 +2,25 @@ #define __JIN_RENDER_WINDOW #include #include "../utils/utils.h" +#include "../common/subsystem.h" namespace jin { namespace render { - class Window + + class Window : public common::Subsystem { public: - struct Setting - { - int width, height; // 窗口大小 - bool vsync; // 垂直同步 - const char* title; // 标题 - }; + struct Setting : SettingBase + { + public: + int width, height; // 窗口大小 + bool vsync; // 垂直同步 + const char* title; // 标题 + }; - onlyonce void init(const Setting& setting); - SDL_Window* getWnd(); SDL_GLContext getCtx(); @@ -39,7 +40,7 @@ namespace render return height; } - inline void swapBuffers(); + inline void swapBuffers(); private: @@ -54,8 +55,12 @@ namespace render int width, height; - inline void _init(const Setting& setting); + onlyonce bool _init(const SettingBase* setting) override; + }; + + typedef Window::Setting WindowSetting; + } } #endif \ No newline at end of file -- cgit v1.1-26-g67d0