diff options
Diffstat (limited to 'src/libjin')
-rw-r--r-- | src/libjin/Graphics/Bitmap.cpp | 96 | ||||
-rw-r--r-- | src/libjin/Graphics/Bitmap.h | 43 | ||||
-rw-r--r-- | src/libjin/Graphics/Canvas.h | 2 | ||||
-rw-r--r-- | src/libjin/Graphics/Color.cpp | 16 | ||||
-rw-r--r-- | src/libjin/Graphics/Color.h | 38 | ||||
-rw-r--r-- | src/libjin/Graphics/Drawable.h | 7 | ||||
-rw-r--r-- | src/libjin/Graphics/Graphics.h | 1 | ||||
-rw-r--r-- | src/libjin/Graphics/Shader.cpp | 14 | ||||
-rw-r--r-- | src/libjin/Graphics/Shader.h | 4 | ||||
-rw-r--r-- | src/libjin/Graphics/Texture.cpp | 92 | ||||
-rw-r--r-- | src/libjin/Graphics/Texture.h | 15 | ||||
-rw-r--r-- | src/libjin/Graphics/Window.h | 4 | ||||
-rw-r--r-- | src/libjin/Graphics/base.shader.h | 4 | ||||
-rw-r--r-- | src/libjin/Math/Vector.cpp | 24 | ||||
-rw-r--r-- | src/libjin/Math/Vector.h | 24 | ||||
-rw-r--r-- | src/libjin/Math/Vector2.cpp | 9 | ||||
-rw-r--r-- | src/libjin/Math/Vector2.h | 29 | ||||
-rw-r--r-- | src/libjin/math/vector.cpp | 24 | ||||
-rw-r--r-- | src/libjin/math/vector.h | 24 |
19 files changed, 272 insertions, 198 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 |