rust/lib-hedgewars-engine/src/render/gear.rs
author unC0Rr
Tue, 14 Feb 2023 08:52:20 +0100
branchtransitional_engine
changeset 15954 9502611bffc1
parent 15918 8a6a2d931bae
permissions -rw-r--r--
Some bug fixes, build fixes and code formatting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
     1
use crate::render::{
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
     2
    atlas::{AtlasCollection, SpriteIndex},
15781
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)]
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
    63
#[repr(u32)]
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
    64
pub enum SpriteId {
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    65
    Mine = 0,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    66
    Grenade,
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
    67
    Cheese,
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
    68
    Cleaver,
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    69
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    70
    MaxSprite,
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    71
}
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    72
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    73
const SPRITE_LOAD_LIST: &[(SpriteId, &str)] = &[
15954
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15918
diff changeset
    74
    (SpriteId::Mine, "Graphics/MineOn.png"),
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15918
diff changeset
    75
    (SpriteId::Grenade, "Graphics/Bomb.png"),
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15918
diff changeset
    76
    (SpriteId::Cheese, "Graphics/cheese.png"),
9502611bffc1 Some bug fixes, build fixes and code formatting
unC0Rr
parents: 15918
diff changeset
    77
    (SpriteId::Cleaver, "Graphics/cleaver.png"),
15781
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
const MAX_SPRITES: usize = SpriteId::MaxSprite as usize + 1;
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
    81
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
    82
type SpriteTexCoords = (u32, [[f32; 2]; 4]);
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
    83
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    84
pub struct GearEntry {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    85
    position: [f32; 2],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    86
    size: Size,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    87
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    88
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    89
impl GearEntry {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    90
    pub fn new(x: f32, y: f32, size: Size) -> Self {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    91
        Self {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    92
            position: [x, y],
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    93
            size,
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
}
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
    97
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
    98
pub struct GearRenderer {
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
    99
    atlas: AtlasCollection,
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   100
    texture: Texture2D,
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   101
    allocation: Box<[SpriteTexCoords; MAX_SPRITES]>,
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   102
    shader: Shader,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   103
    layout: InputLayout,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   104
    vertex_buffer: Buffer,
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   105
}
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   106
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   107
struct SpriteData {
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   108
    size: Size,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   109
    filename: PathBuf,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   110
}
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   111
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   112
const ATLAS_SIZE: Size = Size::square(2048);
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   113
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   114
impl GearRenderer {
15918
8a6a2d931bae Implement data path
unc0rr
parents: 15788
diff changeset
   115
    pub fn new(data_path: &Path) -> Self {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   116
        let mut atlas = AtlasCollection::new(ATLAS_SIZE);
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   117
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   118
        let texture = Texture2D::new(
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   119
            ATLAS_SIZE,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   120
            TextureInternalFormat::Rgba8,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   121
            TextureFilter::Linear,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   122
        );
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   123
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   124
        let mut allocation = Box::new([Default::default(); MAX_SPRITES]);
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   125
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   126
        for (sprite, file) in SPRITE_LOAD_LIST {
15918
8a6a2d931bae Implement data path
unc0rr
parents: 15788
diff changeset
   127
            let path = data_path.join(Path::new(file));
8a6a2d931bae Implement data path
unc0rr
parents: 15788
diff changeset
   128
            let size = load_sprite_size(path.as_path()).expect(&format!("Unable to open {}", file));
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   129
            let index = atlas
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   130
                .insert_sprite(size)
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   131
                .expect(&format!("Could not store sprite {:?}", sprite));
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   132
            let (texture_index, rect) = atlas.get_rect(index).unwrap();
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   133
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   134
            let mut pixels = vec![255u8; size.area() * 4].into_boxed_slice();
15918
8a6a2d931bae Implement data path
unc0rr
parents: 15788
diff changeset
   135
            load_sprite_pixels(path.as_path(), &mut pixels).expect("Unable to load Graphics");
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   136
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   137
            texture.update(
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   138
                rect,
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   139
                &pixels,
15782
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   140
                None,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   141
                TextureFormat::Rgba,
ff1432e873bd safetize texture interface
alfadur
parents: 15781
diff changeset
   142
                TextureDataType::UnsignedByte,
15781
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   143
            );
c929e25a7da2 allow finding sprites
alfadur
parents: 15312
diff changeset
   144
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   145
            let mut tex_coords = [
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   146
                [rect.left() as f32, rect.bottom() as f32 + 1.0],
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   147
                [rect.right() as f32 + 1.0, rect.bottom() as f32 + 1.0],
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   148
                [rect.left() as f32, rect.top() as f32],
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   149
                [rect.right() as f32 + 1.0, rect.top() as f32],
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   150
            ]; //.map(|n| n as f32 / ATLAS_SIZE as f32);
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   151
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   152
            for coords in &mut tex_coords {
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   153
                coords[0] /= ATLAS_SIZE.width as f32;
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   154
                coords[1] /= ATLAS_SIZE.height as f32;
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   155
            }
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   156
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   157
            allocation[*sprite as usize] = (texture_index, tex_coords);
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   158
        }
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   159
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   160
        let shader = Shader::new(
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   161
            VERTEX_SHADER,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   162
            Some(PIXEL_SHADER),
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   163
            &[VariableBinding::Sampler("texture", 0)],
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   164
        )
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   165
        .unwrap();
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   166
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   167
        let layout = InputLayout::new(vec![
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   168
            InputElement {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   169
                shader_slot: 0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   170
                buffer_slot: 0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   171
                format: InputFormat::Float(gl::FLOAT, false),
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   172
                components: 2,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   173
                stride: size_of::<Vertex>() as u32,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   174
                offset: 0,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   175
            },
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   176
            InputElement {
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   177
                shader_slot: 1,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   178
                buffer_slot: 0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   179
                format: InputFormat::Float(gl::FLOAT, false),
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   180
                components: 2,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   181
                stride: size_of::<Vertex>() as u32,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   182
                offset: size_of::<[f32; 2]>() as u32,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   183
            },
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   184
        ]);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   185
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   186
        let vertex_buffer = Buffer::empty(BufferType::Array, BufferUsage::DynamicDraw);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   187
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   188
        Self {
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   189
            atlas,
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   190
            texture,
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   191
            allocation,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   192
            shader,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   193
            layout,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   194
            vertex_buffer,
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   195
        }
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   196
    }
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   197
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   198
    pub fn render(&mut self, camera: &Camera, entries: &[GearEntry]) {
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   199
        let mut data = Vec::with_capacity(entries.len() * 6);
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   200
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   201
        for (index, entry) in entries.iter().enumerate() {
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   202
            let sprite_id = match index & 0b11 {
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   203
                0 => SpriteId::Mine,
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   204
                1 => SpriteId::Grenade,
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   205
                2 => SpriteId::Cheese,
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   206
                _ => SpriteId::Cleaver,
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   207
            };
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   208
            let sprite_coords = &self.allocation[sprite_id as usize].1;
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   209
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   210
            let v = [
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
                    ],
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   216
                    tex_coords: sprite_coords[0],
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   217
                },
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   218
                Vertex {
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   219
                    position: [
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   220
                        entry.position[0] + entry.size.width as f32 / 2.0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   221
                        entry.position[1] + entry.size.height as f32 / 2.0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   222
                    ],
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   223
                    tex_coords: sprite_coords[1],
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   224
                },
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   225
                Vertex {
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   226
                    position: [
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   227
                        entry.position[0] - entry.size.width as f32 / 2.0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   228
                        entry.position[1] - entry.size.height as f32 / 2.0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   229
                    ],
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   230
                    tex_coords: sprite_coords[2],
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   231
                },
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   232
                Vertex {
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   233
                    position: [
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   234
                        entry.position[0] + entry.size.width as f32 / 2.0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   235
                        entry.position[1] - entry.size.height as f32 / 2.0,
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   236
                    ],
15787
713f89f6c6ab get texcoords from atlas
alfadur
parents: 15786
diff changeset
   237
                    tex_coords: sprite_coords[3],
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   238
                },
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   239
            ];
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   240
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   241
            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
   242
        }
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   243
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   244
        let projection = camera.projection();
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   245
        self.shader.bind();
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   246
        self.shader.set_matrix("projection", projection.as_ptr());
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   247
        self.shader.bind_texture_2d(0, &self.texture);
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   248
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   249
        self.vertex_buffer.write_typed(&data);
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   250
        let _buffer_bind = self.layout.bind(&[(0, &self.vertex_buffer)], None);
15786
b10bbfb2b354 add texturing to gear renderer
alfadur
parents: 15785
diff changeset
   251
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   252
        let _state = PipelineState::new().with_blend();
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   253
        unsafe {
15785
f6af9d05b03c fix triangle count
alfadur
parents: 15784
diff changeset
   254
            gl::DrawArrays(gl::TRIANGLES, 0, entries.len() as i32 * 6);
15784
84c07aa94b61 start drawing gears
alfadur
parents: 15782
diff changeset
   255
        }
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   256
    }
15141
febccab419b1 Apply dos2unix to rust sources
unc0rr
parents: 14750
diff changeset
   257
}
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   258
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   259
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
   260
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   261
    let (info, mut reader) = decoder.read_info()?;
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   262
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   263
    let size = Size::new(info.width as usize, info.height as usize);
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   264
    reader.next_frame(buffer)?;
15311
fcf0fb0cddbf transpose atlas sprites where necessary
alfadur
parents: 15309
diff changeset
   265
    Ok(size)
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   266
}
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   267
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   268
fn load_sprite_size(path: &Path) -> io::Result<Size> {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   269
    let decoder = Decoder::new(BufReader::new(File::open(path)?));
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   270
    let (info, mut reader) = decoder.read_info()?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   271
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   272
    let size = Size::new(info.width as usize, info.height as usize);
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   273
    Ok(size)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   274
}
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   275
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   276
fn load_sprites(path: &Path) -> io::Result<Vec<SpriteData>> {
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   277
    let mut result = vec![];
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   278
    for file in read_dir(path)? {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   279
        let file = file?;
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   280
        if let Some(extension) = file.path().extension() {
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   281
            if extension == "png" {
15307
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   282
                let path = file.path();
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   283
                let sprite = load_sprite_size(&path)?;
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   284
                result.push(SpriteData {
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   285
                    size: sprite,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   286
                    filename: path,
16bd389fc735 ship the atlas to the gpu
alfadur
parents: 15211
diff changeset
   287
                });
15211
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   288
            }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   289
        }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   290
    }
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   291
    Ok(result)
e2adb40c7988 fill the atlas with sprites
alfadur
parents: 15141
diff changeset
   292
}