Skip to content

Commit

Permalink
fix microui example
Browse files Browse the repository at this point in the history
  • Loading branch information
ColleagueRiley committed Feb 5, 2025
1 parent 4874b0c commit bb72bd3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ debug: all
./examples/gamepad/gamepad
./examples/first-person-camera/camera
./examples/portableGL/pgl$(EXT)
./examples/microui_demo/microui_demo
./examples/gl33/gl33$(EXT)
ifneq ($(NO_GLES), 1)
./examples/gles2/gles2$(EXT)
Expand Down
26 changes: 23 additions & 3 deletions examples/microui_demo/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
static GLfloat tex_buf[BUFFER_SIZE * 8];
static GLfloat vert_buf[BUFFER_SIZE * 8];
static GLubyte color_buf[BUFFER_SIZE * 16];
static GLuint index_buf[BUFFER_SIZE * 6];

static int width = 800;
static int height = 600;
Expand Down Expand Up @@ -51,7 +52,8 @@ static void flush(void) {
glTexCoordPointer(2, GL_FLOAT, 0, tex_buf);
glVertexPointer(2, GL_FLOAT, 0, vert_buf);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, color_buf);
glDrawArrays(GL_TRIANGLES, 0, buf_idx * 6);
glDrawElements(GL_TRIANGLES, buf_idx * 6, GL_UNSIGNED_INT, index_buf);
// glDrawArrays(GL_TRIANGLES, 0, buf_idx * 6);

glMatrixMode(GL_MODELVIEW);
glPopMatrix();
Expand All @@ -61,11 +63,14 @@ static void flush(void) {
buf_idx = 0;
}


static void push_quad(mu_Rect dst, mu_Rect src, mu_Color color) {
if (buf_idx >= BUFFER_SIZE / 6) { flush(); }
if (buf_idx == BUFFER_SIZE) { flush(); }

int texvert_idx = buf_idx * 8;
int texvert_idx = buf_idx * 8;
int color_idx = buf_idx * 16;
int element_idx = buf_idx * 4;
int index_idx = buf_idx * 6;
buf_idx++;

/* update texture buffer */
Expand Down Expand Up @@ -97,12 +102,22 @@ static void push_quad(mu_Rect dst, mu_Rect src, mu_Color color) {
memcpy(color_buf + color_idx + 4, &color, 4);
memcpy(color_buf + color_idx + 8, &color, 4);
memcpy(color_buf + color_idx + 12, &color, 4);

/* update index buffer */
index_buf[index_idx + 0] = element_idx + 0;
index_buf[index_idx + 1] = element_idx + 1;
index_buf[index_idx + 2] = element_idx + 2;
index_buf[index_idx + 3] = element_idx + 2;
index_buf[index_idx + 4] = element_idx + 3;
index_buf[index_idx + 5] = element_idx + 1;
}


void r_draw_rect(mu_Rect rect, mu_Color color) {
push_quad(rect, atlas[ATLAS_WHITE], color);
}


void r_draw_text(const char *text, mu_Vec2 pos, mu_Color color) {
mu_Rect dst = { pos.x, pos.y, 0, 0 };
for (const char *p = text; *p; p++) {
Expand All @@ -116,13 +131,15 @@ void r_draw_text(const char *text, mu_Vec2 pos, mu_Color color) {
}
}


void r_draw_icon(int id, mu_Rect rect, mu_Color color) {
mu_Rect src = atlas[id];
int x = rect.x + (rect.w - src.w) / 2;
int y = rect.y + (rect.h - src.h) / 2;
push_quad(mu_rect(x, y, src.w, src.h), src, color);
}


int r_get_text_width(const char *text, int len) {
int res = 0;
for (const char *p = text; *p && len--; p++) {
Expand All @@ -133,15 +150,18 @@ int r_get_text_width(const char *text, int len) {
return res;
}


int r_get_text_height(void) {
return 18;
}


void r_set_clip_rect(mu_Rect rect) {
flush();
glScissor(rect.x, height - (rect.y + rect.h), rect.w, rect.h);
}


void r_clear(mu_Color clr) {
flush();
glClearColor(clr.r / 255., clr.g / 255., clr.b / 255., clr.a / 255.);
Expand Down

0 comments on commit bb72bd3

Please sign in to comment.