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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
//! Tetra is a simple 2D game framework written in Rust. It uses SDL2 for event handling and OpenGL 3.2+ for rendering. //! //! **Note that Tetra is still extremely early in development!** It may/will have bugs and missing features. That said, you're welcome to give it a go and let me know what you think :) //! //! ## Features //! //! * XNA/MonoGame-inspired API //! * Efficient 2D rendering, with draw call batching by default //! * Simple input handling //! * Animations/spritesheets //! * TTF font rendering //! * Multiple screen scaling algorithms, including pixel-perfect variants (for those chunky retro pixels) //! * Deterministic game loop, à la [Fix Your Timestep](https://gafferongames.com/post/fix_your_timestep/) //! //! ## Installation //! //! To add Tetra to your project, add the following line to your `Cargo.toml` file: //! //! ```toml //! tetra = "0.2" //! ``` //! //! Tetra currently requires Rust 1.31 or higher. //! //! You will also need to install the SDL2 native libraries, as described [here](https://github.com/Rust-SDL2/rust-sdl2#user-content-requirements). The 'bundled' and 'static linking' features described can be activated using the `sdl2_bundled` and `sdl2_static_link` Cargo features in Tetra. //! //! ## Examples //! //! To get a simple window displayed on screen, the following code can be used: //! //! ```no_run //! use tetra::graphics::{self, Color}; //! use tetra::{Context, ContextBuilder, State}; //! //! struct GameState; //! //! impl State for GameState { //! fn draw(&mut self, ctx: &mut Context, _dt: f64) -> tetra::Result { //! // Cornflour blue, as is tradition //! graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929)); //! Ok(()) //! } //! } //! //! fn main() -> tetra::Result { //! ContextBuilder::new("Hello, world!", 1280, 720) //! .build()? //! .run(&mut GameState) //! } //! ``` //! //! You can see this example in action by running `cargo run --example hello_world`. //! //! The full list of examples is available [here](https://github.com/17cupsofcoffee/tetra/tree/master/examples). //! //! ## Support/Feedback //! //! As mentioned above, Tetra is fairly early in development, so there's likely to be bugs/flaky docs/general weirdness. Please feel free to leave an issue/PR if you find something! //! //! You can also contact me via [Twitter](https://twitter.com/17cupsofcoffee), or find me lurking in the #gamedev channel on the [Rust Community Discord](https://bit.ly/rust-community). #![warn(missing_docs)] pub mod audio; pub mod error; pub mod glm; pub mod graphics; pub mod input; pub mod time; pub mod window; use std::collections::VecDeque; use std::time::{Duration, Instant}; use sdl2::event::{Event, WindowEvent}; use sdl2::video::{FullscreenType, GLProfile, Window}; use sdl2::Sdl; use crate::audio::AudioContext; pub use crate::error::{Result, TetraError}; use crate::graphics::opengl::GLDevice; use crate::graphics::GraphicsContext; use crate::graphics::ScreenScaling; use crate::input::InputContext; /// A trait representing a type that contains game state and provides logic for updating it /// and drawing it to the screen. This is where you'll write your game logic! /// /// The methods on `State` allow you to return a `Result`, either explicitly or via the `?` /// operator. If an error is returned, the game will close and the error will be returned from /// the `run` function that was used to start it. #[allow(unused_variables)] pub trait State { /// Called when it is time for the game to update, at the interval specified by the context's /// tick rate. /// /// The game will update at a fixed time step (defaulting to 60fps), and draw as fast as it is /// allowed to (depending on CPU/vsync settings). This allows for deterministic updates even at /// varying framerates, but may require you to do some interpolation in the `draw` method in /// order to make things look smooth. /// /// See [Fix Your Timestep](https://gafferongames.com/post/fix_your_timestep/) for more info. fn update(&mut self, ctx: &mut Context) -> Result { Ok(()) } /// Called when it is time for the game to be drawn. /// /// As drawing will not necessarily be in step with updating, the `dt` argument is provided - /// this will be a number between 0 and 1, specifying how far through the current tick you are. /// /// For example, if the player is meant to move 16 pixels per frame, and the current `dt` is 0.5, /// you should draw them 8 pixels along. fn draw(&mut self, ctx: &mut Context, dt: f64) -> Result { Ok(()) } } /// A struct containing all of the 'global' state within the framework. pub struct Context { sdl: Sdl, window: Window, gl: GLDevice, graphics: GraphicsContext, input: InputContext, audio: AudioContext, window_width: i32, window_height: i32, fullscreen: bool, running: bool, quit_on_escape: bool, tick_rate: Duration, fps_tracker: VecDeque<f64>, } impl Context { /// Runs the game using the provided `State` implementation. /// /// # Errors /// /// If the `State` returns an error from `update` or `draw`, the game will stop /// running and this method will return the error. /// /// # Examples /// /// ```no_run /// # use tetra::{Context, ContextBuilder, State}; /// # /// struct GameState; /// /// impl State for GameState { } /// /// fn main() -> tetra::Result { /// ContextBuilder::default().build()?.run(&mut GameState) /// } /// ``` pub fn run<S>(&mut self, state: &mut S) -> Result where S: State, { self.window.show(); let mut events = self.sdl.event_pump().map_err(TetraError::Sdl)?; let mut last_time = Instant::now(); let mut lag = Duration::from_secs(0); self.running = true; while self.running { let current_time = Instant::now(); let elapsed = current_time - last_time; last_time = current_time; lag += elapsed; // Since we fill the buffer when we create the context, we can cycle it // here and it shouldn't reallocate. self.fps_tracker.pop_front(); self.fps_tracker.push_back(time::duration_to_f64(elapsed)); for event in events.poll_iter() { if let Err(e) = self .handle_event(event) .and_then(|event| input::handle_event(self, event)) { self.running = false; return Err(e); } } while lag >= self.tick_rate { if let Err(e) = state.update(self) { self.running = false; return Err(e); } input::cleanup_after_state_update(self); lag -= self.tick_rate; } let dt = time::duration_to_f64(lag) / time::duration_to_f64(self.tick_rate); if let Err(e) = state.draw(self, dt) { self.running = false; return Err(e); } graphics::present(self); std::thread::yield_now(); } self.window.hide(); Ok(()) } /// Constructs an implementation of `State` using the given closure, and then runs it. /// /// This is mainly handy when chaining methods, as it allows you to call your `State` constructor /// without breaking the chain. /// /// # Errors /// /// If the `State` returns an error from `update` or `draw`, the game will stop /// running and this method will return the error. /// /// # Examples /// /// ```no_run /// # use tetra::graphics::Texture; /// # use tetra::{Context, ContextBuilder, State}; /// # /// struct GameState { /// texture: Texture, /// } /// /// impl GameState { /// fn new(ctx: &mut Context) -> tetra::Result<GameState> { /// Ok(GameState { /// texture: Texture::new(ctx, "./examples/resources/player.png")?, /// }) /// } /// } /// /// impl State for GameState { } /// /// fn main() -> tetra::Result { /// ContextBuilder::default().build()?.run_with(GameState::new) /// } /// ``` pub fn run_with<S, F>(&mut self, init: F) -> Result where S: State, F: FnOnce(&mut Context) -> Result<S>, { let state = &mut init(self)?; self.run(state) } fn handle_event(&mut self, event: Event) -> Result<Event> { match event { Event::Quit { .. } => self.running = false, // TODO: Add a way to override this Event::Window { win_event, .. } => { if let WindowEvent::SizeChanged(x, y) = win_event { window::set_size_ex(self, x, y, true) } } _ => {} } Ok(event) } } /// Creates a new `Context` based on the provided options. #[derive(Debug, Clone)] pub struct ContextBuilder<'a> { title: &'a str, internal_width: i32, internal_height: i32, window_size: Option<(i32, i32)>, window_scale: Option<i32>, scaling: ScreenScaling, vsync: bool, tick_rate: f64, fullscreen: bool, maximized: bool, minimized: bool, resizable: bool, borderless: bool, show_mouse: bool, quit_on_escape: bool, } impl<'a> ContextBuilder<'a> { /// Creates a new ContextBuilder. pub fn new(title: &'a str, width: i32, height: i32) -> ContextBuilder<'a> { ContextBuilder { title, internal_width: width, internal_height: height, ..ContextBuilder::default() } } /// Sets the title of the window. /// /// Defaults to `"Tetra"`. pub fn title(&mut self, title: &'a str) -> &mut ContextBuilder<'a> { self.title = title; self } /// Sets the internal resolution of the screen. /// /// Defaults to `1280 x 720`. pub fn size(&mut self, width: i32, height: i32) -> &mut ContextBuilder<'a> { self.internal_width = width; self.internal_height = height; self } /// Sets the scaling mode for the game. /// /// Defaults to `ScreenScaling::ShowAllPixelPerfect`, which will maintain the screen's aspect ratio /// by letterboxing. pub fn scaling(&mut self, scaling: ScreenScaling) -> &mut ContextBuilder<'a> { self.scaling = scaling; self } /// Sets the size of the window. /// /// This only needs to be set if you want the internal resolution of the game /// to be different from the window size. /// /// This will take precedence over `window_scale`. pub fn window_size(&mut self, width: i32, height: i32) -> &mut ContextBuilder<'a> { self.window_size = Some((width, height)); self } /// Sets the size of the window, as a multiplier of the internal screen size. /// /// This only needs to be set if you want the internal resolution of the game /// to be different from the window size. /// /// `window_size` will take precedence over this. pub fn window_scale(&mut self, scale: i32) -> &mut ContextBuilder<'a> { self.window_scale = Some(scale); self } /// Enables or disables vsync. /// /// Defaults to `true`. pub fn vsync(&mut self, vsync: bool) -> &mut ContextBuilder<'a> { self.vsync = vsync; self } /// Sets the game's update tick rate, in ticks per second. /// /// Defaults to `60.0`. pub fn tick_rate(&mut self, tick_rate: f64) -> &mut ContextBuilder<'a> { self.tick_rate = 1.0 / tick_rate; self } /// Sets whether or not the window should start in fullscreen. /// /// Defaults to `false`. pub fn fullscreen(&mut self, fullscreen: bool) -> &mut ContextBuilder<'a> { self.fullscreen = fullscreen; self } /// Sets whether or not the window should start maximized. /// /// Defaults to `false`. pub fn maximized(&mut self, maximized: bool) -> &mut ContextBuilder<'a> { self.maximized = maximized; self } /// Sets whether or not the window should start minimized. /// /// Defaults to `false`. pub fn minimized(&mut self, minimized: bool) -> &mut ContextBuilder<'a> { self.minimized = minimized; self } /// Sets whether or not the window should be resizable. /// /// Defaults to `false`. pub fn resizable(&mut self, resizable: bool) -> &mut ContextBuilder<'a> { self.resizable = resizable; self } /// Sets whether or not the window should be borderless. /// /// Defaults to `false`. pub fn borderless(&mut self, borderless: bool) -> &mut ContextBuilder<'a> { self.borderless = borderless; self } /// Sets whether or not the mouse cursor should be visible. /// /// Defaults to `false`. pub fn show_mouse(&mut self, show_mouse: bool) -> &mut ContextBuilder<'a> { self.show_mouse = show_mouse; self } /// Sets whether or not the game should close when the Escape key is pressed. /// /// Defaults to `false`. pub fn quit_on_escape(&mut self, quit_on_escape: bool) -> &mut ContextBuilder<'a> { self.quit_on_escape = quit_on_escape; self } /// Builds the context. /// /// # Errors /// /// If an error is encountered during initialization of the context, this method will /// return the error. This will usually be either `TetraError::Sdl` or `TetraError::OpenGl`. pub fn build(&self) -> Result<Context> { // This needs to be initialized ASAP to avoid https://github.com/tomaka/rodio/issues/214 let audio = AudioContext::new(); let sdl = sdl2::init().map_err(TetraError::Sdl)?; let video = sdl.video().map_err(TetraError::Sdl)?; let gl_attr = video.gl_attr(); gl_attr.set_context_profile(GLProfile::Core); gl_attr.set_context_version(3, 2); gl_attr.set_red_size(8); gl_attr.set_green_size(8); gl_attr.set_blue_size(8); gl_attr.set_alpha_size(8); gl_attr.set_double_buffer(true); // TODO: Will need to add some more here if we start using the depth/stencil buffers let (mut window_width, mut window_height) = if let Some(size) = self.window_size { size } else if let Some(scale) = self.window_scale { (self.internal_width * scale, self.internal_height * scale) } else { (self.internal_width, self.internal_height) }; let mut window_builder = video.window(self.title, window_width as u32, window_height as u32); window_builder.hidden().position_centered().opengl(); if self.resizable { window_builder.resizable(); } if self.borderless { window_builder.borderless(); } sdl.mouse().show_cursor(self.show_mouse); let mut window = window_builder.build()?; // We wait until the window has been created to fiddle with this stuff as: // a) we don't want to blow away the window size settings // b) we don't know what monitor they're on until the window is created if self.maximized { window.maximize(); let size = window.drawable_size(); window_width = size.0 as i32; window_height = size.1 as i32; } else if self.minimized { window.minimize(); let size = window.drawable_size(); window_width = size.0 as i32; window_height = size.1 as i32; } if self.fullscreen { window .display_mode() .and_then(|m| { window_width = m.w; window_height = m.h; window.set_fullscreen(FullscreenType::Desktop) }) .map_err(TetraError::Sdl)?; } let mut gl = GLDevice::new(&video, &window, self.vsync)?; let graphics = GraphicsContext::new( &mut gl, window_width, window_height, self.internal_width, self.internal_height, self.scaling, )?; let input = InputContext::new(&sdl)?; // We fill the buffer with values so that the FPS counter doesn't jitter // at startup. let mut fps_tracker = VecDeque::with_capacity(200); fps_tracker.resize(200, 1.0 / 60.0); Ok(Context { sdl, window, gl, graphics, input, audio, window_width, window_height, fullscreen: self.fullscreen, running: false, quit_on_escape: self.quit_on_escape, tick_rate: time::f64_to_duration(self.tick_rate), fps_tracker, }) } } impl<'a> Default for ContextBuilder<'a> { fn default() -> ContextBuilder<'a> { ContextBuilder { title: "Tetra", internal_width: 1280, internal_height: 720, window_size: None, window_scale: None, scaling: ScreenScaling::ShowAllPixelPerfect, vsync: true, tick_rate: 1.0 / 60.0, fullscreen: false, maximized: false, minimized: false, resizable: false, borderless: false, show_mouse: false, quit_on_escape: false, } } }