diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-10-16 07:04:34 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-10-16 07:04:34 +0200 |
commit | 97f1b1c735775d1e22412bbcf98ef403f9ee2275 (patch) | |
tree | 80c2d729a20996d673e7081c4213b09be1076a74 /src/graphics | |
parent | 08dcbfa7772149bcdcc0ab660a897853a30103a0 (diff) |
Add rectangle and sprite, use pixel coordinates, remote opengl dependency from test
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/rectangle.c | 12 | ||||
-rw-r--r-- | src/graphics/sprite.c | 37 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/graphics/rectangle.c b/src/graphics/rectangle.c new file mode 100644 index 0000000..4f2e40b --- /dev/null +++ b/src/graphics/rectangle.c @@ -0,0 +1,12 @@ +#include "../../include/mgl/graphics/rectangle.h" +#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.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); + context->gl.glVertex3f(rect->position.x + rect->size.x, rect->position.y + rect->size.y, 0.0f); + context->gl.glVertex3f(rect->position.x, rect->position.y + rect->size.y, 0.0f); + context->gl.glEnd(); +} diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c new file mode 100644 index 0000000..1ec942a --- /dev/null +++ b/src/graphics/sprite.c @@ -0,0 +1,37 @@ +#include "../../include/mgl/graphics/sprite.h" +#include "../../include/mgl/graphics/texture.h" +#include "../../include/mgl/mgl.h" + +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->position = (mgl_vec2f){ x, y }; + self->scale = (mgl_vec2f){ 1.0f, 1.0f }; +} + +/* 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.glBindTexture(GL_TEXTURE_2D, sprite->texture->id); + context->gl.glBegin(GL_QUADS); + context->gl.glTexCoord2f(0.0f, 0.0f); + context->gl.glVertex3f(sprite->position.x, sprite->position.y, 0.0f); + + context->gl.glTexCoord2f(1.0f, 0.0f); + context->gl.glVertex3f(sprite->position.x + sprite->texture->width * sprite->scale.x, sprite->position.y, 0.0f); + + context->gl.glTexCoord2f(1.0f, 1.0f); + context->gl.glVertex3f(sprite->position.x + sprite->texture->width * sprite->scale.x, sprite->position.y + sprite->texture->height * sprite->scale.y, 0.0f); + + context->gl.glTexCoord2f(0.0f, 1.0f); + context->gl.glVertex3f(sprite->position.x, sprite->position.y + sprite->texture->height * sprite->scale.y, 0.0f); + context->gl.glEnd(); + context->gl.glBindTexture(GL_TEXTURE_2D, 0); +} + +void mgl_sprite_set_color(mgl_sprite *self, float r, float g, float b, float a) { + self->color.r = r; + self->color.g = g; + self->color.b = b; + self->color.a = a; +} |