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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//! The [`Renderer`] annd [`Quad`] structs. Used for rendering.

use std::collections::BTreeMap;
use std::fmt;
use std::rc::Rc;
use web_sys::WebGlUniformLocation;

use crate::component::Component;
use crate::{gl, mesh, texture, Color32};
use crate::{Camera, Shader, Transform, GL};

use gl::Bind;
use mesh::{Mesh, Vertex};
use texture::{SubTexture, Texture};

/// Maximum [`Quad`]s in a single batch.
pub const MAX_BATCH_QUADS: i32 = 1000;
const MAX_BATCH_VERTICES: i32 = MAX_BATCH_QUADS * 4;
const MAX_BATCH_INDICES: i32 = MAX_BATCH_QUADS * 6;

/// A [`Quad`] is a simple mesh definition with four [`Vertices`](Vertex).
#[derive(Debug)]
pub struct Quad([Vertex; 4]);

impl Default for Quad {
    fn default() -> Self {
        Self([
            Vertex {
                position: [-0.5, 0.5],
                uv: [0.0, 0.0],
                ..Default::default()
            },
            Vertex {
                position: [-0.5, -0.5],
                uv: [0.0, 1.0],
                ..Default::default()
            },
            Vertex {
                position: [0.5, -0.5],
                uv: [1.0, 1.0],
                ..Default::default()
            },
            Vertex {
                position: [0.5, 0.5],
                uv: [1.0, 0.0],
                ..Default::default()
            },
        ])
    }
}

impl Quad {
    /// Create a new [`Quad`] from a given position, rotation, size and color.
    pub fn new_from_position_and_rotation_and_size_and_color(
        pos_x: f32,
        pos_y: f32,
        rotation: f32,
        size_x: f32,
        size_y: f32,
        color: Color32,
    ) -> Self {
        let size_x = size_x / 2.0;
        let size_y = size_y / 2.0;
        let color = <[f32; 4]>::from(color);
        let (sin_theta, cos_theta) = rotation.sin_cos();
        let mut points = [
            [-size_x, -size_y],
            [-size_x, size_y],
            [size_x, size_y],
            [size_x, -size_y],
        ];
        for point in points.iter_mut() {
            let (x, y) = (point[0], point[1]);
            point[0] = pos_x + x * cos_theta - y * sin_theta;
            point[1] = pos_y + x * sin_theta + y * cos_theta;
        }
        Self([
            Vertex {
                position: points[0],
                uv: [0.0, 0.0],
                color,
            },
            Vertex {
                position: points[1],
                uv: [0.0, 1.0],
                color,
            },
            Vertex {
                position: points[2],
                uv: [1.0, 1.0],
                color,
            },
            Vertex {
                position: points[3],
                uv: [1.0, 0.0],
                color,
            },
        ])
    }

    /// Create a new [`Quad`] from a given position, size and color.
    pub fn new_from_position_and_size_and_color(
        pos_x: f32,
        pos_y: f32,
        size_x: f32,
        size_y: f32,
        color: Color32,
    ) -> Self {
        let size_x = size_x / 2.0;
        let size_y = size_y / 2.0;
        let color = <[f32; 4]>::from(color);
        Self([
            Vertex {
                position: [pos_x - size_x, pos_y + size_y],
                uv: [0.0, 0.0],
                color,
            },
            Vertex {
                position: [pos_x - size_x, pos_y - size_y],
                uv: [0.0, 1.0],
                color,
            },
            Vertex {
                position: [pos_x + size_x, pos_y - size_y],
                uv: [1.0, 1.0],
                color,
            },
            Vertex {
                position: [pos_x + size_x, pos_y + size_y],
                uv: [1.0, 0.0],
                color,
            },
        ])
    }

    /// Create a new [`Quad`] from a given position and size.
    pub fn new_from_position_and_size(pos_x: f32, pos_y: f32, size_x: f32, size_y: f32) -> Self {
        Self::new_from_position_and_size_and_color(pos_x, pos_y, size_x, size_y, Color32::WHITE)
    }

