diff options
author | kdx <kikoodx@paranoici.org> | 2023-03-06 23:36:34 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-03-06 23:36:34 +0100 |
commit | 66b3df86e1a9f9b55fdb547f53f8f56a4f8b64bd (patch) | |
tree | 788f4bcc4760ffaa3e00350d29a2bb5805ed10b9 | |
parent | 365ab142504ef46af2994d79ead7a2a0a01f2e6c (diff) | |
download | ule-66b3df86e1a9f9b55fdb547f53f8f56a4f8b64bd.tar.gz |
initial commit
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | build.sh | 2 | ||||
-rw-r--r-- | font.c | 41 | ||||
-rw-r--r-- | font.h | 8 | ||||
-rw-r--r-- | main.c | 90 | ||||
-rw-r--r-- | render.c | 30 | ||||
-rw-r--r-- | render.h | 6 | ||||
-rw-r--r-- | res/font.png | bin | 0 -> 4572 bytes | |||
-rw-r--r-- | text_size.c | 14 | ||||
-rw-r--r-- | text_size.h | 5 | ||||
-rw-r--r-- | ui.h | 3 | ||||
-rw-r--r-- | ui_draw.c | 39 |
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 @@ -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()); +} @@ -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); @@ -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 Binary files differnew file mode 100644 index 0000000..cdfdaa9 --- /dev/null +++ b/res/font.png 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); @@ -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(); +} |