doukutsu-rs/src/framework/backend.rs

135 lines
3.6 KiB
Rust
Raw Normal View History

use std::any::Any;
2021-02-05 22:47:13 +00:00
2022-11-19 17:20:03 +00:00
use imgui::DrawData;
use crate::common::{Color, Rect};
2021-01-28 22:33:43 +00:00
use crate::framework::context::Context;
use crate::framework::error::GameResult;
2022-11-19 17:20:03 +00:00
use crate::framework::graphics::{BlendMode, VSyncMode};
use crate::game::Game;
2021-01-27 18:20:47 +00:00
2021-06-20 19:41:09 +00:00
#[repr(C)]
#[derive(Copy, Clone)]
pub struct VertexData {
pub position: (f32, f32),
pub color: (u8, u8, u8, u8),
pub uv: (f32, f32),
2021-06-20 19:41:09 +00:00
}
#[derive(Copy, Clone, PartialEq)]
pub enum BackendShader {
2022-02-27 02:43:14 +00:00
/// (scale, t, (frame_x, frame_y))
WaterFill(f32, f32, (f32, f32)),
2021-06-20 19:41:09 +00:00
Fill,
Texture,
2021-06-20 19:41:09 +00:00
}
2021-01-28 22:33:43 +00:00
pub trait Backend {
fn create_event_loop(&self, ctx: &Context) -> GameResult<Box<dyn BackendEventLoop>>;
2021-01-27 18:20:47 +00:00
}
2021-01-28 22:33:43 +00:00
pub trait BackendEventLoop {
fn run(&mut self, game: &mut Game, ctx: &mut Context);
2022-04-15 00:51:48 +00:00
fn new_renderer(&self, ctx: *mut Context) -> GameResult<Box<dyn BackendRenderer>>;
2021-01-28 22:33:43 +00:00
}
pub trait BackendRenderer {
2021-04-14 10:09:40 +00:00
fn renderer_name(&self) -> String;
2021-01-28 22:33:43 +00:00
fn clear(&mut self, color: Color);
fn present(&mut self) -> GameResult;
fn set_vsync_mode(&mut self, _mode: VSyncMode) -> GameResult {
Ok(())
}
2022-04-15 00:51:48 +00:00
fn prepare_draw(&mut self, _width: f32, _height: f32) -> GameResult {
2021-02-24 08:28:47 +00:00
Ok(())
}
2021-02-05 09:47:30 +00:00
fn create_texture_mutable(&mut self, width: u16, height: u16) -> GameResult<Box<dyn BackendTexture>>;
2021-01-28 22:33:43 +00:00
fn create_texture(&mut self, width: u16, height: u16, data: &[u8]) -> GameResult<Box<dyn BackendTexture>>;
2021-02-05 09:47:30 +00:00
fn set_blend_mode(&mut self, blend: BlendMode) -> GameResult;
fn set_render_target(&mut self, texture: Option<&Box<dyn BackendTexture>>) -> GameResult;
2021-02-05 22:47:13 +00:00
2021-02-12 23:03:38 +00:00
fn draw_rect(&mut self, rect: Rect, color: Color) -> GameResult;
fn draw_outline_rect(&mut self, rect: Rect, line_width: usize, color: Color) -> GameResult;
2021-12-02 05:57:44 +00:00
fn set_clip_rect(&mut self, rect: Option<Rect>) -> GameResult;
2021-02-05 22:47:13 +00:00
fn imgui(&self) -> GameResult<&mut imgui::Context>;
fn imgui_texture_id(&self, texture: &Box<dyn BackendTexture>) -> GameResult<imgui::TextureId>;
2022-01-08 05:57:04 +00:00
fn prepare_imgui(&mut self, ui: &imgui::Ui) -> GameResult;
2021-02-05 22:47:13 +00:00
fn render_imgui(&mut self, draw_data: &DrawData) -> GameResult;
2021-06-20 19:41:09 +00:00
fn supports_vertex_draw(&self) -> bool {
false
}
fn draw_triangle_list(
&mut self,
vertices: &[VertexData],
texture: Option<&Box<dyn BackendTexture>>,
shader: BackendShader,
) -> GameResult;
2021-01-28 22:33:43 +00:00
}
pub trait BackendTexture {
fn dimensions(&self) -> (u16, u16);
2021-02-05 22:47:13 +00:00
2021-01-28 22:33:43 +00:00
fn add(&mut self, command: SpriteBatchCommand);
2021-02-05 22:47:13 +00:00
2021-01-28 22:33:43 +00:00
fn clear(&mut self);
2021-02-05 22:47:13 +00:00
2021-01-28 22:33:43 +00:00
fn draw(&mut self) -> GameResult;
fn as_any(&self) -> &dyn Any;
2021-01-28 22:33:43 +00:00
}
2022-11-21 14:12:45 +00:00
pub trait BackendGamepad {
fn set_rumble(&mut self, low_freq: u16, high_freq: u16, duration_ms: u32) -> GameResult;
fn instance_id(&self) -> u32;
}
2021-06-27 06:03:35 +00:00
#[allow(unreachable_code)]
2021-10-16 12:59:27 +00:00
pub fn init_backend(headless: bool, size_hint: (u16, u16)) -> GameResult<Box<dyn Backend>> {
2021-10-14 04:54:11 +00:00
if headless {
return crate::framework::backend_null::NullBackend::new();
}
2022-12-01 13:30:59 +00:00
#[cfg(all(feature = "backend-horizon"))]
{
return crate::framework::backend_horizon::HorizonBackend::new();
}
2021-02-24 08:28:47 +00:00
#[cfg(all(feature = "backend-glutin"))]
2022-12-01 13:30:59 +00:00
{
return crate::framework::backend_glutin::GlutinBackend::new();
}
2021-02-24 08:28:47 +00:00
#[cfg(feature = "backend-sdl")]
2022-12-01 13:30:59 +00:00
{
return crate::framework::backend_sdl2::SDL2Backend::new(size_hint);
}
2021-02-17 07:59:29 +00:00
log::warn!("No backend compiled in, using null backend instead.");
crate::framework::backend_null::NullBackend::new()
2021-01-28 22:33:43 +00:00
}
pub enum SpriteBatchCommand {
DrawRect(Rect<f32>, Rect<f32>),
DrawRectFlip(Rect<f32>, Rect<f32>, bool, bool),
2021-01-28 22:33:43 +00:00
DrawRectTinted(Rect<f32>, Rect<f32>, Color),
DrawRectFlipTinted(Rect<f32>, Rect<f32>, bool, bool, Color),
2021-01-28 22:33:43 +00:00
}