summaryrefslogtreecommitdiff
path: root/graphics/src/assets.rs
blob: bb989a383050ddc42ce50c186523bd2cfe4662d8 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//! The `assets` crate stores all audio and image assets that need to be
//! loaded during runtime

use dungeon::Item;
use raylib::{RaylibHandle, RaylibThread, audio::RaylibAudio, texture::Texture2D};

#[expect(dead_code)]
type Sound = raylib::audio::Sound<'static>;

/// The `AudioData` container initalizes the audio subsystem
/// for raylib, leaks it (to gurentee audio is statically loaded),
/// then loads all needed audio samples
#[derive(Debug)]
pub struct AudioData {}
impl AudioData {
	pub(crate) fn load() -> crate::Result<Self> {
		// Phantom handle to the raylib audio subsystem
		// Raylib doesnt use a handle, but the rust bindings
		// have one to ensure memory safety.
		//
		// We must leak this handle after allocating it,
		// if we dont then all audio will be unloaded :(
		//
		// NOTE: would this cause issues if `AudioData::load` was
		// called multiple times?
		let _handle = Box::leak(Box::new(RaylibAudio::init_audio_device()?));

		// TODO: load audio samples

		//let example = handle.new_sound("example.ogg")?;

		Ok(Self {})
	}
}

/// The baseline size of all ingame sprites and tile textures
pub(crate) const BASE_TILE_SIZE: i32 = 32;

/// The height of the wall (offset between tile layers)
pub(crate) const WALL_HEIGHT: i32 = 13;

/// Texture indexes into the atlas
#[derive(Clone, Copy, Debug)]
pub(crate) enum AtlasTexture {
	Wall,
	FloorFull,
	FloorEmpty,
	WallBase,
	WallEdgeNorth,
	WallEdgeEast,
	WallEdgeSouth,
	WallEdgeWest,
	Player,
	InvTopLayer,
	InvBottomLayer,
	Error,
}
impl AtlasTexture {
	/// Returns the x,y position of the sprite on the atlas.bmp texture
	#[must_use]
	pub(crate) const fn xy(&self) -> (i32, i32) {
		match self {
			Self::Wall => (0, 0),
			Self::FloorFull => (1, 0),
			Self::FloorEmpty => (2, 0),
			Self::WallBase => (3, 0),
			Self::WallEdgeNorth => (0, 1),
			Self::WallEdgeEast => (1, 1),
			Self::WallEdgeSouth => (2, 1),
			Self::WallEdgeWest => (3, 1),
			Self::Player => (0, 2),
			Self::InvTopLayer => (1, 2),
			Self::InvBottomLayer => (2, 2),
			Self::Error => (3, 3),
		}
	}

	/// Returns the x position of the sprite on the atlas.bmp texture
	#[must_use]
	pub(crate) const fn x(&self) -> i32 {
		self.xy().0
	}

	/// Returns the y position of the sprite on the atlas.bmp texture
	#[must_use]
	pub(crate) const fn y(&self) -> i32 {
		self.xy().1
	}
}

/// The `ImageData` container loads all game sprites, and other images into memory.
#[derive(Debug)]
pub(crate) struct ImageData {
	pub(crate) atlas: Texture2D,
	pub(crate) heart_full: Texture2D,
	pub(crate) heart_half: Texture2D,
	pub(crate) heart_empty: Texture2D,
	pub(crate) error: Texture2D,
}
impl ImageData {
	pub(crate) fn load(
		handle: &mut RaylibHandle,
		thread: &RaylibThread,
	) -> crate::Result<Self> {
		let atlas = handle.load_texture(thread, "assets/atlas.bmp")?;
		let heart_full = handle.load_texture(thread, "assets/heart_full.bmp")?;
		let heart_half = handle.load_texture(thread, "assets/heart_half.bmp")?;
		let heart_empty = handle.load_texture(thread, "assets/heart_empty.bmp")?;
		let error = handle.load_texture(thread, "assets/error.bmp")?;

		Ok(Self {
			atlas,
			heart_full,
			heart_half,
			heart_empty,
			error,
		})
	}

	pub(crate) fn get_item_texture(&self, _item: &Item) -> &Texture2D {
		// TODO: make item textures
		&self.error
	}
}