    /// Create a new [`Quad`] from a given position, size, and a reference to a [`SubTexture`].
    pub fn new_from_position_and_size_and_sprite(
        pos_x: f32,
        pos_y: f32,
        size_x: f32,
        size_y: f32,
        sprite: &SubTexture,
    ) -> Self {
        let uv = sprite.get_uv_coords();
        let size_x = size_x / 2.0;
        let size_y = size_y / 2.0;
        Self([
            Vertex {
                position: [pos_x - size_x, pos_y + size_y],
                uv: uv[0],
                ..Default::default()
            },
            Vertex {
                position: [pos_x - size_x, pos_y - size_y],
                uv: uv[1],
                ..Default::default()
            },
            Vertex {
                position: [pos_x + size_x, pos_y - size_y],
                uv: uv[2],
                ..Default::default()
            },
            Vertex {
                position: [pos_x + size_x, pos_y + size_y],
                uv: uv[3],
                ..Default::default()
            },
        ])
    }

    /// Create a new [`Quad`] using a given [`Transform`] for its position and scale.
    pub fn new_from_transform(transform: Transform) -> Self {
        Self::new_from_position_and_size(
            transform.position.x,
            transform.position.x,
            transform.scale.x,
            transform.scale.y,
        )
    }

    /// Create a new [`Quad`] using a given [`Transform`] for its position and scale, and a reference to [`SubTexture`].
    pub fn new_from_transform_and_sprite(transform: Transform, sprite: &SubTexture) -> Self {
        Self::new_from_position_and_size_and_sprite(
            transform.position.x,
            transform.position.x,
            transform.scale.x,
            transform.scale.y,
            sprite,
        )
    }

    /// Get the [`Vertices`](Vertex) of the [`Quad`] as a [`Vec`].
    pub fn get_vertices(&self) -> Vec<Vertex> {
        self.0.to_vec()
    }
}

/// The [`Renderer`] is responsible for drawing on the screen. It handles the [`Camera`] and [`Shader`]s.
pub struct Renderer {
    /// The [`WebGl2RenderingContext`](web_sys::WebGl2RenderingContext) used by the [`Renderer`].
    pub gl: GL,
    /// The [`Shader`] used by the [`Renderer`].
    pub program: Shader,
    /// The [`Camera`] used by the [`Renderer`].
    pub camera: Camera,
    batches: Vec<Mesh>,
    /// [`Components`](Component) that can be added to the [`Renderer`].
    pub components: BTreeMap<&'static str, Box<dyn Component>>,
    textures: BTreeMap<&'static str, Rc<Texture>>,
    u_time: Option<WebGlUniformLocation>,
    u_color: Option<WebGlUniformLocation>,
    u_model_matrix: Option<WebGlUniformLocation>,
    u_view_matrix: Option<WebGlUniformLocation>,
    u_projection_matrix: Option<WebGlUniformLocation>,
}

impl Default for Renderer {
    fn default() -> Self {
        let gl = gl::get_context();
        let program = Shader::new(&gl);
        Self {
            camera: Camera::default(),
            batches: Vec::new(),
            components: BTreeMap::new(),
            u_time: program.get_uniform_location(&gl, "uTime"),
            u_color: program.get_uniform_location(&gl, "uColor"),
            u_model_matrix: program.get_uniform_location(&gl, "uModel"),
            u_view_matrix: program.get_uniform_location(&gl, "uView"),
            u_projection_matrix: program.get_uniform_location(&gl, "uProj"),
            program,
            textures: {
                let mut textues = BTreeMap::<&str, Rc<Texture>>::new();
                textues.insert("WHITE", Rc::new(Texture::white(&gl)));
                textues.insert("MAGENTA", Rc::new(Texture::colored(&gl, Color32::MAGENTA)));
                textues.insert("CHECKERBOARD", Rc::new(Texture::checkerboard(&gl)));
                textues
            },
            gl,
        }
    }
}

impl fmt::Debug for Renderer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Renderer")
            .field("gl", &self.gl)
            .field("program", &self.program)
            .field("camera", &self.camera)
            .field("batches", &self.batches)
            .field("textures", &self.textures)
            .field("u_time", &self.u_time)
            .field("u_color", &self.u_color)
            .field("u_model_matrix", &self.u_model_matrix)
            .field("u_view_matrix", &self.u_view_matrix)
            .field("u_projection_matrix", &self.u_projection_matrix)
            .finish()
    }
}

impl Renderer {
    /// Create a new [`Renderer`] with a given [`Camera`].
    pub fn new_with_camera(camera: Camera) -> Self {
        Self {
            camera,
            ..Default::default()
        }
    }

