rust/lib-hedgewars-engine/src/render/gear.rs
author alfadur
Fri, 13 Nov 2020 20:59:13 +0300
changeset 15785 f6af9d05b03c
parent 15784 84c07aa94b61
child 15786 b10bbfb2b354
permissions -rw-r--r--
fix triangle count
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
     1
use crate::render::{
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
     2
    atlas::{AtlasCollection, SpriteIndex, SpriteLocation},
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
     3
    camera::Camera,
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
     4
    gl::{
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
     5
        Buffer, BufferType, BufferUsage, InputElement, InputFormat, InputLayout, PipelineState,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
     6
        Shader, Texture2D, TextureDataType, TextureFilter, TextureFormat, TextureInternalFormat,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
     7
    },
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
     8
};
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
     9
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    10
use integral_geometry::{Rect, Size};
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
    11
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    12
use png::{ColorType, Decoder, DecodingError};
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    13
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    14
use std::{
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    15
    collections::HashMap,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    16
    ffi::OsString,
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    17
    fs::{read_dir, File},
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    18
    io,
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    19
    io::BufReader,
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    20
    mem::size_of,
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    21
    path::{Path, PathBuf},
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    22
};
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    23
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    24
const VERTEX_SHADER: &'static str = r#"
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    25
#version 330 core
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    26
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    27
layout(location = 0) in vec2 position;
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    28
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    29
uniform mat4 projection;
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    30
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    31
void main() {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    32
	gl_Position = projection * vec4(position, 0.0, 1.0);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    33
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    34
"#;
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    35
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    36
const PIXEL_SHADER: &'static str = r#"
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    37
#version 330 core
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    38
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    39
out vec4 outColor;
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    40
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    41
void main() {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    42
	 outColor = vec4(0.0, 1.0, 0.0, 1.0);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    43
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    44
"#;
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    45
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    46
#[repr(C)]
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    47
#[derive(Copy, Clone)]
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    48
struct Vertex {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    49
    pos: [f32; 2],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    50
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    51
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    52
#[derive(PartialEq, Debug, Clone, Copy)]
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
    53
pub enum SpriteId {
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    54
    Mine = 0,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    55
    Grenade,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    56
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    57
    MaxSprite,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    58
}
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    59
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    60
const SPRITE_LOAD_LIST: &[(SpriteId, &str)] = &[
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    61
    (
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    62
        SpriteId::Mine,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    63
        "../../share/hedgewars/Data/Graphics/MineOn.png",
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    64
    ),
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    65
    (
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    66
        SpriteId::Grenade,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    67
        "../../share/hedgewars/Data/Graphics/Bomb.png",
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    68
    ),
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    69
];
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    70
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    71
const MAX_SPRITES: usize = SpriteId::MaxSprite as usize + 1;
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    72
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    73
pub struct GearEntry {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    74
    position: [f32; 2],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    75
    size: Size,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    76
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    77
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    78
impl GearEntry {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    79
    pub fn new(x: f32, y: f32, size: Size) -> Self {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    80
        Self {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    81
            position: [x, y],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    82
            size,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    83
        }
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    84
    }
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    85
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    86
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    87
pub struct GearRenderer {
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
    88
    atlas: AtlasCollection,
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    89
    allocation: Box<[SpriteLocation; MAX_SPRITES]>,
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    90
    shader: Shader,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    91
    layout: InputLayout,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    92
    vertex_buffer: Buffer,
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
    93
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
    94
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    95
struct SpriteData {
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    96
    size: Size,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    97
    filename: PathBuf,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    98
}
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
    99
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   100
const ATLAS_SIZE: Size = Size::square(2048);
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   101
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   102
impl GearRenderer {
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   103
    pub fn new() -> Self {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   104
        let mut atlas = AtlasCollection::new(ATLAS_SIZE);
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   105
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   106
        let texture = Texture2D::new(
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   107
            ATLAS_SIZE,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   108
            TextureInternalFormat::Rgba8,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   109
            TextureFilter::Linear,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   110
        );
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   111
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   112
        let mut allocation = Box::new([(0, Rect::at_origin(Size::EMPTY)); MAX_SPRITES]);
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   113
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   114
        for (sprite, file) in SPRITE_LOAD_LIST {
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   115
            let path = Path::new(file);
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   116
            let size = load_sprite_size(path).expect(&format!("Unable to open {}", file));
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   117
            let index = atlas
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   118
                .insert_sprite(size)
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   119
                .expect(&format!("Could not store sprite {:?}", sprite));
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   120
            let (texture_index, rect) = atlas.get_rect(index).unwrap();
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   121
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   122
            let mut pixels = vec![0; size.area()].into_boxed_slice();
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   123
            load_sprite_pixels(path, mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..]))
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   124
                .expect("Unable to load Graphics");
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   125
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   126
            texture.update(
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   127
                rect,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   128
                mapgen::theme::slice_u32_to_u8_mut(&mut pixels[..]),
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   129
                None,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   130
                TextureFormat::Rgba,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   131
                TextureDataType::UnsignedByte,
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   132
            );
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   133
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   134
            allocation[*sprite as usize] = (texture_index, rect);
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   135
        }
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   136
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   137
        let shader = Shader::new(VERTEX_SHADER, Some(PIXEL_SHADER), &[]).unwrap();
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   138
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   139
        let layout = InputLayout::new(vec![
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   140
            // position
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   141
            InputElement {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   142
                shader_slot: 0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   143
                buffer_slot: 0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   144
                format: InputFormat::Float(gl::FLOAT, false),
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   145
                components: 2,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   146
                stride: size_of::<Vertex>() as u32,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   147
                offset: 0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   148
            },
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   149
        ]);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   150
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   151
        let vertex_buffer = Buffer::empty(BufferType::Array, BufferUsage::DynamicDraw);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   152
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   153
        Self {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   154
            atlas,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   155
            allocation,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   156
            shader,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   157
            layout,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   158
            vertex_buffer,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   159
        }
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   160
    }
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   161
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   162
    pub fn render(&mut self, camera: &Camera, entries: &[GearEntry]) {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   163
        let projection = camera.projection();
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   164
        self.shader.bind();
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   165
        self.shader.set_matrix("projection", projection.as_ptr());
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   166
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   167
        let mut data = Vec::with_capacity(entries.len() * 12);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   168
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   169
        for entry in entries {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   170
            let vertices = [
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   171
                [
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   172
                    entry.position[0] - entry.size.width as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   173
                    entry.position[1] + entry.size.height as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   174
                ],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   175
                [
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   176
                    entry.position[0] + entry.size.width as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   177
                    entry.position[1] + entry.size.height as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   178
                ],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   179
                [
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   180
                    entry.position[0] - entry.size.width as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   181
                    entry.position[1] - entry.size.height as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   182
                ],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   183
                [
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   184
                    entry.position[0] + entry.size.width as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   185
                    entry.position[1] - entry.size.height as f32 / 2.0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   186
                ],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   187
            ];
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   188
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   189
            data.extend_from_slice(&[
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   190
                vertices[0][0],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   191
                vertices[0][1],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   192
                vertices[1][0],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   193
                vertices[1][1],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   194
                vertices[2][0],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   195
                vertices[2][1],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   196
                vertices[1][0],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   197
                vertices[1][1],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   198
                vertices[3][0],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   199
                vertices[3][1],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   200
                vertices[2][0],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   201
                vertices[2][1],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   202
            ]);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   203
        }
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   204
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   205
        self.vertex_buffer.write_typed(&data);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   206
        let _buffer_bind = self.layout.bind(&[(0, &self.vertex_buffer)], None);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   207
        let _state = PipelineState::new().with_blend();
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   208
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   209
        unsafe {
15785
f6af9d05b03c fix triangle count
alfadur
parents: 15784
diff changeset
   210
            gl::DrawArrays(gl::TRIANGLES, 0, entries.len() as i32 * 6);
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   211
        }
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   212
    }
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   213
}
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   214
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   215
fn load_sprite_pixels(path: &Path, buffer: &mut [u8]) -> io::Result<Size> {
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   216
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   217
    let (info, mut reader) = decoder.read_info()?;
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   218
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   219
    let size = Size::new(info.width as usize, info.height as usize);
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   220
    reader.next_frame(buffer)?;
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   221
    Ok(size)
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   222
}
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   223
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   224
fn load_sprite_size(path: &Path) -> io::Result<Size> {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   225
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   226
    let (info, mut reader) = decoder.read_info()?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   227
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   228
    let size = Size::new(info.width as usize, info.height as usize);
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   229
    Ok(size)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   230
}
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   231
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   232
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   233
    let mut result = vec![];
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   234
    for file in read_dir(path)? {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   235
        let file = file?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   236
        if let Some(extension) = file.path().extension() {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   237
            if extension == "png" {
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   238
                let path = file.path();
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   239
                let sprite = load_sprite_size(&path)?;
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   240
                result.push(SpriteData {
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   241
                    size: sprite,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   242
                    filename: path,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   243
                });
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   244
            }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   245
        }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   246
    }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   247
    Ok(result)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   248
}