diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/Common/Array.hpp | 72 | ||||
-rw-r--r-- | src/libjin/Common/common.h | 6 | ||||
-rw-r--r-- | src/libjin/Filesystem/Buffer.h | 26 | ||||
-rw-r--r-- | src/libjin/Graphics/Canvas.h | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Color.h | 10 | ||||
-rw-r--r-- | src/libjin/Graphics/Shader.cpp | 10 | ||||
-rw-r--r-- | src/libjin/jin.h | 1 | ||||
-rw-r--r-- | src/lua/modules/filesystem/filesystem.cpp | 7 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 4 | ||||
-rw-r--r-- | src/lua/modules/net/Buffer.cpp | 10 | ||||
-rw-r--r-- | src/lua/modules/net/Buffer.h | 3 |
11 files changed, 130 insertions, 21 deletions
diff --git a/src/libjin/Common/Array.hpp b/src/libjin/Common/Array.hpp new file mode 100644 index 0000000..de22961 --- /dev/null +++ b/src/libjin/Common/Array.hpp @@ -0,0 +1,72 @@ +#ifndef __LIBJIN_COMMON_ARRAY_H +#define __LIBJIN_COMMON_ARRAY_H + +namespace jin +{ + + /* ԶͷŶڴĶ̬ */ + template<typename T> + class Array + { + public: + Array() : length(0), data(nullptr) {} + + Array(int l) + { + length = l; + data = new T[l]; + } + + ~Array() + { + delete[] data; + length = 0; + } + + T* operator &() + { + return data; + } + + T operator[](int index) + { + return data[index]; + } + + /* ڴ */ + void bind(T* d, int len) + { + if (data != nullptr) + delete data; + data = d; + length = len; + } + + void add(T v) + { + int len = length + 1; + T* d = new T[len]; + memcpy(d, data, size()); + d[length] = v; + bind(d, len); + } + + int size() + { + return sizeof(T) * length; + } + + int count() + { + return length; + } + + private: + T * data; + unsigned int length; + + }; + +} + +#endif
\ No newline at end of file diff --git a/src/libjin/Common/common.h b/src/libjin/Common/common.h new file mode 100644 index 0000000..9586c82 --- /dev/null +++ b/src/libjin/Common/common.h @@ -0,0 +1,6 @@ +#ifndef __LIBJIN_COMMON_H +#define __LIBJIN_COMMON_H + +#include "Array.hpp" + +#endif
\ No newline at end of file diff --git a/src/libjin/Filesystem/Buffer.h b/src/libjin/Filesystem/Buffer.h index 3cd92cd..5cb6468 100644 --- a/src/libjin/Filesystem/Buffer.h +++ b/src/libjin/Filesystem/Buffer.h @@ -8,27 +8,43 @@ namespace jin namespace filesystem { + /** + * ڶϷָռbuffer + */ class Buffer { public: Buffer() : data(0), size(0) {} Buffer(const Buffer& src) { - delete data; + delete[] data; size = src.size; data = new char[size]; memcpy(data, src.data, size); } - inline Buffer(void* d, int s) + Buffer(void* d, int s) { - data = new char(size); + data = new char[size]; memcpy(data, d, size); size = s; } - inline ~Buffer() + Buffer(size_t s) + { + data = new char[s]; + memset(data, 0, s); + size = s; + } + ~Buffer() { + delete[] data; size = 0; - delete[] data; + } + void operator = (const Buffer& buffer) + { + delete[] data; + size = buffer.size; + data = new char[size]; + memcpy(data, buffer.data, size); } public: diff --git a/src/libjin/Graphics/Canvas.h b/src/libjin/Graphics/Canvas.h index ec94d28..2522d32 100644 --- a/src/libjin/Graphics/Canvas.h +++ b/src/libjin/Graphics/Canvas.h @@ -31,4 +31,4 @@ namespace graphics } // jin #endif // LIBJIN_MODULES_RENDER -#endif // __LIBJIN_CANVAS_H +#endif // __LIBJIN_CANVAS_H
\ No newline at end of file diff --git a/src/libjin/Graphics/Color.h b/src/libjin/Graphics/Color.h index 4d78706..170ee0b 100644 --- a/src/libjin/Graphics/Color.h +++ b/src/libjin/Graphics/Color.h @@ -16,7 +16,7 @@ namespace graphics class Color { public: - /* Default Colors */ + /* Built-in Colors */ static const Color WHITE; static const Color BLACK; static const Color RED; @@ -45,6 +45,14 @@ namespace graphics a = c.a; } + void operator = (const Color& c) + { + 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; diff --git a/src/libjin/Graphics/Shader.cpp b/src/libjin/Graphics/Shader.cpp index ae64c7e..7f849a6 100644 --- a/src/libjin/Graphics/Shader.cpp +++ b/src/libjin/Graphics/Shader.cpp @@ -3,11 +3,14 @@ #include "../utils/macros.h" #include "Shader.h" +#include "../Filesystem/Buffer.h" namespace jin { namespace graphics { + using namespace jin::filesystem; + #include "base.shader.h" /* @@ -55,11 +58,10 @@ namespace graphics : currentTextureUnit(DEFAULT_TEXTURE_UNIT) { int size = strlen(program) + SHADER_FORMAT_SIZE; - char* fs = (char*)alloca(size); - memset(fs, 0, size); - formatShader(fs, program); + Buffer b = Buffer(size); + formatShader((char*)b.data, program); GLuint shader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(shader, 1, (const GLchar**)&fs, NULL); + glShaderSource(shader, 1, (const GLchar**)&b.data, NULL); glCompileShader(shader); GLint success; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); diff --git a/src/libjin/jin.h b/src/libjin/jin.h index 2565cfe..6651783 100644 --- a/src/libjin/jin.h +++ b/src/libjin/jin.h @@ -14,6 +14,7 @@ #include "Graphics/Graphics.h" #include "Time/Timer.h" #include "Thread/Thread.h" +#include "Common/common.h" #define LIBJIN_VERSION "Jin 0.1"; #define LIBJIN_AUTHOR "Chai"; diff --git a/src/lua/modules/filesystem/filesystem.cpp b/src/lua/modules/filesystem/filesystem.cpp index f377f0c..1fc0d7c 100644 --- a/src/lua/modules/filesystem/filesystem.cpp +++ b/src/lua/modules/filesystem/filesystem.cpp @@ -112,9 +112,10 @@ namespace lua Filesystem* fs = context.fs; const char* file = luax_checkstring(L, 1); unsigned int len; - char* data = (char*)fs->read(file, &len); - luax_pushstring(L, data); - luax_pushinteger(L, len); + Buffer buffer; + buffer.data = (char*)fs->read(file, &buffer.size); + luax_pushstring(L, (char*)buffer.data); + luax_pushinteger(L, buffer.size); return 2; } diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index ada4754..4d597ae 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -110,7 +110,7 @@ namespace lua error(L, "No such image file %s", f); goto fail; } - Buffer b; + Buffer b = {}; if (!fs->read(f, &b)) { error(L, "Failed to read image %s", f); @@ -459,13 +459,13 @@ namespace lua { const char* path = luax_checkstring(L, 1); Filesystem* fs = Filesystem::get(); - Buffer b = {}; if (!fs->exists(path)) { error(L, "No such font %s\n", path); luax_pushnil(L); return 1; } + Buffer b = {}; fs->read(path, &b); font->loadMemory((const unsigned char*)b.data); } diff --git a/src/lua/modules/net/Buffer.cpp b/src/lua/modules/net/Buffer.cpp index cc9f2b4..1a62c9a 100644 --- a/src/lua/modules/net/Buffer.cpp +++ b/src/lua/modules/net/Buffer.cpp @@ -68,10 +68,12 @@ namespace net { BufferRef ref = checkNetBuffer(L); int offset = luax_checkinteger(L, 2); - int len; - const char* str = ref->grabString(&len, offset); - luax_pushstring(L, str); - luax_pushinteger(L, len); + unsigned int len; + char* data = ref->grabString(&len, offset); + Array<char> str; + str.bind(data, len); + luax_pushstring(L, &str); + luax_pushinteger(L, str.count()); return 2; } diff --git a/src/lua/modules/net/Buffer.h b/src/lua/modules/net/Buffer.h index 31e6df8..172682d 100644 --- a/src/lua/modules/net/Buffer.h +++ b/src/lua/modules/net/Buffer.h @@ -52,7 +52,8 @@ namespace net return; } - const char* grabString(int* length, int offset = 0) + /* grab and create a string */ + char* grabString(unsigned int* length, int offset = 0) { int l = offset; for (; l < size; ++l) |