1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! Functions and types relating to textures.

use std::path::Path;
use std::rc::Rc;

use image;

use error::{Result, TetraError};
use graphics::opengl::GLTexture;
use graphics::{self, DrawParams, Drawable, Rectangle};
use Context;

/// Texture data.
///
/// This type acts as a lightweight handle to the associated graphics hardware data,
/// and so can be cloned with little overhead.
#[derive(Clone, PartialEq)]
pub struct Texture {
    pub(crate) handle: Rc<GLTexture>,
}

impl Texture {
    /// Creates a new texture from the given file.
    pub fn new<P: AsRef<Path>>(ctx: &mut Context, path: P) -> Result<Texture> {
        let image = image::open(path).map_err(TetraError::Image)?.to_rgba();
        let (width, height) = image.dimensions();

        let texture = ctx.gl.new_texture(width as i32, height as i32);
        ctx.gl
            .set_texture_data(&texture, &image, 0, 0, width as i32, height as i32);

        Ok(Texture::from_handle(texture))
    }

    pub(crate) fn from_handle(handle: GLTexture) -> Texture {
        Texture {
            handle: Rc::new(handle),
        }
    }
}

impl Drawable for Texture {
    fn draw<T: Into<DrawParams>>(&self, ctx: &mut Context, params: T) {
        graphics::set_texture(ctx, self);

        assert!(
            ctx.graphics.sprite_count < ctx.graphics.capacity,
            "Renderer is full"
        );

        let params = params.into();

        let texture_width = self.handle.width() as f32;
        let texture_height = self.handle.height() as f32;
        let clip = params
            .clip
            .unwrap_or_else(|| Rectangle::new(0.0, 0.0, texture_width, texture_height));

        // TODO: I feel like there must be a cleaner way of determining the winding order...
        // TODO: We could probably use GLM to do this with vector math, too

        let (x1, x2, u1, u2) = if params.scale.x >= 0.0 {
            (
                params.position.x - params.origin.x,
                params.position.x - params.origin.x + (clip.width * params.scale.x),
                clip.x / texture_width,
                (clip.x + clip.width) / texture_width,
            )
        } else {
            (
                params.position.x + params.origin.x + (clip.width * params.scale.x),
                params.position.x + params.origin.x,
                (clip.x + clip.width) / texture_width,
                clip.x / texture_width,
            )
        };

        let (y1, y2, v1, v2) = if params.scale.y >= 0.0 {
            (
                params.position.y - params.origin.y,
                params.position.y - params.origin.y + (clip.height * params.scale.y),
                clip.y / texture_height,
                (clip.y + clip.height) / texture_height,
            )
        } else {
            (
                params.position.y + params.origin.y + (clip.height * params.scale.y),
                params.position.y + params.origin.y,
                (clip.y + clip.height) / texture_height,
                clip.y / texture_height,
            )
        };

        graphics::push_vertex(ctx, x1, y1, u1, v1, params.color);
        graphics::push_vertex(ctx, x1, y2, u1, v2, params.color);
        graphics::push_vertex(ctx, x2, y2, u2, v2, params.color);
        graphics::push_vertex(ctx, x2, y1, u2, v1, params.color);

        ctx.graphics.sprite_count += 1;
    }
}