diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-10-23 01:39:57 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-10-23 01:39:57 +0200 |
commit | 898b8c95f1f904307c02e978b57301cf1cb0560f (patch) | |
tree | d0bf51298967533ffb3ca6565ee309948820cf3f | |
parent | df2e6771c5bf6f09e62f9d6b86d83a2631ea365f (diff) |
Allow rendering vertices directly
-rw-r--r-- | include/mgl/graphics/primitive_type.h | 18 | ||||
-rw-r--r-- | include/mgl/graphics/sprite.h | 3 | ||||
-rw-r--r-- | include/mgl/graphics/text.h | 2 | ||||
-rw-r--r-- | include/mgl/graphics/vertex.h | 6 | ||||
-rw-r--r-- | include/mgl/graphics/vertex_buffer.h | 12 | ||||
-rw-r--r-- | src/graphics/primitive_type.c | 17 | ||||
-rw-r--r-- | src/graphics/sprite.c | 7 | ||||
-rw-r--r-- | src/graphics/text.c | 7 | ||||
-rw-r--r-- | src/graphics/vertex.c | 13 | ||||
-rw-r--r-- | src/graphics/vertex_buffer.c | 15 |
10 files changed, 73 insertions, 27 deletions
diff --git a/include/mgl/graphics/primitive_type.h b/include/mgl/graphics/primitive_type.h new file mode 100644 index 0000000..0621a8d --- /dev/null +++ b/include/mgl/graphics/primitive_type.h @@ -0,0 +1,18 @@ +#ifndef MGL_PRIMITIVE_TYPE_H +#define MGL_PRIMITIVE_TYPE_H + +typedef enum { + MGL_PRIMITIVE_POINTS, + MGL_PRIMITIVE_LINES, + MGL_PRIMITIVE_LINE_STRIP, + MGL_PRIMITIVE_TRIANGLES, + MGL_PRIMITIVE_TRIANGLE_STRIP, + MGL_PRIMITIVE_TRIANGLE_FAN, + MGL_PRIMITIVE_QUADS, + MGL_PRIMITIVE_QUAD_STRIP, + MGL_PRIMITIVE_POLYGON, +} mgl_primitive_type; + +unsigned int mgl_primitive_type_to_gl_mode(mgl_primitive_type primitive_type); + +#endif /* MGL_PRIMITIVE_TYPE_H */ diff --git a/include/mgl/graphics/sprite.h b/include/mgl/graphics/sprite.h index 9f0fc51..8c84153 100644 --- a/include/mgl/graphics/sprite.h +++ b/include/mgl/graphics/sprite.h @@ -14,8 +14,11 @@ typedef struct { mgl_vec2f scale; } mgl_sprite; +/* |texture| may be NULL */ void mgl_sprite_init(mgl_sprite *self, mgl_texture *texture, float x, float y); +/* |texture| may be NULL */ +void mgl_sprite_set_texture(mgl_sprite *self, mgl_texture *texture); void mgl_sprite_set_position(mgl_sprite *self, mgl_vec2f position); void mgl_sprite_set_color(mgl_sprite *self, mgl_color color); void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite); diff --git a/include/mgl/graphics/text.h b/include/mgl/graphics/text.h index 0356e82..33c75d0 100644 --- a/include/mgl/graphics/text.h +++ b/include/mgl/graphics/text.h @@ -18,6 +18,8 @@ typedef struct { int mgl_text_init(mgl_text *self, mgl_font *font, const char *text, float x, float y); void mgl_text_deinit(mgl_text *self); +/* Note: keeps a reference to |text|. |text| needs to be valid as long as |self| is used. */ +void mgl_text_set_string(mgl_text *self, const char *str); void mgl_text_set_position(mgl_text *self, mgl_vec2f position); void mgl_text_set_color(mgl_text *self, mgl_color color); void mgl_text_draw(mgl_context *context, mgl_text *text); diff --git a/include/mgl/graphics/vertex.h b/include/mgl/graphics/vertex.h index 69400b4..e4fef34 100644 --- a/include/mgl/graphics/vertex.h +++ b/include/mgl/graphics/vertex.h @@ -1,8 +1,12 @@ #ifndef MGL_VERTEX_H #define MGL_VERTEX_H +#include "primitive_type.h" #include "color.h" #include "../system/vec.h" +#include <stddef.h> + +typedef struct mgl_context mgl_context; typedef struct { mgl_vec2f position; @@ -10,4 +14,6 @@ typedef struct { mgl_color color; } mgl_vertex; +void mgl_vertices_draw(mgl_context *context, const mgl_vertex *vertices, size_t vertex_count, mgl_primitive_type primitive_type); + #endif /* MGL_VERTEX_H */ diff --git a/include/mgl/graphics/vertex_buffer.h b/include/mgl/graphics/vertex_buffer.h index 2a32d3d..5cdadfa 100644 --- a/include/mgl/graphics/vertex_buffer.h +++ b/include/mgl/graphics/vertex_buffer.h @@ -8,18 +8,6 @@ typedef struct mgl_context mgl_context; typedef struct mgl_texture mgl_texture; typedef enum { - MGL_PRIMITIVE_POINTS, - MGL_PRIMITIVE_LINES, - MGL_PRIMITIVE_LINE_STRIP, - MGL_PRIMITIVE_TRIANGLES, - MGL_PRIMITIVE_TRIANGLE_STRIP, - MGL_PRIMITIVE_TRIANGLE_FAN, - MGL_PRIMITIVE_QUADS, - MGL_PRIMITIVE_QUAD_STRIP, - MGL_PRIMITIVE_POLYGON, -} mgl_primitive_type; - -typedef enum { MGL_USAGE_STREAM, MGL_USAGE_DYNAMIC, MGL_USAGE_STATIC 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; |