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