diff options
Diffstat (limited to 'src/libjin')
-rw-r--r-- | src/libjin/audio/audio.cpp | 5 | ||||
-rw-r--r-- | src/libjin/audio/audio.h | 14 | ||||
-rw-r--r-- | src/libjin/common/subsystem.h | 33 | ||||
-rw-r--r-- | src/libjin/core/game.h | 6 | ||||
-rw-r--r-- | src/libjin/input/joypad.cpp | 0 | ||||
-rw-r--r-- | src/libjin/input/mouse.h | 15 | ||||
-rw-r--r-- | src/libjin/render/drawable.cpp | 2 | ||||
-rw-r--r-- | src/libjin/render/drawable.h | 22 | ||||
-rw-r--r-- | src/libjin/render/jsl.cpp | 100 | ||||
-rw-r--r-- | src/libjin/render/window.cpp | 33 | ||||
-rw-r--r-- | src/libjin/render/window.h | 27 |
11 files changed, 147 insertions, 110 deletions
diff --git a/src/libjin/audio/audio.cpp b/src/libjin/audio/audio.cpp index 88fe52b..f4b64e6 100644 --- a/src/libjin/audio/audio.cpp +++ b/src/libjin/audio/audio.cpp @@ -5,7 +5,10 @@ namespace jin namespace audio { - + onlyonce bool Audio::_init(const SettingBase* setting) + { + return false; + } } }
\ No newline at end of file diff --git a/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index 9356c8a..fd4ab69 100644 --- a/src/libjin/audio/audio.h +++ b/src/libjin/audio/audio.h @@ -1,23 +1,29 @@ #ifndef __JIN_AUDIO_H #define __JIN_AUDIO_H - +#include "../utils/macros.h" +#include "../common/subsystem.h" namespace jin { namespace audio { - class Audio + class Audio : public common::Subsystem { public: - struct Setting + struct Setting : SettingBase { }; - void init(const Setting& setting); + private: + + onlyonce bool _init(const SettingBase* setting) override; + }; + typedef Audio::Setting AudioSetting; + } } diff --git a/src/libjin/common/subsystem.h b/src/libjin/common/subsystem.h new file mode 100644 index 0000000..a6c5099 --- /dev/null +++ b/src/libjin/common/subsystem.h @@ -0,0 +1,33 @@ +#ifndef __JIN_COMMON_SUBSYSTEM_H +#define __JIN_COMMON_SUBSYSTEM_H + +#include "../utils/macros.h" + +namespace jin +{ +namespace common +{ + + class Subsystem + { + public: + + struct Setting {}; + + bool init(const Setting* setting) + { + static bool result = _init(setting); + return result; + } + + typedef Subsystem::Setting SettingBase; + + private: + + virtual onlyonce bool _init(const Setting* setting) = 0; + }; + +} +} + +#endif
\ No newline at end of file diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h index 069566b..e36a71b 100644 --- a/src/libjin/core/game.h +++ b/src/libjin/core/game.h @@ -9,10 +9,10 @@ namespace core { public: - struct Setting - { + struct Setting + { - }; + }; static inline Game* get() { diff --git a/src/libjin/input/joypad.cpp b/src/libjin/input/joypad.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/libjin/input/joypad.cpp diff --git a/src/libjin/input/mouse.h b/src/libjin/input/mouse.h index 55e3346..b926327 100644 --- a/src/libjin/input/mouse.h +++ b/src/libjin/input/mouse.h @@ -10,21 +10,6 @@ namespace input }; - inline const char* buttonStr(int id) { - switch (id) { - case 1: return "left"; - case 2: return "middle"; - case 3: return "right"; - case 4: return "wheelup"; - case 5: return "wheeldown"; - default: return "?"; - } - } - - inline const char* wheelStr(int dir) - { - - } } } #endif
\ No newline at end of file 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<std::string, GLint>::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 <SDL2/SDL.h> #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 |