diff options
| author | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:26:01 +0100 |
|---|---|---|
| committer | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:26:01 +0100 |
| commit | c15c603b35e86fd42e6db94a7382ba7a1ddadd6e (patch) | |
| tree | ab56040ce4768564bef5fbe9e65fe4c025cea6ce /menu.c | |
Initial commit
Diffstat (limited to 'menu.c')
| -rw-r--r-- | menu.c | 179 |
1 files changed, 179 insertions, 0 deletions
@@ -0,0 +1,179 @@ +#include "game.h" + +struct Widget * +add_button(unsigned char layer, const char *label, int x, int y, + void (*callback)(struct Widget *)) +{ + SDL_Surface *surface; + SDL_Color c = {255, 255, 255, 255}; + + struct Resources *res = &game->resources; + struct Layer *l = &game->layers[layer]; + struct Widget *w; + + w = &l->widgets[l->num_widgets ++]; + + w->callback = callback; + w->is_blended = true; + + surface = TTF_RenderText_Solid(res->font, label, c); + TTF_SizeText(res->font, label, &w->rect.w, &w->rect.h); + w->texture = SDL_CreateTextureFromSurface(game->renderer, surface); + res->textures[res->num_textures ++] = w->texture; + SDL_FreeSurface(surface); + + if(x == POSITION_CENTERED) + w->rect.x = (game->width / 2) - (w->rect.w / 2); + else + w->rect.x = x; + + if(y == POSITION_CENTERED) + w->rect.y = (game->height / 2) - (w->rect.h / 2); + else + w->rect.y = y; + + return w; +} + +void set_color(struct Widget *w, struct Color color) +{ + w->color = color; + w->on_hover_color = color; + w->default_color = color; +} + +struct Widget *add_image(unsigned char layer, SDL_Texture *image, + int x, int y, int w, int h) +{ + struct Layer *l = &game->layers[layer]; + struct Widget *widget; + + widget = &l->widgets[l->num_widgets ++]; + + widget->callback = NULL; + widget->texture = image; + + widget->is_blended = false; + + widget->rect.w = w; + widget->rect.h = h; + + widget->color = (struct Color){255, 255, 255}; + widget->default_color = widget->color; + + if(x == POSITION_CENTERED) + widget->rect.x = (game->width / 2) - (widget->rect.w / 2); + else + widget->rect.x = x; + + if(y == POSITION_CENTERED) + widget->rect.y = (game->height / 2) - (widget->rect.h / 2); + else + widget->rect.y = y; + + return widget; +} + +void process_widget_events(unsigned char type, int x, int y) +{ + int i; + struct Layer *layer = &game->layers[game->current_layer]; + struct Widget *widget; + + for(i = 0; i < layer->num_widgets; i ++) + { + widget = &layer->widgets[i]; + + if(x > widget->rect.x && x < widget->rect.x + widget->rect.w + && y > widget->rect.y && y < widget->rect.y + widget->rect.h) + { + widget->is_hover = true; + widget->color = widget->on_hover_color; + + if(widget->callback != NULL && type == MOUSE_BUTTON) + widget->callback(widget); + } + else + { + widget->is_hover = false; + widget->color = widget->default_color; + } + } +} + +struct Widget *add_label(unsigned char layer, + const char *text, + struct Color color, + int x, int y) +{ + SDL_Surface *surface; + SDL_Color c = (SDL_Color){color.r, color.g, color.b, 255}; + + struct Resources *res = &game->resources; + struct Layer *l = &game->layers[layer]; + struct Widget *w; + + w = &l->widgets[l->num_widgets ++]; + + w->callback = NULL; + w->is_blended = true; + + w->initial_x = x; + w->initial_y = y; + w->default_color = color; + w->color = color; + w->on_hover_color = color; + + surface = TTF_RenderText_Solid(res->font, text, c); + TTF_SizeText(res->font, text, &w->rect.w, &w->rect.h); + + w->texture = SDL_CreateTextureFromSurface(game->renderer, surface); + res->textures[res->num_textures ++] = w->texture; + + SDL_FreeSurface(surface); + + if(x == POSITION_CENTERED) + w->rect.x = (game->width / 2) - (w->rect.w / 2); + else + w->rect.x = x; + + if(y == POSITION_CENTERED) + w->rect.y = (game->height / 2) - (w->rect.h / 2); + else + w->rect.y = y; + + return w; +} + +void set_text(struct Widget *w, const char *text) +{ + int i; + SDL_Surface *surface; + SDL_Texture *texture; + SDL_Color c = (SDL_Color){w->default_color.r, + w->default_color.g, + w->default_color.b, + 255}; + + surface = TTF_RenderText_Solid(game->resources.font, text, c); + TTF_SizeText(game->resources.font, text, &w->rect.w, &w->rect.h); + texture = SDL_CreateTextureFromSurface(game->renderer, surface); + SDL_FreeSurface(surface); + + for(i = 0; i < game->resources.num_textures; i ++) + { + if(game->resources.textures[i] == w->texture) + { + SDL_DestroyTexture(w->texture); + game->resources.textures[i] = texture; + } + } + + w->texture = texture; + + if(w->initial_x == POSITION_CENTERED) + w->rect.x = (game->width / 2) - (w->rect.w / 2); + + if(w->initial_y == POSITION_CENTERED) + w->rect.y = (game->height / 2) - (w->rect.h / 2); +} |
