Browse Source

move HAL stuff around

master
Alison Watson 2 years ago
parent
commit
9c3d1f5155
  1. 18
      source/framework/hal.rs
  2. 11
      source/framework/hal/ctx.rs
  3. 8
      source/framework/hal/evt.rs
  4. 46
      source/framework/hal/win.rs
  5. 2
      source/framework/render/instance.rs
  6. 2
      source/framework/render/surface.rs
  7. 8
      source/main_test/entry.rs

18
source/framework/hal.rs

@ -1,14 +1,8 @@
mod context;
mod event;
mod window;
pub mod ctx;
pub mod evt;
pub mod win;
pub use self::{
context::Context,
event::{Event, EventIterator},
window::Window,
};
use sdl2_sys::SDL_GetError;
pub(self) use sdl2_sys as sdl;
#[derive(Error, Debug)]
pub enum Err {
@ -24,7 +18,9 @@ impl Err {
/// This function will cause undefined behaviour if used from
/// multiple threads.
pub(self) unsafe fn new_sdl() -> Self {
Self::Sdl(std::ffi::CStr::from_ptr(SDL_GetError()).to_string_lossy())
Self::Sdl(
std::ffi::CStr::from_ptr(sdl::SDL_GetError()).to_string_lossy(),
)
}
}

11
source/framework/hal/context.rs → source/framework/hal/ctx.rs

@ -1,15 +1,14 @@
use super::Err;
use sdl2_sys::{SDL_Init, SDL_Quit, SDL_SetMainReady, SDL_INIT_VIDEO};
use super::*;
pub struct Context;
impl Context {
pub fn new() -> Result<Self, Err> {
let init_flags = SDL_INIT_VIDEO;
let init_flags = sdl::SDL_INIT_VIDEO;
unsafe {
SDL_SetMainReady();
sdl::SDL_SetMainReady();
}
if unsafe { SDL_Init(init_flags) } == 0 {
if unsafe { sdl::SDL_Init(init_flags) } == 0 {
Ok(Self)
} else {
Err(unsafe { Err::new_sdl() })
@ -20,7 +19,7 @@ impl Context {
impl Drop for Context {
fn drop(&mut self) {
unsafe {
SDL_Quit();
sdl::SDL_Quit();
}
}
}

8
source/framework/hal/event.rs → source/framework/hal/evt.rs

@ -1,4 +1,4 @@
use sdl2_sys::{SDL_Event, SDL_EventType, SDL_PollEvent};
use super::*;
pub struct EventIterator;
@ -11,7 +11,7 @@ impl Iterator for EventIterator {
fn next(&mut self) -> Option<Self::Item> {
let mut event = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
while unsafe { SDL_PollEvent(&mut event) } == 1 {
while unsafe { sdl::SDL_PollEvent(&mut event) } == 1 {
if let Some(evt) = read_in(event) {
return Some(evt);
}
@ -20,10 +20,10 @@ impl Iterator for EventIterator {
}
}
fn read_in(evt: SDL_Event) -> Option<Event> {
fn read_in(evt: sdl::SDL_Event) -> Option<Event> {
let typ = unsafe { std::mem::transmute(evt.type_) };
match typ {
SDL_EventType::SDL_QUIT => Some(Event::Quit),
sdl::SDL_EventType::SDL_QUIT => Some(Event::Quit),
_ => None,
}
}

46
source/framework/hal/window.rs → source/framework/hal/win.rs

@ -1,37 +1,33 @@
use super::{Context, Err};
use super::*;
use crate::ffi;
use ash::{
version::InstanceV1_0,
vk::{self, Handle},
};
use sdl2_sys::{
SDL_CreateWindow, SDL_DestroyWindow, SDL_Vulkan_CreateSurface,
SDL_Vulkan_GetInstanceExtensions, SDL_Window, SDL_WindowFlags, SDL_bool,
SDL_WINDOWPOS_UNDEFINED_MASK,
};
use std::{marker::PhantomData, os::raw::c_int};
pub struct Window<'a> {
hal: PhantomData<&'a Context>,
handle: *mut SDL_Window,
hal: PhantomData<&'a ctx::Context>,
handle: *mut sdl::SDL_Window,
}
impl<'a> Window<'a> {
pub fn new(
_hal: &'a Context,
title: crate::ffi::Nts,
_hal: &'a ctx::Context,
title: ffi::Nts,
w: i16,
h: i16,
) -> Result<Self, Err> {
let handle = unsafe {
SDL_CreateWindow(
sdl::SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
sdl::SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
sdl::SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
w.into(),
h.into(),
SDL_WindowFlags::SDL_WINDOW_SHOWN as u32
| SDL_WindowFlags::SDL_WINDOW_VULKAN as u32
| SDL_WindowFlags::SDL_WINDOW_RESIZABLE as u32,
sdl::SDL_WindowFlags::SDL_WINDOW_SHOWN as u32
| sdl::SDL_WindowFlags::SDL_WINDOW_VULKAN as u32
| sdl::SDL_WindowFlags::SDL_WINDOW_RESIZABLE as u32,
)
};
if !handle.is_null() {
@ -41,27 +37,25 @@ impl<'a> Window<'a> {
}
}
pub fn vulkan_instance_extensions(
&self,
) -> Result<Vec<crate::ffi::Nts>, Err> {
pub fn vulkan_instance_extensions(&self) -> Result<Vec<ffi::Nts>, Err> {
let mut count = 0;
let res = unsafe {
SDL_Vulkan_GetInstanceExtensions(
sdl::SDL_Vulkan_GetInstanceExtensions(
self.handle,
&mut count,
std::ptr::null_mut(),
)
};
if res != SDL_bool::SDL_FALSE {
if res != sdl::SDL_bool::SDL_FALSE {
let mut names = vec![std::ptr::null(); count as usize];
let res = unsafe {
SDL_Vulkan_GetInstanceExtensions(
sdl::SDL_Vulkan_GetInstanceExtensions(
self.handle,
&mut count,
names.as_mut_ptr(),
)
};
if res != SDL_bool::SDL_FALSE {
if res != sdl::SDL_bool::SDL_FALSE {
Ok(names)
} else {
Err(unsafe { Err::new_sdl() })
@ -77,13 +71,13 @@ impl<'a> Window<'a> {
) -> Result<vk::SurfaceKHR, Err> {
let mut surface = 0;
let res = unsafe {
SDL_Vulkan_CreateSurface(
sdl::SDL_Vulkan_CreateSurface(
self.handle,
(**instance).handle().as_raw() as usize,
&mut surface,
)
};
if res != SDL_bool::SDL_FALSE {
if res != sdl::SDL_bool::SDL_FALSE {
Ok(vk::SurfaceKHR::from_raw(surface))
} else {
Err(unsafe { Err::new_sdl() })
@ -94,7 +88,7 @@ impl<'a> Window<'a> {
impl Drop for Window<'_> {
fn drop(&mut self) {
unsafe {
SDL_DestroyWindow(self.handle);
sdl::SDL_DestroyWindow(self.handle);
}
}
}

2
source/framework/render/instance.rs

@ -49,7 +49,7 @@ impl Instance {
pub fn create(
conf: &Conf,
entry: ash::Entry,
window: &hal::Window,
window: &hal::win::Window,
) -> Result<Rc<Self>, ErrInstanceCreate> {
let exts = window.vulkan_instance_extensions()?;

2
source/framework/render/surface.rs

@ -11,7 +11,7 @@ pub struct Surface {
impl Surface {
pub fn create(
instance: Rc<Instance>,
window: &hal::Window,
window: &hal::win::Window,
) -> Result<Rc<Self>, hal::Err> {
let handle = window.vulkan_create_surface(&instance)?;
Ok(Rc::new(Self { handle, instance }))

8
source/main_test/entry.rs

@ -364,8 +364,8 @@ pub fn run(
/*
let concur_frames = conf.render.concurrent_frames.into();
let hal = hal::Context::new()?;
let window = hal::Window::new(&hal, meta::ffi::name(), 640, 480)?;
let hal = hal::ctx::Context::new()?;
let window = hal::win::Window::new(&hal, meta::ffi::name(), 640, 480)?;
let entry = ash::Entry::new()?;
let instance = Instance::create(&conf.render, entry, &window)?;
@ -436,9 +436,9 @@ pub fn run(
'main_loop: loop {
// FIXME: can remove allow once this has more events
#[allow(clippy::never_loop)]
for event in hal::EventIterator {
for event in hal::evt::EventIterator {
match event {
hal::Event::Quit => break 'main_loop,
hal::evt::Event::Quit => break 'main_loop,
}
}

Loading…
Cancel
Save