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 button = -1;
82
83/* Runs every frame */
84void gf_draw_frame(gf_draw_t* draw) {
86 color.r = color.g = color.b = color.a = 255;
87 if(button == -1) {
88 button = gf_gui_create_button(draw->gui, 0, 0, 200, 100, "\"Test\" text");
89 }
90 if(draw->draw_3d) {
91 gf_graphic_draw_texture_polygon(draw, test_texture, color, GF_GRAPHIC_3D, 4,
92 /* clang-format off */
93 0.0, 0.0,
94 -1.0, 0.0, -1.0,
95
96 0.0, 4.0,
97 -1.0, 0.0, 1.0,
98
99 4.0, 4.0,
100 1.0, 0.0, 1.0,
101
102 4.0, 0.0,
103 1.0, 0.0, -1.0
104 /* clang-format on */
105 );
106 }
107 gf_gui_render(draw->gui);
108}
109
110int gf_draw_step(gf_draw_t* draw) {
111 int ret = gf_draw_platform_step(draw);
112 if(ret != 0) return ret;
113 draw->close = 0;
114
115 return 0;
116}
117
118void gf_draw_destroy(gf_draw_t* draw) {
119 if(draw->driver != NULL) gf_draw_driver_destroy(draw->driver);
120 if(draw->platform != NULL) gf_draw_platform_destroy(draw->platform);
121 gf_log_function(draw->engine, "Destroyed drawing interface", "");
122 free(draw);
123}
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:101
gf_font_t * font
Current font.
Definition draw.h:101
gf_math_vector_t light
Light location.
Definition draw.h:101
int y
Y coord of window.
Definition draw.h:101
char title[128]
Window title.
Definition draw.h:101
gf_math_vector_t lookat
Where to look at.
Definition draw.h:101
int draw_3d
1 if engine should start rendering 3D stuffs, otherwise 0
Definition draw.h:101
gf_draw_driver_t * driver
Drawing driver.
Definition draw.h:101
gf_gui_t * gui
GUI.
Definition draw.h:101
int running
1 if running, otherwise 0
Definition draw.h:101
gf_engine_t * engine
Engine instance.
Definition draw.h:101
int close
1 if it was requested to be closed, otherwise 0
Definition draw.h:101
gf_draw_platform_t * platform
Platform-dependent part of drawing driver.
Definition draw.h:101
gf_math_vector_t camera
Camera location.
Definition draw.h:101
int x
X coord of window.
Definition draw.h:101
int width
Width of window.
Definition draw.h:101
int height
Height of window.
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 a
Alpha.
Definition graphic.h:42
double r
Red.
Definition graphic.h:42
Texture.
Definition texture.h:47