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
use miniquad::Context as QuadContext;
use miniquad::*;

pub use megaui;
pub use megaui::hash;

pub use glam::{vec2, Vec2};

use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;

pub mod drawing;
pub mod exec;

mod camera;
mod models;
mod shapes;
mod texture;
mod time;
mod types;
mod ui;

pub use camera::{Camera, Camera2D, Camera3D, Projection};

pub use macroquad_macro::main;
pub use models::*;
pub use shapes::*;
pub use texture::*;
pub use time::*;
pub use types::*;
pub use ui::*;

pub use miniquad::{Comparison, PipelineParams};
pub use quad_gl::{colors::*, GlPipeline, QuadGl, Vertex};
pub use quad_rand as rand;

#[cfg(feature = "log-impl")]
pub use miniquad::{debug, info, log, warn};

use drawing::DrawContext;

struct Context {
    quad_context: QuadContext,

    screen_width: f32,
    screen_height: f32,

    keys_pressed: HashSet<KeyCode>,
    mouse_pressed: HashSet<MouseButton>,
    mouse_position: Vec2,
    mouse_wheel: Vec2,

    draw_context: DrawContext,

    start_time: f64,
    last_frame_time: f64,
    frame_time: f64,
}

impl Context {
    fn new(mut ctx: QuadContext) -> Context {
        let (screen_width, screen_height) = ctx.screen_size();

        Context {
            screen_width,
            screen_height,

            keys_pressed: HashSet::new(),
            mouse_pressed: HashSet::new(),
            mouse_position: Vec2::new(0., 0.),
            mouse_wheel: Vec2::new(0., 0.),

            draw_context: DrawContext::new(&mut ctx),

            quad_context: ctx,

            start_time: miniquad::date::now(),
            last_frame_time: miniquad::date::now(),
            frame_time: 1. / 60.,
        }
    }

    fn end_frame(&mut self) {
        self.draw_context
            .perform_render_passes(&mut self.quad_context);

        self.quad_context.commit_frame();

        get_context().mouse_wheel = Vec2::new(0., 0.);
    }

    fn clear(&mut self, color: Color) {
        self.quad_context.clear(
            Some((
                color.0[0] as f32 / 255.0,
                color.0[1] as f32 / 255.0,
                color.0[2] as f32 / 255.0,
                color.0[3] as f32 / 255.0,
            )),
            None,
            None,
        );
        self.draw_context.gl.reset();
        self.draw_context
            .update_projection_matrix(&mut self.quad_context);
    }
}

static mut CONTEXT: Option<Context> = None;

fn get_context() -> &'static mut Context {
    unsafe { CONTEXT.as_mut().unwrap_or_else(|| panic!()) }
}

static mut MAIN_FUTURE: Option<Pin<Box<dyn Future<Output = ()>>>> = None;

struct Stage {}

impl EventHandlerFree for Stage {
    fn resize_event(&mut self, width: f32, height: f32) {
        get_context().screen_width = width;
        get_context().screen_height = height;
    }

    fn mouse_motion_event(&mut self, x: f32, y: f32) {
        use megaui::InputHandler;

        let context = get_context();

        context.mouse_position = Vec2::new(x, y);
        context.draw_context.ui.mouse_move((x, y));
    }
    fn mouse_wheel_event(&mut self, x: f32, y: f32) {
        use megaui::InputHandler;

        let context = get_context();

        context.mouse_wheel.set_x(x);
        context.mouse_wheel.set_y(y);

        context.draw_context.ui.mouse_wheel(x, -y);
    }
    fn mouse_button_down_event(&mut self, btn: MouseButton, x: f32, y: f32) {
        use megaui::InputHandler;

        let context = get_context();

        context.mouse_pressed.insert(btn);
        context.draw_context.ui.mouse_down((x, y));
    }

    fn mouse_button_up_event(&mut self, btn: MouseButton, x: f32, y: f32) {
        use megaui::InputHandler;

        let context = get_context();

        context.mouse_pressed.remove(&btn);

        context.draw_context.ui.mouse_up((x, y));
    }

    fn char_event(&mut self, character: char, modifiers: KeyMods, _repeat: bool) {
        use megaui::InputHandler;

        let context = get_context();
        context
            .draw_context
            .ui
            .char_event(character, modifiers.shift, modifiers.ctrl);
    }

