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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::path::PathBuf;
use find_folder::Search;
#[cfg(feature = "display-fps")]
use fps_counter::FPSCounter;
use piston_window::clear;
use piston_window::Button;
use piston_window::ButtonArgs;
use piston_window::ButtonState;
use piston_window::Event;
use piston_window::Glyphs;
use piston_window::Input;
use piston_window::Loop;
use piston_window::OpenGL;
use piston_window::PistonWindow;
use piston_window::RenderArgs;
use piston_window::TextureSettings;
use piston_window::Transformed;
use piston_window::UpdateArgs;
use piston_window::WindowSettings;
#[cfg(feature = "display-fps")]
use piston_window::text::Text;
use elements::Field;
use elements::Scoreboard;
use execution_flow::Result;
use color;
const OPENGL: OpenGL = OpenGL::V3_2;
const SCOREBOARD_HEIGHT: u32 = 120;
pub struct Application {
assets: PathBuf,
window: PistonWindow,
field: Field,
scoreboard: Scoreboard,
#[cfg(feature = "display-fps")]
fps_counter: FPSCounter,
}
impl Application {
pub fn new() -> Result<Application> {
let width: u32 = 800;
let height: u32 = 600;
let title: &str = "Mief";
let window: PistonWindow = WindowSettings::new(title, [width, height])
.exit_on_esc(true)
.opengl(OPENGL)
.resizable(false)
.vsync(true)
.build()?;
let assets: PathBuf = Search::ParentsThenKids(3, 1).for_folder("assets")?;
let application = match () {
#[cfg(feature = "display-fps")]
() => {
Application {
assets,
window,
field: Field::new([width, height - SCOREBOARD_HEIGHT]),
scoreboard: Scoreboard::new([width, SCOREBOARD_HEIGHT], title),
fps_counter: FPSCounter::new(),
}
},
#[cfg(not(feature = "display-fps"))]
() => {
Application {
assets,
window,
field: Field::new([width, height - SCOREBOARD_HEIGHT]),
scoreboard: Scoreboard::new([width, SCOREBOARD_HEIGHT], title),
}
},
};
Ok(application)
}
fn on_button_change(&mut self, button_arguments: ButtonArgs) {
match button_arguments.state {
ButtonState::Press => self.on_button_pressed(button_arguments.button),
ButtonState::Release => self.on_button_released(button_arguments.button),
}
}
fn on_button_pressed(&mut self, button: Button) {
self.field.on_button_pressed(button);
}
fn on_button_released(&mut self, button: Button) {
self.field.on_button_released(button);
}
fn on_render(&mut self, event: &Event, _render_arguments: &RenderArgs) {
let font: PathBuf = self.assets.join("Anonymous Pro.ttf");
let factory = self.window.factory.clone();
let texture_settings = TextureSettings::new();
let mut font = Glyphs::new(font, factory, texture_settings).unwrap();
let field: &Field = &self.field;
let scoreboard: &Scoreboard = &self.scoreboard;
#[cfg(feature = "display-fps")]
let fps: &str = &self.fps_counter.tick().to_string();
let _ = self.window.draw_2d(event, |context, gl_graphics| {
clear(color::BLACK, gl_graphics);
field.on_render(context.trans(0.0, f64::from(SCOREBOARD_HEIGHT)), gl_graphics);
scoreboard.on_render(&mut font, context.trans(0.0, 0.0), gl_graphics);
#[cfg(feature = "display-fps")]
{
let size: u32 = 25;
let margin: f64 = 10.0;
let transformation = context.transform.trans(margin, (size as f64) + margin);
let text_object = Text::new_color(color::GREEN, size);
text_object.draw(fps, &mut font, &context.draw_state, transformation, gl_graphics);
}
});
}
fn on_resize(&mut self, new_width: u32, new_height: u32) {
self.field.on_resize(new_width, new_height - SCOREBOARD_HEIGHT);
self.scoreboard.on_resize(new_width, SCOREBOARD_HEIGHT);
}
fn on_update(&mut self, update_arguments: &UpdateArgs) {
self.field.on_update(update_arguments);
self.scoreboard.on_update(self.field.get_player_scores());
}
pub fn run(&mut self) {
while let Some(event) = self.window.next() {
match event {
Event::Input(input_event) => {
match input_event {
Input::Button(button_arguments) => self.on_button_change(button_arguments),
Input::Resize(width, height) => self.on_resize(width, height),
_ => {},
}
},
Event::Loop(loop_event) => {
match loop_event {
Loop::Render(render_arguments) => self.on_render(&event, &render_arguments),
Loop::Update(update_arguments) => self.on_update(&update_arguments),
_ => {},
}
},
_ => {},
};
}
}
}