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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use sdl2::{self, Sdl};
use sdl2::surface;
use sdl2::pixels;
use image::{self, GenericImage};
use std::fmt;
use std::io::Read;
use audio;
use conf;
use filesystem::Filesystem;
use graphics;
use input;
use timer;
use GameError;
use GameResult;
pub struct Context {
pub conf: conf::Conf,
pub sdl_context: Sdl,
pub filesystem: Filesystem,
pub gfx_context: graphics::GraphicsContext,
pub event_context: sdl2::EventSubsystem,
pub timer_context: timer::TimeContext,
pub audio_context: audio::AudioContext,
pub gamepad_context: input::GamepadContext,
pub default_font: graphics::Font,
}
impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<Context: {:p}>", self)
}
}
fn set_window_icon(context: &mut Context) -> GameResult<()> {
if !context.conf.window_icon.is_empty() {
let icon_path = &context.conf.window_icon;
let mut f = context.filesystem.open(icon_path)?;
let mut buf = Vec::new();
f.read_to_end(&mut buf)?;
let image = image::load_from_memory(&buf)?;
let image_data = &mut image.to_rgba();
let surface = surface::Surface::from_data(image_data,
image.width(),
image.height(),
image.width() * 4,
pixels::PixelFormatEnum::ABGR8888)?;
let window = context.gfx_context.get_window();
window.set_icon(surface);
};
Ok(())
}
impl Context {
fn from_conf(conf: conf::Conf, fs: Filesystem, sdl_context: Sdl) -> GameResult<Context> {
let video = sdl_context.video()?;
let audio_context = audio::AudioContext::new()?;
let event_context = sdl_context.event()?;
let timer_context = timer::TimeContext::new();
let font = graphics::Font::default_font()?;
let graphics_context = graphics::GraphicsContext::new(video,
&conf.window_title,
conf.window_width,
conf.window_height,
conf.vsync,
conf.resizable)?;
let gamepad_context = input::GamepadContext::new(&sdl_context)?;
let mut ctx = Context {
conf: conf,
sdl_context: sdl_context,
filesystem: fs,
gfx_context: graphics_context,
event_context: event_context,
timer_context: timer_context,
audio_context: audio_context,
gamepad_context: gamepad_context,
default_font: font,
};
set_window_icon(&mut ctx)?;
Ok(ctx)
}
pub fn load_from_conf(game_id: &'static str,
author: &'static str,
default_config: conf::Conf)
-> GameResult<Context> {
let sdl_context = sdl2::init()?;
let mut fs = Filesystem::new(game_id, author)?;
let config = fs.read_config().unwrap_or(default_config);
Context::from_conf(config, fs, sdl_context)
}
pub fn print_resource_stats(&mut self) {
if let Err(e) = self.filesystem.print_all() {
println!("Error printing out filesystem info: {}", e)
}
}
pub fn quit(&mut self) -> GameResult<()> {
let now_dur = timer::get_time_since_start(self);
let now = timer::duration_to_f64(now_dur);
let e = sdl2::event::Event::Quit { timestamp: now as u32 };
self.event_context
.push_event(e)
.map_err(GameError::from)
}
}