summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-03-06 23:36:34 +0100
committerkdx <kikoodx@paranoici.org>2023-03-06 23:36:34 +0100
commit66b3df86e1a9f9b55fdb547f53f8f56a4f8b64bd (patch)
tree788f4bcc4760ffaa3e00350d29a2bb5805ed10b9
parent365ab142504ef46af2994d79ead7a2a0a01f2e6c (diff)
downloadule-66b3df86e1a9f9b55fdb547f53f8f56a4f8b64bd.tar.gz
initial commit
-rw-r--r--.gitignore1
-rwxr-xr-xbuild.sh2
-rw-r--r--font.c41
-rw-r--r--font.h8
-rw-r--r--main.c90
-rw-r--r--render.c30
-rw-r--r--render.h6
-rw-r--r--res/font.pngbin0 -> 4572 bytes
-rw-r--r--text_size.c14
-rw-r--r--text_size.h5
-rw-r--r--ui.h3
-rw-r--r--ui_draw.c39
12 files changed, 239 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7102b5a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+ule
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..c0a490d
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+gcc -o ule *.c -lSDL2 -lSDL2_image
diff --git a/font.c b/font.c
new file mode 100644
index 0000000..0ce87a7
--- /dev/null
+++ b/font.c
@@ -0,0 +1,41 @@
+#include <SDL2/SDL_error.h>
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_image.h>
+#include "font.h"
+
+extern SDL_Renderer *renderer;
+static SDL_Texture *font_tex = NULL;
+
+int font_init(const char *path)
+{
+ if ((font_tex = IMG_LoadTexture(renderer, path)) == NULL)
+ return 1;
+ return 0;
+}
+
+void font_deinit(void)
+{
+ if (font_tex != NULL) {
+ SDL_DestroyTexture(font_tex);
+ font_tex = NULL;
+ }
+}
+
+void font_draw(int x, int y, const char *s)
+{
+ const int ox = x;
+ for (; *s != '\0'; s++) {
+ const char c = toupper(*s);
+ const int ix = c % 16 * FONT_W;
+ const int iy = c / 16 * FONT_H;
+ x += 1;
+ }
+}
+
+void font_set_color(int r, int g, int b, int a)
+{
+ if (SDL_SetTextureColorMod(font_tex, r, g, b))
+ fprintf(stderr, "%s\n", SDL_GetError());
+ if (SDL_SetTextureAlphaMod(font_tex, a))
+ fprintf(stderr, "%s\n", SDL_GetError());
+}
diff --git a/font.h b/font.h
new file mode 100644
index 0000000..2692554
--- /dev/null
+++ b/font.h
@@ -0,0 +1,8 @@
+#pragma once
+#define FONT_W 16
+#define FONT_H 32
+
+int font_init(const char *path);
+void font_deinit(void);
+void font_draw(int x, int y, const char *s);
+void font_set_color(int r, int g, int b, int a);
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..ba30aa4
--- /dev/null
+++ b/main.c
@@ -0,0 +1,90 @@
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_video.h>
+#include <SDL2/SDL_image.h>
+#include "microui.h"
+#include "ui.h"
+#include "font.h"
+#include "text_size.h"
+#include "render.h"
+
+SDL_Window *window = NULL;
+SDL_Renderer *renderer = NULL;
+mu_Context *mu_ctx = NULL;
+
+static int init(void);
+static void deinit(void);
+
+int main(int argc, char **argv)
+{
+ if (init()) {
+ deinit();
+ return 1;
+ }
+
+ for (;;) {
+ SDL_Event e;
+ while (SDL_PollEvent(&e)) switch (e.type) {
+ case SDL_QUIT:
+ return 0;
+ default:
+ break;
+ }
+
+ r_draw_set_color(0, 0, 0, 1);
+ SDL_RenderClear(renderer);
+ ui_draw();
+ SDL_RenderPresent(renderer);
+ }
+ return 0;
+}
+
+static int init(void)
+{
+ if (SDL_Init(SDL_INIT_VIDEO)) {
+ fprintf(stderr, "%s\n", SDL_GetError());
+ return 1;
+ }
+ if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
+ fprintf(stderr, "%s\n", IMG_GetError());
+ return 1;
+ }
+ if (SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_RESIZABLE,
+ &window, &renderer)) {
+ fprintf(stderr, "%s\n", SDL_GetError());
+ return 1;
+ }
+ if (font_init("res/font.png"))
+ return 1;
+ if ((mu_ctx = malloc(sizeof(mu_Context))) == NULL) {
+ perror("init");
+ return 1;
+ }
+ mu_init(mu_ctx);
+ mu_ctx->text_width = text_width;
+ mu_ctx->text_height = text_height;
+ if (atexit(deinit)) {
+ perror("init");
+ return 1;
+ }
+ return 0;
+}
+
+static void deinit(void)
+{
+ if (mu_ctx != NULL) {
+ free(mu_ctx);
+ mu_ctx = NULL;
+ }
+ font_deinit();
+ if (renderer != NULL) {
+ SDL_DestroyRenderer(renderer);
+ renderer = NULL;
+ }
+ if (window != NULL) {
+ SDL_DestroyWindow(window);
+ window = NULL;
+ }
+ IMG_Quit();
+ SDL_Quit();
+}
diff --git a/render.c b/render.c
new file mode 100644
index 0000000..9cb86b4
--- /dev/null
+++ b/render.c
@@ -0,0 +1,30 @@
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_surface.h>
+#include "render.h"
+
+extern SDL_Renderer *renderer;
+
+void r_draw_set_color(int r, int g, int b, int a)
+{
+ SDL_SetRenderDrawColor(renderer, r, g, b, a);
+}
+
+void r_draw_fill_rect(int x, int y, int w, int h)
+{
+ const SDL_Rect rect = { x, y, w, h };
+ if (SDL_RenderDrawRect(renderer, &rect))
+ fprintf(stderr, "%s\n", SDL_GetError());
+}
+
+void r_set_clip_rect(int x, int y, int w, int h)
+{
+ const SDL_Rect rect = { x, y, w, h };
+ if (SDL_RenderSetClipRect(renderer, &rect))
+ fprintf(stderr, "%s\n", SDL_GetError());
+}
+
+void r_reset_clip_rect(void)
+{
+ if (SDL_RenderSetClipRect(renderer, NULL))
+ fprintf(stderr, "%s\n", SDL_GetError());
+}
diff --git a/render.h b/render.h
new file mode 100644
index 0000000..7ce78fb
--- /dev/null
+++ b/render.h
@@ -0,0 +1,6 @@
+#pragma once
+
+void r_draw_set_color(int r, int g, int b, int a);
+void r_draw_fill_rect(int x, int y, int w, int h);
+void r_set_clip_rect(int x, int y, int w, int h);
+void r_reset_clip_rect(void);
diff --git a/res/font.png b/res/font.png
new file mode 100644
index 0000000..cdfdaa9
--- /dev/null
+++ b/res/font.png
Binary files differ
diff --git a/text_size.c b/text_size.c
new file mode 100644
index 0000000..db718dd
--- /dev/null
+++ b/text_size.c
@@ -0,0 +1,14 @@
+#include "text_size.h"
+#include "font.h"
+
+int text_width(mu_Font font, const char *s, int len)
+{
+ (void)font, (void)s;
+ return len * FONT_W;
+}
+
+int text_height(mu_Font font)
+{
+ (void)font;
+ return FONT_H;
+}
diff --git a/text_size.h b/text_size.h
new file mode 100644
index 0000000..3241c88
--- /dev/null
+++ b/text_size.h
@@ -0,0 +1,5 @@
+#pragma once
+#include "microui.h"
+
+int text_width(mu_Font, const char *, int);
+int text_height(mu_Font);
diff --git a/ui.h b/ui.h
new file mode 100644
index 0000000..7ab5a0b
--- /dev/null
+++ b/ui.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void ui_draw(void);
diff --git a/ui_draw.c b/ui_draw.c
new file mode 100644
index 0000000..2a73ec1
--- /dev/null
+++ b/ui_draw.c
@@ -0,0 +1,39 @@
+#include <SDL2/SDL_render.h>
+#include "ui.h"
+#include "font.h"
+#include "render.h"
+#include "microui.h"
+
+extern mu_Context *mu_ctx;
+
+#define UNPACK_COLOR(c) (c).r, (c).g, (c).b, (c).a
+#define UNPACK_RECT(r) (r).x, (r).y, (r).w, (r).h
+
+void ui_draw(void)
+{
+ static int warned = 0;
+ mu_Command *cmd = NULL;
+ while (mu_next_command(mu_ctx, &cmd)) switch (cmd->type) {
+ case MU_COMMAND_TEXT:
+ font_set_color(UNPACK_COLOR(cmd->text.color));
+ font_draw(cmd->text.pos.x, cmd->text.pos.y, cmd->text.str);
+ break;
+ case MU_COMMAND_RECT:
+ r_draw_set_color(UNPACK_COLOR(cmd->rect.color));
+ r_draw_fill_rect(UNPACK_RECT(cmd->rect.rect));
+ break;
+ case MU_COMMAND_ICON:
+ break;
+ case MU_COMMAND_CLIP:
+ r_set_clip_rect(UNPACK_RECT(cmd->clip.rect));
+ break;
+ default:
+ if (!warned) {
+ fprintf(stderr, "unsupported MU command: %d\n",
+ cmd->type);
+ warned = 1;
+ }
+ break;
+ }
+ r_reset_clip_rect();
+}