gfx_app 0.9.0

GFX example application framework
Documentation
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
// Copyright 2015 The Gfx-rs Developers.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

extern crate cgmath;
extern crate genmesh;
#[macro_use]
extern crate gfx;
extern crate gfx_app;
extern crate image;
extern crate noise;
extern crate rand;
extern crate winit;

pub use gfx_app::{ColorFormat, DepthFormat};

use cgmath::{Deg, Matrix4, Point3, SquareMatrix, Vector3};
use genmesh::{Vertices, Triangulate};
use genmesh::generators::{Plane, SharedVertex, IndexedPolygon};
use gfx::traits::FactoryExt;
use std::io::Cursor;

// this is a value based on a max buffer size (and hence tilemap size) of 64x64
// I imagine you would have a max buffer length, with multiple TileMap instances
// of varying sizes based on current screen resolution
pub const TILEMAP_BUF_LENGTH: usize = 4096;

// texture loading boilerplate
pub fn load_texture<R, F>(factory: &mut F, data: &[u8])
                    -> Result<gfx::handle::ShaderResourceView<R, [f32; 4]>, String>
        where R: gfx::Resources, F: gfx::Factory<R>
{
    use gfx::format::Rgba8;
    use gfx::texture as t;
    let img = image::load(Cursor::new(data), image::PNG).unwrap().to_rgba();
    let (width, height) = img.dimensions();
    let kind = t::Kind::D2(width as t::Size, height as t::Size, t::AaMode::Single);
    let (_, view) = factory.create_texture_immutable_u8::<Rgba8>(kind, t::Mipmap::Provided, &[&img]).unwrap();
    Ok(view)
}

// Actual tilemap data that makes up the elements of the UBO.
// NOTE: It may be a bug, but it appears that
// [f32;2] won't work as UBO data. Possibly an issue with
// binding generation
gfx_defines!{
    constant TileMapData {
        data: [f32; 4] = "data",
    }

    constant ProjectionStuff {
        model: [[f32; 4]; 4] = "u_Model",
        view: [[f32; 4]; 4] = "u_View",
        proj: [[f32; 4]; 4] = "u_Proj",
    }

    constant TilemapStuff {
        world_size: [f32; 4] = "u_WorldSize",
        tilesheet_size: [f32; 4] = "u_TilesheetSize",
        offsets: [f32; 2] = "u_TileOffsets",
        pad1: [f32; 2] = "u_Pad1",
        pad2: [f32; 4] = "u_Pad2",
    }

    vertex VertexData {
        pos: [f32; 3] = "a_Pos",
        buf_pos: [f32; 2] = "a_BufPos",
    }

    pipeline pipe {
        vbuf: gfx::VertexBuffer<VertexData> = (),
        projection_cb: gfx::ConstantBuffer<ProjectionStuff> = "b_VsLocals",
        // tilemap stuff
        tilemap: gfx::ConstantBuffer<TileMapData> = "b_TileMap",
        tilemap_cb: gfx::ConstantBuffer<TilemapStuff> = "b_PsLocals",
        tilesheet: gfx::TextureSampler<[f32; 4]> = "t_TileSheet",
        // output
        out_color: gfx::RenderTarget<ColorFormat> = "Target0",
        out_depth: gfx::DepthTarget<DepthFormat> =
            gfx::preset::depth::LESS_EQUAL_WRITE,
    }
}

impl TileMapData {
    pub fn new_empty() -> TileMapData {
        TileMapData { data: [0.0, 0.0, 0.0, 0.0] }
    }
    pub fn new(data: [f32; 4]) -> TileMapData {
        TileMapData { data: data }
    }
}

// Abstracts the plane mesh and uniform data
// Also holds a Vec<TileMapData> as a working data
// set for consumers
pub struct TileMapPlane<R> where R: gfx::Resources {
    pub params: pipe::Data<R>,
    pub slice: gfx::Slice<R>,
    proj_stuff: ProjectionStuff,
    proj_dirty: bool,
    tm_stuff: TilemapStuff,
    tm_dirty: bool,
    pub data: Vec<TileMapData>,
}