    /// Create a new [`Renderer`] with a given [`Camera`] and [`Shader`].
    pub fn new_with_camera_and_program(camera: Camera, program: Shader) -> Self {
        let gl = gl::get_context();
        program.bind(&gl);

        Self {
            camera,
            u_time: program.get_uniform_location(&gl, "uTime"),
            u_color: program.get_uniform_location(&gl, "uColor"),
            u_model_matrix: program.get_uniform_location(&gl, "uModel"),
            u_view_matrix: program.get_uniform_location(&gl, "uView"),
            u_projection_matrix: program.get_uniform_location(&gl, "uProj"),
            program,
            gl,
            ..Default::default()
        }
    }

    /// Set the [`Camera`] that the [`Renderer`] will use.
    pub fn set_camera(&mut self, camera: Camera) {
        self.camera = camera;
    }

    /// Set the [`Shader`] that the [`Renderer`] will use.
    pub fn set_shader(&mut self, program: Shader) {
        let gl = &self.gl;

        self.u_time = program.get_uniform_location(gl, "uTime");
        self.u_color = program.get_uniform_location(gl, "uColor");
        self.u_model_matrix = program.get_uniform_location(gl, "uModel");
        self.u_view_matrix = program.get_uniform_location(gl, "uView");
        self.u_projection_matrix = program.get_uniform_location(gl, "uProj");
        self.program = program;
    }

    /// Handle screen resizes.
    pub fn resize(&mut self, width: f32, height: f32) {
        self.camera.set_width_and_height(width, height);
        self.gl.viewport(0, 0, width as i32, height as i32);
        self.gl.uniform_matrix4fv_with_f32_array(
            self.u_projection_matrix.as_ref(),
            false,
            self.camera.projection(),
        );
    }

    /// Initialise the uniforms for the current [`Shader`].
    pub fn init_shader(&mut self) {
        let gl = &self.gl;
        self.program.bind(gl);

        gl.uniform1f(self.u_time.as_ref(), 0.0);
        gl.uniform4f(self.u_color.as_ref(), 1.0, 1.0, 1.0, 1.0);
        gl.uniform_matrix4fv_with_f32_array(
            self.u_view_matrix.as_ref(),
            false,
            crate::Mat4::identity().as_slice(),
        );
        gl.uniform_matrix4fv_with_f32_array(
            self.u_view_matrix.as_ref(),
            false,
            self.camera.transform.matrix_slice(),
        );
        gl.uniform_matrix4fv_with_f32_array(
            self.u_projection_matrix.as_ref(),
            false,
            self.camera.projection(),
        );
    }

    /// Add a [`Texture`] to the [`Renderer`].
    ///
    /// The renderer stores [`Texture`]s that can be retreived later, via a string slice.
    pub fn add_texture(&mut self, key: &'static str, texture: Texture) {
        self.textures.insert(key, Rc::new(texture));
    }

    /// Use the requested [`Texture`].
    ///
    /// Sets the currently bound [`Texture`] to the one that matches the key. If no such texture is found, a default MAGENTA one is found.
    pub fn use_texture(&self, key: &str) {
        let gl = &self.gl;
        self.textures
            .get(key)
            .unwrap_or_else(|| self.textures.get("MAGENTA").unwrap())
            .bind(gl);
    }

    /// Get the requested [`Texture`], or MAGENTA if none is found.
    pub fn get_texture(&mut self, key: &str) -> Rc<Texture> {
        Rc::clone(
            self.textures
                .get(key)
                .unwrap_or_else(|| self.textures.get("MAGENTA").unwrap())
        )
    }

    /// Clear the batch queue and start a new batch.
    pub fn begin_draw(&mut self) {
        let gl = &self.gl;

        self.batches.clear();

        let mesh = Mesh::new(
            gl,
            Vec::with_capacity(MAX_BATCH_VERTICES as usize),
            Vec::with_capacity(MAX_BATCH_INDICES as usize),
        );

        self.batches.push(mesh);
    }

    /// Add a [`Quad`] to the batching queue.
    pub fn add_quad(&mut self, quad: &Quad) {
        let gl = &self.gl;

        // Get last batch. This should never be empty becase begin_draw should have been called before.
        let mut batch = self
            .batches
            .last_mut()
            .expect("Batch list empty. Check if begin_draw was called before.");
        if batch.vertices.len() + 4 > MAX_BATCH_VERTICES as usize {
            let mesh = Mesh::new(
                gl,
                Vec::with_capacity(MAX_BATCH_VERTICES as usize),
                Vec::with_capacity(MAX_BATCH_INDICES as usize),
            );
            self.batches.push(mesh);

            batch = self.batches.last_mut().unwrap();
        }

        let last = batch.vertices.len() as u32;
        batch.vertices.append(&mut quad.get_vertices());
        let mut indices = vec![last, last + 2, last + 1, last, last + 3, last + 2];
        batch.indices.append(&mut indices);
    }

