-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlx_init_loop.m
164 lines (138 loc) · 3.65 KB
/
mlx_init_loop.m
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// mlx_init_loop.m
// By Ol
#import <Cocoa/Cocoa.h>
#import <OpenGL/gl3.h>
#import <AppKit/NSOpenGLView.h>
#include "mlx_int.h"
#include "mlx_new_window.h"
#include "font.c"
@interface MlxLoopHookObj:NSObject
{
mlx_ptr_t *mlx_ptr;
}
- (id) initWithPtr:(void *)mlxptr;
- (void) do_loop_hook;
@end
@implementation MlxLoopHookObj
- (id) initWithPtr:(void *)mlxptr
{
if (self = [super init])
mlx_ptr = mlxptr;
return (self);
}
- (void) do_loop_hook
{
mlx_win_list_t *win;
if (mlx_ptr->loop_hook != NULL)
mlx_ptr->loop_hook(mlx_ptr->loop_hook_data);
win = mlx_ptr->win_list;
while (win)
{
if (win->nb_flush > 0 && win->pixmgt)
{
[(id)win->winid selectGLContext];
[(id)win->winid mlx_gl_draw];
glFlush();
win->nb_flush = 0;
}
win = win->next;
}
[self performSelector:@selector(do_loop_hook) withObject:nil afterDelay:0.0];
}
@end
void *mlx_init()
{
mlx_ptr_t *new_mlx;
int bidon;
int i;
if ((new_mlx = malloc(sizeof(*new_mlx))) == NULL)
return ((void *)0);
new_mlx->win_list = NULL;
new_mlx->img_list = NULL;
new_mlx->loop_hook = NULL;
new_mlx->loop_hook_data = NULL;
new_mlx->appid = [NSApplication sharedApplication];
// super magic trick to detach app from terminal, get menubar & key input events
for (NSRunningApplication * app in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"])
{
[app activateWithOptions:NSApplicationActivateIgnoringOtherApps];
break;
}
usleep(100000);
ProcessSerialNumber psn = { 0, kCurrentProcess };
(void) TransformProcessType(&psn, kProcessTransformToForegroundApplication);
usleep(100000);
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
// load font
new_mlx->font = mlx_new_image(new_mlx, (FONT_WIDTH+2)*95, FONT_HEIGHT);
i = 0;
while (i < 4*(FONT_WIDTH+2)*95*FONT_HEIGHT)
{
new_mlx->font->buffer[i+0] = font_atlas.pixel_data[i+2];
new_mlx->font->buffer[i+1] = font_atlas.pixel_data[i+1];
new_mlx->font->buffer[i+2] = font_atlas.pixel_data[i+0];
((unsigned char *)new_mlx->font->buffer)[i+3] = 0xFF-font_atlas.pixel_data[i+3];
i += 4;
}
new_mlx->font->vertexes[2] = FONT_WIDTH;
new_mlx->font->vertexes[4] = FONT_WIDTH;
new_mlx->font->vertexes[5] = -FONT_HEIGHT-1;
new_mlx->font->vertexes[7] = -FONT_HEIGHT-1;
return ((void *)new_mlx);
}
void mlx_loop(mlx_ptr_t *mlx_ptr)
{
[[[MlxLoopHookObj alloc] initWithPtr:mlx_ptr] performSelector:@selector(do_loop_hook) withObject:nil afterDelay:0.0];
[NSApp run];
}
void mlx_pixel_put(mlx_ptr_t *mlx_ptr, mlx_win_list_t *win_ptr, int x, int y, int color)
{
if (!win_ptr->pixmgt)
return ;
[(id)(win_ptr->winid) selectGLContext];
[(id)(win_ptr->winid) pixelPutColor:color X:x Y:y];
win_ptr->nb_flush ++;
}
void mlx_int_loop_once()
{
NSEvent *event;
NSDate *thedate;
thedate = [NSDate dateWithTimeIntervalSinceNow:0.1];
while (42)
{
event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:thedate
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event == nil)
{
[thedate release];
return ;
}
[NSApp sendEvent:event];
[NSApp updateWindows];
}
}
int mlx_do_sync(mlx_ptr_t *mlx_ptr)
{
mlx_win_list_t *win;
win = mlx_ptr->win_list;
while (win)
{
if (win->pixmgt)
{
[(id)(win->winid) selectGLContext];
[(id)(win->winid) mlx_gl_draw];
glFlush();
mlx_int_loop_once();
}
win = win->next;
}
return (0);
}
int mlx_loop_hook(mlx_ptr_t *mlx_ptr, void (*fct)(void *), void *param)
{
mlx_ptr->loop_hook = fct;
mlx_ptr->loop_hook_data = param;
return (0);
}