impl<R> TileMapPlane<R> where R: gfx::Resources {
    pub fn new<F>(factory: &mut F, width: usize, height: usize, tile_size: usize,
                  targets: gfx_app::WindowTargets<R>)
               -> TileMapPlane<R> where F: gfx::Factory<R> {
        // charmap info
        let half_width = (tile_size * width) / 2;
        let half_height = (tile_size * height) / 2;
        let total_size = width*height;

        // tilesheet info
        let tilesheet_bytes = &include_bytes!("scifitiles-sheet_0.png")[..];
        let tilesheet_width = 14usize;
        let tilesheet_height = 9usize;
        let tilesheet_tilesize = 32usize;

        let tilesheet_total_width = tilesheet_width * tilesheet_tilesize;
        let tilesheet_total_height = tilesheet_height * tilesheet_tilesize;
        // set up vertex data
        let plane = Plane::subdivide(width, width);

        // law out the vertices of the plane slice based on the configured tile size information,
        // setting the a_BufPos vertex data for the vertex shader (that ultimate gets passed through
        // to the frag shader as a varying, used to determine the "current tile" and the frag's offset,
        // which is used to calculate the displayed frag color)
        let vertex_data: Vec<VertexData> = plane.shared_vertex_iter()
            .map(|genmesh::Vertex { pos, .. }| {
                let vertex_x = half_width as f32 * pos[0];
                let vertex_y = half_height as f32 * pos[1];

                let u_pos = (1.0 + pos[0]) / 2.0;
                let v_pos = (1.0 + pos[1]) / 2.0;
                let tilemap_x = (u_pos * width as f32).floor();
                let tilemap_y = (v_pos * height as f32).floor();

                VertexData {
                    pos: [vertex_x, vertex_y, 0.0],
                    buf_pos: [tilemap_x as f32, tilemap_y as f32]
                }
            })
            .collect();

        let index_data: Vec<u32> = plane.indexed_polygon_iter()
            .triangulate()
            .vertices()
            .map(|i| i as u32)
            .collect();

        let (vbuf, slice) = factory.create_vertex_buffer_with_slice(&vertex_data, &index_data[..]);

        let tile_texture = load_texture(factory, tilesheet_bytes).unwrap();

        let params = pipe::Data {
            vbuf: vbuf,
            projection_cb: factory.create_constant_buffer(1),
            tilemap: factory.create_constant_buffer(TILEMAP_BUF_LENGTH),
            tilemap_cb: factory.create_constant_buffer(1),
            tilesheet: (tile_texture, factory.create_sampler_linear()),
            out_color: targets.color,
            out_depth: targets.depth,
        };

        let mut charmap_data = Vec::with_capacity(total_size);
        for _ in 0..total_size {
            charmap_data.push(TileMapData::new_empty());
        }

        let view = Matrix4::look_at(
            Point3::new(0.0, 0.0, 800.0),
            Point3::new(0.0, 0.0, 0.0),
            Vector3::unit_y(),
        );

        TileMapPlane {
            slice: slice,
            params: params,
            proj_stuff: ProjectionStuff {
                model: Matrix4::identity().into(),
                view: view.into(),
                proj: cgmath::perspective(Deg(60.0f32), targets.aspect_ratio, 0.1, 4000.0).into(),
            },
            proj_dirty: true,
            tm_stuff: TilemapStuff {
                world_size: [width as f32, height as f32, tile_size as f32, 0.0],
                tilesheet_size: [tilesheet_width as f32, tilesheet_height as f32, tilesheet_total_width as f32, tilesheet_total_height as f32],
                offsets: [0.0, 0.0],
                pad1: [0.0; 2],
                pad2: [0.0; 4],
            },
            tm_dirty: true,
            data: charmap_data,
        }
    }

    fn resize(&mut self, targets: gfx_app::WindowTargets<R>) {
        self.params.out_color = targets.color;
        self.params.out_depth = targets.depth;
        self.proj_stuff.proj = cgmath::perspective(Deg(60.0f32), targets.aspect_ratio, 0.1, 4000.0).into();
        self.proj_dirty = true;
    }

