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#define GF_EXPOSE_INPUT
4
5#include <gf_pre.h>
6
7/* External library */
8#include <gf_opengl.h>
9
10/* Interface */
11#include <gf_draw_platform.h>
12
13/* Engine */
14#include <gf_draw_driver.h>
15#include <gf_log.h>
16#include <gf_draw.h>
17#include <gf_input.h>
18
19/* Standard */
20#include <string.h>
21#include <stdlib.h>
22
23void gf_draw_platform_begin(void) {
24 glfwInit();
25 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
26 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
27 glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
28 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
29}
30
31void gf_draw_platform_end(void) {}
32
33void gf_glfw_size(GLFWwindow* window, int w, int h) {
34 gf_draw_t* draw = (gf_draw_t*)glfwGetWindowUserPointer(window);
35 draw->width = w;
36 draw->height = h;
37 glfwSetWindowSize(window, w, h);
38 gf_draw_reshape(draw);
39}
40
41void gf_glfw_button(GLFWwindow* window, int button, int action, int mods) {
42 gf_draw_t* draw = (gf_draw_t*)glfwGetWindowUserPointer(window);
43 if(draw->input != NULL) {
44 if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) draw->input->mouse_flag |= GF_INPUT_MOUSE_LEFT_MASK;
45 if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) draw->input->mouse_flag ^= GF_INPUT_MOUSE_LEFT_MASK;
46
47 if(button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) draw->input->mouse_flag |= GF_INPUT_MOUSE_RIGHT_MASK;
48 if(button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE) draw->input->mouse_flag ^= GF_INPUT_MOUSE_RIGHT_MASK;
49
50 if(button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS) draw->input->mouse_flag |= GF_INPUT_MOUSE_MIDDLE_MASK;
51 if(button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_RELEASE) draw->input->mouse_flag ^= GF_INPUT_MOUSE_MIDDLE_MASK;
52 }
53}
54
55void gf_glfw_cursor(GLFWwindow* window, double x, double y) {
56 gf_draw_t* draw = (gf_draw_t*)glfwGetWindowUserPointer(window);
57 if(draw->input != NULL) {
58 draw->input->mouse_x = x;
59 draw->input->mouse_y = y;
60 }
61}
62
63int gf_draw_platform_has_extension(gf_draw_t* draw, const char* query) {
64 const char* ext = NULL;
65 const char* ptr;
66 const int len = strlen(query);
67
68 glfwMakeContextCurrent(draw->platform->window);
69
70 return glfwExtensionSupported(query);
71}
72
73int gf_draw_platform_step(gf_draw_t* draw) {
74 int ret = 0;
75 int w, h;
76 glfwMakeContextCurrent(draw->platform->window);
77 draw->close = glfwWindowShouldClose(draw->platform->window);
78 if(draw->close) glfwSetWindowShouldClose(draw->platform->window, GLFW_FALSE);
79 glfwPollEvents();
80 if(ret == 0) {
81 gf_draw_driver_before(draw);
82 gf_draw_frame(draw);
83 gf_draw_driver_after(draw);
84
85 glfwSwapBuffers(draw->platform->window);
86 }
87 return ret;
88}
89
90gf_draw_platform_t* gf_draw_platform_create(gf_engine_t* engine, gf_draw_t* draw) {
91 gf_draw_platform_t* platform = malloc(sizeof(*platform));
92 memset(platform, 0, sizeof(*platform));
93 platform->engine = engine;
94
95 platform->window = glfwCreateWindow(draw->width, draw->height, draw->title, NULL, NULL);
96 if(platform->window == NULL) {
97 gf_log_function(engine, "Failed to create window", "");
98 gf_draw_platform_destroy(platform);
99 return NULL;
100 }
101
102 glfwSetWindowUserPointer(platform->window, draw);
103 glfwSetCursorPosCallback(platform->window, gf_glfw_cursor);
104 glfwSetWindowSizeCallback(platform->window, gf_glfw_size);
105 glfwSetMouseButtonCallback(platform->window, gf_glfw_button);
106
107 glfwMakeContextCurrent(platform->window);
108#ifdef DO_SWAP_INTERVAL
109 glfwSwapInterval(1);
110#endif
111 return platform;
112}
113
114void gf_draw_platform_destroy(gf_draw_platform_t* platform) {
115 if(platform->window != NULL) {
116 glfwDestroyWindow(platform->window);
117 }
118 gf_log_function(platform->engine, "Destroyed platform-dependent part of drawing driver", "");
119 free(platform);
120}
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.
#define GF_INPUT_MOUSE_MIDDLE_MASK
Mask for middle mouse button.
Definition input.h:34
#define GF_INPUT_MOUSE_LEFT_MASK
Mask for left mouse button.
Definition input.h:28
#define GF_INPUT_MOUSE_RIGHT_MASK
Mask for right mouse button.
Definition input.h:40
Platform-dependent part of drawing driver.
Drawing interface.
Definition draw.h:108
gf_input_t * input
Input interface.
Definition draw.h:108
char title[128]
Window title.
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
int width
Width of window.
Definition draw.h:108
int height
Height of window.
Definition draw.h:108
Engine instance.
Definition core.h:46
int mouse_flag
Mouse flag.
Definition input.h:64
int mouse_y
Y coord of mouse.
Definition input.h:64
int mouse_x
X coord of mouse.
Definition input.h:64