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