diff options
Diffstat (limited to 'src')
33 files changed, 305 insertions, 86 deletions
diff --git a/src/libjin/audio/audio.cpp b/src/libjin/audio/audio.cpp index f4b64e6..7142625 100644 --- a/src/libjin/audio/audio.cpp +++ b/src/libjin/audio/audio.cpp @@ -1,13 +1,58 @@ +#include <SDL2/SDL.h> + #include "audio.h" namespace jin { namespace audio { + + shared Audio* Audio::audio = NULL; + + bool Audio::init(const SettingBase* setting) + { + static bool result = _init(setting); + return result; + } + + void Audio::quit() + { + CallOnce(_quit()); + } + + onlyonce bool Audio::_init(const SettingBase* s) + { + if (SDL_Init(SDL_INIT_AUDIO) < 0) + return false; + + const AudioSetting* setting = (AudioSetting*)s; + SDL_AudioSpec wanted; + zero(wanted); + wanted.freq = setting->freq; + wanted.format = setting->format; + wanted.channels = setting->channels; + wanted.samples = setting->samples; + wanted.userdata = setting->userdata; + wanted.callback = setting->callback; + + if (SDL_OpenAudio(&wanted, NULL) < 0) + { + return false; + } + // start audio + SDL_PauseAudio(0); + return true; + } + + onlyonce void Audio::_quit() + { + SDL_CloseAudio(); + delete audio; + } - onlyonce bool Audio::_init(const SettingBase* setting) + shared void defaultCallback(void *udata, Uint8 *stream, int len) { - return false; + } } diff --git a/src/libjin/audio/audio.h b/src/libjin/audio/audio.h index fd4ab69..80e463f 100644 --- a/src/libjin/audio/audio.h +++ b/src/libjin/audio/audio.h @@ -1,5 +1,8 @@ #ifndef __JIN_AUDIO_H #define __JIN_AUDIO_H + +#include <SDL2/SDL.h> + #include "../utils/macros.h" #include "../common/subsystem.h" @@ -11,15 +14,36 @@ namespace audio class Audio : public common::Subsystem { public: + struct Setting : SettingBase { - + int freq; + int format; + char channels; + int samples; + SDL_AudioCallback callback; + void* userdata; }; + + bool init(const SettingBase* setting) override; + void quit() override; - private: + static inline Audio* get() + { + return (audio == NULL ? (audio = new Audio()) : audio); + } - onlyonce bool _init(const SettingBase* setting) override; + static void defaultCallback(void *udata, Uint8 *stream, int len); + private: + + Audio() {}; + ~Audio() {}; + + static Audio* audio; + + onlyonce bool _init(const SettingBase* setting) override; + onlyonce void _quit() override; }; typedef Audio::Setting AudioSetting; diff --git a/src/libjin/audio/source.h b/src/libjin/audio/source.h index a8e40fe..96da208 100644 --- a/src/libjin/audio/source.h +++ b/src/libjin/audio/source.h @@ -22,6 +22,9 @@ namespace audio void setPitch(float pitch); void setVolume(float volume); + private: + unsigned char * curPos; + unsigned int length; }; } diff --git a/src/libjin/common/subsystem.h b/src/libjin/common/subsystem.h index a6c5099..ad28061 100644 --- a/src/libjin/common/subsystem.h +++ b/src/libjin/common/subsystem.h @@ -8,23 +8,23 @@ namespace jin namespace common { - class Subsystem + class Subsystem { public: struct Setting {}; - bool init(const Setting* setting) - { - static bool result = _init(setting); - return result; - } - typedef Subsystem::Setting SettingBase; + virtual bool init(const Setting* setting) = 0; + + virtual void quit() = 0; + private: virtual onlyonce bool _init(const Setting* setting) = 0; + + virtual onlyonce void _quit() = 0; }; } diff --git a/src/libjin/core/game.h b/src/libjin/core/game.h index e36a71b..cd99b42 100644 --- a/src/libjin/core/game.h +++ b/src/libjin/core/game.h @@ -1,6 +1,10 @@ #ifndef __JIN_CORE_GAME_H #define __JIN_CORE_GAME_H +#include <SDL2/SDL.h> + +#include "../utils/macros.h" + namespace jin { namespace core @@ -19,9 +23,9 @@ namespace core return g_game ? g_game : (g_game = new Game()); } - inline void quit() + inline void quit() // quit game loop { - run = false; + CallOnce(_quit()); } inline bool running() @@ -29,6 +33,11 @@ namespace core return run; } + inline void exit() // exit game + { + CallOnce(_exit()); + } + private: Game(); @@ -36,6 +45,16 @@ namespace core static Game* g_game; bool run; + + inline void _exit() + { + SDL_Quit(); + } + + inline void _quit() + { + run = false; + } }; } } diff --git a/src/libjin/debug/debug.h b/src/libjin/debug/debug.h new file mode 100644 index 0000000..9fa9fe1 --- /dev/null +++ b/src/libjin/debug/debug.h @@ -0,0 +1,6 @@ +#ifndef __JIN_DEBUG_H +#define __JIN_DEBUG_H + + + +#endif
\ No newline at end of file diff --git a/src/libjin/debug/log.h b/src/libjin/debug/log.h new file mode 100644 index 0000000..e1624f5 --- /dev/null +++ b/src/libjin/debug/log.h @@ -0,0 +1,14 @@ +#ifndef __JIN_LOG_H +#define __JIN_LOG_H + +namespace jin +{ +namespace debug +{ + + const char* err; + +} +} + +#endif
\ No newline at end of file diff --git a/src/libjin/math/constant.h b/src/libjin/math/constant.h new file mode 100644 index 0000000..74acaea --- /dev/null +++ b/src/libjin/math/constant.h @@ -0,0 +1,6 @@ +#ifndef __JIN_MATH_CONSTANT_H +#define __JIN_MATH_CONSTANT_H + +#define PI 3.1415926f + +#endif
\ No newline at end of file diff --git a/src/libjin/math/math.h b/src/libjin/math/math.h new file mode 100644 index 0000000..fdf1725 --- /dev/null +++ b/src/libjin/math/math.h @@ -0,0 +1,11 @@ +#ifndef __JIN_UTILS_MATH_H +#define __JIN_UTILS_MATH_H + +#include <math.h> + +#include "constant.h" +#include "matrix.h" +#include "quad.h" +#include "rect.h" + +#endif
\ No newline at end of file diff --git a/src/libjin/utils/matrix.cpp b/src/libjin/math/matrix.cpp index b970ec0..97e9178 100644 --- a/src/libjin/utils/matrix.cpp +++ b/src/libjin/math/matrix.cpp @@ -5,7 +5,7 @@ namespace jin { -namespace util +namespace math { // | e0 e4 e8 e12 | diff --git a/src/libjin/utils/matrix.h b/src/libjin/math/matrix.h index 51d7980..673d253 100644 --- a/src/libjin/utils/matrix.h +++ b/src/libjin/math/matrix.h @@ -3,7 +3,7 @@ #include <math.h> namespace jin { -namespace util +namespace math { struct vertex diff --git a/src/libjin/render/quad.h b/src/libjin/math/quad.h index 3ae4cc4..a50cc9e 100644 --- a/src/libjin/render/quad.h +++ b/src/libjin/math/quad.h @@ -3,7 +3,7 @@ namespace jin { -namespace render +namespace math { struct Quad @@ -14,4 +14,4 @@ namespace render } } -#endif // !__JIN_RENDER_QUAD_H +#endif diff --git a/src/libjin/math/rect.h b/src/libjin/math/rect.h new file mode 100644 index 0000000..79c1df2 --- /dev/null +++ b/src/libjin/math/rect.h @@ -0,0 +1,15 @@ +#ifndef __JIN_RECT_H +#define __JIN_RECT_H + +namespace jin +{ +namespace math +{ + class Rect + { + public: + int x, y, w, h; + }; +} +} +#endif
\ No newline at end of file diff --git a/src/libjin/math/vector.cpp b/src/libjin/math/vector.cpp new file mode 100644 index 0000000..f26d0c4 --- /dev/null +++ b/src/libjin/math/vector.cpp @@ -0,0 +1,2 @@ +#include "vector.h" + diff --git a/src/libjin/math/vector.h b/src/libjin/math/vector.h new file mode 100644 index 0000000..43e249e --- /dev/null +++ b/src/libjin/math/vector.h @@ -0,0 +1,14 @@ +#ifndef __JIN_VECTOR_H +#define __JIN_VECTOR_H + +namespace jin +{ +namespace math +{ + + + +} +} + +#endif
\ No newline at end of file diff --git a/src/libjin/render/drawable.cpp b/src/libjin/render/drawable.cpp index 4ac4f03..7880fd4 100644 --- a/src/libjin/render/drawable.cpp +++ b/src/libjin/render/drawable.cpp @@ -1,5 +1,5 @@ #include "drawable.h" -#include "../utils/matrix.h" +#include "../math/matrix.h" #include <stdlib.h> namespace jin @@ -41,7 +41,7 @@ namespace render // Must set textCoord and vertCoord before renderring if (! textCoord||! vertCoord) return; - static jin::util::Matrix t; + static jin::math::Matrix t; t.setTransformation(x, y, r, sx, sy, ancx, ancy); glEnable(GL_TEXTURE_2D); diff --git a/src/libjin/render/font.cpp b/src/libjin/render/font.cpp index 8a96f25..fa3e265 100644 --- a/src/libjin/render/font.cpp +++ b/src/libjin/render/font.cpp @@ -11,6 +11,8 @@ namespace jin namespace render { + using namespace jin::math; + #define BITMAP_WIDTH 512 #define BITMAP_HEIGHT 512 #define PIXEL_HEIGHT 32 diff --git a/src/libjin/render/font.h b/src/libjin/render/font.h index 98a41b4..55f0a44 100644 --- a/src/libjin/render/font.h +++ b/src/libjin/render/font.h @@ -3,7 +3,7 @@ #include "drawable.h" #include "3rdparty/stb/stb_truetype.h" -#include "quad.h" +#include "../math/quad.h" namespace jin { diff --git a/src/libjin/render/graphics.cpp b/src/libjin/render/graphics.cpp index 254b6a4..a4ccd66 100644 --- a/src/libjin/render/graphics.cpp +++ b/src/libjin/render/graphics.cpp @@ -1,5 +1,6 @@ #include "graphics.h" -#include "../utils/math.h" +#include "../math/matrix.h" +#include "../math/constant.h" #include <string> namespace jin { diff --git a/src/libjin/render/jsl.cpp b/src/libjin/render/jsl.cpp index 3702e14..e883104 100644 --- a/src/libjin/render/jsl.cpp +++ b/src/libjin/render/jsl.cpp @@ -6,25 +6,23 @@ namespace render { //vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) static const char* base_f = " " - "#version 120 \n" - "#define number float \n" - "#define Image sampler2D \n" - "#define Canvas sampler2D \n" - "#define Color vec4 \n" - "#define Texel texture2D \n" - "#define extern uniform \n" - "uniform Image _tex0_; \n" - "%s \n" - "void main(){ \n" + "#version 120 \n" + "#define number float \n" + "#define Image sampler2D \n" + "#define Canvas sampler2D \n" + "#define Color vec4 \n" + "#define Texel texture2D \n" + "#define extern uniform \n" + "uniform Image _tex0_; \n" + "%s \n" + "void main(){ \n" "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 JSLProgram* JSLProgram::currentJSLProgram = nullptr; JSLProgram::JSLProgram(const char* program) + : currentTextureUnit(0) { initialize(program); } @@ -42,9 +40,6 @@ namespace render inline void JSLProgram::initialize(const char* program) { - if (maxTextureUnits == -1) - glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); - char* fs = (char*)alloca(strlen(program) + strlen(base_f)); sprintf(fs, base_f, program); GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); @@ -55,12 +50,20 @@ namespace render glAttachShader(pid, fragmentShader); glLinkProgram(pid); } + + static inline GLint getMaxTextureUnits() + { + GLint maxTextureUnits = 0; + glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureUnits); + return maxTextureUnits; + } GLint JSLProgram::getTextureUnit(const std::string& name) { std::map<std::string, GLint>::iterator texture_unit = texturePool.find(name); if (texture_unit != texturePool.end()) return texture_unit->second; + static GLint maxTextureUnits = getMaxTextureUnits(); if (++currentTextureUnit >= maxTextureUnits) return 0; texturePool[name] = currentTextureUnit; @@ -81,14 +84,13 @@ namespace render { checkJSL(); - GLint texture_unit = JSLProgram::getTextureUnit(variable); - GLint location = glGetUniformLocation(pid, variable); + if (location == -1) + return; + GLint texture_unit = getTextureUnit(variable); glUniform1i(location, texture_unit); - glActiveTexture(GL_TEXTURE0 + texture_unit); - glBindTexture(GL_TEXTURE_2D, image->getTexture()); - + glBindTexture(GL_TEXTURE_2D, image->getTexture()); glActiveTexture(GL_TEXTURE0); } @@ -96,14 +98,13 @@ namespace render { checkJSL(); - GLint texture_unit = getTextureUnit(variable); - GLint location = glGetUniformLocation(pid, variable); + if (location == -1) + return; + GLint texture_unit = getTextureUnit(variable); glUniform1i(location, texture_unit); - glActiveTexture(GL_TEXTURE0 + texture_unit); glBindTexture(GL_TEXTURE_2D, canvas->getTexture()); - glActiveTexture(GL_TEXTURE0); } diff --git a/src/libjin/render/jsl.h b/src/libjin/render/jsl.h index 35479d3..fc1aa48 100644 --- a/src/libjin/render/jsl.h +++ b/src/libjin/render/jsl.h @@ -24,14 +24,12 @@ namespace render { glUseProgram(pid); currentJSLProgram = this; - currentTextureUnit = 0; } static inline void JSLProgram::unuse() { glUseProgram(0); currentJSLProgram = nullptr; - currentTextureUnit = 0; } void sendFloat(const char* name, float number); @@ -49,14 +47,13 @@ namespace render private: + static JSLProgram* currentJSLProgram; + GLuint pid; std::map<std::string, GLint> texturePool; - static JSLProgram* currentJSLProgram; - static GLint currentTextureUnit; - static GLint maxTextureUnits; - + GLint currentTextureUnit; GLint getTextureUnit(const std::string& name); inline void initialize(const char* program); diff --git a/src/libjin/render/rect.h b/src/libjin/render/rect.h deleted file mode 100644 index 56b5bd1..0000000 --- a/src/libjin/render/rect.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef __JIN_RECT_H -#define __JIN_RECT_H - -namespace jin -{ -class Rect -{ -public: - int x, y, w, h; -}; -}// jin -#endif
\ No newline at end of file diff --git a/src/libjin/render/render.h b/src/libjin/render/render.h index 8939480..6c0d6bf 100644 --- a/src/libjin/render/render.h +++ b/src/libjin/render/render.h @@ -7,8 +7,6 @@ #include "graphics.h" #include "image.h" #include "jsl.h" -#include "quad.h" -#include "rect.h" #include "window.h" #endif
\ No newline at end of file diff --git a/src/libjin/render/window.cpp b/src/libjin/render/window.cpp index 1f55be8..1e58dd8 100644 --- a/src/libjin/render/window.cpp +++ b/src/libjin/render/window.cpp @@ -8,8 +8,8 @@ namespace jin namespace render { - shared Window* Window::g_wnd = 0; - + shared Window* Window::g_wnd = NULL; + Window::Window(): wnd(0), ctx(0) { } @@ -18,12 +18,23 @@ namespace render { } + bool Window::init(const SettingBase* setting) + { + static bool result = _init(setting); + return result; + } + + void Window::quit() + { + CallOnce(_quit()); + } + onlyonce bool Window::_init(const SettingBase* s) { if (SDL_Init(SDL_INIT_VIDEO) < 0) return false; - WindowSetting* setting = (WindowSetting*)s; + const WindowSetting* setting = (WindowSetting*)s; width = setting->width; height = setting->height; @@ -73,6 +84,12 @@ namespace render return true; } + onlyonce void Window::_quit() + { + SDL_DestroyWindow(wnd); + delete g_wnd; + } + SDL_Window* Window::getWnd() { return wnd; diff --git a/src/libjin/render/window.h b/src/libjin/render/window.h index 1064c36..1aabfa0 100644 --- a/src/libjin/render/window.h +++ b/src/libjin/render/window.h @@ -42,6 +42,9 @@ namespace render inline void swapBuffers(); + bool init(const SettingBase* setting) override; + void quit() override; + private: Window(); @@ -56,7 +59,7 @@ namespace render int width, height; onlyonce bool _init(const SettingBase* setting) override; - + onlyonce void _quit() override; }; typedef Window::Setting WindowSetting; diff --git a/src/libjin/utils/macros.h b/src/libjin/utils/macros.h index 2ca8a9a..cdec403 100644 --- a/src/libjin/utils/macros.h +++ b/src/libjin/utils/macros.h @@ -1,9 +1,12 @@ #ifndef __JIN_MACROS_H #define __JIN_MACROS_H +#include <cstring> -#define shared +#define shared // ķ -#define CallOnce(func) static char __dummy__=(func, 1) // ֻܵһ -#define onlyonce // ֻܵһ +#define CallOnce(func) static char __dummy__=(func, 1) // ֻһ +#define onlyonce // ֻһ + +#define zero(mem) memset(&mem, 0, sizeof(mem)) #endif
\ No newline at end of file diff --git a/src/libjin/utils/math.h b/src/libjin/utils/math.h deleted file mode 100644 index 5e44ce7..0000000 --- a/src/libjin/utils/math.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __JIN_UTILS_MATH_H -#define __JIN_UTILS_MATH_H - -#include <math.h> - -#define PI 3.1415926f - -#endif
\ No newline at end of file diff --git a/src/libjin/utils/unittest.cpp b/src/libjin/utils/unittest.cpp new file mode 100644 index 0000000..764a2bd --- /dev/null +++ b/src/libjin/utils/unittest.cpp @@ -0,0 +1,34 @@ +#include "utils.h" +#if UNITTEST + +#include <stdio.h> +#include "../audio/audio.h" + +using namespace jin::audio; + +void fill_audio(void *udata, Uint8 *stream, int len) +{ + printf("%d\n", len); + memset(stream, 0x11, len); +} + +int main(int argc, char* argv[]) +{ + Audio* audio = Audio::get(); + + AudioSetting setting; + setting.freq = 22050; + setting.format = AUDIO_S16; + setting.channels = 2; + setting.callback = fill_audio; + setting.samples = 1024; + setting.userdata = NULL; + audio->init(&setting); + while (true) + { + SDL_Delay(100); + } + return 0; +} + +#endif diff --git a/src/libjin/utils/utils.h b/src/libjin/utils/utils.h index 45c8ff9..1a4ef35 100644 --- a/src/libjin/utils/utils.h +++ b/src/libjin/utils/utils.h @@ -10,7 +10,7 @@ #include "macros.h" #include "endian.h" -#include "math.h" -#include "matrix.h" + +#define UNITTEST 1 #endif
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9b0da69..4617beb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,8 @@ #include <Windows.h> +#if !UNITTEST + int main(int argc, char* argv[]) { // global lua state, all lua values are here @@ -48,3 +50,5 @@ int main(int argc, char* argv[]) return 0; } + +#endif // unit test
\ No newline at end of file diff --git a/src/script/core/luaopen_core.cpp b/src/script/core/luaopen_core.cpp index 0d76ada..a222013 100644 --- a/src/script/core/luaopen_core.cpp +++ b/src/script/core/luaopen_core.cpp @@ -11,7 +11,7 @@ namespace lua { bool running = Game::get()->running(); luax_pushboolean(L, running); - return 1; + return 1; } static int l_quit(lua_State* L) @@ -19,10 +19,17 @@ namespace lua Game::get()->quit(); return 0; } + + static int l_exit(lua_State* L) + { + Game::get()->exit(); + return 0; + } static const luaL_Reg f[] = { {"running", l_running}, - {"quit", l_quit}, + {"quit", l_quit}, // for end game loop + {"exit", l_exit}, // for exit whole game {0, 0} }; diff --git a/src/script/embed/boot.lua.h b/src/script/embed/boot.lua.h index cb61468..21c1899 100644 --- a/src/script/embed/boot.lua.h +++ b/src/script/embed/boot.lua.h @@ -145,6 +145,10 @@ local function main() end jin.core.run() end + -- quit subsystems + jin.graphics.destroy() + -- exit whole game + jin.core.exit() end main() diff --git a/src/script/graphics/luaopen_graphics.cpp b/src/script/graphics/luaopen_graphics.cpp index 948e28a..40bdaa1 100644 --- a/src/script/graphics/luaopen_graphics.cpp +++ b/src/script/graphics/luaopen_graphics.cpp @@ -38,6 +38,13 @@ namespace lua luax_pushboolean(L, wnd->init(&setting)); return 1; } + + static int l_destroy(lua_State* L) + { + Window* wnd = Window::get(); + wnd->quit(); + return 0; + } /** * Get windows size. @@ -488,6 +495,8 @@ namespace lua {"circle", l_drawCircle}, {"triangle", l_drawTriangle}, {"polygon", l_drawPolygon}, + // + {"destroy", l_destroy}, {0, 0} }; |