-
Notifications
You must be signed in to change notification settings - Fork 61
/
mlx_window.c
137 lines (109 loc) · 3.89 KB
/
mlx_window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* ************************************************************************** */
/* */
/* :::::::: */
/* mlx_window.c :+: :+: */
/* +:+ */
/* By: W2wizard <w2wizzard@gmail.com> +#+ */
/* +#+ */
/* Created: 2022/02/08 01:14:59 by W2wizard #+# #+# */
/* Updated: 2022/11/22 09:06:54 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include "MLX42/MLX42_Int.h"
//= Private =//
/**
* Recalculate the view projection matrix, used by images for screen pos
* Reference: https://bit.ly/3KuHOu1 (Matrix View Projection)
*/
void mlx_update_matrix(const mlx_t* mlx, int32_t width, int32_t height)
{
const mlx_ctx_t* mlxctx = mlx->context;
const float depth = mlxctx->zdepth;
/**
* In case the setting to stretch the image is set, we maintain the width and height but not
* the depth.
*/
width = mlx_settings[MLX_STRETCH_IMAGE] ? mlxctx->initialWidth : mlx->width;
height = mlx_settings[MLX_STRETCH_IMAGE] ? mlxctx->initialHeight : mlx->height;
const float matrix[16] = {
2.f / width, 0, 0, 0,
0, 2.f / -(height), 0, 0,
0, 0, -2.f / (depth - -depth), 0,
-1, -(height / -height),
-((depth + -depth) / (depth - -depth)), 1
};
glUniformMatrix4fv(glGetUniformLocation(mlxctx->shaderprogram, "ProjMatrix"), 1, GL_FALSE, matrix);
}
static void mlx_resize_callback(GLFWwindow* window, int32_t width, int32_t height)
{
const mlx_t* mlx = glfwGetWindowUserPointer(window);
const mlx_ctx_t* mlxctx = mlx->context;
if (mlxctx->resize_hook.func)
mlxctx->resize_hook.func(width, height, mlxctx->resize_hook.param);
}
static void mlx_close_callback(GLFWwindow* window)
{
const mlx_t* mlx = glfwGetWindowUserPointer(window);
const mlx_close_t close_hook = ((mlx_ctx_t*)mlx->context)->close_hook;
close_hook.func(close_hook.param);
}
//= Public =//
void mlx_close_hook(mlx_t* mlx, mlx_closefunc func, void* param)
{
MLX_NONNULL(mlx);
MLX_NONNULL(func);
mlx_ctx_t* mlxctx = mlx->context;
mlxctx->close_hook.func = func;
mlxctx->close_hook.param = param;
glfwSetWindowCloseCallback(mlx->window, mlx_close_callback);
}
void mlx_resize_hook(mlx_t* mlx, mlx_resizefunc func, void* param)
{
MLX_NONNULL(mlx);
MLX_NONNULL(func);
mlx_ctx_t* mlxctx = mlx->context;
mlxctx->resize_hook.func = func;
mlxctx->resize_hook.param = param;
glfwSetWindowSizeCallback(mlx->window, mlx_resize_callback);
}
void mlx_set_icon(mlx_t* mlx, mlx_texture_t* image)
{
MLX_NONNULL(mlx);
MLX_NONNULL(image);
const GLFWimage icon = {
.width = image->width,
.height = image->height,
.pixels = image->pixels
};
glfwSetWindowIcon(mlx->window, 1, &icon);
}
void mlx_set_window_pos(mlx_t* mlx, int32_t xpos, int32_t ypos)
{
MLX_NONNULL(mlx);
glfwSetWindowPos(mlx->window, xpos, ypos);
}
void mlx_get_window_pos(mlx_t* mlx, int32_t* xpos, int32_t* ypos)
{
MLX_NONNULL(mlx);
MLX_NONNULL(xpos);
MLX_NONNULL(ypos);
glfwGetWindowPos(mlx->window, xpos, ypos);
}
void mlx_set_window_size(mlx_t* mlx, int32_t new_width, int32_t new_height)
{
MLX_NONNULL(mlx);
mlx->width = new_width;
mlx->height = new_height;
glfwSetWindowSize(mlx->window, new_width, new_height);
}
void mlx_set_window_limit(mlx_t* mlx, int32_t min_w, int32_t min_h, int32_t max_w, int32_t max_h)
{
MLX_NONNULL(mlx);
glfwSetWindowSizeLimits(mlx->window, min_w, min_h, max_w, max_h);
}
void mlx_set_window_title(mlx_t* mlx, const char* title)
{
MLX_NONNULL(mlx);
MLX_NONNULL(title);
glfwSetWindowTitle(mlx->window, title);
}