GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_draw_common.c
1#define GF_EXPOSE_DRAW
2
3#include <gf_pre.h>
4
5/* External library */
6#include <stb_image.h>
7
8/* Interface */
9#include <gf_draw.h>
10
11/* Engine */
12#include <gf_core.h>
13#include <gf_log.h>
14#include <gf_draw_platform.h>
15#include <gf_draw_driver.h>
16#include <gf_texture.h>
17#include <gf_graphic.h>
18#include <gf_font.h>
19#include <gf_gui.h>
20
21/* Standard */
22#include <stdlib.h>
23#include <string.h>
24#include <math.h>
25
26void gf_draw_begin(void) { gf_draw_platform_begin(); }
27
28void gf_draw_end(void) { gf_draw_platform_end(); }
29
30gf_texture_t* test_texture;
31
32gf_draw_t* gf_draw_create(gf_engine_t* engine, const char* title) {
33 gf_draw_t* draw = malloc(sizeof(*draw));
34 memset(draw, 0, sizeof(*draw));
35 draw->engine = engine;
36 draw->x = 0;
37 draw->y = 0;
38 draw->width = 640;
39 draw->height = 480;
40 draw->running = 0;
41 draw->draw_3d = 0;
42 draw->font = NULL;
43 strcpy(draw->title, title);
44 draw->platform = gf_draw_platform_create(engine, draw);
45 if(draw->platform != NULL) {
46 draw->driver = gf_draw_driver_create(engine, draw);
47 gf_draw_reshape(draw);
48 draw->running = 1;
49
50 draw->light[0] = 0.0;
51 draw->light[1] = 10.0;
52 draw->light[2] = 0.0;
53 draw->light[3] = 1.0;
54
55 draw->camera[0] = 0;
56 draw->camera[1] = 2;
57 draw->camera[2] = 2;
58
59 draw->lookat[0] = 0;
60 draw->lookat[1] = 0;
61 draw->lookat[2] = 0;
62
63 draw->font = gf_font_create(draw, "font/helvR24.bdf");
64
65 draw->gui = gf_gui_create(engine, draw);
66 if(1) {
67 int w, h, c;
68 unsigned char* d = stbi_load("texture/test.bmp", &w, &h, &c, 4);
69 test_texture = gf_texture_create(draw, w, h, d);
70 free(d);
71 }
72 } else {
73 gf_draw_destroy(draw);
74 draw = NULL;
75 }
76 return draw;
77}
78
79void gf_draw_reshape(gf_draw_t* draw) { gf_draw_driver_reshape(draw); }
80
81gf_gui_id_t button1 = -1;
82gf_gui_id_t button2 = -1;
83
84void gf_button_callback(gf_engine_t* engine, gf_draw_t* draw, gf_gui_id_t id, int type) {
85 if(type == GF_GUI_PRESS_EVENT) {
86 gf_log_function(engine, "GUI component %d was pressed", id);
87 }
88}
89
90/* Runs every frame */
91void gf_draw_frame(gf_draw_t* draw) {
93 color.r = color.g = color.b = color.a = 255;
94 if(button1 == -1) {
95 button1 = gf_gui_create_button(draw->gui, 0, 0, 200, 50, "\"Test\" text");
96 gf_gui_set_callback(draw->gui, button1, gf_button_callback);
97 }
98 if(button2 == -1) {
99 button2 = gf_gui_create_button(draw->gui, 100, 25, 200, 50, "\"Test\" text");
100 gf_gui_set_callback(draw->gui, button2, gf_button_callback);
101 }
102 if(draw->draw_3d) {
103 gf_graphic_draw_texture_polygon(draw, test_texture, color, GF_GRAPHIC_3D, 4,
104 /* clang-format off */
105 0.0, 0.0,
106 -1.0, 0.0, -1.0,
107
108 0.0, 4.0,
109 -1.0, 0.0, 1.0,
110
111 4.0, 4.0,
112 1.0, 0.0, 1.0,
113
114 4.0, 0.0,
115 1.0, 0.0, -1.0
116 /* clang-format on */
117 );
118 }
119 gf_gui_render(draw->gui);
120}
121
122int gf_draw_step(gf_draw_t* draw) {
123 int ret = gf_draw_platform_step(draw);
124 if(ret != 0) return ret;
125 draw->close = 0;
126
127 return 0;
128}
129
130void gf_draw_destroy(gf_draw_t* draw) {
131 if(draw->font != NULL) {
132 gf_font_destroy(draw->font);
133 }
134 if(draw->driver != NULL) gf_draw_driver_destroy(draw->driver);
135 if(draw->platform != NULL) gf_draw_platform_destroy(draw->platform);
136 gf_log_function(draw->engine, "Destroyed drawing interface", "");
137 free(draw);
138}
139
140void gf_draw_set_input(gf_draw_t* draw, gf_input_t* input) { draw->input = input; }
Drawing interface.
Drawing driver.
Platform-dependent part of drawing driver.
Graphic interface.
#define GF_GRAPHIC_3D
Dimension parameter for 3D.
Definition gf_graphic.h:32
Logger.
#define gf_log_function(engine, fmt,...)
Output log with line number and function name.
Definition gf_log.h:26
Required headers before anything.
Texture.
int gf_gui_id_t
Component ID.
Definition gui.h:17
Type definitions related to math.
Drawing interface.
Definition draw.h:108
gf_font_t * font
Current font.
Definition draw.h:108
gf_math_vector_t light
Light location.
Definition draw.h:108
int y
Y coord of window.
Definition draw.h:108
gf_input_t * input
Input interface.
Definition draw.h:108
char title[128]
Window title.
Definition draw.h:108
gf_math_vector_t lookat
Where to look at.
Definition draw.h:108
int draw_3d
1 if engine should start rendering 3D stuffs, otherwise 0
Definition draw.h:108
gf_draw_driver_t * driver
Drawing driver.
Definition draw.h:108
gf_gui_t * gui
GUI.
Definition draw.h:108
int running
1 if running, otherwise 0
Definition draw.h:108
gf_engine_t * engine
Engine instance.
Definition draw.h:108
int close
1 if it was requested to be closed, otherwise 0
Definition draw.h:108
gf_draw_platform_t * platform
Platform-dependent part of drawing driver.
Definition draw.h:108
gf_math_vector_t camera
Camera location.
Definition draw.h:108
int x
X coord of window.
Definition draw.h:108
int width
Width of window.
Definition draw.h:108
int height
Height of window.
Definition draw.h:108
Engine instance.
Definition core.h:46
double g
Green.
Definition graphic.h:44
double b
Blue.
Definition graphic.h:44
double a
Alpha.
Definition graphic.h:44
double r
Red.
Definition graphic.h:44
Input interface.
Definition input.h:64
Texture.
Definition texture.h:49