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_directx.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) {}
22void gf_draw_platform_end(void) {}
23
24LRESULT CALLBACK gf_draw_platform_proc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
25 PAINTSTRUCT ps;
26 RECT rect;
27 gf_draw_t* draw = (gf_draw_t*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
28 switch(msg) {
29 case WM_PAINT:
30 BeginPaint(hWnd, &ps);
31 EndPaint(hWnd, &ps);
32 break;
33 case WM_SIZE:
34 if(draw->platform != NULL) {
35 GetClientRect(hWnd, &rect);
36 draw->x = rect.left;
37 draw->y = rect.top;
38 draw->width = rect.right - rect.left;
39 draw->height = rect.bottom - rect.top;
40 gf_draw_reshape(draw);
41 }
42 break;
43 case WM_CLOSE:
44 draw->close = 1;
45 break;
46 case WM_DESTROY:
47 PostQuitMessage(0);
48 break;
49 default:
50 return DefWindowProc(hWnd, msg, wp, lp);
51 }
52 return 0;
53}
54
55int gf_draw_platform_has_extension(gf_draw_t* draw, const char* query) { return 0; }
56
57int gf_draw_platform_step(gf_draw_t* draw) {
58 MSG msg;
59 int ret = 0;
60 while(PeekMessage(&msg, draw->platform->window, 0, 0, PM_NOREMOVE)) {
61 if(GetMessage(&msg, draw->platform->window, 0, 0)) {
62 TranslateMessage(&msg);
63 DispatchMessage(&msg);
64 } else {
65 ret = 1;
66 break;
67 }
68 }
69 if(ret == 0) {
70 gf_draw_driver_before(draw);
71 gf_draw_frame(draw);
72 gf_draw_driver_after(draw);
73
74 SwapBuffers(draw->platform->dc);
75 }
76 return ret;
77}
78
79gf_draw_platform_t* gf_draw_platform_create(gf_engine_t* engine, gf_draw_t* draw) {
80 WNDCLASSEX wc;
81 RECT rect;
82 DWORD style;
83 gf_draw_platform_t* platform = malloc(sizeof(*platform));
84 memset(platform, 0, sizeof(*platform));
85 platform->engine = engine;
86
87 platform->instance = (HINSTANCE)GetModuleHandle(NULL);
88 if(platform->instance == NULL) {
89 gf_log_function(engine, "Failed to get instance", "");
90 gf_draw_platform_destroy(platform);
91 return NULL;
92 }
93
94 wc.cbSize = sizeof(wc);
95 wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
96 wc.lpfnWndProc = gf_draw_platform_proc;
97 wc.cbClsExtra = 0;
98 wc.cbWndExtra = 0;
99 wc.hInstance = platform->instance;
100 wc.hIcon = LoadIcon(platform->instance, "GAME");
101 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
102 wc.hbrBackground = NULL;
103 wc.lpszMenuName = NULL;
104 wc.lpszClassName = "goldfish";
105 wc.hIconSm = LoadIcon(platform->instance, "GAME");
106 if(!RegisterClassEx(&wc)) {
107 gf_log_function(engine, "Failed to register class", "");
108 gf_draw_platform_destroy(platform);
109 return NULL;
110 }
111
112 platform->window = CreateWindow("goldfish", draw->title, (WS_OVERLAPPEDWINDOW), draw->x, draw->y, draw->width, draw->height, NULL, 0, platform->instance, NULL);
113 if(platform->window == NULL) {
114 gf_log_function(engine, "Failed to create window", "");
115 gf_draw_platform_destroy(platform);
116 return NULL;
117 }
118
119 SetWindowLongPtr(platform->window, GWLP_USERDATA, (LONG_PTR)draw);
120
121 platform->dc = GetDC(platform->window);
122
123 SetRect(&rect, 0, 0, draw->width, draw->height);
124
125 style = (DWORD)GetWindowLongPtr(platform->window, GWL_STYLE);
126 AdjustWindowRect(&rect, style, FALSE);
127 SetWindowPos(platform->window, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE);
128
129 ShowWindow(platform->window, SW_NORMAL);
130 UpdateWindow(platform->window);
131
132 return platform;
133}
134
135void gf_draw_platform_destroy(gf_draw_platform_t* platform) {
136 if(platform->dc != NULL) {
137 ReleaseDC(platform->window, platform->dc);
138 }
139 if(platform->window != NULL) {
140 DestroyWindow(platform->window);
141 }
142 gf_log_function(platform->engine, "Destroyed platform-dependent part of drawing driver", "");
143 free(platform);
144}
DirectX headers.
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
Required headers before anything.
Platform-dependent part of drawing driver.
Drawing interface.
Definition draw.h:101
int y
Y coord of window.
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 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