aboutsummaryrefslogtreecommitdiff
path: root/src/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics')
-rw-r--r--src/graphics/primitive_type.c17
-rw-r--r--src/graphics/sprite.c7
-rw-r--r--src/graphics/text.c7
-rw-r--r--src/graphics/vertex.c13
-rw-r--r--src/graphics/vertex_buffer.c15
5 files changed, 44 insertions, 15 deletions
diff --git a/src/graphics/primitive_type.c b/src/graphics/primitive_type.c
new file mode 100644
index 0000000..c970a33
--- /dev/null
+++ b/src/graphics/primitive_type.c
@@ -0,0 +1,17 @@
+#include "../../include/mgl/graphics/primitive_type.h"
+#include "../../include/mgl/gl_macro.h"
+
+unsigned int mgl_primitive_type_to_gl_mode(mgl_primitive_type primitive_type) {
+ switch(primitive_type) {
+ case MGL_PRIMITIVE_POINTS: return GL_POINTS;
+ case MGL_PRIMITIVE_LINES: return GL_LINES;
+ case MGL_PRIMITIVE_LINE_STRIP: return GL_LINE_STRIP;
+ case MGL_PRIMITIVE_TRIANGLES: return GL_TRIANGLES;
+ case MGL_PRIMITIVE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
+ case MGL_PRIMITIVE_TRIANGLE_FAN: return GL_TRIANGLE_FAN;
+ case MGL_PRIMITIVE_QUADS: return GL_QUADS;
+ case MGL_PRIMITIVE_QUAD_STRIP: return GL_QUAD_STRIP;
+ case MGL_PRIMITIVE_POLYGON: return GL_POLYGON;
+ }
+ return 0;
+}
diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c
index 7bc9db1..231b554 100644
--- a/src/graphics/sprite.c
+++ b/src/graphics/sprite.c
@@ -9,6 +9,10 @@ void mgl_sprite_init(mgl_sprite *self, mgl_texture *texture, float x, float 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;
}
@@ -19,6 +23,9 @@ 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) {
+ 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);
diff --git a/src/graphics/text.c b/src/graphics/text.c
index 7f90216..e7b31a0 100644
--- a/src/graphics/text.c
+++ b/src/graphics/text.c
@@ -14,6 +14,10 @@ void mgl_text_deinit(mgl_text *self) {
}
+void mgl_text_set_string(mgl_text *self, const char *str) {
+ self->text = str;
+}
+
void mgl_text_set_position(mgl_text *self, mgl_vec2f position) {
self->position = position;
}
@@ -40,6 +44,9 @@ static void mgl_text_draw_glyph(mgl_context *context, mgl_font_glyph *glyph, mgl
/* TODO: Cache texture bind to not bind texture if its already bound and do not bind texture 0 */
void mgl_text_draw(mgl_context *context, mgl_text *text) {
const char *str = text->text;
+ if(!str)
+ return;
+
mgl_font_glyph glyph;
mgl_vec2f position = text->position;
position.y += text->font->character_size;
diff --git a/src/graphics/vertex.c b/src/graphics/vertex.c
new file mode 100644
index 0000000..629b421
--- /dev/null
+++ b/src/graphics/vertex.c
@@ -0,0 +1,13 @@
+#include "../../include/mgl/graphics/vertex.h"
+#include "../../include/mgl/mgl.h"
+
+void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type) {
+ context->gl.glBegin(mgl_primitive_type_to_gl_mode(primitive_type));
+ for(size_t i = 0; i < vertex_count; ++i) {
+ const mgl_vertex *vertex = &vertices[i];
+ context->gl.glColor4ub(vertex->color.r, vertex->color.g, vertex->color.b, vertex->color.a);
+ context->gl.glTexCoord2f(vertex->texcoords.x, vertex->texcoords.y);
+ context->gl.glVertex3f(vertex->position.x, vertex->position.y, 0.0f);
+ }
+ context->gl.glEnd();
+}
diff --git a/src/graphics/vertex_buffer.c b/src/graphics/vertex_buffer.c
index 746cfeb..ccdcd84 100644
--- a/src/graphics/vertex_buffer.c
+++ b/src/graphics/vertex_buffer.c
@@ -17,21 +17,6 @@ static unsigned int mgl_vertex_buffer_usage_to_gl_usage(mgl_vertex_buffer_usage
return 0;
}
-static unsigned int mgl_primitive_type_to_gl_mode(mgl_primitive_type primitive_type) {
- switch(primitive_type) {
- case MGL_PRIMITIVE_POINTS: return GL_POINTS;
- case MGL_PRIMITIVE_LINES: return GL_LINES;
- case MGL_PRIMITIVE_LINE_STRIP: return GL_LINE_STRIP;
- case MGL_PRIMITIVE_TRIANGLES: return GL_TRIANGLES;
- case MGL_PRIMITIVE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
- case MGL_PRIMITIVE_TRIANGLE_FAN: return GL_TRIANGLE_FAN;
- case MGL_PRIMITIVE_QUADS: return GL_QUADS;
- case MGL_PRIMITIVE_QUAD_STRIP: return GL_QUAD_STRIP;
- case MGL_PRIMITIVE_POLYGON: return GL_POLYGON;
- }
- return 0;
-}
-
int mgl_vertex_buffer_init(mgl_vertex_buffer *self, mgl_primitive_type primitive_type, mgl_vertex_buffer_usage usage) {
self->id = 0;
self->vertex_count = 0;