doukutsu-rs/src/framework/backend_null.rs

173 lines
4.4 KiB
Rust
Raw Normal View History

use std::any::Any;
2021-02-17 07:59:29 +00:00
use std::cell::RefCell;
use std::mem;
2022-01-08 05:57:04 +00:00
use imgui::{DrawData, TextureId, Ui};
use crate::common::{Color, Rect};
use crate::framework::backend::{
Backend, BackendEventLoop, BackendRenderer, BackendShader, BackendTexture, SpriteBatchCommand, VertexData,
};
use crate::framework::context::Context;
use crate::framework::error::GameResult;
use crate::framework::graphics::BlendMode;
2022-11-19 17:20:03 +00:00
use crate::game::Game;
2021-02-17 07:59:29 +00:00
pub struct NullBackend;
impl NullBackend {
pub fn new() -> GameResult<Box<dyn Backend>> {
Ok(Box::new(NullBackend))
}
}
impl Backend for NullBackend {
fn create_event_loop(&self, _ctx: &Context) -> GameResult<Box<dyn BackendEventLoop>> {
2021-02-17 07:59:29 +00:00
Ok(Box::new(NullEventLoop))
}
}
2022-11-21 14:15:51 +00:00
#[cfg(target_os = "horizon")]
#[repr(C)]
pub struct PrintConsole {}
#[cfg(target_os = "horizon")]
extern "C" { fn consoleUpdate(unk: *mut PrintConsole); }
2021-02-17 07:59:29 +00:00
pub struct NullEventLoop;
impl BackendEventLoop for NullEventLoop {
fn run(&mut self, game: &mut Game, ctx: &mut Context) {
2022-11-21 14:15:51 +00:00
println!("BackendEventLoop::run");
#[cfg(target_os = "horizon")]
unsafe {
consoleUpdate(std::ptr::null_mut());
}
2021-02-17 07:59:29 +00:00
let state_ref = unsafe { &mut *game.state.get() };
2021-02-24 08:28:47 +00:00
ctx.screen_size = (640.0, 480.0);
state_ref.handle_resize(ctx).unwrap();
2021-02-17 07:59:29 +00:00
loop {
game.update(ctx).unwrap();
2021-02-17 07:59:29 +00:00
if state_ref.shutdown {
log::info!("Shutting down...");
break;
}
2021-02-17 07:59:29 +00:00
if state_ref.next_scene.is_some() {
mem::swap(&mut game.scene, &mut state_ref.next_scene);
state_ref.next_scene = None;
game.scene.as_mut().unwrap().init(state_ref, ctx).unwrap();
game.loops = 0;
state_ref.frame_time = 0.0;
}
2021-10-14 04:54:11 +00:00
std::thread::sleep(std::time::Duration::from_millis(10));
2021-03-23 01:49:18 +00:00
2021-10-14 04:54:11 +00:00
game.draw(ctx).unwrap();
2022-11-21 14:15:51 +00:00
#[cfg(target_os = "horizon")]
unsafe {
consoleUpdate(std::ptr::null_mut());
}
2021-02-17 07:59:29 +00:00
}
}
2022-04-15 00:51:48 +00:00
fn new_renderer(&self, _ctx: *mut Context) -> GameResult<Box<dyn BackendRenderer>> {
2021-02-17 07:59:29 +00:00
let mut imgui = imgui::Context::create();
imgui.io_mut().display_size = [640.0, 480.0];
imgui.fonts().build_alpha8_texture();
Ok(Box::new(NullRenderer(RefCell::new(imgui))))
}
}
pub struct NullTexture(u16, u16);
impl BackendTexture for NullTexture {
fn dimensions(&self) -> (u16, u16) {
(self.0, self.1)
}
fn add(&mut self, _command: SpriteBatchCommand) {}
2021-02-17 07:59:29 +00:00
fn clear(&mut self) {}
2021-02-17 07:59:29 +00:00
fn draw(&mut self) -> GameResult<()> {
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
2021-02-17 07:59:29 +00:00
}
pub struct NullRenderer(RefCell<imgui::Context>);
impl BackendRenderer for NullRenderer {
2021-04-14 10:09:40 +00:00
fn renderer_name(&self) -> String {
"Null".to_owned()
}
fn clear(&mut self, _color: Color) {}
2021-02-17 07:59:29 +00:00
fn present(&mut self) -> GameResult {
Ok(())
}
fn create_texture_mutable(&mut self, width: u16, height: u16) -> GameResult<Box<dyn BackendTexture>> {
Ok(Box::new(NullTexture(width, height)))
}
fn create_texture(&mut self, width: u16, height: u16, _data: &[u8]) -> GameResult<Box<dyn BackendTexture>> {
2021-02-17 07:59:29 +00:00
Ok(Box::new(NullTexture(width, height)))
}
fn set_blend_mode(&mut self, _blend: BlendMode) -> GameResult {
2021-02-17 07:59:29 +00:00
Ok(())
}
fn set_render_target(&mut self, _texture: Option<&Box<dyn BackendTexture>>) -> GameResult {
2021-02-17 07:59:29 +00:00
Ok(())
}
fn draw_rect(&mut self, _rect: Rect<isize>, _color: Color) -> GameResult {
2021-02-17 07:59:29 +00:00
Ok(())
}
fn draw_outline_rect(&mut self, _rect: Rect<isize>, _line_width: usize, _color: Color) -> GameResult {
2021-02-17 07:59:29 +00:00
Ok(())
}
2022-01-05 04:50:16 +00:00
fn set_clip_rect(&mut self, _rect: Option<Rect>) -> GameResult {
2021-12-02 05:57:44 +00:00
Ok(())
}
2021-02-17 07:59:29 +00:00
fn imgui(&self) -> GameResult<&mut imgui::Context> {
unsafe { Ok(&mut *self.0.as_ptr()) }
}
fn imgui_texture_id(&self, _texture: &Box<dyn BackendTexture>) -> GameResult<TextureId> {
Ok(TextureId::from(0))
}
2022-01-08 05:57:04 +00:00
fn prepare_imgui(&mut self, _ui: &Ui) -> GameResult {
Ok(())
}
fn render_imgui(&mut self, _draw_data: &DrawData) -> GameResult {
2021-02-17 07:59:29 +00:00
Ok(())
}
2021-06-20 19:41:09 +00:00
fn draw_triangle_list(
&mut self,
_vertices: &[VertexData],
_texture: Option<&Box<dyn BackendTexture>>,
_shader: BackendShader,
) -> GameResult<()> {
2021-06-20 19:41:09 +00:00
Ok(())
}
2021-02-17 07:59:29 +00:00
}