From fc98ecc0fb9461688a5f82515248f56f507080f2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 18 Oct 2021 01:39:47 +0200 Subject: Add set_position and set_color to rectangle,sprite and text --- src/graphics/sprite.c | 15 ++--- src/graphics/text.c | 8 +++ src/window.c | 167 -------------------------------------------------- src/window/window.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 183 insertions(+), 174 deletions(-) delete mode 100644 src/window.c create mode 100644 src/window/window.c (limited to 'src') diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c index 274278c..2b290bb 100644 --- a/src/graphics/sprite.c +++ b/src/graphics/sprite.c @@ -9,6 +9,14 @@ 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_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) { context->gl.glColor4f(sprite->color.r, sprite->color.g, sprite->color.b, sprite->color.a); @@ -28,10 +36,3 @@ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) { 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/graphics/text.c b/src/graphics/text.c index 204d1e0..9afc204 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -14,6 +14,14 @@ void mgl_text_deinit(mgl_text *self) { } +void mgl_text_set_position(mgl_text *self, mgl_vec2f position) { + self->position = position; +} + +void mgl_text_set_color(mgl_text *self, mgl_color color) { + self->color = color; +} + static void mgl_text_draw_glyph(mgl_context *context, mgl_font_glyph *glyph, mgl_vec2f position) { context->gl.glTexCoord2f(glyph->texture_position.x, glyph->texture_position.y); context->gl.glVertex3f(position.x + glyph->position.x, position.y + glyph->position.y, 0.0f); diff --git a/src/window.c b/src/window.c deleted file mode 100644 index 8042ad3..0000000 --- a/src/window.c +++ /dev/null @@ -1,167 +0,0 @@ -#include "../include/mgl/window/window.h" -#include "../include/mgl/mgl.h" -#include -#include -#include - -/* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ -static void set_vertical_sync_enabled(Window window, int enabled) { - int result = 0; - mgl_context *context = mgl_get_context(); - - if(context->gl.glXSwapIntervalEXT) { - context->gl.glXSwapIntervalEXT(context->connection, window, enabled ? 1 : 0); - } else if(context->gl.glXSwapIntervalMESA) { - result = context->gl.glXSwapIntervalMESA(enabled ? 1 : 0); - } else if(context->gl.glXSwapIntervalSGI) { - result = context->gl.glXSwapIntervalSGI(enabled ? 1 : 0); - } else { - static int warned = 0; - if (!warned) { - warned = 1; - fprintf(stderr, "Warning: setting vertical sync not supported\n"); - } - } - - if(result != 0) - 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->size.x = width; - self->size.y = height; - context->gl.glViewport(0, 0, self->size.x, self->size.y); - 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, 0, callback); -} - -int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, unsigned long parent_window, mgl_window_callback *callback) { - self->window = 0; - self->callback = *callback; - - mgl_context *context = mgl_get_context(); - - if(parent_window == 0) - parent_window = DefaultRootWindow(context->connection); - - Colormap color_map = XCreateColormap(context->connection, DefaultRootWindow(context->connection), ((XVisualInfo*)context->visual_info)->visual, AllocNone); - if(!color_map) { - fprintf(stderr, "XCreateColormap failed\n"); - return -1; - } - - XSetWindowAttributes window_attr; - window_attr.colormap = color_map; - window_attr.event_mask = - KeyPressMask | KeyReleaseMask | - ButtonPressMask | ButtonReleaseMask | - PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask | ButtonMotionMask | - StructureNotifyMask; - window_attr.bit_gravity = NorthWestGravity; - - 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 | CWBitGravity, &window_attr); - XFreeColormap(context->connection, color_map); - if(!self->window) { - fprintf(stderr, "XCreateWindow failed\n"); - mgl_window_deinit(self); - return -1; - } - - XStoreName(context->connection, self->window, title); - XMapWindow(context->connection, self->window); - XFlush(context->connection); - - /* TODO: Switch current when rendering to another window, and set current to NULL when destroying the currently selected context */ - context->gl.glXMakeCurrent(context->connection, self->window, context->glx_context); - set_vertical_sync_enabled(self->window, 1); - 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); - - Window dummy_w; - int dummy_i; - unsigned int dummy_u; - XQueryPointer(context->connection, self->window, &dummy_w, &dummy_w, &dummy_i, &dummy_i, &self->cursor_position.x, &self->cursor_position.y, &dummy_u); - - mgl_window_on_resize(self, gwa.width, gwa.height); - mgl_window_draw(self); - return 0; -} - -void mgl_window_deinit(mgl_window *self) { - mgl_context *context = mgl_get_context(); - if(self->window) { - XDestroyWindow(context->connection, self->window); - self->window = 0; - } -} - -static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev) { - switch(xev->type) { - case ConfigureNotify: { - while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, ConfigureNotify, xev)) {} - if(xev->xconfigure.width != self->size.x || xev->xconfigure.height != self->size.x) { - mgl_window_on_resize(self, xev->xconfigure.width, xev->xconfigure.height); - /*fprintf(stderr, "resize!\n");*/ - } - break; - } - case MotionNotify: { - while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, MotionNotify, xev)) {} - self->cursor_position.x = xev->xmotion.x; - self->cursor_position.y = xev->xmotion.y; - break; - } - } -} - -void mgl_window_events_poll(mgl_window *self) { - Display *display = mgl_get_context()->connection; - const int x11_fd = ConnectionNumber(display); - - fd_set in_fds; - FD_ZERO(&in_fds); /* TODO: Optimize */ - FD_SET(x11_fd, &in_fds); - - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - /*tv.tv_sec = timeout_ms / 1000; - tv.tv_usec = (timeout_ms * 1000) - (tv.tv_sec * 1000 * 1000);*/ - - /* TODO: Is this needed when using XPending? */ - /*const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv);*/ - const int num_ready_fds = 1; - if(num_ready_fds > 0) { - XEvent xev; - while(self->window && XPending(display)) { - XNextEvent(display, &xev); - mgl_window_on_receive_event(self, &xev); - } - } else if(num_ready_fds == -1 && errno != EINTR) { - /* TODO: */ - fprintf(stderr, "Disconnected!\n"); - } -} - -void mgl_window_draw(mgl_window *self) { - mgl_context *context = mgl_get_context(); - context->gl.glClear(GL_COLOR_BUFFER_BIT); - context->gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - if(self->callback.draw) - self->callback.draw(self, self->callback.userdata); - context->gl.glXSwapBuffers(context->connection, self->window); -} diff --git a/src/window/window.c b/src/window/window.c new file mode 100644 index 0000000..3c5ce56 --- /dev/null +++ b/src/window/window.c @@ -0,0 +1,167 @@ +#include "../../include/mgl/window/window.h" +#include "../../include/mgl/mgl.h" +#include +#include +#include + +/* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ +static void set_vertical_sync_enabled(Window window, int enabled) { + int result = 0; + mgl_context *context = mgl_get_context(); + + if(context->gl.glXSwapIntervalEXT) { + context->gl.glXSwapIntervalEXT(context->connection, window, enabled ? 1 : 0); + } else if(context->gl.glXSwapIntervalMESA) { + result = context->gl.glXSwapIntervalMESA(enabled ? 1 : 0); + } else if(context->gl.glXSwapIntervalSGI) { + result = context->gl.glXSwapIntervalSGI(enabled ? 1 : 0); + } else { + static int warned = 0; + if (!warned) { + warned = 1; + fprintf(stderr, "Warning: setting vertical sync not supported\n"); + } + } + + if(result != 0) + 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->size.x = width; + self->size.y = height; + context->gl.glViewport(0, 0, self->size.x, self->size.y); + 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, 0, callback); +} + +int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, unsigned long parent_window, mgl_window_callback *callback) { + self->window = 0; + self->callback = *callback; + + mgl_context *context = mgl_get_context(); + + if(parent_window == 0) + parent_window = DefaultRootWindow(context->connection); + + Colormap color_map = XCreateColormap(context->connection, DefaultRootWindow(context->connection), ((XVisualInfo*)context->visual_info)->visual, AllocNone); + if(!color_map) { + fprintf(stderr, "XCreateColormap failed\n"); + return -1; + } + + XSetWindowAttributes window_attr; + window_attr.colormap = color_map; + window_attr.event_mask = + KeyPressMask | KeyReleaseMask | + ButtonPressMask | ButtonReleaseMask | + PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask | ButtonMotionMask | + StructureNotifyMask; + window_attr.bit_gravity = NorthWestGravity; + + 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 | CWBitGravity, &window_attr); + XFreeColormap(context->connection, color_map); + if(!self->window) { + fprintf(stderr, "XCreateWindow failed\n"); + mgl_window_deinit(self); + return -1; + } + + XStoreName(context->connection, self->window, title); + XMapWindow(context->connection, self->window); + XFlush(context->connection); + + /* TODO: Switch current when rendering to another window, and set current to NULL when destroying the currently selected context */ + context->gl.glXMakeCurrent(context->connection, self->window, context->glx_context); + set_vertical_sync_enabled(self->window, 1); + 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); + + Window dummy_w; + int dummy_i; + unsigned int dummy_u; + XQueryPointer(context->connection, self->window, &dummy_w, &dummy_w, &dummy_i, &dummy_i, &self->cursor_position.x, &self->cursor_position.y, &dummy_u); + + mgl_window_on_resize(self, gwa.width, gwa.height); + mgl_window_draw(self); + return 0; +} + +void mgl_window_deinit(mgl_window *self) { + mgl_context *context = mgl_get_context(); + if(self->window) { + XDestroyWindow(context->connection, self->window); + self->window = 0; + } +} + +static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev) { + switch(xev->type) { + case ConfigureNotify: { + while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, ConfigureNotify, xev)) {} + if(xev->xconfigure.width != self->size.x || xev->xconfigure.height != self->size.x) { + mgl_window_on_resize(self, xev->xconfigure.width, xev->xconfigure.height); + /*fprintf(stderr, "resize!\n");*/ + } + break; + } + case MotionNotify: { + while(XCheckTypedWindowEvent(mgl_get_context()->connection, self->window, MotionNotify, xev)) {} + self->cursor_position.x = xev->xmotion.x; + self->cursor_position.y = xev->xmotion.y; + break; + } + } +} + +void mgl_window_events_poll(mgl_window *self) { + Display *display = mgl_get_context()->connection; + const int x11_fd = ConnectionNumber(display); + + fd_set in_fds; + FD_ZERO(&in_fds); /* TODO: Optimize */ + FD_SET(x11_fd, &in_fds); + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + /*tv.tv_sec = timeout_ms / 1000; + tv.tv_usec = (timeout_ms * 1000) - (tv.tv_sec * 1000 * 1000);*/ + + /* TODO: Is this needed when using XPending? */ + /*const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv);*/ + const int num_ready_fds = 1; + if(num_ready_fds > 0) { + XEvent xev; + while(self->window && XPending(display)) { + XNextEvent(display, &xev); + mgl_window_on_receive_event(self, &xev); + } + } else if(num_ready_fds == -1 && errno != EINTR) { + /* TODO: */ + fprintf(stderr, "Disconnected!\n"); + } +} + +void mgl_window_draw(mgl_window *self) { + mgl_context *context = mgl_get_context(); + context->gl.glClear(GL_COLOR_BUFFER_BIT); + context->gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + if(self->callback.draw) + self->callback.draw(self, self->callback.userdata); + context->gl.glXSwapBuffers(context->connection, self->window); +} -- cgit v1.2.3