menu

a graphical menu within a window
Index Commits Files Refs
commit 06e5cceda9ca59e1a6c8d24d71e2ccedb7a6fa5a
parent 0fa3d3718bd186f5b8632478f4fbab9de12e1ee3
Author: klewer-martin <martin.cachari@gmail.com>
Date:   Tue, 10 May 2022 00:24:44 -0300

Added window minimum width and height, plus mk_SDL_Color

Added variables to stores the minimum width and height of the window
then sets it to the window

Diffstat:
MMakefile | 2+-
Mmain.c | 18++++++++----------
Mmenu.c | 23++++-------------------
Mmenu.h | 4++--
Mutil.h | 5+++++
5 files changed, 20 insertions(+), 32 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
 CC := gcc
 CLIBS := `sdl2-config --libs` -lSDL2_ttf -lm
-CFLAGS := `sdl2-config --cflags` -Wall -Wshadow -pedantic -ansi -std=c99 -O3
+CFLAGS := `sdl2-config --cflags` -Wall -Wshadow -pedantic -ansi -std=c99
 SRCS := $(wildcard *.c)
 OBJS := $(SRCS:.c=.o)
 
diff --git a/main.c b/main.c
@@ -11,6 +11,8 @@
 #define WIN_DEFAULT_W    800
 #define WIN_DEFAULT_H    600
 
+#define BG_COLOR    0x323232FF
+
 static char *entries_text[TOTAL_ENTRIES] = {
     "Bubble sort",
     "Bubble sort (improved)",
@@ -41,6 +43,7 @@ Uint32 buttons;
 int main (void) {
     SDL_Window *win;
     SDL_Renderer *rend;
+    int min_w, min_h;
     size_t i;
 
     SDL_Init(SDL_INIT_VIDEO);
@@ -61,6 +64,10 @@ int main (void) {
 
     font = TTF_OpenFont(FONT_NAME, FONT_SIZE);
 
+    min_h = (TOTAL_ENTRIES * (BAR_H + ELEMENTS_PADDING)) + ELEMENTS_PADDING;
+    min_w = (((float)min_h * 4) / 3);
+    SDL_SetWindowMinimumSize(win, min_w, min_h);
+
     bool run = true;
     SDL_Event event;
     win_w = WIN_DEFAULT_W;
@@ -77,15 +84,6 @@ int main (void) {
         };
     }
 
-    /* size_t len, len_max; */
-    /* for(i = len = len_max = 0; i < TOTAL_ENTRIES; i++, len = strlen(entries_text[i])) */
-    /*     len_max = (len > len_max) ? len : len_max; */
-
-    /* for(i = 0; i < TOTAL_ENTRIES; i++) { */
-    /*     entries[i].selected = false; */
-    /*     strcpy(entries.text, entries_text[i]); */
-    /* } */
-
     entries[0].selected = true;
     SDL_Keymod mod;
     size_t index = TOTAL_ENTRIES;
@@ -143,7 +141,7 @@ int main (void) {
         buttons = SDL_GetMouseState(&x, &y);
         hover_at(entries, x, y);
 
-        SDL_SetRenderDrawColor(rend, 32, 32, 32, 0);
+        SDL_SetRenderDrawColor(rend, UNHEX(BG_COLOR));
         SDL_RenderClear(rend);
 
         compute_entries_pos(entries, win_w, win_h);
diff --git a/menu.c b/menu.c
@@ -1,12 +1,5 @@
 #include "menu.h"
 
-/* void Entry_new(Entry *entry, Entry values) { */
-/*     if(!(entry = (Entry)malloc(sizeof(struct _Entry)))) return; */
-
-/*     strcpy(entry->text, values.text); */
-/*     entry->selected = values.selected; */
-/* } */
-
 /* Finds the selected entry then selects next */
 void select_next(Entry *entries) {
     for(int i = 0; i < TOTAL_ENTRIES; i++) {
@@ -91,20 +84,12 @@ void draw_entry(SDL_Renderer *rend, TTF_Font *font, int win_w, int win_h, const 
     SDL_Color text_color, text_color_shadow;
     int text_w, text_h;
 
-    text_color_shadow.r = (char)(SHADOW_COLOR >> 16) & 0xFF;
-    text_color_shadow.g = (char)(SHADOW_COLOR >> 8) & 0xFF;
-    text_color_shadow.b = (char)(SHADOW_COLOR) & 0xFF;
-
-    text_color.r = (char)(FONT_COLOR >> 16) & 0xFF;
-    text_color.g = (char)(FONT_COLOR >> 8) & 0xFF;
-    text_color.b = (char)(FONT_COLOR) & 0xFF;
+    text_color = mk_SDL_Color(UNHEX(FONT_COLOR));
+    text_color_shadow = mk_SDL_Color(UNHEX(SHADOW_COLOR));
 
-    box.x = BAR_BORDER;
-    box.y = entry.pos.y;
-    box.w = win_w - (BAR_BORDER * 2);
-    box.h = -BAR_H;
+    box = (SDL_Rect){ BAR_BORDER, entry.pos.y, win_w - (BAR_BORDER * 2), -BAR_H };
 
-    SDL_SetRenderDrawColor(rend, UNHEX(entry.selected ? SEL_COLOR : entry.hover ? HOVER_COLOR : NORM_COLOR));
+    SDL_SetRenderDrawColor(rend, UNHEX(entry.selected ? SEL_COLOR : (entry.hover ? HOVER_COLOR : NORM_COLOR)));
     SDL_RenderFillRect(rend, &box);
 
     TTF_SizeText(font, entry.text, &text_w, &text_h);
diff --git a/menu.h b/menu.h
@@ -9,8 +9,8 @@
 
 #include "util.h"
 
-#define FONT_COLOR        0xFFFCF9
-#define SHADOW_COLOR    0x000000
+#define FONT_COLOR        0xFFFCF9FF
+#define SHADOW_COLOR    0x000000FF
 
 #define BAR_H 14
 #define BAR_BORDER 2
diff --git a/util.h b/util.h
@@ -5,4 +5,9 @@ typedef struct _Coord {
     int x, y;
 } Coord;
 
+static inline SDL_Color mk_SDL_Color(int r, int g, int b, int a) {
+    SDL_Color const c = { r, g, b, a };
+    return c;
+}
+
 #endif