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
18
gf_graphic_color_t
gf_gui_base_color;
19
gf_graphic_color_t
gf_gui_font_color;
20
21
gf_gui_t
* gf_gui_create(
gf_engine_t
* engine,
gf_draw_t
* draw) {
22
gf_gui_t
* gui = malloc(
sizeof
(*gui));
23
gf_gui_id_t
i;
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
38
void
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 */
41
gf_graphic_color_t
col;
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
61
gf_gui_component_t
* gf_gui_first_unused(
gf_gui_t
* gui,
gf_gui_id_t
*
id
) {
62
gf_gui_id_t
i;
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
72
gf_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
88
void
gf_gui_render(
gf_gui_t
* gui) {
89
gf_gui_id_t
i;
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
}
gf_draw.h
Drawing interface.
gf_graphic.h
Graphic interface.
GF_SET_COLOR
#define GF_SET_COLOR(color, red, green, blue, alpha)
Macro to set color safely and shorter.
Definition
gf_graphic.h:39
GF_GRAPHIC_2D
#define GF_GRAPHIC_2D
Dimension parameter for 2D.
Definition
gf_graphic.h:26
gf_gui.h
GUI.
GF_GUI_FONT_SIZE
#define GF_GUI_FONT_SIZE
Default GUI font size.
Definition
gf_gui.h:38
GF_GUI_NORMAL
#define GF_GUI_NORMAL
Draw border normally.
Definition
gf_gui.h:26
gf_pre.h
Required headers before anything.
gf_gui_id_t
int gf_gui_id_t
Component ID.
Definition
gui.h:17
GF_GUI_MAX_COMPONENTS
#define GF_GUI_MAX_COMPONENTS
Max components engine GUI can handle.
Definition
gui.h:32
gf_draw_t
Drawing interface.
Definition
draw.h:101
gf_engine_t
Engine instance.
Definition
core.h:44
gf_graphic_color_t
Color.
Definition
graphic.h:42
gf_graphic_color_t::g
double g
Green.
Definition
graphic.h:42
gf_graphic_color_t::b
double b
Blue.
Definition
graphic.h:42
gf_graphic_color_t::r
double r
Red.
Definition
graphic.h:42
gf_gui_button_t::pressed
int pressed
1 if pressed, otherwise 0
Definition
gui.h:57
gf_gui_button_t::text
char * text
Button text.
Definition
gui.h:57
gf_gui_component_t
Component.
Definition
gui.h:102
gf_gui_component_t::y
double y
X coord of component.
Definition
gui.h:102
gf_gui_component_t::width
double width
Width of component.
Definition
gui.h:102
gf_gui_component_t::height
double height
Height of component.
Definition
gui.h:102
gf_gui_component_t::u
gf_gui_union_t u
Component union.
Definition
gui.h:102
gf_gui_component_t::type
int type
Component type.
Definition
gui.h:102
gf_gui_component_t::x
double x
X coord of component.
Definition
gui.h:102
gf_gui_t
GUI.
Definition
gui.h:122
gf_gui_t::engine
gf_engine_t * engine
Engine instance.
Definition
gui.h:122
gf_gui_t::draw
gf_draw_t * draw
Drawing interface.
Definition
gui.h:122
gf_gui_t::area
gf_gui_component_t area[64]
Created components.
Definition
gui.h:122
gf_gui_union_t::button
gf_gui_button_t button
Button component.
Definition
gui.h:68
gf_gui.c
Generated by
1.9.8