2021-11-30 21:29:18 -06:00
|
|
|
#include "window.h"
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
#include <string.h>
|
2022-01-19 16:43:21 -06:00
|
|
|
#include "texture.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
#include "log.h"
|
2022-02-06 10:14:57 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
struct mSDLWindow *mainwin;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
static struct mSDLWindow *windows[5];
|
|
|
|
static int numWindows = 0;
|
|
|
|
|
|
|
|
struct mSDLWindow *MakeSDLWindow(const char *name, int width, int height,
|
|
|
|
uint32_t flags)
|
|
|
|
{
|
|
|
|
struct mSDLWindow *w = calloc(1, sizeof(struct mSDLWindow));
|
|
|
|
w->width = width;
|
|
|
|
w->height = height;
|
2022-06-23 02:34:51 -05:00
|
|
|
w->window = glfwCreateWindow(width, height, name, NULL, NULL);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
if (!w->window) {
|
|
|
|
printf("Couldn't make GLFW window\n");
|
2021-11-30 21:29:18 -06:00
|
|
|
} else {
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwMakeContextCurrent(w->window);
|
|
|
|
gladLoadGL(glfwGetProcAddress);
|
2022-02-01 14:50:25 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwSwapInterval(1);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
w->id = numWindows;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
|
|
|
|
if (numWindows < 5)
|
|
|
|
windows[numWindows++] = w;
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
mainwin = windows[0];
|
2021-11-30 21:29:18 -06:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
void window_destroy(struct mSDLWindow *w)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwDestroyWindow(w->window);
|
|
|
|
}
|
|
|
|
|
|
|
|
void window_handle_event(struct mSDLWindow *w)
|
|
|
|
{
|
|
|
|
/*
|
2021-11-30 21:29:18 -06:00
|
|
|
if (e->type == SDL_WINDOWEVENT && e->window.windowID == w->id) { // TODO: Check ptr direct?
|
|
|
|
switch (e->window.event) {
|
|
|
|
case SDL_WINDOWEVENT_SHOWN:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO, "Showed window %d.", w->id);
|
|
|
|
w->shown = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_HIDDEN:
|
|
|
|
w->shown = false;
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO, "Hid window %d.", w->id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
|
|
|
|
|
|
|
w->width = e->window.data1;
|
|
|
|
w->height = e->window.data2;
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO,
|
|
|
|
"Changed size of window %d: width %d, height %d.",
|
|
|
|
w->id, w->width, w->height);
|
|
|
|
window_makecurrent(w);
|
2022-02-06 10:14:57 -06:00
|
|
|
w.projection =
|
2021-11-30 21:29:18 -06:00
|
|
|
glm::ortho(0.f, (float) width, 0.f, (float) height, -1.f,
|
2022-02-06 10:14:57 -06:00
|
|
|
1.f);
|
2021-11-30 21:29:18 -06:00
|
|
|
w->render = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_EXPOSED:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO, "Exposed window %d.", w->id);
|
|
|
|
w->render = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_ENTER:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO, "Entered window %d.", w->id);
|
|
|
|
w->mouseFocus = true;
|
|
|
|
SDL_RaiseWindow(w->window);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_LEAVE:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO, "Left window %d.", w->id);
|
|
|
|
w->mouseFocus = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO,
|
|
|
|
"Lost focus on window %d.", w->id);
|
|
|
|
w->keyboardFocus = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO,
|
|
|
|
"Gained focus on window %d.", w->id);
|
|
|
|
w->keyboardFocus = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_MINIMIZED:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO,
|
|
|
|
"Minimized window %d.", w->id);
|
|
|
|
w->minimized = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_MAXIMIZED:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO,
|
|
|
|
"Maximized window %d.", w->id);
|
|
|
|
w->minimized = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_RESTORED:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO,
|
|
|
|
"Restored window %d.", w->id);
|
|
|
|
w->minimized = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_WINDOWEVENT_CLOSE:
|
|
|
|
YughLog(0, SDL_LOG_PRIORITY_INFO, "Closed window %d.", w->id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-06 10:14:57 -06:00
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-02-06 10:14:57 -06:00
|
|
|
void window_all_handle_events()
|
2022-01-19 16:43:21 -06:00
|
|
|
{
|
|
|
|
for (int i = 0; i < numWindows; i++) {
|
2022-02-06 10:14:57 -06:00
|
|
|
window_handle_event(windows[i]);
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
void window_makefullscreen(struct mSDLWindow *w)
|
|
|
|
{
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwMaximizeWindow(w->window);
|
2021-11-30 21:29:18 -06:00
|
|
|
w->fullscreen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void window_togglefullscreen(struct mSDLWindow *w)
|
|
|
|
{
|
|
|
|
w->fullscreen = !w->fullscreen;
|
|
|
|
|
|
|
|
if (w->fullscreen) {
|
|
|
|
window_makefullscreen(w);
|
|
|
|
} else {
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwRestoreWindow(w->window);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void window_makecurrent(struct mSDLWindow *w)
|
|
|
|
{
|
2022-06-23 11:05:47 -05:00
|
|
|
|
|
|
|
if (w->window != glfwGetCurrentContext())
|
|
|
|
glfwMakeContextCurrent(w->window);
|
2021-11-30 21:29:18 -06:00
|
|
|
glViewport(0, 0, w->width, w->height);
|
2022-06-23 11:05:47 -05:00
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void window_swap(struct mSDLWindow *w)
|
|
|
|
{
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwSwapBuffers(w->window);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2022-01-19 16:43:21 -06:00
|
|
|
|
|
|
|
void window_seticon(struct mSDLWindow *w, struct Texture *icon)
|
|
|
|
{
|
2022-06-23 11:05:47 -05:00
|
|
|
|
|
|
|
static GLFWimage images[1];
|
|
|
|
images[0].width = icon->width;
|
|
|
|
images[0].height = icon->height;
|
|
|
|
images[0].pixels = icon->data;
|
2022-02-06 10:14:57 -06:00
|
|
|
glfwSetWindowIcon(w->window, 1, images);
|
2022-06-23 11:05:47 -05:00
|
|
|
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int window_hasfocus(struct mSDLWindow *w)
|
|
|
|
{
|
2022-02-06 10:14:57 -06:00
|
|
|
return glfwGetWindowAttrib(w->window, GLFW_FOCUSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
double frame_time()
|
|
|
|
{
|
|
|
|
return glfwGetTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
double elapsed_time()
|
|
|
|
{
|
|
|
|
static double last_time;
|
|
|
|
static double elapsed;
|
|
|
|
elapsed = frame_time() - last_time;
|
|
|
|
return elapsed;
|
|
|
|
}
|
2022-06-23 11:05:47 -05:00
|
|
|
|
|
|
|
int elapsed_time_ms()
|
|
|
|
{
|
|
|
|
return elapsed_time() * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
|