diff options
Diffstat (limited to 'src/libjin/Graphics')
-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 |
11 files changed, 95 insertions, 35 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) |