wasm_rgame/graphics/
mod.rs

1use std::mem;
2
3mod drawables;
4use self::drawables::{DrawRect, StringProperties, DrawActionColor};
5
6// NOTE: this must match in render.js
7const MAX_DRAW_ARRAY_SIZE : usize = 500;
8const MAX_STRING_ARRAY_SIZE : usize = 1_000;
9
10pub type Color = [u8; 4];
11
12pub struct Graphics {
13    ordering: usize,
14    draw_rects: [DrawRect; MAX_DRAW_ARRAY_SIZE],
15    draw_rects_index: usize,
16    draw_action_colors: [DrawActionColor; MAX_DRAW_ARRAY_SIZE],
17    draw_action_colors_index: usize,
18    strings: [u8; MAX_STRING_ARRAY_SIZE],
19    strings_index: usize,
20    string_properties: [StringProperties; MAX_DRAW_ARRAY_SIZE],
21    string_properties_index: usize,
22}
23
24impl Graphics {
25    /// WARNING: JS Exported Function - not intended for normal use
26    pub fn new() -> Graphics {
27        Graphics {
28            ordering: 1,
29            draw_rects: [DrawRect::EMPTY; MAX_DRAW_ARRAY_SIZE],
30            draw_rects_index: 0,
31            draw_action_colors: [DrawActionColor::EMPTY; MAX_DRAW_ARRAY_SIZE],
32            draw_action_colors_index: 0,
33            strings: [0; MAX_STRING_ARRAY_SIZE],
34            strings_index: 0,
35            string_properties: [StringProperties::EMPTY; MAX_DRAW_ARRAY_SIZE],
36            string_properties_index: 0,
37        }
38    }
39
40    /// WARNING: JS Exported Function - not intended for normal use
41    pub fn draw_rects_ptr(&self) -> *const f32 { unsafe { mem::transmute::<*const DrawRect, *const f32>(self.draw_rects.as_ptr()) } }
42
43    /// WARNING: JS Exported Function - not intended for normal use
44    pub fn draw_rects_len(&self) -> usize { self.draw_rects_index }
45
46    /// WARNING: JS Exported Function - not intended for normal use
47    pub fn draw_action_colors_ptr(&self) -> *const u8 { unsafe { mem::transmute::<*const DrawActionColor, *const u8>(self.draw_action_colors.as_ptr()) } }
48
49    /// WARNING: JS Exported Function - not intended for normal use
50    pub fn draw_action_colors_len(&self) -> usize { self.draw_action_colors_index }
51
52    /// WARNING: JS Exported Function - not intended for normal use
53    pub fn strings_ptr(&self) -> *const u8 { self.strings.as_ptr() }
54
55    /// WARNING: JS Exported Function - not intended for normal use
56    pub fn string_properties_ptr(&self) -> *const f32 { unsafe { mem::transmute::<*const StringProperties, *const f32>(self.string_properties.as_ptr()) } }
57
58    /// WARNING: JS Exported Function - not intended for normal use
59    pub fn string_properties_len(&self) -> usize { self.string_properties_index }
60
61    /// Clearing Graphics for the next frame
62    /// WARNING: JS Exported Function - not intended for normal use
63    pub fn reset(&mut self) {
64        self.ordering = 1;
65        self.draw_rects[0] = DrawRect::EMPTY;
66        self.draw_rects_index = 0;
67        self.draw_action_colors[0] = DrawActionColor::EMPTY;
68        self.draw_action_colors_index = 0;
69        self.string_properties[0] = StringProperties::EMPTY;
70        self.string_properties_index = 0;
71        self.strings_index = 0;
72    }
73
74    pub fn draw_rect(&mut self, pos_x: f32, pos_y: f32, width: f32, height: f32, color: Color) {
75        assert!(self.draw_rects_index < MAX_DRAW_ARRAY_SIZE);
76
77        self.draw_rects[self.draw_rects_index] = DrawRect {
78            ordering: self.ordering as f32,
79            pos_x,
80            pos_y,
81            width,
82            height,
83        };
84        self.draw_rects_index += 1;
85
86        self.set_color(color);
87        self.ordering += 1;
88    }
89
90    pub fn draw_string(&mut self, s: &str, pos_x: f32, pos_y: f32, font_size: f32, color: Color) {
91        for byte in s.as_bytes() {
92            self.strings[self.strings_index] = *byte;
93            self.strings_index += 1;
94        }
95        // terminate the string with 0
96        self.strings[self.strings_index] = 0;
97        self.strings_index += 1;
98
99        self.string_properties[self.string_properties_index] = StringProperties {
100            ordering: self.ordering as f32,
101            pos_x,
102            pos_y,
103            font_size,
104        };
105        self.string_properties_index += 1;
106
107        self.set_color(color);
108        self.ordering += 1;
109    }
110
111    /// Sets the color if it is different from the last draw color change
112    fn set_color(&mut self, color: Color) {
113        // if no color is set yet or the last color is not the same as this color
114        if self.draw_action_colors_index == 0 ||
115            !self.draw_action_colors[self.draw_action_colors_index - 1].same_color(color)
116        {
117            assert!(self.draw_action_colors_index < MAX_DRAW_ARRAY_SIZE);
118
119            self.draw_action_colors[self.draw_action_colors_index] = DrawActionColor {
120                ordering: self.ordering as u8,
121                color_r: color[0],
122                color_g: color[1],
123                color_b: color[2],
124                alpha: color[3],
125            };
126            self.draw_action_colors_index += 1;
127        }
128
129    }
130}