From 51ba9cb2a6b0b9395a2912eadeb954c95e4c1d3c Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 20 May 2018 23:37:11 +0800 Subject: =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libjin/audio/audio.cpp | 49 ++++++++- src/libjin/audio/audio.h | 30 +++++- src/libjin/audio/source.h | 3 + src/libjin/common/subsystem.h | 14 +-- src/libjin/core/game.h | 23 +++- src/libjin/debug/debug.h | 6 ++ src/libjin/debug/log.h | 14 +++ src/libjin/math/constant.h | 6 ++ src/libjin/math/math.h | 11 ++ src/libjin/math/matrix.cpp | 177 +++++++++++++++++++++++++++++++ src/libjin/math/matrix.h | 153 ++++++++++++++++++++++++++ src/libjin/math/quad.h | 17 +++ src/libjin/math/rect.h | 15 +++ src/libjin/math/vector.cpp | 2 + src/libjin/math/vector.h | 14 +++ src/libjin/render/drawable.cpp | 4 +- src/libjin/render/font.cpp | 2 + src/libjin/render/font.h | 2 +- src/libjin/render/graphics.cpp | 3 +- src/libjin/render/jsl.cpp | 51 ++++----- src/libjin/render/jsl.h | 9 +- src/libjin/render/quad.h | 17 --- src/libjin/render/rect.h | 12 --- src/libjin/render/render.h | 2 - src/libjin/render/window.cpp | 23 +++- src/libjin/render/window.h | 5 +- src/libjin/utils/macros.h | 9 +- src/libjin/utils/math.h | 8 -- src/libjin/utils/matrix.cpp | 177 ------------------------------- src/libjin/utils/matrix.h | 153 -------------------------- src/libjin/utils/unittest.cpp | 34 ++++++ src/libjin/utils/utils.h | 4 +- src/main.cpp | 4 + src/script/core/luaopen_core.cpp | 11 +- src/script/embed/boot.lua.h | 4 + src/script/graphics/luaopen_graphics.cpp | 9 ++ 36 files changed, 648 insertions(+), 429 deletions(-) create mode 100644 src/libjin/debug/debug.h create mode 100644 src/libjin/debug/log.h create mode 100644 src/libjin/math/constant.h create mode 100644 src/libjin/math/math.h create mode 100644 src/libjin/math/matrix.cpp create mode 100644 src/libjin/math/matrix.h create mode 100644 src/libjin/math/quad.h create mode 100644 src/libjin/math/rect.h create mode 100644 src/libjin/math/vector.cpp create mode 100644 src/libjin/math/vector.h delete mode 100644 src/libjin/render/quad.h delete mode 100644 src/libjin/render/rect.h delete mode 100644 src/libjin/utils/math.h delete mode 100644 src/libjin/utils/matrix.cpp delete mode 100644 src/libjin/utils/matrix.h create mode 100644 src/libjin/utils/unittest.cpp (limited to 'src') 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 + #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 + #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 + +#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 + +#include "constant.h" +#include "matrix.h" +#include "quad.h" +#include "rect.h" + +#endif \ No newline at end of file diff --git a/src/libjin/math/matrix.cpp b/src/libjin/math/matrix.cpp new file mode 100644 index 0000000..97e9178 --- /dev/null +++ b/src/libjin/math/matrix.cpp @@ -0,0 +1,177 @@ +#include "Matrix.h" + +#include // memcpy +#include + +namespace jin +{ +namespace math +{ + + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + + Matrix::Matrix() + { + setIdentity(); + } + + Matrix::~Matrix() + { + } + + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + + Matrix Matrix::operator * (const Matrix & m) const + { + Matrix t; + + t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]); + t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]); + t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]); + t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]); + + t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]); + t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]); + t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]); + t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]); + + t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]); + t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]); + t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]); + t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]); + + t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]); + t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]); + t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]); + t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]); + + return t; + } + + void Matrix::operator *= (const Matrix & m) + { + Matrix t = (*this) * m; + memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16); + } + + const float * Matrix::getElements() const + { + return e; + } + + void Matrix::setIdentity() + { + memset(e, 0, sizeof(float) * 16); + e[0] = e[5] = e[10] = e[15] = 1; + } + + void Matrix::setTranslation(float x, float y) + { + setIdentity(); + e[12] = x; + e[13] = y; + } + + void Matrix::setRotation(float rad) + { + setIdentity(); + float c = cos(rad), s = sin(rad); + e[0] = c; e[4] = -s; + e[1] = s; e[5] = c; + } + + void Matrix::setScale(float sx, float sy) + { + setIdentity(); + e[0] = sx; + e[5] = sy; + } + + void Matrix::setShear(float kx, float ky) + { + setIdentity(); + e[1] = ky; + e[4] = kx; + } + + void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy) + { + memset(e, 0, sizeof(float) * 16); // zero out matrix + float c = cos(angle), s = sin(angle); + // matrix multiplication carried out on paper: + // |1 x| |c -s | |sx | |1 -ox| + // | 1 y| |s c | | sy | | 1 -oy| + // | 1 | | 1 | | 1 | | 1 | + // | 1| | 1| | 1| | 1 | + // move rotate scale origin + e[10] = e[15] = 1.0f; + e[0] = c * sx ; // = a + e[1] = s * sx ; // = b + e[4] = - s * sy; // = c + e[5] = c * sy; // = d + e[12] = x - ox * e[0] - oy * e[4]; + e[13] = y - ox * e[1] - oy * e[5]; + } + + void Matrix::translate(float x, float y) + { + Matrix t; + t.setTranslation(x, y); + this->operator *=(t); + } + + void Matrix::rotate(float rad) + { + Matrix t; + t.setRotation(rad); + this->operator *=(t); + } + + void Matrix::scale(float sx, float sy) + { + Matrix t; + t.setScale(sx, sy); + this->operator *=(t); + } + + void Matrix::shear(float kx, float ky) + { + Matrix t; + t.setShear(kx, ky); + this->operator *=(t); + } + + // | x | + // | y | + // | 0 | + // | 1 | + // | e0 e4 e8 e12 | + // | e1 e5 e9 e13 | + // | e2 e6 e10 e14 | + // | e3 e7 e11 e15 | + + void Matrix::transform(vertex * dst, const vertex * src, int size) const + { + for (int i = 0; i +namespace jin +{ +namespace math +{ + + struct vertex + { + unsigned char r, g, b, a; + float x, y; + float s, t; + }; + /** + * This class is the basis for all transformations in LOVE. Althought not + * really needed for 2D, it contains 4x4 elements to be compatible with + * OpenGL without conversions. + **/ + class Matrix + { + private: + + /** + * | e0 e4 e8 e12 | + * | e1 e5 e9 e13 | + * | e2 e6 e10 e14 | + * | e3 e7 e11 e15 | + **/ + float e[16]; + + public: + + /** + * Creates a new identity matrix. + **/ + Matrix(); + + /** + * Destructor. + **/ + ~Matrix(); + + /** + * Multiplies this Matrix with another Matrix, changing neither. + * @param m The Matrix to multiply with this Matrix. + * @return The combined matrix. + **/ + Matrix operator * (const Matrix & m) const; + + /** + * Multiplies a Matrix into this Matrix. + * @param m The Matrix to combine into this Matrix. + **/ + void operator *= (const Matrix & m); + + /** + * Gets a pointer to the 16 array elements. + * @return The array elements. + **/ + const float * getElements() const; + + /** + * Resets this Matrix to the identity matrix. + **/ + void setIdentity(); + + /** + * Resets this Matrix to a translation. + * @param x Translation along x-axis. + * @param y Translation along y-axis. + **/ + void setTranslation(float x, float y); + + /** + * Resets this Matrix to a rotation. + * @param r The angle in radians. + **/ + void setRotation(float r); + + /** + * Resets this Matrix to a scale transformation. + * @param sx Scale factor along the x-axis. + * @param sy Scale factor along the y-axis. + **/ + void setScale(float sx, float sy); + + /** + * Resets this Matrix to a shear transformation. + * @param kx Shear along x-axis. + * @param ky Shear along y-axis. + **/ + void setShear(float kx, float ky); + + /** + * Creates a transformation with a certain position, orientation, scale + * and offset. Perfect for Drawables -- what a coincidence! + * + * @param x The translation along the x-axis. + * @param y The translation along the y-axis. + * @param angle The rotation (rad) around the center with offset (ox,oy). + * @param sx Scale along x-axis. + * @param sy Scale along y-axis. + * @param ox The offset for rotation along the x-axis. + * @param oy The offset for rotation along the y-axis. + * @param kx Shear along x-axis + * @param ky Shear along y-axis + **/ + void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy); + + /** + * Multiplies this Matrix with a translation. + * @param x Translation along x-axis. + * @param y Translation along y-axis. + **/ + void translate(float x, float y); + + /** + * Multiplies this Matrix with a rotation. + * @param r Angle in radians. + **/ + void rotate(float r); + + /** + * Multiplies this Matrix with a scale transformation. + * @param sx Scale factor along the x-axis. + * @param sy Scale factor along the y-axis. + **/ + void scale(float sx, float sy); + + /** + * Multiplies this Matrix with a shear transformation. + * @param kx Shear along the x-axis. + * @param ky Shear along the y-axis. + **/ + void shear(float kx, float ky); + + /** + * Transforms an array of vertices by this Matrix. The sources and + * destination arrays may be the same. + * + * @param dst Storage for the transformed vertices. + * @param src The source vertices. + * @param size The number of vertices. + **/ + void transform(vertex * dst, const vertex * src, int size) const; + + }; + +} +} + +#endif diff --git a/src/libjin/math/quad.h b/src/libjin/math/quad.h new file mode 100644 index 0000000..a50cc9e --- /dev/null +++ b/src/libjin/math/quad.h @@ -0,0 +1,17 @@ +#ifndef __JIN_QUAD_H +#define __JIN_QUAD_H + +namespace jin +{ +namespace math +{ + + struct Quad + { + float x, y, w, 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 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 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::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 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/quad.h b/src/libjin/render/quad.h deleted file mode 100644 index 3ae4cc4..0000000 --- a/src/libjin/render/quad.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef __JIN_QUAD_H -#define __JIN_QUAD_H - -namespace jin -{ -namespace render -{ - - struct Quad - { - float x, y, w, h; - }; - -} -} - -#endif // !__JIN_RENDER_QUAD_H 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 -#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 - -#define PI 3.1415926f - -#endif \ No newline at end of file diff --git a/src/libjin/utils/matrix.cpp b/src/libjin/utils/matrix.cpp deleted file mode 100644 index b970ec0..0000000 --- a/src/libjin/utils/matrix.cpp +++ /dev/null @@ -1,177 +0,0 @@ -#include "Matrix.h" - -#include // memcpy -#include - -namespace jin -{ -namespace util -{ - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix::Matrix() - { - setIdentity(); - } - - Matrix::~Matrix() - { - } - - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - Matrix Matrix::operator * (const Matrix & m) const - { - Matrix t; - - t.e[0] = (e[0] * m.e[0]) + (e[4] * m.e[1]) + (e[8] * m.e[2]) + (e[12] * m.e[3]); - t.e[4] = (e[0] * m.e[4]) + (e[4] * m.e[5]) + (e[8] * m.e[6]) + (e[12] * m.e[7]); - t.e[8] = (e[0] * m.e[8]) + (e[4] * m.e[9]) + (e[8] * m.e[10]) + (e[12] * m.e[11]); - t.e[12] = (e[0] * m.e[12]) + (e[4] * m.e[13]) + (e[8] * m.e[14]) + (e[12] * m.e[15]); - - t.e[1] = (e[1] * m.e[0]) + (e[5] * m.e[1]) + (e[9] * m.e[2]) + (e[13] * m.e[3]); - t.e[5] = (e[1] * m.e[4]) + (e[5] * m.e[5]) + (e[9] * m.e[6]) + (e[13] * m.e[7]); - t.e[9] = (e[1] * m.e[8]) + (e[5] * m.e[9]) + (e[9] * m.e[10]) + (e[13] * m.e[11]); - t.e[13] = (e[1] * m.e[12]) + (e[5] * m.e[13]) + (e[9] * m.e[14]) + (e[13] * m.e[15]); - - t.e[2] = (e[2] * m.e[0]) + (e[6] * m.e[1]) + (e[10] * m.e[2]) + (e[14] * m.e[3]); - t.e[6] = (e[2] * m.e[4]) + (e[6] * m.e[5]) + (e[10] * m.e[6]) + (e[14] * m.e[7]); - t.e[10] = (e[2] * m.e[8]) + (e[6] * m.e[9]) + (e[10] * m.e[10]) + (e[14] * m.e[11]); - t.e[14] = (e[2] * m.e[12]) + (e[6] * m.e[13]) + (e[10] * m.e[14]) + (e[14] * m.e[15]); - - t.e[3] = (e[3] * m.e[0]) + (e[7] * m.e[1]) + (e[11] * m.e[2]) + (e[15] * m.e[3]); - t.e[7] = (e[3] * m.e[4]) + (e[7] * m.e[5]) + (e[11] * m.e[6]) + (e[15] * m.e[7]); - t.e[11] = (e[3] * m.e[8]) + (e[7] * m.e[9]) + (e[11] * m.e[10]) + (e[15] * m.e[11]); - t.e[15] = (e[3] * m.e[12]) + (e[7] * m.e[13]) + (e[11] * m.e[14]) + (e[15] * m.e[15]); - - return t; - } - - void Matrix::operator *= (const Matrix & m) - { - Matrix t = (*this) * m; - memcpy((void*)this->e, (void*)t.e, sizeof(float) * 16); - } - - const float * Matrix::getElements() const - { - return e; - } - - void Matrix::setIdentity() - { - memset(e, 0, sizeof(float) * 16); - e[0] = e[5] = e[10] = e[15] = 1; - } - - void Matrix::setTranslation(float x, float y) - { - setIdentity(); - e[12] = x; - e[13] = y; - } - - void Matrix::setRotation(float rad) - { - setIdentity(); - float c = cos(rad), s = sin(rad); - e[0] = c; e[4] = -s; - e[1] = s; e[5] = c; - } - - void Matrix::setScale(float sx, float sy) - { - setIdentity(); - e[0] = sx; - e[5] = sy; - } - - void Matrix::setShear(float kx, float ky) - { - setIdentity(); - e[1] = ky; - e[4] = kx; - } - - void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy) - { - memset(e, 0, sizeof(float) * 16); // zero out matrix - float c = cos(angle), s = sin(angle); - // matrix multiplication carried out on paper: - // |1 x| |c -s | |sx | |1 -ox| - // | 1 y| |s c | | sy | | 1 -oy| - // | 1 | | 1 | | 1 | | 1 | - // | 1| | 1| | 1| | 1 | - // move rotate scale origin - e[10] = e[15] = 1.0f; - e[0] = c * sx ; // = a - e[1] = s * sx ; // = b - e[4] = - s * sy; // = c - e[5] = c * sy; // = d - e[12] = x - ox * e[0] - oy * e[4]; - e[13] = y - ox * e[1] - oy * e[5]; - } - - void Matrix::translate(float x, float y) - { - Matrix t; - t.setTranslation(x, y); - this->operator *=(t); - } - - void Matrix::rotate(float rad) - { - Matrix t; - t.setRotation(rad); - this->operator *=(t); - } - - void Matrix::scale(float sx, float sy) - { - Matrix t; - t.setScale(sx, sy); - this->operator *=(t); - } - - void Matrix::shear(float kx, float ky) - { - Matrix t; - t.setShear(kx, ky); - this->operator *=(t); - } - - // | x | - // | y | - // | 0 | - // | 1 | - // | e0 e4 e8 e12 | - // | e1 e5 e9 e13 | - // | e2 e6 e10 e14 | - // | e3 e7 e11 e15 | - - void Matrix::transform(vertex * dst, const vertex * src, int size) const - { - for (int i = 0; i -namespace jin -{ -namespace util -{ - - struct vertex - { - unsigned char r, g, b, a; - float x, y; - float s, t; - }; - /** - * This class is the basis for all transformations in LOVE. Althought not - * really needed for 2D, it contains 4x4 elements to be compatible with - * OpenGL without conversions. - **/ - class Matrix - { - private: - - /** - * | e0 e4 e8 e12 | - * | e1 e5 e9 e13 | - * | e2 e6 e10 e14 | - * | e3 e7 e11 e15 | - **/ - float e[16]; - - public: - - /** - * Creates a new identity matrix. - **/ - Matrix(); - - /** - * Destructor. - **/ - ~Matrix(); - - /** - * Multiplies this Matrix with another Matrix, changing neither. - * @param m The Matrix to multiply with this Matrix. - * @return The combined matrix. - **/ - Matrix operator * (const Matrix & m) const; - - /** - * Multiplies a Matrix into this Matrix. - * @param m The Matrix to combine into this Matrix. - **/ - void operator *= (const Matrix & m); - - /** - * Gets a pointer to the 16 array elements. - * @return The array elements. - **/ - const float * getElements() const; - - /** - * Resets this Matrix to the identity matrix. - **/ - void setIdentity(); - - /** - * Resets this Matrix to a translation. - * @param x Translation along x-axis. - * @param y Translation along y-axis. - **/ - void setTranslation(float x, float y); - - /** - * Resets this Matrix to a rotation. - * @param r The angle in radians. - **/ - void setRotation(float r); - - /** - * Resets this Matrix to a scale transformation. - * @param sx Scale factor along the x-axis. - * @param sy Scale factor along the y-axis. - **/ - void setScale(float sx, float sy); - - /** - * Resets this Matrix to a shear transformation. - * @param kx Shear along x-axis. - * @param ky Shear along y-axis. - **/ - void setShear(float kx, float ky); - - /** - * Creates a transformation with a certain position, orientation, scale - * and offset. Perfect for Drawables -- what a coincidence! - * - * @param x The translation along the x-axis. - * @param y The translation along the y-axis. - * @param angle The rotation (rad) around the center with offset (ox,oy). - * @param sx Scale along x-axis. - * @param sy Scale along y-axis. - * @param ox The offset for rotation along the x-axis. - * @param oy The offset for rotation along the y-axis. - * @param kx Shear along x-axis - * @param ky Shear along y-axis - **/ - void setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy); - - /** - * Multiplies this Matrix with a translation. - * @param x Translation along x-axis. - * @param y Translation along y-axis. - **/ - void translate(float x, float y); - - /** - * Multiplies this Matrix with a rotation. - * @param r Angle in radians. - **/ - void rotate(float r); - - /** - * Multiplies this Matrix with a scale transformation. - * @param sx Scale factor along the x-axis. - * @param sy Scale factor along the y-axis. - **/ - void scale(float sx, float sy); - - /** - * Multiplies this Matrix with a shear transformation. - * @param kx Shear along the x-axis. - * @param ky Shear along the y-axis. - **/ - void shear(float kx, float ky); - - /** - * Transforms an array of vertices by this Matrix. The sources and - * destination arrays may be the same. - * - * @param dst Storage for the transformed vertices. - * @param src The source vertices. - * @param size The number of vertices. - **/ - void transform(vertex * dst, const vertex * src, int size) const; - - }; - -} -} - -#endif 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 +#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 +#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} }; -- cgit v1.1-26-g67d0