1use htl::mlua::{FromLua, Function, Table, Value};
75use htl::{Htl, TealRecord, host_module};
76use std::cell::RefCell;
77use std::rc::Rc;
78
79pub use macroquad;
85
86#[derive(TealRecord, Clone, Copy, Debug, PartialEq)]
88pub struct Color {
89 pub r: f32,
90 pub g: f32,
91 pub b: f32,
92 pub a: f32,
93}
94
95impl From<Color> for macroquad::color::Color {
96 fn from(c: Color) -> Self {
97 macroquad::color::Color::new(c.r, c.g, c.b, c.a)
98 }
99}
100
101impl From<macroquad::color::Color> for Color {
102 fn from(c: macroquad::color::Color) -> Self {
103 Color {
104 r: c.r,
105 g: c.g,
106 b: c.b,
107 a: c.a,
108 }
109 }
110}
111
112#[derive(TealRecord, Clone, Copy, Debug, PartialEq)]
114pub struct Vec2 {
115 pub x: f32,
116 pub y: f32,
117}
118
119impl From<Vec2> for macroquad::math::Vec2 {
120 fn from(v: Vec2) -> Self {
121 macroquad::math::Vec2::new(v.x, v.y)
122 }
123}
124
125impl From<macroquad::math::Vec2> for Vec2 {
126 fn from(v: macroquad::math::Vec2) -> Self {
127 Vec2 { x: v.x, y: v.y }
128 }
129}
130
131#[derive(TealRecord, Clone, Copy, Debug, PartialEq, Eq)]
135pub enum Key {
136 Space,
137 Escape,
138 Enter,
139 Tab,
140 Backspace,
141 Delete,
142 Insert,
143 Home,
144 End,
145 PageUp,
146 PageDown,
147 Up,
148 Down,
149 Left,
150 Right,
151 LeftShift,
152 RightShift,
153 LeftControl,
154 RightControl,
155 LeftAlt,
156 RightAlt,
157 A,
158 B,
159 C,
160 D,
161 E,
162 F,
163 G,
164 H,
165 I,
166 J,
167 K,
168 L,
169 M,
170 N,
171 O,
172 P,
173 Q,
174 R,
175 S,
176 T,
177 U,
178 V,
179 W,
180 X,
181 Y,
182 Z,
183 Key0,
184 Key1,
185 Key2,
186 Key3,
187 Key4,
188 Key5,
189 Key6,
190 Key7,
191 Key8,
192 Key9,
193 F1,
194 F2,
195 F3,
196 F4,
197 F5,
198 F6,
199 F7,
200 F8,
201 F9,
202 F10,
203 F11,
204 F12,
205}
206
207impl From<Key> for macroquad::input::KeyCode {
208 fn from(k: Key) -> Self {
209 match k {
210 Key::Space => macroquad::input::KeyCode::Space,
211 Key::Escape => macroquad::input::KeyCode::Escape,
212 Key::Enter => macroquad::input::KeyCode::Enter,
213 Key::Tab => macroquad::input::KeyCode::Tab,
214 Key::Backspace => macroquad::input::KeyCode::Backspace,
215 Key::Delete => macroquad::input::KeyCode::Delete,
216 Key::Insert => macroquad::input::KeyCode::Insert,
217 Key::Home => macroquad::input::KeyCode::Home,
218 Key::End => macroquad::input::KeyCode::End,
219 Key::PageUp => macroquad::input::KeyCode::PageUp,
220 Key::PageDown => macroquad::input::KeyCode::PageDown,
221 Key::Up => macroquad::input::KeyCode::Up,
222 Key::Down => macroquad::input::KeyCode::Down,
223 Key::Left => macroquad::input::KeyCode::Left,
224 Key::Right => macroquad::input::KeyCode::Right,
225 Key::LeftShift => macroquad::input::KeyCode::LeftShift,
226 Key::RightShift => macroquad::input::KeyCode::RightShift,
227 Key::LeftControl => macroquad::input::KeyCode::LeftControl,
228 Key::RightControl => macroquad::input::KeyCode::RightControl,
229 Key::LeftAlt => macroquad::input::KeyCode::LeftAlt,
230 Key::RightAlt => macroquad::input::KeyCode::RightAlt,
231 Key::A => macroquad::input::KeyCode::A,
232 Key::B => macroquad::input::KeyCode::B,
233 Key::C => macroquad::input::KeyCode::C,
234 Key::D => macroquad::input::KeyCode::D,
235 Key::E => macroquad::input::KeyCode::E,
236 Key::F => macroquad::input::KeyCode::F,
237 Key::G => macroquad::input::KeyCode::G,
238 Key::H => macroquad::input::KeyCode::H,
239 Key::I => macroquad::input::KeyCode::I,
240 Key::J => macroquad::input::KeyCode::J,
241 Key::K => macroquad::input::KeyCode::K,
242 Key::L => macroquad::input::KeyCode::L,
243 Key::M => macroquad::input::KeyCode::M,
244 Key::N => macroquad::input::KeyCode::N,
245 Key::O => macroquad::input::KeyCode::O,
246 Key::P => macroquad::input::KeyCode::P,
247 Key::Q => macroquad::input::KeyCode::Q,
248 Key::R => macroquad::input::KeyCode::R,
249 Key::S => macroquad::input::KeyCode::S,
250 Key::T => macroquad::input::KeyCode::T,
251 Key::U => macroquad::input::KeyCode::U,
252 Key::V => macroquad::input::KeyCode::V,
253 Key::W => macroquad::input::KeyCode::W,
254 Key::X => macroquad::input::KeyCode::X,
255 Key::Y => macroquad::input::KeyCode::Y,
256 Key::Z => macroquad::input::KeyCode::Z,
257 Key::Key0 => macroquad::input::KeyCode::Key0,
258 Key::Key1 => macroquad::input::KeyCode::Key1,
259 Key::Key2 => macroquad::input::KeyCode::Key2,
260 Key::Key3 => macroquad::input::KeyCode::Key3,
261 Key::Key4 => macroquad::input::KeyCode::Key4,
262 Key::Key5 => macroquad::input::KeyCode::Key5,
263 Key::Key6 => macroquad::input::KeyCode::Key6,
264 Key::Key7 => macroquad::input::KeyCode::Key7,
265 Key::Key8 => macroquad::input::KeyCode::Key8,
266 Key::Key9 => macroquad::input::KeyCode::Key9,
267 Key::F1 => macroquad::input::KeyCode::F1,
268 Key::F2 => macroquad::input::KeyCode::F2,
269 Key::F3 => macroquad::input::KeyCode::F3,
270 Key::F4 => macroquad::input::KeyCode::F4,
271 Key::F5 => macroquad::input::KeyCode::F5,
272 Key::F6 => macroquad::input::KeyCode::F6,
273 Key::F7 => macroquad::input::KeyCode::F7,
274 Key::F8 => macroquad::input::KeyCode::F8,
275 Key::F9 => macroquad::input::KeyCode::F9,
276 Key::F10 => macroquad::input::KeyCode::F10,
277 Key::F11 => macroquad::input::KeyCode::F11,
278 Key::F12 => macroquad::input::KeyCode::F12,
279 }
280 }
281}
282
283#[derive(TealRecord, Clone, Copy, Debug, PartialEq, Eq)]
284pub enum MouseButton {
285 Left,
286 Middle,
287 Right,
288}
289
290impl From<MouseButton> for macroquad::input::MouseButton {
291 fn from(b: MouseButton) -> Self {
292 match b {
293 MouseButton::Left => macroquad::input::MouseButton::Left,
294 MouseButton::Middle => macroquad::input::MouseButton::Middle,
295 MouseButton::Right => macroquad::input::MouseButton::Right,
296 }
297 }
298}
299
300pub struct Mq;
307
308#[host_module(
309 name = "mq",
310 dts = "dts/mq.d.tl",
311 records = [Color, Vec2, Key, MouseButton, Game]
312)]
313impl Mq {
314 pub fn screen_width(&self) -> f32 {
316 macroquad::window::screen_width()
317 }
318
319 pub fn screen_height(&self) -> f32 {
321 macroquad::window::screen_height()
322 }
323
324 pub fn frame_time(&self) -> f32 {
326 macroquad::time::get_frame_time()
327 }
328
329 pub fn time(&self) -> f64 {
331 macroquad::time::get_time()
332 }
333
334 pub fn fps(&self) -> i32 {
336 macroquad::time::get_fps()
337 }
338
339 pub fn clear_background(&self, color: Color) {
341 macroquad::window::clear_background(color.into());
342 }
343
344 pub fn draw_rectangle(&self, x: f32, y: f32, w: f32, h: f32, color: Color) {
346 macroquad::shapes::draw_rectangle(x, y, w, h, color.into());
347 }
348
349 pub fn draw_rectangle_lines(
351 &self,
352 x: f32,
353 y: f32,
354 w: f32,
355 h: f32,
356 thickness: f32,
357 color: Color,
358 ) {
359 macroquad::shapes::draw_rectangle_lines(x, y, w, h, thickness, color.into());
360 }
361
362 pub fn draw_circle(&self, x: f32, y: f32, r: f32, color: Color) {
364 macroquad::shapes::draw_circle(x, y, r, color.into());
365 }
366
367 pub fn draw_circle_lines(&self, x: f32, y: f32, r: f32, thickness: f32, color: Color) {
369 macroquad::shapes::draw_circle_lines(x, y, r, thickness, color.into());
370 }
371
372 pub fn draw_line(&self, x1: f32, y1: f32, x2: f32, y2: f32, thickness: f32, color: Color) {
374 macroquad::shapes::draw_line(x1, y1, x2, y2, thickness, color.into());
375 }
376
377 pub fn draw_text(&self, text: &str, x: f32, y: f32, font_size: f32, color: Color) {
379 macroquad::text::draw_text(text, x, y, font_size, color.into());
380 }
381
382 pub fn is_key_down(&self, key: Key) -> bool {
384 macroquad::input::is_key_down(key.into())
385 }
386
387 pub fn is_key_pressed(&self, key: Key) -> bool {
389 macroquad::input::is_key_pressed(key.into())
390 }
391
392 pub fn is_key_released(&self, key: Key) -> bool {
394 macroquad::input::is_key_released(key.into())
395 }
396
397 pub fn mouse_position(&self) -> Vec2 {
399 let (x, y) = macroquad::input::mouse_position();
400 Vec2 { x, y }
401 }
402
403 pub fn is_mouse_button_down(&self, button: MouseButton) -> bool {
405 macroquad::input::is_mouse_button_down(button.into())
406 }
407
408 pub fn is_mouse_button_pressed(&self, button: MouseButton) -> bool {
410 macroquad::input::is_mouse_button_pressed(button.into())
411 }
412}
413
414#[derive(Debug, Clone, Default, PartialEq, Eq)]
421pub struct Hooks {
422 pub frames: Option<u64>,
423 pub shot: Option<String>,
424}
425
426impl Hooks {
427 pub const NONE: Hooks = Hooks {
428 frames: None,
429 shot: None,
430 };
431
432 pub fn from_env() -> anyhow::Result<Hooks> {
434 Self::parse(
435 std::env::var("HTL_MQ_FRAMES").ok().as_deref(),
436 std::env::var("HTL_MQ_SHOT").ok().as_deref(),
437 )
438 }
439
440 pub fn parse(frames: Option<&str>, shot: Option<&str>) -> anyhow::Result<Hooks> {
443 let frames = match frames {
444 None | Some("") => None,
445 Some(s) => {
446 let n: u64 = s.parse().map_err(|_| {
447 anyhow::anyhow!("HTL_MQ_FRAMES: expected a frame count of 1 or more, got `{s}`")
448 })?;
449 if n == 0 {
450 anyhow::bail!("HTL_MQ_FRAMES: expected a frame count of 1 or more, got `{s}`");
451 }
452 Some(n)
453 }
454 };
455 let shot = match shot {
456 None | Some("") => None,
457 Some(s) => Some(s.to_string()),
458 };
459 Ok(Hooks { frames, shot })
460 }
461}
462
463pub fn conf(title: &str, width: i32, height: i32) -> macroquad::conf::Conf {
465 macroquad::conf::Conf {
466 miniquad_conf: macroquad::miniquad::conf::Conf {
467 window_title: title.to_string(),
468 window_width: width,
469 window_height: height,
470 ..Default::default()
471 },
472 ..Default::default()
473 }
474}
475
476#[derive(TealRecord)]
484struct Game {
485 #[teal(noyield)]
486 load: Option<Function>,
487 #[teal(noyield)]
488 update: Function,
489 #[teal(noyield)]
490 draw: Function,
491}
492
493pub fn run(h: Htl, game: Table, conf: macroquad::conf::Conf) -> anyhow::Result<()> {
498 run_with(h, game, conf, Hooks::from_env()?)
499}
500
501pub fn run_with(
503 h: Htl,
504 game: Table,
505 conf: macroquad::conf::Conf,
506 hooks: Hooks,
507) -> anyhow::Result<()> {
508 let game = Game::from_lua(Value::Table(game), h.lua())?;
509 let failed: Rc<RefCell<Option<anyhow::Error>>> = Rc::new(RefCell::new(None));
510 macroquad::Window::from_config(conf, {
511 let failed = Rc::clone(&failed);
512 async move {
513 let _h = h;
514 if let Err(e) = frames(game, hooks).await {
515 *failed.borrow_mut() = Some(e);
516 }
517 }
518 });
519 match failed.borrow_mut().take() {
520 Some(e) => Err(e),
521 None => Ok(()),
522 }
523}
524
525async fn frames(game: Game, hooks: Hooks) -> anyhow::Result<()> {
529 if let Some(f) = game.load {
530 f.call::<()>(())?;
531 }
532 let mut n: u64 = 0;
533 loop {
534 let dt = macroquad::time::get_frame_time();
535 let go: bool = game.update.call::<bool>(dt)?;
536 if !go {
538 break;
539 }
540 game.draw.call::<()>(())?;
541 n += 1;
542 if hooks.frames == Some(n) {
543 break;
544 }
545 macroquad::window::next_frame().await;
546 }
547 if let Some(path) = &hooks.shot {
548 macroquad::texture::get_screen_data().export_png(path);
549 }
550 Ok(())
551}