    fn prepare_buffers<C>(&mut self, encoder: &mut gfx::Encoder<R, C>, update_data: bool) where C: gfx::CommandBuffer<R> {
        if update_data {
            encoder.update_buffer(&self.params.tilemap, &self.data, 0).unwrap();
        }
        if self.proj_dirty {
            encoder.update_constant_buffer(&self.params.projection_cb, &self.proj_stuff);
            self.proj_dirty = false;
        }
        if self.tm_dirty {
            encoder.update_constant_buffer(&self.params.tilemap_cb, &self.tm_stuff);
            self.tm_dirty = false;
        }
    }
    fn clear<C>(&self, encoder: &mut gfx::Encoder<R, C>) where C: gfx::CommandBuffer<R> {
        encoder.clear(&self.params.out_color,
            [16.0 / 256.0, 14.0 / 256.0, 22.0 / 256.0, 1.0]);
        encoder.clear_depth(&self.params.out_depth, 1.0);
    }
    pub fn update_view(&mut self, view: &Matrix4<f32>) {
        self.proj_stuff.view = view.clone().into();
        self.proj_dirty = true;
    }
    pub fn update_x_offset(&mut self, amt: f32) {
        self.tm_stuff.offsets[0] = amt;
        self.tm_dirty = true;
    }
    pub fn update_y_offset(&mut self, amt: f32) {
        self.tm_stuff.offsets[1] = amt;
        self.tm_dirty = true;
    }
}

#[derive(Clone)]
struct InputState {
    distance: f32,
    x_pos: f32,
    y_pos: f32,
    move_amt: f32,
    offset_amt: f32,
}

// Encapsulates the TileMapPlane and holds state for the current
// visible set of tiles. Is responsible for updating the UBO
// within the TileMapData when the visible set of tiles changes
pub struct TileMap<R> where R: gfx::Resources {
    pub tiles: Vec<TileMapData>,
    pso: gfx::PipelineState<R, pipe::Meta>,
    tilemap_plane: TileMapPlane<R>,
    tile_size: f32,
    tilemap_size: [usize; 2],
    charmap_size: [usize; 2],
    limit_coords: [usize; 2],
    focus_coords: [usize; 2],
    focus_dirty: bool,
    input: InputState,
}

impl<R: gfx::Resources> TileMap<R> {
    pub fn set_focus(&mut self, focus: [usize; 2]) {
        if focus[0] <= self.limit_coords[0] && focus[1] <= self.limit_coords[1] {
            self.focus_coords = focus;
            let mut charmap_ypos = 0;
            for ypos in self.focus_coords[1] .. self.focus_coords[1]+self.charmap_size[1] {
                let mut charmap_xpos = 0;
                for xpos in self.focus_coords[0] .. self.focus_coords[0]+self.charmap_size[0] {
                    let tile_idx = (ypos * self.tilemap_size[0]) + xpos;
                    let charmap_idx = (charmap_ypos * self.charmap_size[0]) + charmap_xpos;
                    self.tilemap_plane.data[charmap_idx] = self.tiles[tile_idx];
                    charmap_xpos += 1;
                }
                charmap_ypos += 1;
            }
            self.focus_dirty = true;
        } else {
            panic!("tried to set focus to {:?} with tilemap_size of {:?}", focus, self.tilemap_size);
        }
    }
    pub fn apply_x_offset(&mut self, offset_amt: f32) {
        let mut new_offset = self.tilemap_plane.tm_stuff.offsets[0] + offset_amt;
        let curr_focus = self.focus_coords;
        let new_x = if new_offset < 0.0 {
            // move down
            if self.focus_coords[0] == 0 {
                new_offset = 0.0;
                0
            } else {
                new_offset = self.tile_size + new_offset as f32;
                self.focus_coords[0] - 1
            }
        } else if self.focus_coords[0] == self.limit_coords[0] {
            // at top, no more offset
            new_offset = 0.0;
            self.focus_coords[0]
        } else if new_offset >= self.tile_size {
            new_offset = new_offset - self.tile_size as f32;
            self.focus_coords[0] + 1
        } else {
            // no move
            self.focus_coords[0]
        };
        if new_x != self.focus_coords[0] {
            self.set_focus([new_x, curr_focus[1]]);
        }
        self.tilemap_plane.update_x_offset(new_offset);
    }
    pub fn apply_y_offset(&mut self, offset_amt: f32) {
        let mut new_offset = self.tilemap_plane.tm_stuff.offsets[1] + offset_amt;
        let curr_focus = self.focus_coords;
        let new_y = if new_offset < 0.0 {
            // move down
            if self.focus_coords[1] == 0 {
                new_offset = 0.0;
                0
            } else {
                new_offset = self.tile_size + new_offset as f32;
                self.focus_coords[1] - 1
            }
        } else if self.focus_coords[1] == (self.tilemap_size[1] - self.charmap_size[1]) {
            // at top, no more offset
            new_offset = 0.0;
            self.focus_coords[1]
        } else if new_offset >= self.tile_size {
            new_offset = new_offset - self.tile_size as f32;
            self.focus_coords[1] + 1
        } else {
            // no move
            self.focus_coords[1]
        };
        if new_y != self.focus_coords[1] {
            self.set_focus([curr_focus[0], new_y]);
        }
        self.tilemap_plane.update_y_offset(new_offset);
    }
    fn calc_idx(&self, xpos: usize, ypos: usize) -> usize {
        (ypos * self.tilemap_size[0]) + xpos
    }
    pub fn set_tile(&mut self, xpos: usize, ypos: usize, data: [f32; 4]) {
        let idx = self.calc_idx(xpos, ypos);
        self.tiles[idx] = TileMapData::new(data);
    }
}


