Skip to content

Commit

Permalink
Merge pull request #12 from Xila-Project/Feature/Display
Browse files Browse the repository at this point in the history
Feature/display
  • Loading branch information
AlixANNERAUD authored Jun 29, 2024
2 parents 59af25c + 4d059e1 commit 92d9ea0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 38 deletions.
87 changes: 50 additions & 37 deletions Modules/Screen/src/Drivers/SDL2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
use crate::Generics;
use crate::{
Generics,
Prelude::{Error_type, Result_type},
};

use sdl2::{event, mouse, pixels, render::Canvas, video, EventPump};
use sdl2::{
event, mouse, pixels,
render::Canvas,
video::{self, WindowBuildError},
EventPump, IntegerOrSdlError,
};

use std::{
cell::RefCell,
process::exit,
sync::{Arc, RwLock},
};

impl From<String> for Error_type {
fn from(Value: String) -> Self {
Self::Unknown(Value)
}
}

impl From<WindowBuildError> for Error_type {
fn from(Error: WindowBuildError) -> Self {
match Error {
WindowBuildError::SdlError(Error) => Error.into(),
WindowBuildError::HeightOverflows(_) | WindowBuildError::WidthOverflows(_) => {
Error_type::Invalid_dimension
}
WindowBuildError::InvalidTitle(_) => Error_type::Unknown("Invalid title.".to_string()),
}
}
}

impl From<IntegerOrSdlError> for Error_type {
fn from(Error: IntegerOrSdlError) -> Self {
Error_type::Unknown(Error.to_string())
}
}

pub struct Screen_type(Canvas<video::Window>);

impl Screen_type {
pub fn New(Window: video::Window) -> Result<Self, ()> {
let mut Canvas = match Window.into_canvas().build() {
Ok(Canvas) => Canvas,
Err(_) => return Err(()),
};
pub fn New(Window: video::Window) -> Result_type<Self> {
let mut Canvas = Window.into_canvas().build()?;

Canvas.clear();
Canvas.present();
Expand Down Expand Up @@ -42,11 +72,11 @@ impl<const Buffer_size: usize> Generics::Screen_traits<Buffer_size> for Screen_t
self.0.present();
}

fn Get_resolution(&self) -> Result<Generics::Point_type, ()> {
match self.0.output_size() {
Ok((Width, Height)) => Ok(Generics::Point_type::New(Width as i16, Height as i16)),
Err(_) => Err(()),
}
fn Get_resolution(&self) -> Result_type<Generics::Point_type> {
Ok(self
.0
.output_size()
.map(|(Width, Height)| Generics::Point_type::New(Width as i16, Height as i16))?)
}
}

Expand Down Expand Up @@ -135,37 +165,21 @@ impl Generics::Input_traits for Pointer_type {
}
}

pub fn New_touchscreen(Size: Generics::Point_type) -> Result<(Screen_type, Pointer_type), ()> {
let Context = match sdl2::init() {
Ok(Context) => Context,
Err(_) => return Err(()),
};
pub fn New_touchscreen(Size: Generics::Point_type) -> Result_type<(Screen_type, Pointer_type)> {
let Context = sdl2::init()?;

let Video_subsystem = match Context.video() {
Ok(Video_subsystem) => Video_subsystem,
Err(_) => return Err(()),
};
let Video_subsystem = Context.video()?;

let Window = match Video_subsystem
let Window = Video_subsystem
.window("Xila", Size.X as u32, Size.Y as u32)
.position_centered()
.build()
{
Ok(Window) => Window,
Err(_) => return Err(()),
};
.build()?;

let Event_pump = match Context.event_pump() {
Ok(Event_pump) => Event_pump,
Err(_) => return Err(()),
};
let Event_pump = Context.event_pump()?;

let Pointer = Pointer_type::New(Window.id(), Event_pump);

let Screen = match Screen_type::New(Window) {
Ok(Canvas) => Canvas,
Err(_) => return Err(()),
};
let Screen = Screen_type::New(Window)?;

Ok((Screen, Pointer))
}
Expand All @@ -178,7 +192,6 @@ mod tests {
fn Test_touchscreen() {
const Horizontal_resolution: u32 = 800;
const Vertical_resolution: u32 = 480;
const Buffer_size: usize = (Horizontal_resolution * Vertical_resolution / 2) as usize;

let Touchscreen = New_touchscreen(Generics::Point_type::New(
Horizontal_resolution as i16,
Expand All @@ -187,7 +200,7 @@ mod tests {

assert!(Touchscreen.is_ok());

let (mut Screen, mut Pointer) = Touchscreen.unwrap();
let (_, _) = Touchscreen.unwrap();

unsafe {
sdl2::sys::SDL_Quit(); // Force SDL2 to quit to avoid conflicts with other tests.
Expand Down
9 changes: 9 additions & 0 deletions Modules/Screen/src/Generics/Error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(non_camel_case_types)]

pub type Result_type<T> = std::result::Result<T, Error_type>;

#[derive(Debug, Clone)]
pub enum Error_type {
Invalid_dimension,
Unknown(String),
}
5 changes: 4 additions & 1 deletion Modules/Screen/src/Generics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod Error;
pub use Error::*;

#[derive(Clone, Copy)]
pub struct Point_type {
pub X: i16,
Expand Down Expand Up @@ -35,7 +38,7 @@ pub struct Refresh_area_type<const Buffer_size: usize> {

pub trait Screen_traits<const Buffer_size: usize> {
fn Update(&mut self, Refresh_area: &Refresh_area_type<Buffer_size>);
fn Get_resolution(&self) -> Result<Point_type, ()>;
fn Get_resolution(&self) -> Result_type<Point_type>;
}

#[derive(Clone, Copy)]
Expand Down

0 comments on commit 92d9ea0

Please sign in to comment.