diff options
author | chai <chaifix@163.com> | 2018-09-07 13:30:44 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-09-07 13:30:44 +0800 |
commit | e72188433348c270a54879da9f086f1b527b580f (patch) | |
tree | b2cb92b37579c6f9cff77f52fc591426eb2d8286 /src | |
parent | 28ca1f570d417671904a25c2a9c589fdb1eb7a03 (diff) |
*update
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/Graphics/Bitmap.cpp | 57 | ||||
-rw-r--r-- | src/libjin/Graphics/Bitmap.h | 19 | ||||
-rw-r--r-- | src/libjin/Graphics/Canvas.cpp | 4 | ||||
-rw-r--r-- | src/libjin/Graphics/Color.cpp | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Color.h | 8 | ||||
-rw-r--r-- | src/libjin/Graphics/Drawable.h | 6 | ||||
-rw-r--r-- | src/libjin/Graphics/Shader.cpp | 8 | ||||
-rw-r--r-- | src/libjin/Graphics/Texture.cpp | 8 | ||||
-rw-r--r-- | src/libjin/Graphics/Window.cpp | 6 | ||||
-rw-r--r-- | src/libjin/Graphics/Window.h | 6 | ||||
-rw-r--r-- | src/libjin/Graphics/base.shader.h | 6 | ||||
-rw-r--r-- | src/libjin/Input/Mouse.cpp | 5 | ||||
-rw-r--r-- | src/libjin/Input/Mouse.h | 1 | ||||
-rw-r--r-- | src/libjin/Math/Vector2.cpp | 9 | ||||
-rw-r--r-- | src/libjin/Math/Vector2.h | 29 | ||||
-rw-r--r-- | src/libjin/Math/Vector2.hpp | 39 | ||||
-rw-r--r-- | src/libjin/Math/Vector3.hpp | 41 | ||||
-rw-r--r-- | src/libjin/Math/Vector4.hpp | 44 | ||||
-rw-r--r-- | src/libjin/input/mouse.cpp | 5 | ||||
-rw-r--r-- | src/libjin/input/mouse.h | 1 | ||||
-rw-r--r-- | src/lua/modules/graphics/bitmap.cpp | 26 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 15 | ||||
-rw-r--r-- | src/lua/modules/mouse/mouse.cpp | 3 |
23 files changed, 268 insertions, 80 deletions
diff --git a/src/libjin/Graphics/Bitmap.cpp b/src/libjin/Graphics/Bitmap.cpp index 93ea1a9..c2acff4 100644 --- a/src/libjin/Graphics/Bitmap.cpp +++ b/src/libjin/Graphics/Bitmap.cpp @@ -24,12 +24,23 @@ namespace graphics return bitmap; } - /*static*/ Bitmap* Bitmap::createBitmap(int w, int h) + /*static*/ Bitmap* Bitmap::createBitmap(int w, int h, Color color) { Bitmap* bitmap = new Bitmap(w, h); + if (color != Color::BLACK) + bitmap->setPixels(color); return bitmap; } + /*static */ Bitmap* Bitmap::clone(const Bitmap* bitmap) + { + Bitmap* b = new Bitmap(); + int w = bitmap->getWidth(); + int h = bitmap->getHeight(); + b->resetPixels(bitmap->getPixels(), w, h); + return b; + } + Bitmap::Bitmap() : width(0) , height(0) @@ -58,7 +69,7 @@ namespace graphics height = h; } - void Bitmap::setPixels(Color* p, int w, int h) + void Bitmap::resetPixels(const Color* p, int w, int h) { if (pixels != nullptr) delete[] pixels; @@ -69,15 +80,49 @@ namespace graphics height = h; } - void Bitmap::setPixel(const Color& pixel, int x, int y) + void Bitmap::setPixel(const Color& c, int x, int y) { if (pixels == nullptr) return; if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1)) return; - pixels[x + y * width] = pixel; + pixels[x + y * width] = c; } - + + void Bitmap::resetPixels(const Color& c, int w, int h) + { + if (pixels != nullptr) + delete[] pixels; + size_t s = w * h * sizeof(Color); + pixels = (Color*)calloc(1, s); + width = w; + height = h; + for (int x = 0; x < w; ++x) + { + for (int y = 0; y < h; ++y) + { + pixels[x + y * w] = c; + } + } + } + + void Bitmap::setPixels(Color* p) + { + size_t s = width * height * sizeof(Color); + memcpy(pixels, p, s); + } + + void Bitmap::setPixels(Color c) + { + for (int x = 0; x < width; ++x) + { + for (int y = 0; y < height; ++y) + { + pixels[x + y * width] = c; + } + } + } + Color Bitmap::getPixel(int x, int y) { if (pixels == nullptr) @@ -87,7 +132,7 @@ namespace graphics return pixels[x + y * width]; } - const Color* Bitmap::getPixels() + const Color* Bitmap::getPixels() const { return pixels; } diff --git a/src/libjin/Graphics/Bitmap.h b/src/libjin/Graphics/Bitmap.h index 464ca7f..6333215 100644 --- a/src/libjin/Graphics/Bitmap.h +++ b/src/libjin/Graphics/Bitmap.h @@ -3,7 +3,7 @@ #include "../modules.h" #if JIN_MODULES_RENDER -#include "../Math/Vector2.h" +#include "../Math/Vector2.hpp" #include "../3rdparty/GLee/GLee.h" #include "Color.h" namespace jin @@ -15,16 +15,23 @@ namespace graphics { public: static Bitmap* createBitmap(const void* imgData, size_t size); - static Bitmap* createBitmap(int w, int h); + static Bitmap* createBitmap(int w, int h, Color color = Color::BLACK); + static Bitmap* clone(const Bitmap* bitmap); ~Bitmap(); + /* init pixels */ void bindPixels(Color* pixels, int w, int h); - void setPixels(Color* pixels, int w, int h); + void resetPixels(const Color* pixels, int w, int h); + void resetPixels(const Color& pixels, int w, int h); + /* modify pixels */ void setPixel(const Color& pixel, int x, int y); + void setPixels(Color pixels); + void setPixels(Color* pixels); Color getPixel(int x, int y); - const Color* getPixels(); - inline int getWidth() { return width; } - inline int getHeight() { return height; } + const Color* getPixels() const; + /* get width and height */ + inline int getWidth() const { return width; } + inline int getHeight() const { return height; } private: Bitmap(); diff --git a/src/libjin/Graphics/Canvas.cpp b/src/libjin/Graphics/Canvas.cpp index c88429f..953278d 100644 --- a/src/libjin/Graphics/Canvas.cpp +++ b/src/libjin/Graphics/Canvas.cpp @@ -76,8 +76,8 @@ namespace graphics glPushMatrix(); glLoadIdentity(); - glViewport(0, 0, size.x, size.y); - glOrtho(0, size.x, size.y, 0, -1, 1); + glViewport(0, 0, size.w, size.h); + glOrtho(0, size.w, size.h, 0, -1, 1); // Switch back to modelview matrix glMatrixMode(GL_MODELVIEW); diff --git a/src/libjin/Graphics/Color.cpp b/src/libjin/Graphics/Color.cpp index 34adbfa..4ddbf7e 100644 --- a/src/libjin/Graphics/Color.cpp +++ b/src/libjin/Graphics/Color.cpp @@ -10,7 +10,7 @@ namespace graphics const Color Color::RED = Color(255, 0, 0); const Color Color::GREEN = Color(0, 255, 0); const Color Color::BLUE = Color(0, 0, 255); - const Color Color::MAGENTA = Color(255, 0, 255); + const Color Color::MAGENTA = Color(255, 255, 0); } }
\ No newline at end of file diff --git a/src/libjin/Graphics/Color.h b/src/libjin/Graphics/Color.h index 18dc5e7..786abde 100644 --- a/src/libjin/Graphics/Color.h +++ b/src/libjin/Graphics/Color.h @@ -35,6 +35,14 @@ namespace graphics b = _b; a = _a; } + bool operator == (const Color& c) + { + return r == c.r && g == c.g && b == c.b && a == c.a; + } + bool operator != (const Color& c) + { + return !(r == c.r && g == c.g && b == c.b && a == c.a); + } #if JIN_BYTEORDER == JIN_BIG_ENDIAN unsigned char r, g, b, a; diff --git a/src/libjin/Graphics/Drawable.h b/src/libjin/Graphics/Drawable.h index bdec5b4..8bc528e 100644 --- a/src/libjin/Graphics/Drawable.h +++ b/src/libjin/Graphics/Drawable.h @@ -3,7 +3,7 @@ #include "../modules.h" #if JIN_MODULES_RENDER -#include "../math/Vector2.h" +#include "../math/Vector2.hpp" #include "../3rdparty/GLee/GLee.h" namespace jin @@ -18,8 +18,8 @@ namespace graphics virtual ~Drawable(); void setAnchor(int x, int y); void draw(int x, int y, float sx, float sy, float r); - inline int getWidth() const { return size.x; } - inline int getHeight() const { return size.y; } + inline int getWidth() const { return size.w; } + inline int getHeight() const { return size.h; } inline GLuint getTexture() const { return texture; } protected: diff --git a/src/libjin/Graphics/Shader.cpp b/src/libjin/Graphics/Shader.cpp index 13e259d..f078648 100644 --- a/src/libjin/Graphics/Shader.cpp +++ b/src/libjin/Graphics/Shader.cpp @@ -33,7 +33,7 @@ namespace graphics * it clear to anybody reading your code that you really mean to sample from * texture unit 0, and did not just forget to set the value. */ - const int DEFAULT_TEX_UNIT = 0; + const int DEFAULT_TEXTURE_UNIT = 0; /*static*/ JSLProgram* JSLProgram::currentJSLProgram = nullptr; @@ -43,7 +43,7 @@ namespace graphics } JSLProgram::JSLProgram(const char* program) - : currentTextureUnit(DEFAULT_TEX_UNIT) + : currentTextureUnit(DEFAULT_TEXTURE_UNIT) { char* fs = (char*)calloc(1, strlen(program) + SHADER_FORMAT_SIZE); formatShader(fs, program); @@ -84,8 +84,8 @@ namespace graphics void JSLProgram::bindDefaultTexture() { - int loc = glGetUniformLocation(pid, default_tex); - glUniform1i(loc, DEFAULT_TEX_UNIT); + int loc = glGetUniformLocation(pid, default_texture); + glUniform1i(loc, DEFAULT_TEXTURE_UNIT); } GLint JSLProgram::claimTextureUnit(const std::string& name) diff --git a/src/libjin/Graphics/Texture.cpp b/src/libjin/Graphics/Texture.cpp index 3ef3e7e..2095af4 100644 --- a/src/libjin/Graphics/Texture.cpp +++ b/src/libjin/Graphics/Texture.cpp @@ -17,10 +17,10 @@ namespace graphics { Texture* tex = new Texture(); const Color* pixels = bitmap->getPixels(); - tex->size.x = bitmap->getWidth(); - tex->size.y = bitmap->getHeight(); - unsigned int w = tex->size.x; - unsigned int h = tex->size.y; + tex->size.w = bitmap->getWidth(); + tex->size.h = bitmap->getHeight(); + unsigned int w = tex->size.w; + unsigned int h = tex->size.h; glGenTextures(1, &tex->texture); glBindTexture(GL_TEXTURE_2D, tex->texture); diff --git a/src/libjin/Graphics/Window.cpp b/src/libjin/Graphics/Window.cpp index ede202e..3eb76ad 100644 --- a/src/libjin/Graphics/Window.cpp +++ b/src/libjin/Graphics/Window.cpp @@ -24,8 +24,8 @@ namespace graphics return false; const Setting* setting = (Setting*)s; - size.x = setting->width; - size.y = setting->height; + size.w = setting->width; + size.h = setting->height; fps = setting->fps; bool vsync = setting->vsync; const char* title = setting->title; @@ -59,7 +59,7 @@ namespace graphics if (setting->fullscreen) flag |= SDL_WINDOW_FULLSCREEN; if (setting->resizable) flag |= SDL_WINDOW_RESIZABLE; - wnd = SDL_CreateWindow(title, wx, wy, size.x, size.y, flag); + wnd = SDL_CreateWindow(title, wx, wy, size.w, size.h, flag); if (wnd == NULL) return false; ctx = SDL_GL_CreateContext(wnd); diff --git a/src/libjin/Graphics/Window.h b/src/libjin/Graphics/Window.h index 22033ee..2ecd7e4 100644 --- a/src/libjin/Graphics/Window.h +++ b/src/libjin/Graphics/Window.h @@ -5,7 +5,7 @@ #include "SDL2/SDL.h" #include "../utils/utils.h" -#include "../math/Vector2.h" +#include "../math/Vector2.hpp" #include "../common/Subsystem.hpp" namespace jin @@ -28,8 +28,8 @@ namespace graphics }; void setTitle(const char* title); - inline int getW(){ return size.x; } - inline int getH(){ return size.y; } + inline int getW(){ return size.w; } + inline int getH(){ return size.h; } inline int getFPS(){ return fps; } inline void swapBuffers(); diff --git a/src/libjin/Graphics/base.shader.h b/src/libjin/Graphics/base.shader.h index 067a60b..080e27e 100644 --- a/src/libjin/Graphics/base.shader.h +++ b/src/libjin/Graphics/base.shader.h @@ -5,7 +5,7 @@ * texture units (because GL_TEXTURE0 is the default anyway). */ -static const char* default_tex = "_tex0_"; +static const char* default_texture = "_tex0_"; static const char* base_shader = R"( #define number float @@ -28,7 +28,7 @@ void main() } )"; -static const int SHADER_FORMAT_SIZE = strlen(base_shader) + strlen(default_tex) * 2; +static const int SHADER_FORMAT_SIZE = strlen(base_shader) + strlen(default_texture) * 2; #define formatShader(buf, program)\ - sprintf(buf, base_shader, default_tex, program, default_tex) + sprintf(buf, base_shader, default_texture, program, default_texture) diff --git a/src/libjin/Input/Mouse.cpp b/src/libjin/Input/Mouse.cpp index a3c8e27..e284f78 100644 --- a/src/libjin/Input/Mouse.cpp +++ b/src/libjin/Input/Mouse.cpp @@ -16,6 +16,11 @@ namespace input #endif // JIN_INPUT_SDL } + void Mouse::setVisible(bool visible) + { + SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE); + } + } // input } // jin diff --git a/src/libjin/Input/Mouse.h b/src/libjin/Input/Mouse.h index 584c516..9f6bf5d 100644 --- a/src/libjin/Input/Mouse.h +++ b/src/libjin/Input/Mouse.h @@ -14,6 +14,7 @@ namespace input { public: void getState(int* x, int* y); + void setVisible(bool visible); private: SINGLETON(Mouse); diff --git a/src/libjin/Math/Vector2.cpp b/src/libjin/Math/Vector2.cpp deleted file mode 100644 index 737c496..0000000 --- a/src/libjin/Math/Vector2.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "Vector2.h" - -namespace jin -{ -namespace math -{ - -} // math -} // jin
\ No newline at end of file diff --git a/src/libjin/Math/Vector2.h b/src/libjin/Math/Vector2.h deleted file mode 100644 index ce96ac8..0000000 --- a/src/libjin/Math/Vector2.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __JIN_VECTOR_H -#define __JIN_VECTOR_H - -namespace jin -{ -namespace math -{ - - template<typename T> - class Vector2 - { - public: - Vector2() : x(0), y(0) {}; - Vector2(T _x, T _y) :x(_x), y(_y) {}; - Vector2(const Vector2<T>& v) - { - x = v.x; - y = v.y; - } - - T x; - T y; - - }; - -} // math -} // jin - -#endif
\ No newline at end of file diff --git a/src/libjin/Math/Vector2.hpp b/src/libjin/Math/Vector2.hpp new file mode 100644 index 0000000..a657c83 --- /dev/null +++ b/src/libjin/Math/Vector2.hpp @@ -0,0 +1,39 @@ +#ifndef __JIN_VECTOR_H +#define __JIN_VECTOR_H + +namespace jin +{ +namespace math +{ + + template<typename T> + class Vector2 + { + public: + Vector2() + { + data[0] = data[1] = 0; + } + Vector2(T _x, T _y) + { + data[0] = _x; + data[1] = _y; + } + Vector2(const Vector2<T>& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + } + + T &x = data[0], &y = data[1]; // xy + T &w = data[0], &h = data[1]; // wh + + private: + T data[2]; + + }; + +} // math +} // jin + +#endif
\ No newline at end of file diff --git a/src/libjin/Math/Vector3.hpp b/src/libjin/Math/Vector3.hpp new file mode 100644 index 0000000..846dea8 --- /dev/null +++ b/src/libjin/Math/Vector3.hpp @@ -0,0 +1,41 @@ +#ifndef __JIN_VECTOR3_H +#define __JIN_VECTOR3_H + +namespace jin +{ +namespace math +{ + + template<typename T> + class Vector3 + { + public: + Vector3() + { + data[0] = data[1] = data[2] = 0; + } + Vector3(T _x, T _y, T _z) + { + data[0] = _x; + data[1] = _y; + data[2] = _z; + } + Vector3(const Vector3<T>& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + data[2] = v.data[2]; + } + + T &x = data[0], &y = data[1], &z = data[2]; // xyz + T &r = data[0], &g = data[1], &b = data[2]; // rgb + + private: + T data[3]; + + }; + +} // math +} // jin + +#endif
\ No newline at end of file diff --git a/src/libjin/Math/Vector4.hpp b/src/libjin/Math/Vector4.hpp new file mode 100644 index 0000000..e869ddf --- /dev/null +++ b/src/libjin/Math/Vector4.hpp @@ -0,0 +1,44 @@ +#ifndef __JIN_VECTOR4_H +#define __JIN_VECTOR4_H + +namespace jin +{ +namespace math +{ + + template<typename T> + class Vector4 + { + public: + Vector4() + { + data[0] = data[1] = data[2] = data[3] = 0; + } + Vector4(T _x, T _y, T _z, T _t) + { + data[0] = _x; + data[1] = _y; + data[2] = _z; + data[3] = _t; + } + Vector4(const Vector4<T>& v) + { + data[0] = v.data[0]; + data[1] = v.data[1]; + data[2] = v.data[2]; + data[3] = v.data[3]; + } + + T &x = data[0], &y = data[1], &z = data[2], &t = data[3]; // xyzt + T &w = data[2], &h = data[3]; // xywh + T &r = data[0], &g = data[1], &b = data[2], &a = data[3]; // rgb + + private: + T data[4]; + + }; + +} // math +} // jin + +#endif
\ No newline at end of file diff --git a/src/libjin/input/mouse.cpp b/src/libjin/input/mouse.cpp index a3c8e27..e284f78 100644 --- a/src/libjin/input/mouse.cpp +++ b/src/libjin/input/mouse.cpp @@ -16,6 +16,11 @@ namespace input #endif // JIN_INPUT_SDL } + void Mouse::setVisible(bool visible) + { + SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE); + } + } // input } // jin diff --git a/src/libjin/input/mouse.h b/src/libjin/input/mouse.h index 584c516..9f6bf5d 100644 --- a/src/libjin/input/mouse.h +++ b/src/libjin/input/mouse.h @@ -14,6 +14,7 @@ namespace input { public: void getState(int* x, int* y); + void setVisible(bool visible); private: SINGLETON(Mouse); diff --git a/src/lua/modules/graphics/bitmap.cpp b/src/lua/modules/graphics/bitmap.cpp index 81c36f1..6067442 100644 --- a/src/lua/modules/graphics/bitmap.cpp +++ b/src/lua/modules/graphics/bitmap.cpp @@ -20,7 +20,6 @@ namespace lua static int l_gc(lua_State* L) { - printf("collect bitmap\n"); BitmapRef ref = checkBitmap(L); ref.release(); return 0; @@ -70,14 +69,29 @@ namespace lua BitmapRef ref = checkBitmap(L); int x = luax_checkinteger(L, 2); int y = luax_checkinteger(L, 3); - unsigned char r = luax_checkinteger(L, 4); - unsigned char g = luax_checkinteger(L, 5); - unsigned char b = luax_checkinteger(L, 6); - unsigned char a = luax_checkinteger(L, 7); + if (!luax_istable(L, 4)) + { + luax_typerror(L, 4, "table"); + return 1; + } + unsigned int r = luax_rawgetnumber(L, 4, 1); + unsigned int g = luax_rawgetnumber(L, 4, 2); + unsigned int b = luax_rawgetnumber(L, 4, 3); + unsigned int a = luax_rawgetnumber(L, 4, 4); ref->setPixel(Color(r, g, b, a), x, y); return 0; } + static int l_clone(lua_State* L) + { + BitmapRef ref = checkBitmap(L); + Bitmap* bitmap = ref.getObject(); + Bitmap* b = Bitmap::clone(bitmap); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); + proxy->bind(new Ref<Bitmap>(b, JIN_GRAPHICS_BITMAP)); + return 1; + } + static const luaL_Reg f[] = { { "__gc", l_gc }, { "getWidth", l_getWidth }, @@ -85,6 +99,7 @@ namespace lua { "getSize", l_getSize }, { "getPixel", l_getPixel }, { "setPixel", l_setPixel }, + { "clone", l_clone }, { 0, 0 } }; @@ -94,6 +109,5 @@ namespace lua return 0; } - } }
\ No newline at end of file diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 573392d..8a58ff0 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -86,6 +86,21 @@ namespace lua int h = luax_checkinteger(L, 2); bitmap = Bitmap::createBitmap(w, h); } + else if (luax_gettop(L) == 3) + { + int w = luax_checkinteger(L, 1); + int h = luax_checkinteger(L, 2); + if (!luax_istable(L, 3)) + { + luax_typerror(L, 3, "table"); + return 1; + } + unsigned int r = luax_rawgetnumber(L, 3, 1); + unsigned int g = luax_rawgetnumber(L, 3, 2); + unsigned int b = luax_rawgetnumber(L, 3, 3); + unsigned int a = luax_rawgetnumber(L, 3, 4); + bitmap = Bitmap::createBitmap(w, h, Color(r, g, b, a)); + } else { const char* f = luax_checkstring(L, 1); diff --git a/src/lua/modules/mouse/mouse.cpp b/src/lua/modules/mouse/mouse.cpp index a43c275..390936c 100644 --- a/src/lua/modules/mouse/mouse.cpp +++ b/src/lua/modules/mouse/mouse.cpp @@ -21,7 +21,8 @@ namespace lua static int l_setVisible(lua_State* L) { bool visible = luax_checkbool(L, 1); - SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE); + Mouse* mouse = Mouse::get(); + mouse->setVisible(visible); return 0; } |