aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-09-06 19:57:40 +0800
committerchai <chaifix@163.com>2018-09-06 19:57:40 +0800
commit50084b0b3451328a4dfe6db65c78a225e9c8f288 (patch)
tree020e5b6c834b792aada4cd23cd2551a6b81c22dc /src
parent7281a70b1e38fc2bdfb2d4b6d8f7daf7ceedbd1a (diff)
+bitmap
Diffstat (limited to 'src')
-rw-r--r--src/libjin/Graphics/Bitmap.cpp96
-rw-r--r--src/libjin/Graphics/Bitmap.h43
-rw-r--r--src/libjin/Graphics/Canvas.h2
-rw-r--r--src/libjin/Graphics/Color.cpp16
-rw-r--r--src/libjin/Graphics/Color.h38
-rw-r--r--src/libjin/Graphics/Drawable.h7
-rw-r--r--src/libjin/Graphics/Graphics.h1
-rw-r--r--src/libjin/Graphics/Shader.cpp14
-rw-r--r--src/libjin/Graphics/Shader.h4
-rw-r--r--src/libjin/Graphics/Texture.cpp92
-rw-r--r--src/libjin/Graphics/Texture.h15
-rw-r--r--src/libjin/Graphics/Window.h4
-rw-r--r--src/libjin/Graphics/base.shader.h4
-rw-r--r--src/libjin/Math/Vector.cpp24
-rw-r--r--src/libjin/Math/Vector.h24
-rw-r--r--src/libjin/Math/Vector2.cpp9
-rw-r--r--src/libjin/Math/Vector2.h29
-rw-r--r--src/libjin/math/vector.cpp24
-rw-r--r--src/libjin/math/vector.h24
-rw-r--r--src/lua/modules/graphics/bitmap.cpp99
-rw-r--r--src/lua/modules/graphics/graphics.cpp66
-rw-r--r--src/lua/modules/graphics/shader.cpp (renamed from src/lua/modules/graphics/jsl.cpp)0
-rw-r--r--src/lua/modules/graphics/texture.cpp14
-rw-r--r--src/lua/modules/thread/Thread.cpp3
-rw-r--r--src/lua/modules/types.h1
25 files changed, 414 insertions, 239 deletions
diff --git a/src/libjin/Graphics/Bitmap.cpp b/src/libjin/Graphics/Bitmap.cpp
new file mode 100644
index 0000000..93ea1a9
--- /dev/null
+++ b/src/libjin/Graphics/Bitmap.cpp
@@ -0,0 +1,96 @@
+#include "Bitmap.h"
+#include "../3rdparty/stb/stb_image.h"
+#include "../Math/math.h"
+
+using namespace jin::math;
+
+namespace jin
+{
+namespace graphics
+{
+
+ /*static*/ Bitmap* Bitmap::createBitmap(const void* imgData, size_t size)
+ {
+ if (imgData == nullptr)
+ return nullptr;
+ int w, h;
+ void* data = stbi_load_from_memory((unsigned char *)imgData, size, &w, &h, NULL, STBI_rgb_alpha);
+ if (data == nullptr)
+ return nullptr;
+ Bitmap* bitmap = new Bitmap();
+ bitmap->pixels = (Color*)data;
+ bitmap->width = w;
+ bitmap->height = h;
+ return bitmap;
+ }
+
+ /*static*/ Bitmap* Bitmap::createBitmap(int w, int h)
+ {
+ Bitmap* bitmap = new Bitmap(w, h);
+ return bitmap;
+ }
+
+ Bitmap::Bitmap()
+ : width(0)
+ , height(0)
+ , pixels(nullptr)
+ {
+ }
+
+ Bitmap::Bitmap(int w, int h)
+ {
+ width = w;
+ height = h;
+ pixels = (Color*)calloc(1, w*h*sizeof(Color));
+ }
+
+ Bitmap::~Bitmap()
+ {
+ stbi_image_free(pixels);
+ }
+
+ void Bitmap::bindPixels(Color* p, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ pixels = p;
+ width = w;
+ height = h;
+ }
+
+ void Bitmap::setPixels(Color* p, int w, int h)
+ {
+ if (pixels != nullptr)
+ delete[] pixels;
+ size_t s = w * h * sizeof(Color);
+ pixels = (Color*)calloc(1, s);
+ memcpy(pixels, p, s);
+ width = w;
+ height = h;
+ }
+
+ void Bitmap::setPixel(const Color& pixel, 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;
+ }
+
+ Color Bitmap::getPixel(int x, int y)
+ {
+ if (pixels == nullptr)
+ return Color::BLACK;
+ if (without<int>(x, 0, width - 1) || without<int>(y, 0, height - 1))
+ return Color::BLACK;
+ return pixels[x + y * width];
+ }
+
+ const Color* Bitmap::getPixels()
+ {
+ return pixels;
+ }
+
+}
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/Bitmap.h b/src/libjin/Graphics/Bitmap.h
new file mode 100644
index 0000000..d050c43
--- /dev/null
+++ b/src/libjin/Graphics/Bitmap.h
@@ -0,0 +1,43 @@
+#ifndef __JIN_BITMAP_H
+#define __JIN_BITMAP_H
+#include "../modules.h"
+#if JIN_MODULES_RENDER
+
+#include "../Math/Vector2.h"
+#include "../3rdparty/GLee/GLee.h"
+#include "Color.h"
+namespace jin
+{
+namespace graphics
+{
+
+ class Bitmap
+ {
+ public:
+ static Bitmap* createBitmap(const void* imgData, size_t size);
+ static Bitmap* createBitmap(int w, int h);
+
+ ~Bitmap();
+
+ void bindPixels(Color* pixels, int w, int h);
+ void setPixels(Color* pixels, int w, int h);
+ void setPixel(const Color& pixel, int x, int y);
+ Color getPixel(int x, int y);
+ const Color* getPixels();
+ inline int getWidth() { return width; }
+ inline int getHeight() { return height; }
+
+ private:
+ Bitmap();
+ Bitmap(int w, int h);
+
+ Color * pixels;
+ int width, height;
+
+ };
+
+}
+}
+
+#endif
+#endif \ No newline at end of file
diff --git a/src/libjin/Graphics/Canvas.h b/src/libjin/Graphics/Canvas.h
index 4e86e50..b5cf8a2 100644
--- a/src/libjin/Graphics/Canvas.h
+++ b/src/libjin/Graphics/Canvas.h
@@ -8,6 +8,7 @@ namespace jin
{
namespace graphics
{
+
class Canvas: public Drawable
{
public:
@@ -24,6 +25,7 @@ namespace graphics
GLuint fbo;
static GLint cur;
};
+
} // render
} // jin
diff --git a/src/libjin/Graphics/Color.cpp b/src/libjin/Graphics/Color.cpp
new file mode 100644
index 0000000..34adbfa
--- /dev/null
+++ b/src/libjin/Graphics/Color.cpp
@@ -0,0 +1,16 @@
+#include "Color.h"
+
+namespace jin
+{
+namespace graphics
+{
+
+ const Color Color::WHITE = Color(255, 255, 255);
+ const Color Color::BLACK = Color(0, 0, 0);
+ 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);
+
+}
+} \ No newline at end of file
diff --git a/src/libjin/Graphics/Color.h b/src/libjin/Graphics/Color.h
index a78234e..18dc5e7 100644
--- a/src/libjin/Graphics/Color.h
+++ b/src/libjin/Graphics/Color.h
@@ -13,15 +13,35 @@ namespace jin
namespace graphics
{
- union color {
- struct {
-#if JIN_BYTEORDER == JIN_BIG_ENDIAN
- unsigned char r, g, b, a;
-#else
- unsigned char a, b, g, r;
-#endif
- }rgba;
- int word;
+ class Color
+ {
+ public:
+ /* Default Colors */
+ static const Color WHITE;
+ static const Color BLACK;
+ static const Color RED;
+ static const Color GREEN;
+ static const Color BLUE;
+ static const Color MAGENTA;
+
+ Color() { r = g = b = a = 0; };
+ Color(unsigned char _r
+ , unsigned char _g
+ , unsigned char _b
+ , unsigned char _a = 255)
+ {
+ r = _r;
+ g = _g;
+ b = _b;
+ a = _a;
+ }
+
+ #if JIN_BYTEORDER == JIN_BIG_ENDIAN
+ unsigned char r, g, b, a;
+ #else
+ unsigned char a, b, g, r;
+ #endif
+
};
} // render
diff --git a/src/libjin/Graphics/Drawable.h b/src/libjin/Graphics/Drawable.h
index 86cc919..b3fc565 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/Vector.h"
+#include "../math/Vector2.h"
#include "../3rdparty/GLee/GLee.h"
namespace jin
@@ -27,9 +27,10 @@ namespace graphics
static const int DRAWABLE_V_SIZE = 8;
GLuint texture;
+ /* TODO: vertex buffer object */
GLuint vbo;
- jin::math::Vector2 size;
- jin::math::Vector2 anchor;
+ jin::math::Vector2<unsigned int> size;
+ jin::math::Vector2<int> anchor;
float vertCoord[DRAWABLE_V_SIZE];
float textCoord[DRAWABLE_V_SIZE];
diff --git a/src/libjin/Graphics/Graphics.h b/src/libjin/Graphics/Graphics.h
index bca3812..9c4ed35 100644
--- a/src/libjin/Graphics/Graphics.h
+++ b/src/libjin/Graphics/Graphics.h
@@ -10,6 +10,7 @@
#include "texture.h"
#include "Shader.h"
#include "window.h"
+#include "Bitmap.h"
#endif // JIN_MODULES_RENDER
#endif // __JIN_GRAPHICS_H \ No newline at end of file
diff --git a/src/libjin/Graphics/Shader.cpp b/src/libjin/Graphics/Shader.cpp
index a40f78d..45ad3d6 100644
--- a/src/libjin/Graphics/Shader.cpp
+++ b/src/libjin/Graphics/Shader.cpp
@@ -72,7 +72,7 @@ namespace graphics
{
glUseProgram(pid);
currentJSLProgram = this;
- bindDefaultTex();
+ bindDefaultTexture();
}
/*static*/ void JSLProgram::unuse()
@@ -81,7 +81,7 @@ namespace graphics
currentJSLProgram = nullptr;
}
- void JSLProgram::bindDefaultTex()
+ void JSLProgram::bindDefaultTexture()
{
int loc = glGetUniformLocation(pid, default_tex);
glUniform1i(loc, DEFAULT_TEX_UNIT);
@@ -181,15 +181,15 @@ namespace graphics
glUniform4f(loc, x, y, z, w);
}
- void JSLProgram::sendColor(const char* name, const color* col)
+ 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
+ col->r / 255.f,
+ col->g / 255.f,
+ col->b / 255.f,
+ col->a / 255.f
);
}
diff --git a/src/libjin/Graphics/Shader.h b/src/libjin/Graphics/Shader.h
index 24c889d..0356dd5 100644
--- a/src/libjin/Graphics/Shader.h
+++ b/src/libjin/Graphics/Shader.h
@@ -32,12 +32,12 @@ namespace graphics
void sendVec3(const char* name, float x, float y, float z);
void sendVec4(const char* name, float x, float y, float z, float w);
void sendCanvas(const char* name, const Canvas* canvas);
- void sendColor(const char* name, const color* col);
+ void sendColor(const char* name, const Color* col);
protected:
GLint claimTextureUnit(const std::string& name);
JSLProgram(const char* program);
- void bindDefaultTex();
+ void bindDefaultTexture();
static JSLProgram* currentJSLProgram;
diff --git a/src/libjin/Graphics/Texture.cpp b/src/libjin/Graphics/Texture.cpp
index 708042e..3ef3e7e 100644
--- a/src/libjin/Graphics/Texture.cpp
+++ b/src/libjin/Graphics/Texture.cpp
@@ -3,7 +3,6 @@
#include <fstream>
#include "texture.h"
-#include "../3rdparty/stb/stb_image.h"
#include "../utils/utils.h"
#include "../Math/Math.h"
@@ -14,84 +13,41 @@ namespace graphics
using namespace jin::math;
- Texture* Texture::createTexture(const char* file)
+ /*static*/ Texture* Texture::createTexture(Bitmap* bitmap)
{
- std::ifstream fs;
- fs.open(file, std::ios::binary);
- Texture* tex = nullptr;
- if (fs.is_open())
- {
- fs.seekg(0, std::ios::end);
- int size = fs.tellg();
- fs.seekg(0, std::ios::beg);
- char* buffer = (char*)malloc(size);
- memset(buffer, 0, size);
- fs.read(buffer, size);
- tex = createTexture(buffer, size);
- free(buffer);
- }
- fs.close();
- return tex;
- }
+ 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;
+
+ glGenTextures(1, &tex->texture);
+ glBindTexture(GL_TEXTURE_2D, tex->texture);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
+
+ tex->vertCoord[0] = 0; tex->vertCoord[1] = 1;
+ tex->vertCoord[2] = 0; tex->vertCoord[3] = h;
+ tex->vertCoord[4] = w; tex->vertCoord[5] = h;
+ tex->vertCoord[6] = w; tex->vertCoord[7] = 1;
+
+ tex->textCoord[0] = 0; tex->textCoord[1] = 0;
+ tex->textCoord[2] = 0; tex->textCoord[3] = 1;
+ tex->textCoord[4] = 1; tex->textCoord[5] = 1;
+ tex->textCoord[6] = 1; tex->textCoord[7] = 0;
- Texture* Texture::createTexture(const void* mem, size_t size)
- {
- Texture* tex = new Texture();
- if(!tex->loadb(mem, size))
- {
- delete tex;
- tex = nullptr;
- }
return tex;
}
Texture::Texture()
- : Drawable(), pixels(0)
+ : Drawable()
{
}
Texture::~Texture()
{
- stbi_image_free(pixels);
- }
-
- color Texture::getPixel(int x, int y)
- {
- int w = size.x;
- int h = size.y;
- if (without(x, 0, w) || without(y, 0, h))
- {
- return { 0 };
- }
- return pixels[x + y * w];
- }
-
- bool Texture::loadb(const void* b, size_t s)
- {
- int w;
- int h;
- pixels = (color*)stbi_load_from_memory((unsigned char *)b, s, &w, &h, NULL, STBI_rgb_alpha);
- if (pixels == 0) return false;
- size.x = w;
- size.y = h;
-
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
-
- vertCoord[0] = 0; vertCoord[1] = 1;
- vertCoord[2] = 0; vertCoord[3] = h;
- vertCoord[4] = w; vertCoord[5] = h;
- vertCoord[6] = w; vertCoord[7] = 1;
-
- textCoord[0] = 0; textCoord[1] = 0;
- textCoord[2] = 0; textCoord[3] = 1;
- textCoord[4] = 1; textCoord[5] = 1;
- textCoord[6] = 1; textCoord[7] = 0;
-
- return true;
}
} // graphics
diff --git a/src/libjin/Graphics/Texture.h b/src/libjin/Graphics/Texture.h
index 947021a..2586d63 100644
--- a/src/libjin/Graphics/Texture.h
+++ b/src/libjin/Graphics/Texture.h
@@ -4,8 +4,9 @@
#if JIN_MODULES_RENDER
#include "../3rdparty/GLee/GLee.h"
-#include "color.h"
-#include "drawable.h"
+#include "Color.h"
+#include "Drawable.h"
+#include "Bitmap.h"
namespace jin
{
namespace graphics
@@ -13,22 +14,14 @@ namespace graphics
class Texture: public Drawable
{
-
public:
- static Texture* createTexture(const char* file);
- static Texture* createTexture(const void* mem, size_t size);
+ static Texture* createTexture(Bitmap* bitmap);
~Texture();
- color getPixel(int x, int y);
-
private:
Texture();
- bool loadb(const void* buffer, size_t size);
-
- color* pixels;
-
};
} // graphics
diff --git a/src/libjin/Graphics/Window.h b/src/libjin/Graphics/Window.h
index 23b932d..09ec6dd 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/Vector.h"
+#include "../math/Vector2.h"
#include "../common/Subsystem.hpp"
namespace jin
@@ -42,7 +42,7 @@ namespace graphics
void quitSystem() override;
SDL_Window* wnd;
- jin::math::Vector2 size;
+ jin::math::Vector2<unsigned int> size;
int fps;
};
diff --git a/src/libjin/Graphics/base.shader.h b/src/libjin/Graphics/base.shader.h
index 4ab3f7a..577a5f0 100644
--- a/src/libjin/Graphics/base.shader.h
+++ b/src/libjin/Graphics/base.shader.h
@@ -8,6 +8,10 @@ static const char* base_shader = R"(
#define Color vec4
#define Texel texture2D
#define extern uniform
+#define Vec2 vec2
+#define Vec3 vec3
+#define Vec4 vec4
+#define Number number
extern Texture %s;
%s
diff --git a/src/libjin/Math/Vector.cpp b/src/libjin/Math/Vector.cpp
deleted file mode 100644
index b4e347a..0000000
--- a/src/libjin/Math/Vector.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#include "vector.h"
-
-namespace jin
-{
-namespace math
-{
-
- Vector2::Vector2()
- :x(0), y(0)
- {
- }
-
- Vector2::Vector2(float _x, float _y)
- : x(_x), y(_y)
- {
- };
-
- Vector2::Vector2(const Vector2& v)
- : x(v.x), y(v.y)
- {
- }
-
-} // math
-} // jin \ No newline at end of file
diff --git a/src/libjin/Math/Vector.h b/src/libjin/Math/Vector.h
deleted file mode 100644
index 0fbdc87..0000000
--- a/src/libjin/Math/Vector.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef __JIN_VECTOR_H
-#define __JIN_VECTOR_H
-
-namespace jin
-{
-namespace math
-{
-
- class Vector2
- {
- public:
- Vector2();
- Vector2(float _x, float _y);
- Vector2(const Vector2& v);
-
- float x;
- float y;
-
- };
-
-} // math
-} // jin
-
-#endif \ No newline at end of file
diff --git a/src/libjin/Math/Vector2.cpp b/src/libjin/Math/Vector2.cpp
new file mode 100644
index 0000000..737c496
--- /dev/null
+++ b/src/libjin/Math/Vector2.cpp
@@ -0,0 +1,9 @@
+#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
new file mode 100644
index 0000000..ce96ac8
--- /dev/null
+++ b/src/libjin/Math/Vector2.h
@@ -0,0 +1,29 @@
+#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/vector.cpp b/src/libjin/math/vector.cpp
deleted file mode 100644
index b4e347a..0000000
--- a/src/libjin/math/vector.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-#include "vector.h"
-
-namespace jin
-{
-namespace math
-{
-
- Vector2::Vector2()
- :x(0), y(0)
- {
- }
-
- Vector2::Vector2(float _x, float _y)
- : x(_x), y(_y)
- {
- };
-
- Vector2::Vector2(const Vector2& v)
- : x(v.x), y(v.y)
- {
- }
-
-} // math
-} // jin \ No newline at end of file
diff --git a/src/libjin/math/vector.h b/src/libjin/math/vector.h
deleted file mode 100644
index 0fbdc87..0000000
--- a/src/libjin/math/vector.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef __JIN_VECTOR_H
-#define __JIN_VECTOR_H
-
-namespace jin
-{
-namespace math
-{
-
- class Vector2
- {
- public:
- Vector2();
- Vector2(float _x, float _y);
- Vector2(const Vector2& v);
-
- float x;
- float y;
-
- };
-
-} // math
-} // jin
-
-#endif \ No newline at end of file
diff --git a/src/lua/modules/graphics/bitmap.cpp b/src/lua/modules/graphics/bitmap.cpp
new file mode 100644
index 0000000..81c36f1
--- /dev/null
+++ b/src/lua/modules/graphics/bitmap.cpp
@@ -0,0 +1,99 @@
+#include "lua/modules/luax.h"
+#include "lua/modules/types.h"
+#include "lua/common/common.h"
+#include "libjin/jin.h"
+
+namespace jin
+{
+namespace lua
+{
+
+ using namespace jin::graphics;
+
+ typedef Ref<Bitmap>& BitmapRef;
+
+ static inline BitmapRef checkBitmap(lua_State* L)
+ {
+ Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP);
+ return proxy->getRef<Bitmap>();
+ }
+
+ static int l_gc(lua_State* L)
+ {
+ printf("collect bitmap\n");
+ BitmapRef ref = checkBitmap(L);
+ ref.release();
+ return 0;
+ }
+
+ static int l_getWidth(lua_State* L)
+ {
+ BitmapRef ref = checkBitmap(L);
+ int w = ref->getWidth();
+ luax_pushinteger(L, w);
+ return 1;
+ }
+
+ static int l_getHeight(lua_State* L)
+ {
+ BitmapRef ref = checkBitmap(L);
+ int h = ref->getHeight();
+ luax_pushinteger(L, h);
+ return 1;
+ }
+
+ static int l_getSize(lua_State* L)
+ {
+ BitmapRef ref = checkBitmap(L);
+ int w = ref->getWidth();
+ int h = ref->getHeight();
+ luax_pushinteger(L, w);
+ luax_pushinteger(L, h);
+ return 2;
+ }
+
+ static int l_getPixel(lua_State* L)
+ {
+ BitmapRef ref = checkBitmap(L);
+ int x = luax_checkinteger(L, 2);
+ int y = luax_checkinteger(L, 3);
+ Color col = ref->getPixel(x, y);
+ luax_pushinteger(L, col.r);
+ luax_pushinteger(L, col.g);
+ luax_pushinteger(L, col.b);
+ luax_pushinteger(L, col.a);
+ return 4;
+ }
+
+ static int l_setPixel(lua_State* L)
+ {
+ 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);
+ ref->setPixel(Color(r, g, b, a), x, y);
+ return 0;
+ }
+
+ static const luaL_Reg f[] = {
+ { "__gc", l_gc },
+ { "getWidth", l_getWidth },
+ { "getHeight", l_getHeight },
+ { "getSize", l_getSize },
+ { "getPixel", l_getPixel },
+ { "setPixel", l_setPixel },
+ { 0, 0 }
+ };
+
+ int luaopen_Bitmap(lua_State* L)
+ {
+ luax_newtype(L, JIN_GRAPHICS_BITMAP, f);
+ 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 62bfb26..909e70c 100644
--- a/src/lua/modules/graphics/graphics.cpp
+++ b/src/lua/modules/graphics/graphics.cpp
@@ -15,8 +15,8 @@ namespace lua
static struct
{
- color curRenderColor;
- color curClearColor;
+ Color curRenderColor;
+ Color curClearColor;
Font* curFont = nullptr;
Font* defaultFont = nullptr;
} context;
@@ -77,9 +77,9 @@ namespace lua
return 1;
}
- static int l_newTexture(lua_State* L)
+ static int l_newBitmap(lua_State* L)
{
- Filesystem* fs = Filesystem::get();
+ Filesystem* fs = Filesystem::get();
const char* f = luax_checkstring(L, 1);
if (!fs->exists(f))
{
@@ -88,9 +88,20 @@ namespace lua
}
Buffer b;
fs->read(f, &b);
+ Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy));
+ Bitmap* bitmap = Bitmap::createBitmap(b.data, b.size);
+ proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP));
+ return 1;
+ }
+ /* jin.graphics.newTexture(bitmap) */
+ static int l_newTexture(lua_State* L)
+ {
+ Proxy* p = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_BITMAP);
+ Ref<Bitmap>& refBitmap = p->getRef<Bitmap>();
+ Bitmap* bitmap = refBitmap.getObject();
Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy));
- Texture* img = Texture::createTexture(b.data, b.size);
+ Texture* img = Texture::createTexture(bitmap);
proxy->bind(new Ref<Texture>(img, JIN_GRAPHICS_TEXTURE));
return 1;
}
@@ -140,15 +151,15 @@ namespace lua
return 0;
}
- context.curClearColor.rgba.r = luax_checknumber(L, 1);
- context.curClearColor.rgba.g = luax_checknumber(L, 2);
- context.curClearColor.rgba.b = luax_checknumber(L, 3);
- context.curClearColor.rgba.a = luax_checknumber(L, 4);
+ context.curClearColor.r = luax_checknumber(L, 1);
+ context.curClearColor.g = luax_checknumber(L, 2);
+ context.curClearColor.b = luax_checknumber(L, 3);
+ context.curClearColor.a = luax_checknumber(L, 4);
- glClearColor(context.curClearColor.rgba.r / 255.f,
- context.curClearColor.rgba.g / 255.f,
- context.curClearColor.rgba.b / 255.f,
- context.curClearColor.rgba.a / 255.f);
+ glClearColor(context.curClearColor.r / 255.f,
+ context.curClearColor.g / 255.f,
+ context.curClearColor.b / 255.f,
+ context.curClearColor.a / 255.f);
return 0;
}
@@ -192,27 +203,27 @@ namespace lua
return 0;
}
- context.curRenderColor.rgba.r = luax_checknumber(L, 1);
- context.curRenderColor.rgba.g = luax_checknumber(L, 2);
- context.curRenderColor.rgba.b = luax_checknumber(L, 3);
+ context.curRenderColor.r = luax_checknumber(L, 1);
+ context.curRenderColor.g = luax_checknumber(L, 2);
+ context.curRenderColor.b = luax_checknumber(L, 3);
if (luax_gettop(L) == 4)
- context.curRenderColor.rgba.a = luax_checknumber(L, 4);
+ context.curRenderColor.a = luax_checknumber(L, 4);
else
- context.curClearColor.rgba.a = 255;
+ context.curClearColor.a = 255;
- glColor4f(context.curRenderColor.rgba.r / 255.f,
- context.curRenderColor.rgba.g / 255.f,
- context.curRenderColor.rgba.b / 255.f,
- context.curRenderColor.rgba.a / 255.f);
+ glColor4f(context.curRenderColor.r / 255.f,
+ context.curRenderColor.g / 255.f,
+ context.curRenderColor.b / 255.f,
+ context.curRenderColor.a / 255.f);
return 0;
}
static int l_palette(lua_State * L)
{
- luax_pushnumber(L, context.curRenderColor.rgba.r);
- luax_pushnumber(L, context.curRenderColor.rgba.g);
- luax_pushnumber(L, context.curRenderColor.rgba.b);
- luax_pushnumber(L, context.curRenderColor.rgba.a);
+ luax_pushnumber(L, context.curRenderColor.r);
+ luax_pushnumber(L, context.curRenderColor.g);
+ luax_pushnumber(L, context.curRenderColor.b);
+ luax_pushnumber(L, context.curRenderColor.a);
return 4;
}
@@ -478,6 +489,7 @@ namespace lua
{ "getHeight", l_getHeight },
{ "destroy", l_destroy },
/* creators */
+ { "newBitmap", l_newBitmap },
{ "newTexture", l_newTexture },
{ "newShader", l_newShader },
{ "newCanvas", l_newCanvas },
@@ -513,10 +525,12 @@ namespace lua
extern int luaopen_Font(lua_State* L);
extern int luaopen_Canvas(lua_State* L);
extern int luaopen_JSL(lua_State* L);
+ extern int luaopen_Bitmap(lua_State* L);
int luaopen_graphics(lua_State* L)
{
// register types
+ luaopen_Bitmap(L);
luaopen_Texture(L);
luaopen_Canvas(L);
luaopen_Font(L);
diff --git a/src/lua/modules/graphics/jsl.cpp b/src/lua/modules/graphics/shader.cpp
index c2808ca..c2808ca 100644
--- a/src/lua/modules/graphics/jsl.cpp
+++ b/src/lua/modules/graphics/shader.cpp
diff --git a/src/lua/modules/graphics/texture.cpp b/src/lua/modules/graphics/texture.cpp
index d1d187d..77a93b8 100644
--- a/src/lua/modules/graphics/texture.cpp
+++ b/src/lua/modules/graphics/texture.cpp
@@ -32,19 +32,6 @@ namespace lua
return 1;
}
- static int l_getPixel(lua_State* L)
- {
- TextureRef ref = checkTexture(L);
- int x = luax_checknumber(L, 2);
- int y = luax_checknumber(L, 3);
- color c = ref->getPixel(x, y);
- luax_pushnumber(L, c.rgba.r);
- luax_pushnumber(L, c.rgba.g);
- luax_pushnumber(L, c.rgba.b);
- luax_pushnumber(L, c.rgba.a);
- return 4;
- }
-
static int l_setAnchor(lua_State* L)
{
TextureRef ref = checkTexture(L);
@@ -74,7 +61,6 @@ namespace lua
{ "getWidth", l_getWidth },
{ "getHeight", l_getHeight },
{ "getSize", l_getSize },
- { "getPixel", l_getPixel },
{ "setAnchor", l_setAnchor },
{ 0, 0 }
};
diff --git a/src/lua/modules/thread/Thread.cpp b/src/lua/modules/thread/Thread.cpp
index 7dfac25..5f458ab 100644
--- a/src/lua/modules/thread/Thread.cpp
+++ b/src/lua/modules/thread/Thread.cpp
@@ -161,7 +161,8 @@ namespace lua
case thread::Thread::Variant::POINTER:
Proxy* p = (Proxy*)v.pointer;
- Proxy* proxy = (Proxy*)luax_newinstance(L, p->getObjectType(), sizeof(Proxy));
+ const char* objType = p->getObjectType();
+ Proxy* proxy = (Proxy*)luax_newinstance(L, objType, sizeof(Proxy));
p->reference->retain();
proxy->bind(p->reference);
break;
diff --git a/src/lua/modules/types.h b/src/lua/modules/types.h
index 4e824c2..d61ce38 100644
--- a/src/lua/modules/types.h
+++ b/src/lua/modules/types.h
@@ -6,6 +6,7 @@
#define JIN_GRAPHICS_SHADER "jin.graphics.Shader"
#define JIN_GRAPHICS_CANVAS "jin.graphics.Canvas"
#define JIN_GRAPHICS_FONT "jin.graphics.Font"
+#define JIN_GRAPHICS_BITMAP "jin.graphics.Bitmap"
// audio module
#define JIN_AUDIO_SOURCE "jin.Audio.Source"