GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_gui.c
1#define GF_EXPOSE_GUI
2
3#include <gf_pre.h>
4
5/* External library */
6
7/* Interface */
8#include <gf_gui.h>
9
10/* Engine */
11#include <gf_graphic.h>
12#include <gf_draw.h>
13
14/* Standard */
15#include <stdlib.h>
16#include <string.h>
17
18gf_graphic_color_t gf_gui_base_color;
19gf_graphic_color_t gf_gui_font_color;
20
21gf_gui_t* gf_gui_create(gf_engine_t* engine, gf_draw_t* draw) {
22 gf_gui_t* gui = malloc(sizeof(*gui));
24 memset(gui, 0, sizeof(*gui));
25 gui->engine = engine;
26 gui->draw = draw;
27
28 GF_SET_COLOR(gf_gui_base_color, 48, 96, 48, 255);
29 GF_SET_COLOR(gf_gui_font_color, 256 - 32, 256 - 32, 256 - 32, 255);
30
31 for(i = 0; i < GF_GUI_MAX_COMPONENTS; i++) gui->area[i].type = -1;
32
33 return gui;
34}
35
36/* note... left top should be the lightest in the border */
37
38void gf_gui_draw_box(gf_gui_t* gui, int mul, double x, double y, double w, double h) {
39 const int color_diff = 32; /* color diff */
40 const double bw = 2; /* border width */
42
43 int cd = mul * color_diff;
44
45 col = gf_gui_base_color;
46 col.r += cd;
47 col.g += cd;
48 col.b += cd;
49 gf_graphic_fill_rect(gui->draw, x, y, w, h, col);
50
51 col = gf_gui_base_color;
52 col.r -= cd;
53 col.g -= cd;
54 col.b -= cd;
55 gf_graphic_fill_polygon(gui->draw, col, GF_GRAPHIC_2D, 5, x + w, y + h, x + w, y, x + w - bw, y + bw, x + bw, y + h - bw, x, y + h);
56
57 col = gf_gui_base_color;
58 gf_graphic_fill_rect(gui->draw, x + bw, y + bw, w - bw * 2, h - bw * 2, col);
59}
60
61gf_gui_component_t* gf_gui_first_unused(gf_gui_t* gui, gf_gui_id_t* id) {
63 for(i = 0; i < GF_GUI_MAX_COMPONENTS; i++) {
64 if(gui->area[i].type == GF_GUI_UNUSED) {
65 *id = i;
66 return &gui->area[i];
67 }
68 }
69 return NULL;
70}
71
72gf_gui_id_t gf_gui_create_button(gf_gui_t* gui, double x, double y, double w, double h, const char* text) {
73 gf_gui_id_t id;
74 gf_gui_component_t* c = gf_gui_first_unused(gui, &id);
75
76 c->type = GF_GUI_BUTTON;
77 c->x = x;
78 c->y = y;
79 c->width = w;
80 c->height = h;
81
82 c->u.button.pressed = 0;
83 c->u.button.text = malloc(strlen(text) + 1);
84 strcpy(c->u.button.text, text);
85 return id;
86}
87
88void gf_gui_render(gf_gui_t* gui) {
90 for(i = 0; i < GF_GUI_MAX_COMPONENTS; i++) {
91 gf_gui_component_t* c = &gui->area[i];
92 double cx = c->x;
93 double cy = c->y;
94 double cw = c->width;
95 double ch = c->height;
96 switch(c->type) {
97 case GF_GUI_BUTTON: {
98 double x = cx + cw / 2 - gf_graphic_text_width(gui->draw, GF_GUI_FONT_SIZE, c->u.button.text) / 2;
99 double y = cy + ch / 2 - GF_GUI_FONT_SIZE / 2;
100 gf_gui_draw_box(gui, GF_GUI_NORMAL, cx, cy, cw, ch);
101 gf_graphic_text(gui->draw, x, y, GF_GUI_FONT_SIZE, c->u.button.text, gf_gui_font_color);
102 break;
103 }
104 }
105 }
106}
Drawing interface.
Graphic interface.
#define GF_SET_COLOR(color, red, green, blue, alpha)
Macro to set color safely and shorter.
Definition gf_graphic.h:39
#define GF_GRAPHIC_2D
Dimension parameter for 2D.
Definition gf_graphic.h:26
#define GF_GUI_FONT_SIZE
Default GUI font size.
Definition gf_gui.h:38
#define GF_GUI_NORMAL
Draw border normally.
Definition gf_gui.h:26
Required headers before anything.
int gf_gui_id_t
Component ID.
Definition gui.h:17
#define GF_GUI_MAX_COMPONENTS
Max components engine GUI can handle.
Definition gui.h:32
Drawing interface.
Definition draw.h:101
Engine instance.
Definition core.h:44
double g
Green.
Definition graphic.h:42
double b
Blue.
Definition graphic.h:42
double r
Red.
Definition graphic.h:42
int pressed
1 if pressed, otherwise 0
Definition gui.h:57
char * text
Button text.
Definition gui.h:57
Component.
Definition gui.h:102
double y
X coord of component.
Definition gui.h:102
double width
Width of component.
Definition gui.h:102
double height
Height of component.
Definition gui.h:102
gf_gui_union_t u
Component union.
Definition gui.h:102
int type
Component type.
Definition gui.h:102
double x
X coord of component.
Definition gui.h:102
GUI.
Definition gui.h:122
gf_engine_t * engine
Engine instance.
Definition gui.h:122
gf_draw_t * draw
Drawing interface.
Definition gui.h:122
gf_gui_component_t area[64]
Created components.
Definition gui.h:122
gf_gui_button_t button
Button component.
Definition gui.h:68