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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
pub mod animation;
pub mod color;
pub(crate) mod opengl;
pub mod shader;
pub mod texture;
pub mod ui;
pub use self::animation::Animation;
pub use self::color::Color;
pub use self::shader::Shader;
pub use self::texture::Texture;
pub use self::ui::NineSlice;
use glm::{self, Mat3, Mat4, Vec2, Vec3};
use graphics::opengl::{BufferUsage, GLDevice, GLFramebuffer, GLIndexBuffer, GLVertexBuffer};
use Context;
const VERTEX_STRIDE: usize = 8;
const VERTEX_CAPACITY: usize = 4096;
const INDEX_STRIDE: usize = 6;
const INDEX_CAPACITY: usize = VERTEX_CAPACITY / 4 * INDEX_STRIDE;
const INDEX_ARRAY: [u32; INDEX_STRIDE] = [0, 1, 2, 2, 3, 0];
const DEFAULT_VERTEX_SHADER: &str = include_str!("../resources/shader.vert");
const DEFAULT_FRAGMENT_SHADER: &str = include_str!("../resources/shader.frag");
pub(crate) struct GraphicsContext {
vertex_buffer: GLVertexBuffer,
index_buffer: GLIndexBuffer,
framebuffer: GLFramebuffer,
framebuffer_texture: Texture,
texture: Option<Texture>,
shader: Option<Shader>,
default_shader: Shader,
internal_projection: Mat4,
window_projection: Mat4,
vertex_data: Vec<f32>,
vertex_capacity: usize,
vertex_count: usize,
element_count: usize,
internal_width: i32,
internal_height: i32,
window_width: i32,
window_height: i32,
letterbox: Rectangle,
}
impl GraphicsContext {
pub(crate) fn new(
device: &mut GLDevice,
internal_width: i32,
internal_height: i32,
window_width: i32,
window_height: i32,
) -> GraphicsContext {
assert!(
VERTEX_CAPACITY <= 32767,
"Can't have more than 32767 vertices to a single buffer"
);
let framebuffer = device.new_framebuffer();
let framebuffer_texture =
Texture::from_handle(device.new_texture(internal_width, internal_height));
device.attach_texture_to_framebuffer(&framebuffer, &framebuffer_texture.handle, false);
device.set_viewport(0, 0, internal_width, internal_height);
let indices: Vec<u32> = INDEX_ARRAY
.iter()
.cycle()
.take(INDEX_CAPACITY)
.enumerate()
.map(|(i, vertex)| vertex + i as u32 / INDEX_STRIDE as u32 * 4)
.collect();
let vertex_buffer = device.new_vertex_buffer(
VERTEX_CAPACITY * VERTEX_STRIDE,
VERTEX_STRIDE,
BufferUsage::DynamicDraw,
);
device.set_vertex_buffer_attribute(&vertex_buffer, 0, 4, 0);
device.set_vertex_buffer_attribute(&vertex_buffer, 1, 3, 4);
let index_buffer = device.new_index_buffer(INDEX_CAPACITY, BufferUsage::StaticDraw);
device.set_index_buffer_data(&index_buffer, &indices, 0);
let default_shader = Shader::from_handle(
device.compile_program(DEFAULT_VERTEX_SHADER, DEFAULT_FRAGMENT_SHADER),
);
GraphicsContext {
vertex_buffer,
index_buffer,
framebuffer,
framebuffer_texture,
texture: None,
shader: None,
default_shader,
internal_projection: ortho(
0.0,
internal_width as f32,
internal_height as f32,
0.0,
-1.0,
1.0,
),
window_projection: ortho(
0.0,
window_width as f32,
window_height as f32,
0.0,
-1.0,
1.0,
),
vertex_data: Vec::with_capacity(VERTEX_CAPACITY * VERTEX_STRIDE),
vertex_capacity: VERTEX_CAPACITY,
vertex_count: 0,
element_count: 0,
internal_width,
internal_height,
window_width,
window_height,
letterbox: letterbox(internal_width, internal_height, window_width, window_height),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Rectangle {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rectangle {
pub fn new(x: f32, y: f32, width: f32, height: f32) -> Rectangle {
Rectangle {
x,
y,
width,
height,
}
}
pub fn row(x: f32, y: f32, width: f32, height: f32) -> impl Iterator<Item = Rectangle> {
RectangleRow {
next_rect: Rectangle::new(x, y, width, height),
}
}
pub fn column(x: f32, y: f32, width: f32, height: f32) -> impl Iterator<Item = Rectangle> {
RectangleColumn {
next_rect: Rectangle::new(x, y, width, height),
}
}
}
struct RectangleRow {
next_rect: Rectangle,
}
impl Iterator for RectangleRow {
type Item = Rectangle;
fn next(&mut self) -> Option<Rectangle> {
let current_rect = self.next_rect;
self.next_rect.x += self.next_rect.width;
Some(current_rect)
}
}
struct RectangleColumn {
next_rect: Rectangle,
}
impl Iterator for RectangleColumn {
type Item = Rectangle;
fn next(&mut self) -> Option<Rectangle> {
let current_rect = self.next_rect;
self.next_rect.y += self.next_rect.height;
Some(current_rect)
}
}
#[derive(Clone)]
pub struct DrawParams {
pub(crate) position: Vec2,
pub(crate) scale: Vec2,
pub(crate) origin: Vec2,
pub(crate) rotation: f32,
pub(crate) color: Color,
pub(crate) clip: Option<Rectangle>,
}
impl DrawParams {
pub fn new() -> DrawParams {
DrawParams::default()
}
pub fn position(mut self, position: Vec2) -> DrawParams {
self.position = position;
self
}
pub fn scale(mut self, scale: Vec2) -> DrawParams {
self.scale = scale;
self
}
pub fn origin(mut self, origin: Vec2) -> DrawParams {
self.origin = origin;
self
}
pub fn rotation(mut self, rotation: f32) -> DrawParams {
self.rotation = rotation;
self
}
pub fn color(mut self, color: Color) -> DrawParams {
self.color = color;
self
}
pub fn clip(mut self, clip: Rectangle) -> DrawParams {
self.clip = Some(clip);
self
}
pub fn build_matrix(&self) -> Mat3 {
glm::translation2d(&self.position)
* glm::rotation2d(self.rotation)
* glm::scaling2d(&self.scale)
* glm::translation2d(&-self.origin)
}
}
impl Default for DrawParams {
fn default() -> DrawParams {
DrawParams {
position: Vec2::new(0.0, 0.0),
scale: Vec2::new(1.0, 1.0),
origin: Vec2::new(0.0, 0.0),
rotation: 0.0,
color: color::WHITE,
clip: None,
}
}
}
impl From<Vec2> for DrawParams {
fn from(position: Vec2) -> DrawParams {
DrawParams {
position,
..DrawParams::default()
}
}
}
pub trait Drawable {
fn draw<T: Into<DrawParams>>(&self, ctx: &mut Context, params: T);
}
pub fn get_width(ctx: &Context) -> i32 {
ctx.graphics.internal_width
}
pub fn get_height(ctx: &Context) -> i32 {
ctx.graphics.internal_height
}
pub fn get_window_width(ctx: &Context) -> i32 {
ctx.graphics.window_width
}
pub fn get_window_height(ctx: &Context) -> i32 {
ctx.graphics.window_height
}
pub(crate) fn get_letterbox(ctx: &Context) -> Rectangle {
ctx.graphics.letterbox
}
pub fn clear(ctx: &mut Context, color: Color) {
ctx.gl.clear(color.r, color.g, color.b, color.a);
}
fn push_vertex(ctx: &mut Context, x: f32, y: f32, u: f32, v: f32, params: &DrawParams) {
assert!(
ctx.graphics.vertex_count < ctx.graphics.vertex_capacity,
"Renderer is full"
);
let pos = params.build_matrix() * Vec3::new(x, y, 1.0);
ctx.graphics.vertex_data.push(pos.x);
ctx.graphics.vertex_data.push(pos.y);
ctx.graphics.vertex_data.push(u);
ctx.graphics.vertex_data.push(v);
ctx.graphics.vertex_data.push(params.color.r);
ctx.graphics.vertex_data.push(params.color.g);
ctx.graphics.vertex_data.push(params.color.b);
ctx.graphics.vertex_data.push(params.color.a);
ctx.graphics.vertex_count += 1;
}
pub(crate) fn push_quad(
ctx: &mut Context,
mut x1: f32,
mut y1: f32,
mut x2: f32,
mut y2: f32,
mut u1: f32,
mut v1: f32,
mut u2: f32,
mut v2: f32,
params: &DrawParams,
) {
if params.scale.x < 0.0 {
std::mem::swap(&mut x1, &mut x2);
std::mem::swap(&mut u1, &mut u2);
}
if params.scale.y < 0.0 {
std::mem::swap(&mut y1, &mut y2);
std::mem::swap(&mut v1, &mut v2);
}
push_vertex(ctx, x1, y1, u1, v1, params);
push_vertex(ctx, x1, y2, u1, v2, params);
push_vertex(ctx, x2, y2, u2, v2, params);
push_vertex(ctx, x2, y1, u2, v1, params);
ctx.graphics.element_count += INDEX_STRIDE;
}
pub fn draw<D: Drawable, P: Into<DrawParams>>(ctx: &mut Context, drawable: &D, params: P) {
drawable.draw(ctx, params);
}
pub fn set_texture(ctx: &mut Context, texture: &Texture) {
match ctx.graphics.texture {
Some(ref inner) if inner == texture => {}
None => {
ctx.graphics.texture = Some(texture.clone());
}
_ => {
flush(ctx);
ctx.graphics.texture = Some(texture.clone());
}
}
}
pub fn flush(ctx: &mut Context) {
if !ctx.graphics.vertex_data.is_empty() && ctx.graphics.texture.is_some() {
let texture = ctx.graphics.texture.as_ref().unwrap();
let shader = ctx
.graphics
.shader
.as_ref()
.unwrap_or(&ctx.graphics.default_shader);
ctx.gl.set_uniform(
&shader.handle,
"projection",
&ctx.graphics.internal_projection,
);
ctx.gl
.set_vertex_buffer_data(&ctx.graphics.vertex_buffer, &ctx.graphics.vertex_data, 0);
ctx.gl.draw(
&ctx.graphics.vertex_buffer,
&ctx.graphics.index_buffer,
&shader.handle,
&texture.handle,
ctx.graphics.element_count, );
ctx.graphics.vertex_data.clear();
ctx.graphics.vertex_count = 0;
ctx.graphics.element_count = 0;
}
}
pub fn present(ctx: &mut Context) {
flush(ctx);
ctx.gl.bind_default_framebuffer();
ctx.gl
.set_viewport(0, 0, ctx.graphics.window_width, ctx.graphics.window_height);
clear(ctx, color::BLACK);
let letterbox = ctx.graphics.letterbox;
push_quad(
ctx,
letterbox.x,
letterbox.y,
letterbox.x + letterbox.width,
letterbox.y + letterbox.height,
0.0,
1.0,
1.0,
0.0,
&DrawParams::new(),
);
ctx.gl.set_uniform(
&ctx.graphics.default_shader.handle,
"projection",
&ctx.graphics.window_projection,
);
ctx.gl
.set_vertex_buffer_data(&ctx.graphics.vertex_buffer, &ctx.graphics.vertex_data, 0);
ctx.gl.draw(
&ctx.graphics.vertex_buffer,
&ctx.graphics.index_buffer,
&ctx.graphics.default_shader.handle,
&ctx.graphics.framebuffer_texture.handle,
INDEX_STRIDE,
);
ctx.graphics.vertex_data.clear();
ctx.graphics.vertex_count = 0;
ctx.graphics.element_count = 0;
ctx.window.gl_swap_window();
ctx.gl.bind_framebuffer(&ctx.graphics.framebuffer);
ctx.gl.set_viewport(
0,
0,
ctx.graphics.internal_width,
ctx.graphics.internal_height,
);
}
pub(crate) fn set_window_size(ctx: &mut Context, width: i32, height: i32) {
ctx.graphics.window_width = width;
ctx.graphics.window_height = height;
ctx.graphics.window_projection = ortho(0.0, width as f32, height as f32, 0.0, -1.0, 1.0);
ctx.graphics.letterbox = letterbox(
ctx.graphics.internal_width,
ctx.graphics.internal_height,
width,
height,
);
}
fn letterbox(
internal_width: i32,
internal_height: i32,
window_width: i32,
window_height: i32,
) -> Rectangle {
let scale_factor = if window_width <= window_height {
window_width / internal_width
} else {
window_height / internal_height
};
let letterbox_width = internal_width * scale_factor;
let letterbox_height = internal_height * scale_factor;
let letterbox_x = (window_width - letterbox_width) / 2;
let letterbox_y = (window_height - letterbox_height) / 2;
Rectangle::new(
letterbox_x as f32,
letterbox_y as f32,
letterbox_width as f32,
letterbox_height as f32,
)
}
pub(crate) fn ortho(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 {
let c0r0 = 2.0 / (right - left);
let c0r1 = 0.0;
let c0r2 = 0.0;
let c0r3 = 0.0;
let c1r0 = 0.0;
let c1r1 = 2.0 / (top - bottom);
let c1r2 = 0.0;
let c1r3 = 0.0;
let c2r0 = 0.0;
let c2r1 = 0.0;
let c2r2 = -2.0 / (far - near);
let c2r3 = 0.0;
let c3r0 = -(right + left) / (right - left);
let c3r1 = -(top + bottom) / (top - bottom);
let c3r2 = -(far + near) / (far - near);
let c3r3 = 1.0;
Mat4::from([
[c0r0, c0r1, c0r2, c0r3],
[c1r0, c1r1, c1r2, c1r3],
[c2r0, c2r1, c2r2, c2r3],
[c3r0, c3r1, c3r2, c3r3],
])
}