From 3bdf82eec2c915e91ae487e29d72639f9efcad67 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 19 Oct 2021 07:04:21 +0200 Subject: Use uint8_t for color instead of float --- include/mgl/gl.h | 4 ++-- include/mgl/graphics/color.h | 4 +++- project.conf | 2 +- src/gl.c | 5 ++--- src/graphics/rectangle.c | 2 +- src/graphics/sprite.c | 4 ++-- src/graphics/text.c | 4 ++-- tests/main.c | 4 ++-- 8 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/mgl/gl.h b/include/mgl/gl.h index 36b2743..6de98d9 100644 --- a/include/mgl/gl.h +++ b/include/mgl/gl.h @@ -2,6 +2,7 @@ #define MGL_GL_H #include "gl_macro.h" +#include typedef struct _XVisualInfo _XVisualInfo; typedef struct _XDisplay Display; @@ -31,9 +32,8 @@ typedef struct { void (*glHint)(unsigned int target, unsigned int mode); void (*glBegin)(unsigned int mode); void (*glEnd)(void); - void (*glColor3f)(float red, float green, float blue); void (*glVertex3f)(float x, float y, float z); - void (*glColor4f)(float red, float green, float blue, float alpha); + void (*glColor4ub)(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); void (*glTexCoord2f)(float s, float t); void (*glOrtho)(double left, double right, double bottom, double top, double near_val, double far_val); void (*glMatrixMode)(unsigned int mode); diff --git a/include/mgl/graphics/color.h b/include/mgl/graphics/color.h index 62ded72..68cc0c1 100644 --- a/include/mgl/graphics/color.h +++ b/include/mgl/graphics/color.h @@ -1,8 +1,10 @@ #ifndef MGL_COLOR_H #define MGL_COLOR_H +#include + typedef struct { - float r, g, b, a; + uint8_t r, g, b, a; } mgl_color; #endif /* MGL_COLOR_H */ diff --git a/project.conf b/project.conf index 62353a2..8b939d6 100644 --- a/project.conf +++ b/project.conf @@ -8,4 +8,4 @@ platforms = ["posix"] expose_include_dirs = ["include"] [dependencies] -x11 = ">=1" \ No newline at end of file +x11 = "1" \ No newline at end of file diff --git a/src/gl.c b/src/gl.c index a06f75a..45280f1 100644 --- a/src/gl.c +++ b/src/gl.c @@ -1,6 +1,6 @@ #include "../include/mgl/gl.h" #include -/*#include */ +#include #include typedef struct { @@ -48,9 +48,8 @@ int mgl_gl_load(mgl_gl *self) { { &self->glHint, "glHint" }, { &self->glBegin, "glBegin" }, { &self->glEnd, "glEnd" }, - { &self->glColor3f, "glColor3f" }, { &self->glVertex3f, "glVertex3f" }, - { &self->glColor4f, "glColor4f" }, + { &self->glColor4ub, "glColor4ub" }, { &self->glTexCoord2f, "glTexCoord2f" }, { &self->glOrtho, "glOrtho" }, { &self->glMatrixMode, "glMatrixMode" }, diff --git a/src/graphics/rectangle.c b/src/graphics/rectangle.c index 4f2e40b..c6b2126 100644 --- a/src/graphics/rectangle.c +++ b/src/graphics/rectangle.c @@ -2,7 +2,7 @@ #include "../../include/mgl/mgl.h" void mgl_rectangle_draw(mgl_context *context, mgl_rectangle *rect) { - context->gl.glColor4f(rect->color.r, rect->color.g, rect->color.b, rect->color.a); + context->gl.glColor4ub(rect->color.r, rect->color.g, rect->color.b, rect->color.a); context->gl.glBegin(GL_QUADS); context->gl.glVertex3f(rect->position.x, rect->position.y, 0.0f); context->gl.glVertex3f(rect->position.x + rect->size.x, rect->position.y, 0.0f); diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c index 2b290bb..7bc9db1 100644 --- a/src/graphics/sprite.c +++ b/src/graphics/sprite.c @@ -4,7 +4,7 @@ void mgl_sprite_init(mgl_sprite *self, mgl_texture *texture, float x, float y) { self->texture = texture; - self->color = (mgl_color){ 1.0f, 1.0f, 1.0f, 1.0f }; + self->color = (mgl_color){ 255, 255, 255, 255 }; self->position = (mgl_vec2f){ x, y }; self->scale = (mgl_vec2f){ 1.0f, 1.0f }; } @@ -19,7 +19,7 @@ void mgl_sprite_set_color(mgl_sprite *self, mgl_color color) { /* TODO: Cache texture bind to not bind texture if its already bound and do not bind texture 0 */ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) { - context->gl.glColor4f(sprite->color.r, sprite->color.g, sprite->color.b, sprite->color.a); + context->gl.glColor4ub(sprite->color.r, sprite->color.g, sprite->color.b, sprite->color.a); context->gl.glBindTexture(GL_TEXTURE_2D, sprite->texture->id); context->gl.glBegin(GL_QUADS); context->gl.glTexCoord2f(0.0f, 0.0f); diff --git a/src/graphics/text.c b/src/graphics/text.c index 9afc204..0aa33cf 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -5,7 +5,7 @@ int mgl_text_init(mgl_text *self, mgl_font *font, const char *text, float x, float y) { self->font = font; self->text = text; - self->color = (mgl_color){ 1.0f, 1.0f, 1.0f, 1.0f }; + self->color = (mgl_color){ 255, 255, 255, 255 }; self->position = (mgl_vec2f){ x, y }; return 0; } @@ -44,7 +44,7 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { mgl_vec2f position = text->position; position.y += text->font->size; - context->gl.glColor4f(text->color.r, text->color.g, text->color.b, text->color.a); + context->gl.glColor4ub(text->color.r, text->color.g, text->color.b, text->color.a); context->gl.glBindTexture(GL_TEXTURE_2D, text->font->texture.id); context->gl.glBegin(GL_QUADS); while(*str) { diff --git a/tests/main.c b/tests/main.c index 5841924..38a0931 100644 --- a/tests/main.c +++ b/tests/main.c @@ -17,7 +17,7 @@ static void draw(mgl_window *window, void *userdata) { Userdata *u = userdata; mgl_rectangle rect = { - .color = { 1.0f, 0.0f, 0.0f, 1.0f }, + .color = { 255, 0, 0, 255 }, .position = { window->cursor_position.x, window->cursor_position.y }, .size = { 100.0f, 500.0f } }; @@ -25,7 +25,7 @@ static void draw(mgl_window *window, void *userdata) { mgl_sprite sprite; mgl_sprite_init(&sprite, u->texture, 100.0f - 10.0f, 0.0f); - mgl_sprite_set_color(&sprite, (mgl_color){1.0f, 1.0f, 1.0f, 0.5f}); + mgl_sprite_set_color(&sprite, (mgl_color){255, 255, 255, 128}); mgl_sprite_draw(context, &sprite); mgl_text text; -- cgit v1.2.3