aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--include/mgl/gl.h5
-rw-r--r--include/mgl/window.h6
-rw-r--r--src/graphics/texture.c13
-rw-r--r--src/window.c12
-rw-r--r--tests/main.c8
6 files changed, 33 insertions, 14 deletions
diff --git a/README.md b/README.md
index 1b80299..4fed650 100644
--- a/README.md
+++ b/README.md
@@ -7,4 +7,5 @@ Written in C.
## Runtime
`libglvnd (libGL.so)`
## TODO
-Handle window close (window destroyed event, disconnected from server and socket becomes invalid (check select return?)) \ No newline at end of file
+Handle window close (window destroyed event, disconnected from server and socket becomes invalid (check select return?)).\
+Bind texture and cache the bound texture to reduce calls to opengl. \ No newline at end of file
diff --git a/include/mgl/gl.h b/include/mgl/gl.h
index e715ec2..7d9900c 100644
--- a/include/mgl/gl.h
+++ b/include/mgl/gl.h
@@ -39,6 +39,11 @@
#define GL_LUMINANCE8_ALPHA8 0x8045
#define GL_RGB8 0x8051
#define GL_RGBA8 0x8058
+#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
+#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
+#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
+#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
+
#define GL_CLAMP_TO_EDGE 0x812F
#define GL_LINEAR 0x2601
diff --git a/include/mgl/window.h b/include/mgl/window.h
index 7db4ecb..1cee698 100644
--- a/include/mgl/window.h
+++ b/include/mgl/window.h
@@ -4,17 +4,17 @@
typedef struct mgl_window mgl_window;
typedef struct {
+ void *userdata;
void (*draw)(mgl_window *window, void *userdata);
} mgl_window_callback;
struct mgl_window {
unsigned long window;
mgl_window_callback callback;
- void *callback_userdata;
};
-int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *callback, void *userdata);
-int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, unsigned long parent_window, mgl_window_callback *callback, void *userdata);
+int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *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);
void mgl_window_deinit(mgl_window *self);
void mgl_window_events_poll(mgl_window *self);
diff --git a/src/graphics/texture.c b/src/graphics/texture.c
index 3d3ea6f..ebc4e58 100644
--- a/src/graphics/texture.c
+++ b/src/graphics/texture.c
@@ -19,6 +19,16 @@ static int mgl_texture_format_to_opengl_format(mgl_texture_format format) {
return 0;
}
+static int mgl_texture_format_to_compressed_opengl_format(mgl_texture_format format) {
+ switch(format) {
+ case MGL_TEXTURE_GRAY: return GL_LUMINANCE8;
+ case MGL_TEXTURE_GRAY_ALPHA: return GL_LUMINANCE8_ALPHA8;
+ case MGL_TEXTURE_RGB: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
+ case MGL_TEXTURE_RGB_ALPHA: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
+ }
+ return 0;
+}
+
static int mgl_texture_format_to_source_opengl_format(mgl_texture_format format) {
switch(format) {
case MGL_TEXTURE_GRAY: return GL_LUMINANCE8_ALPHA8;
@@ -30,6 +40,7 @@ static int mgl_texture_format_to_source_opengl_format(mgl_texture_format format)
}
/* TODO: Ensure texture is power of 2 if the hardware doesn't support non power of two textures */
+/* TODO: Verify if source format should always be 4 components (RGBA) because apparently if its another format then opengl will internally convert it to RGBA */
int mgl_texture_load_from_file(mgl_texture *self, const char *filepath) {
self->id = 0;
@@ -48,7 +59,7 @@ int mgl_texture_load_from_file(mgl_texture *self, const char *filepath) {
return -1;
}
- const int opengl_texture_format = mgl_texture_format_to_opengl_format(self->format);
+ const int opengl_texture_format = mgl_texture_format_to_compressed_opengl_format(self->format);
context->gl.glBindTexture(GL_TEXTURE_2D, self->id);
context->gl.glTexImage2D(GL_TEXTURE_2D, 0, opengl_texture_format, self->width, self->height, 0, mgl_texture_format_to_source_opengl_format(self->format), GL_UNSIGNED_BYTE, image_data);
diff --git a/src/window.c b/src/window.c
index 7d13ffe..ccfab93 100644
--- a/src/window.c
+++ b/src/window.c
@@ -1,6 +1,7 @@
#include "../include/mgl/window.h"
#include "../include/mgl/mgl.h"
#include <X11/Xutil.h>
+#include <errno.h>
#include <stdio.h>
/* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */
@@ -26,14 +27,13 @@ static void set_vertical_sync_enabled(Window window, int enabled) {
fprintf(stderr, "Warning: setting vertical sync failed\n");
}
-int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *callback, void *userdata) {
- return mgl_window_create_with_params(self, title, width, height, DefaultRootWindow(mgl_get_context()->connection), callback, userdata);
+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);
}
-int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, unsigned long parent_window, mgl_window_callback *callback, void *userdata) {
+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;
- self->callback_userdata = userdata;
mgl_context *context = mgl_get_context();
@@ -104,7 +104,7 @@ void mgl_window_events_poll(mgl_window *self) {
XNextEvent(display, &xev);
on_receive_x11_event(self, &xev);
}
- } else if(num_ready_fds == -1) {
+ } else if(num_ready_fds == -1 && errno != EINTR) {
/* TODO: */
fprintf(stderr, "Disconnected!\n");
}
@@ -121,6 +121,6 @@ void mgl_window_draw(mgl_window *self) {
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);
+ self->callback.draw(self, self->callback.userdata);
context->gl.glXSwapBuffers(context->connection, self->window);
}
diff --git a/tests/main.c b/tests/main.c
index fdd6cfe..7a40d80 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -43,14 +43,16 @@ int main(int argc, char **argv) {
return 1;
mgl_texture texture;
- mgl_window_callback window_callback;
- window_callback.draw = draw;
Userdata userdata;
userdata.texture = &texture;
+ mgl_window_callback window_callback;
+ window_callback.userdata = &userdata;
+ window_callback.draw = draw;
+
mgl_window window;
- if(mgl_window_create(&window, "mgl", 1280, 720, &window_callback, &userdata) != 0)
+ if(mgl_window_create(&window, "mgl", 1280, 720, &window_callback) != 0)
return 1;
if(mgl_texture_load_from_file(&texture, "X11.png") != 0)