1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#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){ 255, 255, 255, 255 };
self->position = (mgl_vec2f){ x, y };
self->scale = (mgl_vec2f){ 1.0f, 1.0f };
}
void mgl_sprite_set_texture(mgl_sprite *self, mgl_texture *texture) {
self->texture = texture;
}
void mgl_sprite_set_position(mgl_sprite *self, mgl_vec2f position) {
self->position = position;
}
void mgl_sprite_set_color(mgl_sprite *self, mgl_color color) {
self->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) {
if(!sprite->texture)
return;
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);
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);
}
|