    /// Begin a new layer.
    ///
    /// A new mesh is added to the batches and subsequent calls are made on this layer.
    pub fn begin_layer(&mut self) {
        let gl = &self.gl;
        let mesh = Mesh::new(
            gl,
            Vec::with_capacity(MAX_BATCH_VERTICES as usize),
            Vec::with_capacity(MAX_BATCH_INDICES as usize),
        );

        self.batches.push(mesh);
    }

    /// Remove the last layer.
    pub fn delete_layer(&mut self) -> Option<Mesh> {
        self.batches.pop()
    }

    /// Draw the current layer.
    pub fn draw_layer(&self) {
        let gl = &self.gl;
        if let Some(batch) = self.batches.last() {
            batch.setup(gl);
            gl.draw_elements_with_i32(
                GL::TRIANGLES,
                batch.indices.len() as i32,
                GL::UNSIGNED_INT,
                0,
            );
        }
    }

    /// Draw all batched geometry.
    pub fn end_draw(&mut self) {
        let gl = &self.gl;
        gl.uniform_matrix4fv_with_f32_array(
            self.u_view_matrix.as_ref(),
            false,
            self.camera.transform.matrix_slice(),
        );
        self.program.bind(gl);
        for batch in self.batches.iter() {
            batch.setup(gl);
            gl.draw_elements_with_i32(
                GL::TRIANGLES,
                batch.indices.len() as i32,
                GL::UNSIGNED_INT,
                0,
            );
        }
    }

    /// Clear the screen with a given Color.
    pub fn clear(&mut self, color: [f32; 4]) {
        let gl = &self.gl;
        gl.clear_color(color[0], color[1], color[2], color[3]);
        gl.clear(GL::COLOR_BUFFER_BIT);
    }

    /// Add a [`Component`] to the [`Renderer`].
    pub fn add_component(&mut self, name: &'static str, component: Box<dyn Component>) {
        self.components.insert(name, component);
    }

    /// (Re)Initialize the [`Components`](Component) of the [`Renderer`].
    pub fn init_components(&mut self) {
        for component in self.components.values_mut() {
            component.init()
        }
    }

    /// Update the [`Components`](Component) of the [`Renderer`].
    pub fn update_components(&mut self, delta_time: f32) {
        for component in self.components.values_mut() {
            component.update(delta_time)
        }
    }

    /// Get a [`Components`](Component) using a key, and ty to cast it to a given type.
    pub fn get_component<T: 'static + Component>(&self, key: &'static str) -> Result<&T, String> {
        self.components
            .get(key)
            .unwrap()
            .as_any()
            .downcast_ref::<T>()
            .ok_or_else(|| String::from("Could not cast to type."))
    }

    /// Get a mutable [`Components`](Component) using a key, and ty to cast it to a given type.
    pub fn get_mut_component<T: 'static + Component>(
        &mut self,
        key: &'static str,
    ) -> Result<&mut T, String> {
        self.components
            .get_mut(key)
            .unwrap()
            .as_mut_any()
            .downcast_mut::<T>()
            .ok_or_else(|| String::from("Could not cast to type."))
    }

    /// Draw the [`Components`](Component) of the [`Renderer`].
    pub fn draw_components(&mut self) {
        let gl = &self.gl;
        let mut layers: Vec<Vec<Quad>> = self
            .components
            .values()
            .filter_map(|component| component.get_quads())
            .collect();
        for layer in layers.iter_mut() {
            let mut mesh = Mesh::new(
                gl,
                Vec::with_capacity(layer.len() * 4),
                Vec::with_capacity(layer.len() * 6),
            );
            for (id, quad) in layer.iter_mut().enumerate() {
                let last: u32 = id as u32 * 4;
                mesh.vertices.append(&mut quad.get_vertices());
                let mut indices = vec![last, last + 2, last + 1, last, last + 3, last + 2];
                mesh.indices.append(&mut indices);
            }
            mesh.setup(gl);
            gl.draw_elements_with_i32(
                GL::TRIANGLES,
                mesh.indices.len() as i32,
                GL::UNSIGNED_INT,
                0,
            );
        }
    }
}