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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use crate::graphics::{
Graphics,
Graphics2DAttributes,
};
use cat_engine_basement::windows::{
WindowClass,
EventLoop,
OpenGraphicsLibrary,
};
pub use cat_engine_basement::{
windows::{
Window,
WinError,
CursorIcon,
Background,
Fullscreen,
Monitor,
OpenGLRenderContext,
WindowAttributes,
WindowClassAttributes,
VirtualKeyCode,
LoopControl,
EventLoopAttributes,
OpenGLRenderContextAttributes,
UpdateInterval,
WindowProcedure,
quit,
},
event::{
Event,
WindowEvent,
},
};
pub struct WindowGraphics{
pub window_id:usize,
pub graphics:Graphics,
pub context:OpenGLRenderContext,
}
impl WindowGraphics{
}
struct WindowStorage{
free_ids:Vec<usize>,
windows:Vec<Option<Window>>,
window_graphics:Vec<Option<WindowGraphics>>,
}
impl WindowStorage{
pub fn empty(capacity:usize)->WindowStorage{
let mut free_ids=Vec::with_capacity(capacity);
let mut windows=Vec::with_capacity(capacity);
let mut window_graphics=Vec::with_capacity(capacity);
for id in (0..capacity).rev(){
free_ids.push(id);
windows.push(None);
window_graphics.push(None);
}
Self{
free_ids,
windows,
window_graphics,
}
}
pub fn add_window<W:WindowProcedure<Option<WindowGraphics>>>(
&mut self,
window_class:&WindowClass,
window_attributes:WindowAttributes,
context_attributes:OpenGLRenderContextAttributes,
graphics_library:&mut Option<OpenGraphicsLibrary>,
graphics_attributes:Graphics2DAttributes,
)->Result<bool,WinError>{
if let Some(id)=self.free_ids.pop(){
// let window_subclass_args=WindowSubclassArguments::new(main_thread_id as usize,id);
// self.window_subclass_args[id]=Some(window_subclass_args);
//.as_ref().unwrap();
match Window::new::<W,Option<WindowGraphics>>(
window_class,
window_attributes,
&mut self.window_graphics[id]
){
Ok(window)=>{
let context=OpenGLRenderContext::new(
window.get_context(),
context_attributes
).unwrap();
if graphics_library.is_none(){
let library=OpenGraphicsLibrary::new();
library.load_functions();
*graphics_library=Some(library)
}
let graphics=Graphics::new(graphics_attributes);
self.windows[id]=Some(window);
let window_graphics=WindowGraphics{
window_id:id,
graphics,
context,
};
self.window_graphics[id]=Some(window_graphics);
Ok(true)
}
Err(e)=>Err(e),
}
}
else{
Ok(false)
}
}
pub fn remove_window(&mut self,id:usize)->Option<Window>{
if let Some(maybe_window)=self.windows.get_mut(id){
if let Some(window)=maybe_window.take(){
unsafe{
let _window_graphics=self.window_graphics.get_unchecked_mut(id).take().unwrap();
}
self.free_ids.push(id);
Some(window)
}
else{
None
}
}
else{
None
}
}
pub fn is_any_window(&self)->bool{
self.free_ids.len()<self.free_ids.capacity()
}
pub fn get_any_window(&self)->Option<&Window>{
for maybe_window in &self.windows{
if let Some(window)=maybe_window{
return Some(window)
}
}
None
}
pub fn get_window(&self,id:usize)->Option<&Window>{
if let Some(maybe_window)=self.windows.get(id){
maybe_window.as_ref()
}
else{
None
}
}
pub fn get_window_unchecked(&self,id:usize)->&Window{
unsafe{
let maybe_window=self.windows.get_unchecked(id).as_ref();
std::mem::transmute(maybe_window)
}
}
// pub fn get_render_context(&self,id:usize)->Option<&OpenGLRenderContext>{
// if let Some(render_context)=self.render_contexts.get(id){
// render_context.as_ref()
// }
// else{
// None
// }
// }
// pub fn get_render_context_unchecked(&self,id:usize)->&OpenGLRenderContext{
// unsafe{
// let maybe_render_context=self.render_contexts.get_unchecked(id).as_ref();
// std::mem::transmute(maybe_render_context)
// }
// }
pub fn get_window_graphics(&self,id:usize)->Option<&WindowGraphics>{
if let Some(graphics)=self.window_graphics.get(id){
graphics.as_ref()
}
else{
None
}
}
pub fn get_window_graphics_unchecked(&self,id:usize)->&WindowGraphics{
unsafe{
let maybe_graphics=self.window_graphics.get_unchecked(id).as_ref();
std::mem::transmute(maybe_graphics)
}
}
pub fn get_window_graphics_mut(&mut self,id:usize)->Option<&mut WindowGraphics>{
if let Some(graphics)=self.window_graphics.get_mut(id){
graphics.as_mut()
}
else{
None
}
}
pub fn get_window_graphics_unchecked_mut(&mut self,id:usize)->&mut WindowGraphics{
unsafe{
let maybe_graphics=self.window_graphics.get_unchecked_mut(id).as_mut();
std::mem::transmute(maybe_graphics)
}
}
}
pub struct App{
graphics_library:Option<OpenGraphicsLibrary>,
event_loop:EventLoop,
window_class:WindowClass,
window_storage:WindowStorage,
}
impl App{
pub fn new(attributes:AppAttributes)->App{
let mut graphics_library=None;
let mut event_loop=EventLoop::new(attributes.event_loop);
let class=WindowClass::new(attributes.class).unwrap();
let mut window_storage=WindowStorage::empty(attributes.windows_limit as usize);
Self{
window_class:class,
window_storage,
graphics_library,
event_loop,
}
}
}
impl App{
pub fn create_window<W:WindowProcedure<Option<WindowGraphics>>>(
&mut self,
window_attributes:WindowAttributes,
render_context_attributes:OpenGLRenderContextAttributes,
graphics_attributes:Graphics2DAttributes,
)->Result<bool,WinError>{
self.window_storage.add_window::<W>(
&self.window_class,
window_attributes,
render_context_attributes,
&mut self.graphics_library,
graphics_attributes,
)
}
pub fn remove_window(&mut self,id:usize)->Option<Window>{
self.window_storage.remove_window(id)
}
}
impl App{
pub fn is_any_window(&self)->bool{
self.window_storage.is_any_window()
}
pub fn get_window(&self,id:usize)->Option<&Window>{
self.window_storage.get_window(id)
}
pub fn get_window_unchecked(&self,id:usize)->&Window{
self.window_storage.get_window_unchecked(id)
}
// pub fn get_render_context(&self,id:usize)->Option<&OpenGLRenderContext>{
// self.window_storage.get_render_context(id)
// }
// pub fn get_render_context_unchecked(&self,id:usize)->&OpenGLRenderContext{
// self.window_storage.get_render_context_unchecked(id)
// }
pub fn get_window_graphics(&self,id:usize)->Option<&WindowGraphics>{
self.window_storage.get_window_graphics(id)
}
pub fn ge_windowt_graphics_unchecked(&self,id:usize)->&WindowGraphics{
self.window_storage.get_window_graphics_unchecked(id)
}
pub fn get_window_graphics_mut(&mut self,id:usize)->Option<&mut WindowGraphics>{
self.window_storage.get_window_graphics_mut(id)
}
pub fn get_window_graphics_unchecked_mut(&mut self,id:usize)->&mut WindowGraphics{
self.window_storage.get_window_graphics_unchecked_mut(id)
}
}
impl App{
pub fn run<F:FnMut(Event,&mut AppControl)>(&mut self,mut event_handler:F){
let event_loop:&'static mut EventLoop=unsafe{std::mem::transmute(&mut self.event_loop)};
event_loop.run(|event,loop_control|{
let mut app_control=AppControl::new(self,loop_control);
match &event{
// Event::WindowEvent{window_event,window_id}=>match window_event{
// WindowEvent::Destroy=>{
// let _window=app_control.app.window_storage.remove_window(*window_id);
// }
// _=>{}
// }
_=>{},
}
event_handler(event,&mut app_control);
});
}
}
pub struct AppAttributes{
pub windows_limit:u8,
pub event_loop:EventLoopAttributes,
pub class:WindowClassAttributes,
// pub window:WindowAttributes,
// pub render_context:OpenGLRenderContextAttributes,
// pub graphics:Graphics2DAttributes,
}
impl AppAttributes{
pub fn new()->AppAttributes{
Self{
windows_limit:1u8,
event_loop:EventLoopAttributes::new(),
class:WindowClassAttributes::new("NewWindowClass"),
// window:WindowAttributes::new("NewWindow"),
// render_context:OpenGLRenderContextAttributes::new(),
// graphics:Graphics2DAttributes::new(),
}
}
}
pub struct AppControl{
app:&'static mut App,
loop_control:&'static mut LoopControl,
}
impl AppControl{
pub fn new(app:&mut App,loop_control:&mut LoopControl)->AppControl{
unsafe{
Self{
app:std::mem::transmute(app),
loop_control:std::mem::transmute(loop_control)
}
}
}
pub fn create_window<W:WindowProcedure<Option<WindowGraphics>>>(
&mut self,
window_attributes:WindowAttributes,
render_context_attributes:OpenGLRenderContextAttributes,
graphics_attributes:Graphics2DAttributes
)->Result<bool,WinError>{
self.app.create_window::<W>(
window_attributes,
render_context_attributes,
graphics_attributes
)
}
/// Break app's event loop.
pub fn break_loop(&mut self){
*self.loop_control=LoopControl::Break;
}
/// Sets the 'lazy' mode flag.
pub fn lazy(&mut self,lazy:bool){
if lazy{
*self.loop_control=LoopControl::Lazy;
}
else{
*self.loop_control=LoopControl::Run;
}
}
}
impl AppControl{
/// Checks whether an app has any windows.
pub fn is_any_window(&self)->bool{
self.app.window_storage.is_any_window()
}
pub fn get_window(&self,id:usize)->Option<&Window>{
self.app.window_storage.get_window(id)
}
pub fn get_window_unchecked(&self,id:usize)->&Window{
self.app.window_storage.get_window_unchecked(id)
}
// pub fn get_render_context(&self,id:usize)->Option<&OpenGLRenderContext>{
// self.app.window_storage.get_render_context(id)
// }
// pub fn get_render_context_unchecked(&self,id:usize)->&OpenGLRenderContext{
// self.app.window_storage.get_render_context_unchecked(id)
// }
pub fn get_window_graphics(&self,id:usize)->Option<&WindowGraphics>{
self.app.window_storage.get_window_graphics(id)
}
pub fn get_window_graphics_unchecked(&self,id:usize)->&WindowGraphics{
self.app.window_storage.get_window_graphics_unchecked(id)
}
pub fn get_window_graphics_mut(&mut self,id:usize)->Option<&mut WindowGraphics>{
self.app.window_storage.get_window_graphics_mut(id)
}
pub fn get_window_graphics_unchecked_mut(&mut self,id:usize)->&mut WindowGraphics{
self.app.window_storage.get_window_graphics_unchecked_mut(id)
}
// pub fn draw<F:FnMut(&Window,&mut Graphics)>(&mut self,id:usize,mut f:F)->Result<(),WinError>{
// if let Some(window)=self.app.window_storage.get_window(id){
// let window:&'static Window=unsafe{std::mem::transmute(window)};
// // Указатель на графические функции (чтобы не ругался)
// let graphics:&'static mut Graphics=unsafe{std::mem::transmute(
// self.app.window_storage.get_graphics_unchecked_mut(id) as *mut Graphics
// )};
// let render_context=self.app.window_storage.get_render_context_unchecked(id);
// render_context.make_current(true)?;
// let [width,height]=window.client_size();
// graphics.core().viewport().set([0,0,width as i32,height as i32]);
// graphics.draw_parameters().change_viewport([0f32,0f32,width as f32,height as f32]);
// f(window,graphics);
// render_context.swap_buffers()?;
// }
// Ok(())
// }
}