From 2ed3153670ffba9d43f6033ce89dc33c241de193 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 25 Sep 2021 18:39:15 -0400 Subject: tk: Define colors within local header --- (limited to 'src/tk') diff --git a/src/tk/box.c b/src/tk/box.c index da0ef5d..003af66 100644 --- a/src/tk/box.c +++ b/src/tk/box.c @@ -22,6 +22,7 @@ #include #include #include "../tk.h" +#include "style.h" #include "widget.h" struct mftk_box { @@ -29,7 +30,6 @@ struct mftk_box { int container_w; int container_h; int padding; - SDL_Color *bg_color; struct mftk_widget *child; int child_x; int child_y; @@ -97,8 +97,8 @@ _mftk_box_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) rect.w = w->w; rect.h = w->h; if (SDL_SetRenderDrawColor(renderer, - b->bg_color->r, b->bg_color->g, - b->bg_color->b, b->bg_color->a) < 0 || + MFTK_COLOR_FORM_R, MFTK_COLOR_FORM_G, + MFTK_COLOR_FORM_B, MFTK_COLOR_FORM_A) < 0 || SDL_RenderFillRect(renderer, &rect) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", @@ -125,7 +125,7 @@ _mftk_box_destroy(struct mftk_widget *w) struct mftk_widget * mftk_box_new(int container_w, int container_h, int own_w, int own_h, - int padding, SDL_Color *bg_color, struct mftk_widget *child) + int padding, struct mftk_widget *child) { struct mftk_widget *w; struct mftk_box *b; @@ -137,7 +137,6 @@ mftk_box_new(int container_w, int container_h, int own_w, int own_h, w->w = own_w; w->h = own_h; b->padding = padding; - b->bg_color = bg_color; b->child = child; child->container = w; diff --git a/src/tk/button.c b/src/tk/button.c index 1152bd9..47f59f6 100644 --- a/src/tk/button.c +++ b/src/tk/button.c @@ -19,12 +19,12 @@ #include #include "../tk.h" +#include "style.h" #include "widget.h" struct mftk_button { struct mftk_widget parent; struct mftk_widget *label; - SDL_Color *fill_color; int padding; int (*action)(void *); void *user_data; @@ -113,8 +113,8 @@ _mftk_button_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) rect.w = w->w; rect.h = w->h; if (SDL_SetRenderDrawColor(renderer, - b->fill_color->r, b->fill_color->g, - b->fill_color->b, b->fill_color->a) < 0 || + MFTK_COLOR_BUTN_R, MFTK_COLOR_BUTN_G, + MFTK_COLOR_BUTN_B, MFTK_COLOR_BUTN_A) < 0 || SDL_RenderFillRect(renderer, &rect) < 0) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", @@ -139,8 +139,7 @@ _mftk_button_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_button_new(TTF_Font *font, const char *text, SDL_Color *text_color, - SDL_Color *fill_color, int padding, +mftk_button_new(TTF_Font *font, const char *text, int padding, int (*action)(void *), void *user_data, SDL_Renderer *renderer) { struct mftk_widget *w; @@ -148,16 +147,15 @@ mftk_button_new(TTF_Font *font, const char *text, SDL_Color *text_color, mftk_widget_init_focusable(w, b, button); - b->label = mftk_label_new(font, text, text_color, renderer); + b->label = mftk_label_new(font, text, renderer); if (b->label == NULL) { free(b); return NULL; } - b->fill_color = fill_color; - b->padding = padding; - b->action = action; - b->user_data = user_data; + b->padding = padding; + b->action = action; + b->user_data = user_data; return w; } diff --git a/src/tk/check.c b/src/tk/check.c index 0b2ea56..b24db0b 100644 --- a/src/tk/check.c +++ b/src/tk/check.c @@ -20,6 +20,7 @@ #include #include #include "../tk.h" +#include "style.h" #include "widget.h" struct mftk_check { @@ -27,8 +28,6 @@ struct mftk_check { enum mftk_check_shape shape; int butn_width; int butn_padding; - SDL_Color *butn_color; - SDL_Color *mark_color; int state; int label_padding; struct mftk_widget *label; @@ -38,6 +37,15 @@ struct mftk_check { void *user_data; }; +static SDL_Color _mftk_check_butn_color = { + .r = MFTK_COLOR_CHKB_R, .g = MFTK_COLOR_CHKB_G, + .b = MFTK_COLOR_CHKB_B, .a = MFTK_COLOR_CHKB_A +}; +static SDL_Color _mftk_check_mark_color = { + .r = MFTK_COLOR_CHKM_R, .g = MFTK_COLOR_CHKM_G, + .b = MFTK_COLOR_CHKM_B, .a = MFTK_COLOR_CHKM_A +}; + void mftk_check_set_shape(struct mftk_widget *w, enum mftk_check_shape shape) { @@ -220,11 +228,13 @@ _mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) rect.w = c->butn_width; rect.h = c->butn_width; if (c->shape == MFTK_CHECK_SHAPE_BOX) { - if (_mftk_check_render_box(&rect, c->butn_color, renderer) < 0){ + if (_mftk_check_render_box(&rect, &_mftk_check_butn_color, + renderer) < 0) { e = -1; } } else { - if (_mftk_check_render_cir(&rect, c->butn_color, renderer) < 0){ + if (_mftk_check_render_cir(&rect, &_mftk_check_butn_color, + renderer) < 0) { e = -1; } } @@ -235,12 +245,14 @@ _mftk_check_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) rect.w -= c->butn_padding * 2; rect.h -= c->butn_padding * 2; if (c->shape == MFTK_CHECK_SHAPE_BOX) { - if (_mftk_check_render_box(&rect, c->mark_color, + if (_mftk_check_render_box(&rect, + &_mftk_check_mark_color, renderer) < 0) { e = -1; } } else { - if (_mftk_check_render_cir(&rect, c->mark_color, + if (_mftk_check_render_cir(&rect, + &_mftk_check_mark_color, renderer) < 0) { e = -1; } @@ -266,19 +278,17 @@ _mftk_check_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_check_new(int butn_width, int butn_padding, SDL_Color *butn_color, - SDL_Color *mark_color, int state, int label_padding, - TTF_Font *font, const char *text, SDL_Color *text_color, - int (*action)(void *, int), int (*submit)(void *), - void *user_data, SDL_Renderer *renderer) +mftk_check_new(int butn_width, int butn_padding, int state, int label_padding, + TTF_Font *font, const char *text, int (*action)(void *, int), + int (*submit)(void *), void *user_data, SDL_Renderer *renderer) { struct mftk_widget *w; struct mftk_check *c; mftk_widget_init_focusable(w, c, check); - if (font != NULL && text != NULL && text_color != NULL) { - c->label = mftk_label_new(font, text, text_color, renderer); + if (font != NULL && text != NULL) { + c->label = mftk_label_new(font, text, renderer); if (c->label == NULL) { free(c); return NULL; @@ -288,8 +298,6 @@ mftk_check_new(int butn_width, int butn_padding, SDL_Color *butn_color, c->shape = MFTK_CHECK_SHAPE_BOX; c->butn_width = butn_width; c->butn_padding = butn_padding; - c->butn_color = butn_color; - c->mark_color = mark_color; c->state = state; c->label_padding = label_padding; c->steals_focus = SDL_TRUE; diff --git a/src/tk/label.c b/src/tk/label.c index dcd827e..bd4e5a4 100644 --- a/src/tk/label.c +++ b/src/tk/label.c @@ -20,6 +20,7 @@ #include #include #include "../tk.h" +#include "style.h" #include "widget.h" struct mftk_label { @@ -29,6 +30,11 @@ struct mftk_label { int h; }; +static SDL_Color _mftk_label_color = { + .r = MFTK_COLOR_FORE_R, .g = MFTK_COLOR_FORE_G, + .b = MFTK_COLOR_FORE_B, .a = MFTK_COLOR_FORE_A +}; + static void _mftk_label_layout(struct mftk_widget *w __attribute__((__unused__))) { @@ -75,8 +81,7 @@ _mftk_label_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_label_new(TTF_Font *font, const char *text, SDL_Color *color, - SDL_Renderer *renderer) +mftk_label_new(TTF_Font *font, const char *text, SDL_Renderer *renderer) { struct mftk_widget *w; struct mftk_label *l; @@ -89,7 +94,7 @@ mftk_label_new(TTF_Font *font, const char *text, SDL_Color *color, mftk_widget_init(w, l, label); - surface = TTF_RenderUTF8_Blended(font, text, *color); + surface = TTF_RenderUTF8_Blended(font, text, _mftk_label_color); if (surface == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface: %s", diff --git a/src/tk/radio.c b/src/tk/radio.c index 755be15..8fb86bd 100644 --- a/src/tk/radio.c +++ b/src/tk/radio.c @@ -169,12 +169,10 @@ _mftk_radio_destroy(struct mftk_widget *w) } struct mftk_widget * -mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color, - SDL_Color *mark_color, int label_padding, int item_padding, - TTF_Font *font, SDL_Color *text_color, - int (*action)(void *, int), int (*submit)(void *), - void *user_data, SDL_Renderer *renderer, int state, int options, - ...) +mftk_radio_new(int butn_width, int butn_padding, int label_padding, + int item_padding, TTF_Font *font, int (*action)(void *, int), + int (*submit)(void *), void *user_data, SDL_Renderer *renderer, + int state, int options, ...) { struct mftk_widget *w; struct mftk_radio *r; @@ -204,9 +202,9 @@ mftk_radio_new(int butn_width, int butn_padding, SDL_Color *butn_color, va_start(ap, options); for (i = 0; i < options; ++i) { r->children[i] = mftk_check_new(butn_width, butn_padding, - butn_color, mark_color, (i == state), - label_padding, font, va_arg(ap, const char *), - text_color, &_mftk_radio_state_change, submit, + (i == state), label_padding, font, + va_arg(ap, const char *), + &_mftk_radio_state_change, submit, &r->states[i], renderer); if (r->children[i] == NULL) { for (; i >= 0; --i) { diff --git a/src/tk/style.h b/src/tk/style.h new file mode 100644 index 0000000..16b4e25 --- /dev/null +++ b/src/tk/style.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2021 P. J. McDermott + * + * This file is part of Maze Fight + * + * Maze Fight is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Maze Fight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Maze Fight. If not, see . + */ + +#ifndef MFTK_STYLE_H_ +#define MFTK_STYLE_H_ + +#define MFTK_COLOR_FORM_R 0xDF /* Form background color */ +#define MFTK_COLOR_FORM_G 0xDF +#define MFTK_COLOR_FORM_B 0xDF +#define MFTK_COLOR_FORM_A 0xDF +#define MFTK_COLOR_FORE_R 0x00 /* Text color */ +#define MFTK_COLOR_FORE_G 0x00 +#define MFTK_COLOR_FORE_B 0x00 +#define MFTK_COLOR_FORE_A 0xFF +#define MFTK_COLOR_BUTN_R 0xAF /* Button fill color */ +#define MFTK_COLOR_BUTN_G 0xAF +#define MFTK_COLOR_BUTN_B 0xAF +#define MFTK_COLOR_BUTN_A 0xFF +#define MFTK_COLOR_CHKB_R 0xAF /* Radio button and check box button fill color */ +#define MFTK_COLOR_CHKB_G 0xAF +#define MFTK_COLOR_CHKB_B 0xAF +#define MFTK_COLOR_CHKB_A 0xFF +#define MFTK_COLOR_CHKM_R 0x00 /* Radio button and check box mark fill color */ +#define MFTK_COLOR_CHKM_G 0x00 +#define MFTK_COLOR_CHKM_B 0x00 +#define MFTK_COLOR_CHKM_A 0xFF + +#endif /* MFTK_STYLE_H_ */ diff --git a/src/tk/text.c b/src/tk/text.c index 8622e63..c96c924 100644 --- a/src/tk/text.c +++ b/src/tk/text.c @@ -23,6 +23,7 @@ #include #include "../tk.h" #include "../util.h" +#include "style.h" #include "widget.h" struct mftk_text { @@ -39,7 +40,6 @@ struct mftk_text { TTF_Font *font; int line_skip; int ascent; - SDL_Color *color; SDL_Texture *texture; int editable; int (*allowed)(void *, char); @@ -48,6 +48,11 @@ struct mftk_text { void *user_data; }; +static SDL_Color _mftk_text_color = { + .r = MFTK_COLOR_FORE_R, .g = MFTK_COLOR_FORE_G, + .b = MFTK_COLOR_FORE_B, .a = MFTK_COLOR_FORE_A +}; + static void _mftk_text_layout(struct mftk_widget *w __attribute__((__unused__))) { @@ -202,7 +207,7 @@ _mftk_text_render_val(struct mftk_text *t, SDL_Renderer *renderer) const char *c; int glyph_max_y; - surface = TTF_RenderUTF8_Blended(t->font, t->val, *t->color); + surface = TTF_RenderUTF8_Blended(t->font, t->val, _mftk_text_color); if (surface == NULL) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface: %s", @@ -271,9 +276,9 @@ _mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y) } if (t->editable == SDL_TRUE && ( SDL_SetRenderDrawColor(renderer, - t->color->r, t->color->g, - t->color->b, t->color->a) < 0 || - SDL_RenderDrawLine(renderer, + _mftk_text_color.r, _mftk_text_color.g, + _mftk_text_color.b, _mftk_text_color.a) + < 0 || SDL_RenderDrawLine(renderer, x, y + w->h, x + w->w, y + w->h) < 0)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't render widget: %s", @@ -303,8 +308,7 @@ _mftk_text_destroy(struct mftk_widget *w) struct mftk_widget * mftk_text_new(char min_char, char max_char, int len, const char *val, - TTF_Font *font, SDL_Color *color, int editable, - int (*allowed)(void *, char), + TTF_Font *font, int editable, int (*allowed)(void *, char), int (*action)(void *, const char *), int (*submit)(void *), void *user_data) { @@ -325,7 +329,6 @@ mftk_text_new(char min_char, char max_char, int len, const char *val, t->font = font; t->line_skip = TTF_FontLineSkip(font); t->ascent = TTF_FontAscent (font); - t->color = color; t->texture = NULL; t->editable = editable; t->allowed = allowed; -- cgit v0.9.1