aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-16 03:54:26 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-16 03:54:26 +0100
commit61c7efc6dd2a036b5d14aa223d06a18cb7d1388e (patch)
tree602a5553bc780ea438a5e0780806a35d2d98ba44 /src
parentb82049c7ad77603537d419bdd0ebebfd3f007916 (diff)
Sprite: add rotation and origin
Diffstat (limited to 'src')
-rw-r--r--src/gl.c1
-rw-r--r--src/graphics/sprite.c25
2 files changed, 20 insertions, 6 deletions
diff --git a/src/gl.c b/src/gl.c
index 5999b80..4d8fb58 100644
--- a/src/gl.c
+++ b/src/gl.c
@@ -57,6 +57,7 @@ int mgl_gl_load(mgl_gl *self) {
{ &self->glLoadIdentity, "glLoadIdentity" },
{ &self->glLoadMatrixf, "glLoadMatrixf" },
{ &self->glTranslatef, "glTranslatef" },
+ { &self->glRotatef, "glRotatef" },
{ &self->glGenBuffers, "glGenBuffers" },
{ &self->glBindBuffer, "glBindBuffer" },
{ &self->glDeleteBuffers, "glDeleteBuffers" },
diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c
index 2aecdd1..df3e902 100644
--- a/src/graphics/sprite.c
+++ b/src/graphics/sprite.c
@@ -2,11 +2,13 @@
#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) {
+void mgl_sprite_init(mgl_sprite *self, mgl_texture *texture) {
self->texture = texture;
self->color = (mgl_color){ 255, 255, 255, 255 };
- self->position = (mgl_vec2f){ x, y };
+ self->position = (mgl_vec2f){ 0.0f, 0.0f };
self->scale = (mgl_vec2f){ 1.0f, 1.0f };
+ self->origin = (mgl_vec2f){ 0.0f, 0.0f };
+ self->rotation = 0.0f;
}
void mgl_sprite_set_texture(mgl_sprite *self, mgl_texture *texture) {
@@ -21,6 +23,14 @@ void mgl_sprite_set_color(mgl_sprite *self, mgl_color color) {
self->color = color;
}
+void mgl_sprite_set_rotation(mgl_sprite *self, float degrees) {
+ self->rotation = degrees;
+}
+
+void mgl_sprite_set_origin(mgl_sprite *self, mgl_vec2f origin) {
+ self->origin = origin;
+}
+
/* 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) {
if(!sprite->texture)
@@ -35,18 +45,21 @@ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) {
context->gl.glColor4ub(sprite->color.r, sprite->color.g, sprite->color.b, sprite->color.a);
mgl_texture_use(sprite->texture);
+ context->gl.glTranslatef(sprite->position.x, sprite->position.y, 0.0f);
+ context->gl.glRotatef(sprite->rotation, 0.0f, 0.0f, 1.0f);
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.glVertex3f(-sprite->origin.x * sprite->scale.x, -sprite->origin.y * sprite->scale.y, 0.0f);
context->gl.glTexCoord2f(texture_right, 0.0f);
- context->gl.glVertex3f(sprite->position.x + sprite->texture->width * sprite->scale.x, sprite->position.y, 0.0f);
+ context->gl.glVertex3f((sprite->texture->width - sprite->origin.x) * sprite->scale.x, -sprite->origin.y * sprite->scale.y, 0.0f);
context->gl.glTexCoord2f(texture_right, texture_bottom);
- 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.glVertex3f((sprite->texture->width - sprite->origin.x) * sprite->scale.x, (sprite->texture->height - sprite->origin.y) * sprite->scale.y, 0.0f);
context->gl.glTexCoord2f(0.0f, texture_bottom);
- context->gl.glVertex3f(sprite->position.x, sprite->position.y + sprite->texture->height * sprite->scale.y, 0.0f);
+ context->gl.glVertex3f(-sprite->origin.x * sprite->scale.x, (sprite->texture->height - sprite->origin.y) * sprite->scale.y, 0.0f);
context->gl.glEnd();
mgl_texture_use(NULL);
+ context->gl.glLoadIdentity(); /* TODO: Remove, but what about glRotatef above */
}