Skip to main content

demo/
demo.rs

1extern crate egaku2d;
2use glutin::event::{Event, VirtualKeyCode, WindowEvent};
3use glutin::event_loop::ControlFlow;
4
5const COL1: [f32; 4] = [0.0, 1.0, 0.1, 0.3];
6const COL2: [f32; 4] = [0.8, 0.8, 1.0, 1.0];
7const COL3: [f32; 4] = [1.0, 0.0, 1.0, 0.4];
8const COL4: [f32; 4] = [0.5, 1.0, 0.5, 1.0];
9const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
10
11fn main() {
12    let events_loop = glutin::event_loop::EventLoop::new();
13    let mut sys = egaku2d::WindowedSystem::new([640, 480], &events_loop, "shapes example");
14    //let mut sys=egaku2d::FullScreenSystem::new(&events_loop);
15
16    //Make a bunch of textures
17    let sky = sys.texture("day_sky.png", [1, 1]).unwrap();
18    let food_tex = sys.texture("food.png", [8, 8]).unwrap();
19    let adventurer = sys.texture("adventurer.png", [7, 11]).unwrap();
20    let ascii_tex = sys.texture("ascii.png", [16, 14]).unwrap();
21    let tall_tiles_tex = sys.texture("tall_tiles.png", [2, 3]).unwrap();
22    let fat_tiles_tex = sys.texture("fat_tiles.png", [2, 3]).unwrap();
23    let leaves = sys.texture("leaves.png", [1, 1]).unwrap();
24
25    //Make a bunch of static vbos
26    let canvas = sys.canvas_mut();
27    let background = { canvas.rects().add([0.0, 640.0, 0.0, 480.0]).save(canvas) };
28    let rect_save = {
29        let mut k = canvas.rects();
30        k.add([300., 500., 300., 500.]);
31        k.save(canvas)
32    };
33    let square_save = {
34        let mut k = canvas.squares();
35        for x in (0..1000).step_by(100).map(|a| a as f32) {
36            for y in (0..1000).step_by(100).map(|a| a as f32) {
37                k.add([x, y]);
38            }
39        }
40        k.save(canvas)
41    };
42    let arrow_save = {
43        canvas
44            .arrows(5.0)
45            .add([40., 40.], [40., 200.])
46            .add([40., 40.], [200., 40.])
47            .save(canvas)
48    };
49    let line_save = {
50        canvas
51            .lines(3.0)
52            .add([400., 0.], [600., 400.])
53            .add([10., 300.], [300., 400.])
54            .save(canvas)
55    };
56    let sprite_save = {
57        let mut k = canvas.sprites();
58        for (i, x) in (032..200)
59            .step_by(32)
60            .enumerate()
61            .map(|(a, b)| (a as u8, b as f32))
62        {
63            for (j, y) in (032..200)
64                .step_by(32)
65                .enumerate()
66                .map(|(a, b)| (a as u8, b as f32))
67            {
68                k.add([x, y], food_tex.coord_to_index([i, j]), 0.0);
69            }
70        }
71        k.save(canvas)
72    };
73
74    //Draw 60 frames per second.
75    let mut timer = egaku2d::RefreshTimer::new(16);
76
77    let mut counter = 0;
78    let mut cursor = [0.0; 2];
79    events_loop.run(move |event, _, control_flow| match event {
80        Event::WindowEvent { event, .. } => match event {
81            WindowEvent::KeyboardInput { input, .. } => match input.virtual_keycode {
82                Some(VirtualKeyCode::Escape) => {
83                    *control_flow = ControlFlow::Exit;
84                }
85                _ => {}
86            },
87            WindowEvent::CursorMoved {
88                device_id: _,
89                position: p,
90                ..
91            } => {
92                cursor = [p.x as f32, p.y as f32];
93            }
94            WindowEvent::CloseRequested => {
95                *control_flow = ControlFlow::Exit;
96            }
97            WindowEvent::Resized(_dim) => {}
98            _ => {}
99        },
100
101        Event::MainEventsCleared => {
102            if timer.is_ready() {
103
104                let canvas = sys.canvas_mut();
105
106                //canvas.set_default_offset([-cursor[0],-cursor[1]]);
107
108
109                let cc = counter as f32 * 0.1;
110                let wobble = [cc.cos() * 10.0, cc.sin() * 10.0];
111
112                canvas.clear_color([0.2; 3]);
113
114                //draw static VBOs already on the gpu.
115                background
116                    .uniforms(canvas)
117                    .with_texture(&sky, 2.0, [0.0; 2])
118                    .draw();
119
120                sprite_save
121                    .uniforms(canvas, &food_tex, 32.0)
122                    .with_color(COL4)
123                    .with_offset([-wobble[0], -wobble[1]])
124                    .draw();
125
126                arrow_save.uniforms(canvas).draw();
127                line_save.uniforms(canvas).with_color(COL2).draw();
128                square_save.uniforms(canvas, 10.0).with_color(COL3).draw();
129
130                let w=[wobble[0]+300.0,wobble[1]+300.0];
131                rect_save
132                    .uniforms(canvas)
133                    .with_texture(&fat_tiles_tex, 2.0, w)
134                    .with_color(WHITE)
135                    .with_offset(wobble)
136                    .draw();
137
138                //Draw a bunch of dyanmic shapes.
139                let mut builder = canvas.circles();
140                for x in (0..1000).step_by(12).map(|a| a as f32) {
141                    for y in (0..1000).step_by(12).map(|a| a as f32) {
142                        let c = (counter as f32 + x + y) * 0.01;
143                        let x = x + c.sin() * y * 0.1;
144                        let y = y + c.cos() * x * 0.1;
145                        builder.add([x, y]);
146                    }
147                }
148                builder
149                    .send_and_uniforms(canvas, 8.0)
150                    .with_color(COL1)
151                    .draw();
152
153                let mut builder = canvas.sprites();
154                for y in (100..500).step_by(80).map(|a| a as f32) {
155                    for x in (100..500).step_by(80).map(|a| a as f32) {
156                        let c = (counter as f32 + x + y) * 0.01;
157                        let cc = ((counter as f32 + x + y) * 0.1) as u32;
158                        let x = x + c.sin() * 20.0;
159                        let y = y + c.cos() * 20.0;
160                        builder.add([x, y], (cc % 64) as u16, c);
161                    }
162                }
163                builder
164                    .send_and_uniforms(canvas, &adventurer, 100.0)
165                    .with_color(WHITE)
166                    .draw();
167
168                let mut builder = canvas.sprites();
169                add_ascii(
170                    [100., 400.],
171                    20.0,
172                    cc.cos() * 0.5 - 0.2,
173                    "testing? TESTING!",
174                    &mut builder,
175                );
176                builder.add([100., 100.], ascii_tex.coord_to_index([2, 2]), 1.0);
177                builder.send_and_uniforms(canvas, &ascii_tex, 20.0).draw();
178
179                let c = ((counter as f32 * 0.06).sin() * 100.0).abs();
180                canvas
181                    .circles()
182                    .add(cursor)
183                    .send_and_uniforms(canvas, c)
184                    .with_texture(&leaves, 1.0, [0.0; 2])
185                    .draw();
186
187                //draw a moving line
188                let c = counter as f32 * 0.07;
189                canvas
190                    .lines(10.)
191                    .add([50., 500.], [500., 50. + c.sin() * 50.])
192                    .send_and_uniforms(canvas)
193                    .with_texture(&leaves, 4.0, [0.0; 2])
194                    .draw();
195
196                //draw a rotating arrow
197                let c = counter as f32 * 0.04;
198                let center = [400., 400.];
199
200                let other = [center[0] + c.cos() * 80., center[1] + c.sin() * 80.];
201                canvas
202                    .arrows(10.0)
203                    .add(center, other)
204                    .send_and_uniforms(canvas)
205                    .with_color(COL4)
206                    .draw();
207
208                canvas
209                    .sprites()
210                    .add([500., 200.], c as u16, c)
211                    .send_and_uniforms(canvas, &fat_tiles_tex, 100.)
212                    .draw();
213                canvas
214                    .sprites()
215                    .add([500., 50.], c as u16, c)
216                    .send_and_uniforms(canvas, &tall_tiles_tex, 100.)
217                    .draw();
218
219                //display what we drew
220                sys.swap_buffers();
221
222                counter += 1;
223            }
224        }
225        _ => {}
226    });
227}
228
229fn add_ascii(
230    start: [f32; 2],
231    width: f32,
232    rotation: f32,
233    st: &str,
234    sprites: &mut egaku2d::sprite::SpriteSession,
235) {
236    let mut cc = start;
237    for (i, a) in st.chars().enumerate() {
238        let ascii = a as u8;
239        assert!(ascii >= 32);
240        sprites.add(cc, (ascii - 32) as u16, rotation + (i as f32 * 0.1));
241        cc[0] += width;
242    }
243}