    fn key_down_event(&mut self, keycode: KeyCode, modifiers: KeyMods, _: bool) {
        use megaui::InputHandler;

        let context = get_context();
        context.keys_pressed.insert(keycode);

        match keycode {
            KeyCode::Up => context.draw_context.ui.key_down(
                megaui::KeyCode::Up,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Down => context.draw_context.ui.key_down(
                megaui::KeyCode::Down,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Right => context.draw_context.ui.key_down(
                megaui::KeyCode::Right,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Left => context.draw_context.ui.key_down(
                megaui::KeyCode::Left,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Home => context.draw_context.ui.key_down(
                megaui::KeyCode::Home,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::End => context.draw_context.ui.key_down(
                megaui::KeyCode::End,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Delete => context.draw_context.ui.key_down(
                megaui::KeyCode::Delete,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Backspace => context.draw_context.ui.key_down(
                megaui::KeyCode::Backspace,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Enter => context.draw_context.ui.key_down(
                megaui::KeyCode::Enter,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Z => context.draw_context.ui.key_down(
                megaui::KeyCode::Z,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::Y => context.draw_context.ui.key_down(
                megaui::KeyCode::Y,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::C => context.draw_context.ui.key_down(
                megaui::KeyCode::C,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::X => context.draw_context.ui.key_down(
                megaui::KeyCode::X,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::V => context.draw_context.ui.key_down(
                megaui::KeyCode::V,
                modifiers.shift,
                modifiers.ctrl,
            ),
            KeyCode::A => context.draw_context.ui.key_down(
                megaui::KeyCode::A,
                modifiers.shift,
                modifiers.ctrl,
            ),
            _ => {}
        }
    }

    fn key_up_event(&mut self, keycode: KeyCode, _: KeyMods) {
        let context = get_context();
        context.keys_pressed.remove(&keycode);
    }

    fn update(&mut self) {}

    fn draw(&mut self) {
        if let Some(future) = unsafe { MAIN_FUTURE.as_mut() } {
            if exec::resume(future) {
                unsafe {
                    MAIN_FUTURE = None;
                }
                info!("macroquad is done");
                get_context().quad_context.quit();
                return;
            }
        }

        get_context().end_frame();

        get_context().frame_time = date::now() - get_context().last_frame_time;
        get_context().last_frame_time = date::now();
    }
}

pub struct Window {}

impl Window {
    pub fn new(label: &str, future: impl Future<Output = ()> + 'static) {
        miniquad::start(
            conf::Conf {
                sample_count: 4,
                window_title: label.to_string(),
                ..Default::default()
            },
            |ctx| {
                unsafe {
                    MAIN_FUTURE = Some(Box::pin(future));
                }
                unsafe { CONTEXT = Some(Context::new(ctx)) };
                if exec::resume(unsafe { MAIN_FUTURE.as_mut().unwrap() }) {
                    unsafe {
                        MAIN_FUTURE = None;
                    }
                }

                UserData::free(Stage {})
            },
        );
    }
}

pub fn next_frame() -> exec::FrameFuture {
    exec::FrameFuture
}

pub use miniquad::{KeyCode, MouseButton};

pub fn mouse_position() -> (f32, f32) {
    let context = get_context();

    (context.mouse_position.x(), context.mouse_position.y())
}

pub fn mouse_wheel() -> (f32, f32) {
    let context = get_context();

    (context.mouse_wheel.x(), context.mouse_wheel.y())
}

pub fn is_key_down(key_code: KeyCode) -> bool {
    let context = get_context();

    context.keys_pressed.contains(&key_code)
}

pub fn is_mouse_button_down(btn: MouseButton) -> bool {
    let context = get_context();

    context.mouse_pressed.contains(&btn)
}

pub fn mouse_over_ui() -> bool {
    let context = get_context();

    context.draw_context.ui.is_mouse_over(megaui::Vector2::new(
        context.mouse_position.x(),
        context.mouse_position.y(),
    ))
}

pub fn clear_background(color: Color) {
    let context = get_context();

    context
        .quad_context
        .begin_default_pass(PassAction::clear_color(
            color.0[0] as f32 / 255.,
            color.0[1] as f32 / 255.,
            color.0[2] as f32 / 255.,
            color.0[3] as f32 / 255.,
        ));
    context.quad_context.end_render_pass();

    context.clear(color);
}

/// Set active 2D or 3D camera
pub fn set_camera<T: Camera>(camera: T) {
    let context = get_context();

    context.draw_context.gl.depth_test(true);
    context.draw_context.camera_matrix = Some(camera.matrix());
    context
        .draw_context
        .update_projection_matrix(&mut context.quad_context);
}

/// Reset default 2D camera mode
pub fn set_default_camera() {
    let context = get_context();

    context.draw_context.gl.depth_test(false);
    context.draw_context.camera_matrix = None;
    context
        .draw_context
        .update_projection_matrix(&mut context.quad_context);
}

pub fn screen_width() -> f32 {
    let context = get_context();

    context.screen_width
}

pub fn screen_height() -> f32 {
    let context = get_context();

    context.screen_height
}

pub unsafe fn get_internal_gl<'a>() -> &'a mut quad_gl::QuadGl {
    let context = &mut get_context().draw_context;

    &mut context.gl
}

pub fn gl_make_pipeline(
    fragment_shader: &str,
    vertex_shader: &str,
    params: PipelineParams,
) -> Result<GlPipeline, ShaderError> {
    let context = &mut get_context();

    context.draw_context.gl.make_pipeline(
        &mut context.quad_context,
        fragment_shader,
        vertex_shader,
        params,
    )
}

pub fn gl_use_pipeline(pipeline: GlPipeline) {
    let context = &mut get_context().draw_context;

    context.gl.pipeline(Some(pipeline));
}

pub fn gl_use_default_pipeline() {
    let context = &mut get_context().draw_context;

    context.gl.pipeline(None);
}