GoldFish Engine
Quite simple and lightweight game engine
Loading...
Searching...
No Matches
gf_draw.c
1#define GF_EXPOSE_DRAW_PLATFORM
2#define GF_EXPOSE_DRAW
3
4#include <gf_pre.h>
5
6/* External library */
7#include <gf_opengl.h>
8
9/* Interface */
10#include <gf_draw_platform.h>
11
12/* Engine */
13#include <gf_draw_driver.h>
14#include <gf_log.h>
15#include <gf_draw.h>
16
17/* Standard */
18#include <string.h>
19#include <stdlib.h>
20
21void gf_draw_platform_begin(void) {
22 glfwInit();
23 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
24 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
25 glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
26 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
27}
28
29void gf_draw_platform_end(void) {}
30
31void gf_glfw_size(GLFWwindow* window, int w, int h) {
32 gf_draw_t* draw = (gf_draw_t*)glfwGetWindowUserPointer(window);
33 draw->width = w;
34 draw->height = h;
35 glfwSetWindowSize(window, w, h);
36 gf_draw_reshape(draw);
37}
38
39int gf_draw_platform_has_extension(gf_draw_t* draw, const char* query) {
40 const char* ext = NULL;
41 const char* ptr;
42 const int len = strlen(query);
43
44 glfwMakeContextCurrent(draw->platform->window);
45
46 return glfwExtensionSupported(query);
47}
48
49int gf_draw_platform_step(gf_draw_t* draw) {
50 int ret = 0;
51 int w, h;
52 glfwMakeContextCurrent(draw->platform->window);
53 draw->close = glfwWindowShouldClose(draw->platform->window);
54 if(draw->close) glfwSetWindowShouldClose(draw->platform->window, GLFW_FALSE);
55 glfwPollEvents();
56 if(ret == 0) {
57 gf_draw_driver_before(draw);
58 gf_draw_frame(draw);
59 gf_draw_driver_after(draw);
60
61 glfwSwapBuffers(draw->platform->window);
62 }
63 return ret;
64}
65
66gf_draw_platform_t* gf_draw_platform_create(gf_engine_t* engine, gf_draw_t* draw) {
67 gf_draw_platform_t* platform = malloc(sizeof(*platform));
68 memset(platform, 0, sizeof(*platform));
69 platform->engine = engine;
70
71 platform->window = glfwCreateWindow(draw->width, draw->height, draw->title, NULL, NULL);
72 if(platform->window == NULL) {
73 gf_log_function(engine, "Failed to create window", "");
74 gf_draw_platform_destroy(platform);
75 return NULL;
76 }
77
78 glfwSetWindowUserPointer(platform->window, draw);
79 glfwSetWindowSizeCallback(platform->window, gf_glfw_size);
80
81 glfwMakeContextCurrent(platform->window);
82#ifdef DO_SWAP_INTERVAL
83 glfwSwapInterval(1);
84#endif
85 return platform;
86}
87
88void gf_draw_platform_destroy(gf_draw_platform_t* platform) {
89 if(platform->window != NULL) {
90 glfwDestroyWindow(platform->window);
91 }
92 gf_log_function(platform->engine, "Destroyed platform-dependent part of drawing driver", "");
93 free(platform);
94}
Drawing interface.
Drawing driver.
Platform-dependent part of drawing driver.
Logger.
#define gf_log_function(engine, fmt,...)
Output log with line number and function name.
Definition gf_log.h:26
OpenGL headers.
Required headers before anything.
Platform-dependent part of drawing driver.
Drawing interface.
Definition draw.h:101
char title[128]
Window title.
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
int width
Width of window.
Definition draw.h:101
int height
Height of window.
Definition draw.h:101
Engine instance.
Definition core.h:44