1 module rpui.platform_glfw;
2 
3 version(rpuiGlfw):
4 
5 import std..string;
6 
7 import rpui.events;
8 import rpui.platform;
9 import rpui.events_observer;
10 
11 import gapi.opengl;
12 import derelict.sdl2.sdl;
13 import derelict.sdl2.ttf;
14 import derelict.sdl2.image;
15 import derelict.glfw3.glfw3;
16 
17 extern(C) void platformInit() {
18     // Load shared libraries
19     DerelictGL3.load();
20     DerelictSDL2.load();
21     DerelictSDL2Image.load();
22     DerelictSDL2TTF.load();
23     DerelictGLFW3.load();
24 
25     // Init
26 
27     if (SDL_Init(SDL_INIT_VIDEO) < 0)
28         throw new Error("Failed to init SDL");
29 
30     if (TTF_Init() < 0)
31         throw new Error("Failed to init SDL TTF");
32 
33     if (!glfwInit())
34         throw new Error("Failed to init GLFW3");
35 
36     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
37     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
38     glfwSwapInterval(1);
39 }
40 
41 extern(C) void platformShutdown() {
42     glfwTerminate();
43 }
44 
45 extern(C) Window platformCreateWindow(in string title, uint width, uint height) {
46     auto window = glfwCreateWindow(width, height, toStringz(title), null, null);
47 
48     if (!window) {
49         throw new Error("Failed to create window");
50     }
51 
52     glfwMakeContextCurrent(window);
53     DerelictGL3.reload();
54 
55     return Window(window, null);
56 }
57 
58 extern(C) void platformDestroyWindow(void* window) {
59     glfwDestroyWindow(cast(GLFWwindow*) window);
60 }
61 
62 extern(C) void platformSwapWindow(void* window) {
63     glfwSwapBuffers(cast(GLFWwindow*) window);
64 }
65 
66 extern(C) float platformGetTicks() {
67     return glfwGetTime() * 1000f;
68 }
69 
70 extern(C) bool platformEventLoop(void* window, EventsObserver events) {
71     int width;
72     int height;
73 
74     glfwGetFramebufferSize(cast(GLFWwindow*) window, &width, &height);
75     events.notify(WindowResizeEvent(width, height));
76 
77     glfwPollEvents();
78     return !glfwWindowShouldClose(cast(GLFWwindow*) window);
79 }
80 
81 extern(C) void platformShowSystemCursor() {
82 }
83 
84 extern(C) void platformHideSystemCursor() {
85 }
86 
87 extern(C) void platformSetMousePosition(void* window, in int x, in int y) {
88 }
89 
90 extern(C) void platformGapiDeleteContext(void* context) {
91 }