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 | |
parent | 08dcbfa7772149bcdcc0ab660a897853a30103a0 (diff) |
Add rectangle and sprite, use pixel coordinates, remote opengl dependency from test
Diffstat (limited to 'src')
-rw-r--r-- | src/gl.c | 9 | ||||
-rw-r--r-- | src/graphics/rectangle.c | 12 | ||||
-rw-r--r-- | src/graphics/sprite.c | 37 | ||||
-rw-r--r-- | src/window.c | 32 |
4 files changed, 82 insertions, 8 deletions
@@ -46,6 +46,15 @@ int mgl_gl_load(mgl_gl *self) { { &self->glBindTexture, "glBindTexture" }, { &self->glTexParameteri, "glTexParameteri" }, { &self->glHint, "glHint" }, + { &self->glBegin, "glBegin" }, + { &self->glEnd, "glEnd" }, + { &self->glColor3f, "glColor3f" }, + { &self->glVertex3f, "glVertex3f" }, + { &self->glColor4f, "glColor4f" }, + { &self->glTexCoord2f, "glTexCoord2f" }, + { &self->glOrtho, "glOrtho" }, + { &self->glMatrixMode, "glMatrixMode" }, + { &self->glLoadIdentity, "glLoadIdentity" }, { NULL, NULL } }; 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; +} diff --git a/src/window.c b/src/window.c index ccfab93..818805d 100644 --- a/src/window.c +++ b/src/window.c @@ -27,6 +27,16 @@ static void set_vertical_sync_enabled(Window window, int enabled) { fprintf(stderr, "Warning: setting vertical sync failed\n"); } +static void mgl_window_on_resize(mgl_window *self, int width, int height) { + mgl_context *context = mgl_get_context(); + self->width = width; + self->height = height; + context->gl.glViewport(0, 0, self->width, self->height); + context->gl.glMatrixMode(GL_PROJECTION); + context->gl.glLoadIdentity(); + context->gl.glOrtho(0.0, width, height, 0.0, -1.0, 1.0); +} + int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *callback) { return mgl_window_create_with_params(self, title, width, height, DefaultRootWindow(mgl_get_context()->connection), callback); } @@ -45,7 +55,7 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width XSetWindowAttributes window_attr; window_attr.colormap = color_map; - window_attr.event_mask = KeyPressMask; + window_attr.event_mask = KeyPressMask | StructureNotifyMask; self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask, &window_attr); XFreeColormap(context->connection, color_map); @@ -66,6 +76,13 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width context->gl.glEnable(GL_TEXTURE_2D); context->gl.glEnable(GL_BLEND); context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + XWindowAttributes gwa; + gwa.width = 0; + gwa.height = 0; + XGetWindowAttributes(context->connection, self->window, &gwa); + + mgl_window_on_resize(self, gwa.width, gwa.height); mgl_window_draw(self); return 0; } @@ -79,7 +96,12 @@ void mgl_window_deinit(mgl_window *self) { } static void on_receive_x11_event(mgl_window *window, XEvent *xev) { - + if(xev->type == ConfigureNotify) { + if(xev->xconfigure.width != window->width || xev->xconfigure.height != window->height) { + mgl_window_on_resize(window, xev->xconfigure.width, xev->xconfigure.height); + /*fprintf(stderr, "resize!\n");*/ + } + } } void mgl_window_events_poll(mgl_window *self) { @@ -112,12 +134,6 @@ void mgl_window_events_poll(mgl_window *self) { void mgl_window_draw(mgl_window *self) { mgl_context *context = mgl_get_context(); - - /* TODO: Get window size from window resize event instead */ - XWindowAttributes gwa; - XGetWindowAttributes(context->connection, self->window, &gwa); - - context->gl.glViewport(0, 0, gwa.width, gwa.height); context->gl.glClear(GL_COLOR_BUFFER_BIT); context->gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); if(self->callback.draw) |