Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
window.cpp
Go to the documentation of this file.
1#include "window.h"
2
3#ifdef SAMURAI_WINDOWS
4#define GLFW_EXPOSE_NATIVE_WIN32
5#include <Windows.h>
6#include <GLFW/glfw3native.h>
7#endif
8
9
10
11
12
14{
15 int x = 100, y = 100, z = 640, w = 480;
16};
17
19{
20
21 WindowRect wr = {};
22
23#if PIKA_DEVELOPMENT
24 if (sfs::safeLoad(&wr, sizeof(wr), PIKA_ENGINE_SAVES_PATH "windowPos", false) != sfs::noError)
25 {
26 wr = {};
27 }
28#endif
29
30 if (wr.x < 0 || wr.y < 0 || wr.z <= 0 || wr.w <= 0)
31 {
32 wr = {};
33 }
34#if !SAMURAI_SDL
35 //todo debug from engine
36 //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
37
38 //glfwWindowHint(GLFW_SAMPLES, 1);
39
40 //glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
41 //glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
42 //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
43
44 context.wind = glfwCreateWindow(wr.z, wr.w, "SamuraiPika", NULL, NULL);
45 glfwSetWindowPos(context.wind, wr.x, wr.y);
46
47 input.hasFocus = true;
48
49 SAMURAI_ASSERT(context.wind, "problem initializing window");
50 glfwMakeContextCurrent(context.wind);
51
52 glfwSetWindowUserPointer(context.wind, this);
53
54 glfwSetMouseButtonCallback(context.wind, mouseCallback);
55 glfwSetWindowFocusCallback(context.wind, windowFocusCallback);
56 glfwSetCharCallback(context.wind, characterCallback);
57 glfwSetKeyCallback(context.wind, keyCallback);
58#else
59
60 context.wind = SDL_CreateWindow("SamuraiPika", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, wr.z, wr.w, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
61 SAMURAI_ASSERT(context.wind, "problem initializing window");
62 context.renderer = SDL_CreateRenderer(context.wind, -1, SDL_RENDERER_TARGETTEXTURE);
63 input.hasFocus = true;
64#endif
65 // allocate imgui memory with separate allocator
67
68
69
70
71 timer = std::chrono::high_resolution_clock::now();
72}
73
75{
76
77
78 WindowRect wr = {};
79#if !SAMURAI_SDL
80 glfwGetWindowPos(context.wind, &wr.x, &wr.y);
81#else
82 SDL_GetWindowPosition(context.wind, &wr.x, &wr.y);
83#endif
84
85 wr.z = windowState.w;
86 wr.w = windowState.h;
87
88 sfs::safeSave(&wr, sizeof(wr), SAMURAI_ENGINE_SAVES_PATH "windowPos", false);
89
90
91
92}
93
95{
96#if !SAMURAI_SDL
97 return glfwWindowShouldClose(context.wind);
98#else
99 return SDL_QuitRequested();
100#endif
101}
102
104{
105#pragma region deltaTime
106 auto end = std::chrono::high_resolution_clock::now();
107 input.deltaTime = (std::chrono::duration_cast<std::chrono::microseconds>(end - timer)).count() / 1000000.0f;
108 timer = end;
109
110 if (input.deltaTime > 1.f / 10) { input.deltaTime = 1.f / 10; }
111#pragma endregion
112
113#pragma region input
114
115 auto processInputBefore = [](samurai::Button &b)
116 {
117 b.setTyped(false);
118 };
119
120 processInputBefore(input.lMouse);
121 processInputBefore(input.rMouse);
122
123 for (int i = 0; i < Button::BUTTONS_COUNT; i++)
124 {
125 processInputBefore(input.buttons[i]);
126 }
127
128 memset(input.typedInput, 0, sizeof(input.typedInput));
129
130#pragma endregion
131
132#if !SAMURAI_SDL
133 glfwPollEvents();
134 glfwSwapBuffers(context.wind);
135#else
136 input.listen();
137 SDL_RenderPresent(context.renderer);
138#endif
139
140
141#pragma region window state
142
143 {
144 int w = 0;
145 int h = 0;
146#if !SAMURAI_SDL
147 glfwGetWindowSize(context.wind, &w, &h);
148#else
149 SDL_GetWindowSize(context.wind, &w, &h);
150#endif
151 windowState.w = w;
152 windowState.h = h;
153
154 }
155
156#pragma endregion
157
158
159#pragma region input
160
161 double mouseX = 0;
162 double mouseY = 0;
163#if !SAMURAI_SDL
164 glfwGetCursorPos(context.wind, &mouseX, &mouseY);
165#else
166 SDL_GetMouseState((int*)&mouseX, (int*)&mouseY);
167#endif
168 input.mouseX = (int)mouseX;
169 input.mouseY = (int)mouseY;
170
171 auto processInput = [](samurai::Button &b)
172 {
173
174 if (!b.lastState() && b.held())
175 {
176 b.setPressed(true);
177 b.setTyped(true);
178 }
179 else
180 {
181 b.setPressed(false);
182 }
183
184 if (b.lastState() && !b.held())
185 {
186 b.setReleased(true);
187 }
188 else
189 {
190 b.setReleased(false);
191 }
192
193 b.setLastState(b.held());
194
195 };
196
197 processInput(input.lMouse);
198 processInput(input.rMouse);
199
200 for (int i = 0; i < Button::BUTTONS_COUNT; i++)
201 {
202 processInput(input.buttons[i]);
203 }
204
205
206#pragma endregion
207
208}
#define SAMURAI_ENGINE_SAVES_PATH
Definition Config.h:11
#define SAMURAI_ASSERT(expression, comment)
Definition assert.h:40
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
Definition callbacks.cpp:64
void mouseCallback(GLFWwindow *window, int key, int action, int mods)
void characterCallback(GLFWwindow *window, unsigned int codepoint)
Definition callbacks.cpp:22
void windowFocusCallback(GLFWwindow *window, int focused)
Definition callbacks.cpp:30
size_t constexpr MB(size_t x)
Definition Sizes.h:6
samurai::memory::CustomAllocator imguiAllocator
Definition Context.h:25
GLFWwindow * wind
Definition Context.h:19
bool hasFocus
Definition input.h:74
void saveWindowPositions()
Definition window.cpp:74
std::chrono::steady_clock::time_point timer
Definition window.h:45
bool shouldClose()
Definition window.cpp:94
Input input
Definition window.h:31
void create()
Definition window.cpp:18
samurai::Context context
Definition window.h:26
void init(void *baseMemory, size_t memorySize)