fn populate_tilemap<R>(tilemap: &mut TileMap<R>, tilemap_size: [usize; 2]) where R: gfx::Resources {
    // paper in with dummy data
    for ypos in 0 .. tilemap_size[1] {
        for xpos in 0 .. tilemap_size[0] {
            tilemap.set_tile(xpos, ypos, [1.0, 7.0, 0.0, 0.0]);
        }
    }
    tilemap.set_tile(1,3,[5.0, 0.0, 0.0, 0.0]);
    tilemap.set_tile(2,3,[6.0, 0.0, 0.0, 0.0]);
    tilemap.set_tile(3,3,[7.0, 0.0, 0.0, 0.0]);
    tilemap.set_tile(1,2,[5.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(2,2,[4.0, 0.0, 0.0, 0.0]);
    tilemap.set_tile(3,2,[11.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(1,1,[5.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(2,1,[6.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(3,1,[7.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(1,0,[4.0, 7.0, 0.0, 0.0]);
    tilemap.set_tile(2,0,[4.0, 7.0, 0.0, 0.0]);
    tilemap.set_tile(3,0,[4.0, 7.0, 0.0, 0.0]);
    tilemap.set_tile(4,2,[4.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(5,2,[4.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(6,2,[11.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(4,1,[4.0, 7.0, 0.0, 0.0]);
    tilemap.set_tile(5,1,[4.0, 7.0, 0.0, 0.0]);
    tilemap.set_tile(6,1,[4.0, 7.0, 0.0, 0.0]);
    tilemap.set_tile(6,3,[4.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(6,4,[4.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(6,5,[4.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(6,6,[4.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(6,7,[4.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(5,10,[5.0, 0.0, 0.0, 0.0]);
    tilemap.set_tile(7,10,[7.0, 0.0, 0.0, 0.0]);
    tilemap.set_tile(5,9,[5.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(6,9,[6.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(7,9,[7.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(5,8,[5.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(6,8,[8.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(7,8,[7.0, 2.0, 0.0, 0.0]);
    tilemap.set_tile(5,7,[2.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(7,7,[2.0, 1.0, 0.0, 0.0]);
    tilemap.set_tile(6,10,[2.0, 3.0, 0.0, 0.0]);
    tilemap.set_tile(6,11,[2.0, 2.0, 0.0, 0.0]);
}

impl<R: gfx::Resources> gfx_app::Application<R> for TileMap<R> {
    fn new<F: gfx::Factory<R>>(factory: &mut F, backend: gfx_app::shade::Backend,
           window_targets: gfx_app::WindowTargets<R>) -> Self {
        use gfx::traits::FactoryExt;

        let vs = gfx_app::shade::Source {
            glsl_150: include_bytes!("shader/tilemap_150_core.glslv"),
            glsl_es_300: include_bytes!("shader/tilemap_300_es.glslv"),
            hlsl_40:  include_bytes!("data/vertex.fx"),
            .. gfx_app::shade::Source::empty()
        };
        let ps = gfx_app::shade::Source {
            glsl_150: include_bytes!("shader/tilemap_150_core.glslf"),
            glsl_es_300: include_bytes!("shader/tilemap_300_es.glslf"),
            hlsl_40:  include_bytes!("data/pixel.fx"),
            .. gfx_app::shade::Source::empty()
        };

        // set up charmap plane and configure its tiles
        let tilemap_size = [24, 24];
        let charmap_size = [16, 16];
        let tile_size = 32;

        let mut tiles = Vec::new();
        for _ in 0 .. tilemap_size[0]*tilemap_size[1] {
            tiles.push(TileMapData::new_empty());
        }

        // TODO: should probably check that charmap is smaller than tilemap
        let mut tm = TileMap {
            tiles: tiles,
            pso: factory.create_pipeline_simple(
                vs.select(backend).unwrap(),
                ps.select(backend).unwrap(),
                pipe::new()
                ).unwrap(),
            tilemap_plane: TileMapPlane::new(factory,
                charmap_size[0], charmap_size[1], tile_size,
                window_targets),
            tile_size: tile_size as f32,
            tilemap_size: tilemap_size,
            charmap_size: charmap_size,
            limit_coords: [tilemap_size[0] - charmap_size[0], tilemap_size[1] - charmap_size[1]],
            focus_coords: [0, 0],
            focus_dirty: false,
            input: InputState {
                distance: 800.0,
                x_pos: 0.0,
                y_pos: 0.0,
                move_amt: 10.0,
                offset_amt: 1.0,
            },
        };

        populate_tilemap(&mut tm, tilemap_size);
        tm.set_focus([0, 0]);
        tm
    }

    fn render<C: gfx::CommandBuffer<R>>(&mut self, encoder: &mut gfx::Encoder<R, C>) {
        // view configuration based on current position
        let view = Matrix4::look_at(
            Point3::new(self.input.x_pos, -self.input.y_pos, self.input.distance),
            Point3::new(self.input.x_pos, -self.input.y_pos, 0.0),
            Vector3::unit_y(),
        );

        self.tilemap_plane.update_view(&view);
        self.tilemap_plane.prepare_buffers(encoder, self.focus_dirty);
        self.focus_dirty = false;

        self.tilemap_plane.clear(encoder);

        encoder.draw(&self.tilemap_plane.slice, &self.pso, &self.tilemap_plane.params);
    }

    fn on(&mut self, event: winit::WindowEvent) {
        if let winit::WindowEvent::KeyboardInput {
            input: winit::KeyboardInput {
                state: winit::ElementState::Pressed,
                virtual_keycode: Some(key),
                .. },
            .. } = event {
            use winit::VirtualKeyCode::*;
            let i = self.input.clone();

            match key {
                // zooming in/out
                Equals => self.input.distance -= i.move_amt,
                Minus | Subtract => self.input.distance += i.move_amt,
                // panning around
                Up => self.input.y_pos -= i.move_amt,
                Down => self.input.y_pos += i.move_amt,
                Left => self.input.x_pos -= i.move_amt,
                Right => self.input.x_pos += i.move_amt,
                W => self.apply_y_offset(i.offset_amt),
                S => self.apply_y_offset(-i.offset_amt),
                D => self.apply_x_offset(i.offset_amt),
                A => self.apply_x_offset(-i.offset_amt),
                _ => ()
            }
        }
    }

    fn on_resize(&mut self, window_targets: gfx_app::WindowTargets<R>) {
        self.tilemap_plane.resize(window_targets);
    }
}

pub fn main() {
    use gfx_app::Application;
    let wb = winit::WindowBuilder::new().with_title("Tilemap example");
    TileMap::launch_default(wb);
}