-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.rs
152 lines (148 loc) · 5.01 KB
/
engine.rs
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
use crate::camera;
use super::{camera::Camera, input::Input};
use sdl2::{
self, event::Event, keyboard::Keycode, pixels::Color, render::Canvas, video::Window, EventPump,
};
use std::time::{Duration, Instant};
fn find_sdl_gl_driver() -> Option<u32> {
for (index, item) in sdl2::render::drivers().enumerate() {
if item.name == "opengl" {
return Some(index as u32);
}
}
None
}
pub struct Engine {
current_input: Input,
canvas: Canvas<Window>,
event_listener: EventPump,
camera: Camera,
}
impl Engine {
pub fn new_square_screen(screen_size: u32) -> Self {
let sdl = sdl2::init().unwrap();
let video = sdl.video().unwrap();
let window = video
.window("Mandelbrot", screen_size, screen_size)
.position_centered()
.opengl()
.build()
.unwrap();
let canvas = window
.into_canvas()
.index(find_sdl_gl_driver().unwrap())
.build()
.unwrap();
let event_listener = sdl.event_pump().unwrap();
Self {
current_input: Input::new(),
canvas,
event_listener,
camera: Camera::new(screen_size, screen_size, camera::BASE_DEPTH),
}
}
pub fn new_rect_screen(screen_width: u32, screen_height: u32) -> Self {
let sdl = sdl2::init().unwrap();
let video = sdl.video().unwrap();
let window = video
.window("Mandelbrot", screen_width, screen_height)
.position_centered()
.opengl()
.build()
.unwrap();
let canvas = window
.into_canvas()
.index(find_sdl_gl_driver().unwrap())
.build()
.unwrap();
let event_listener = sdl.event_pump().unwrap();
Self {
current_input: Input::new(),
canvas,
event_listener,
camera: Camera::new(screen_width, screen_height, camera::BASE_DEPTH),
}
}
pub fn engine_loop(&mut self) {
let mut time_elapsed: Duration = Duration::ZERO;
let mut now: Instant;
while !self.current_input.exit {
now = Instant::now();
self.process_input();
self.camera
.update_with_delta_time(&self.current_input, time_elapsed);
self.render();
time_elapsed = Instant::now() - now;
let remaining = ((1_000_000_u32 / 60) as i64) - time_elapsed.as_millis() as i64;
if remaining > 0 {
std::thread::sleep(Duration::from_nanos(remaining as u64));
}
}
}
fn process_input(&mut self) {
for event in self.event_listener.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => self.current_input.exit = true,
Event::KeyDown {
keycode: Some(Keycode::A),
..
} => self.current_input.left = true,
Event::KeyDown {
keycode: Some(Keycode::D),
..
} => self.current_input.right = true,
Event::KeyDown {
keycode: Some(Keycode::W),
..
} => self.current_input.up = true,
Event::KeyDown {
keycode: Some(Keycode::S),
..
} => self.current_input.down = true,
Event::KeyDown {
keycode: Some(Keycode::O),
..
} => self.current_input.zoom_in = true,
Event::KeyDown {
keycode: Some(Keycode::P),
..
} => self.current_input.zoom_out = true,
Event::KeyUp {
keycode: Some(Keycode::A),
..
} => self.current_input.left = false,
Event::KeyUp {
keycode: Some(Keycode::D),
..
} => self.current_input.right = false,
Event::KeyUp {
keycode: Some(Keycode::W),
..
} => self.current_input.up = false,
Event::KeyUp {
keycode: Some(Keycode::S),
..
} => self.current_input.down = false,
Event::KeyUp {
keycode: Some(Keycode::O),
..
} => self.current_input.zoom_in = false,
Event::KeyUp {
keycode: Some(Keycode::P),
..
} => self.current_input.zoom_out = false,
_ => {}
}
}
}
fn render(&mut self) {
self.canvas.set_draw_color(Color::BLACK);
self.canvas.clear();
self.camera.render_mandelbrot(&mut self.canvas);
self.canvas.present();
}
}