Browse Source

minor fixes, add resize event

master
Alison Watson 1 year ago
parent
commit
4fed562142
10 changed files with 75 additions and 44 deletions
  1. +10
    -5
      source/hal/context.rs
  2. +2
    -3
      source/hal/err.rs
  3. +28
    -13
      source/hal/event.rs
  4. +25
    -15
      source/hal/window.rs
  5. +3
    -2
      source/main.rs
  6. +1
    -1
      source/render/device.rs
  7. +0
    -1
      source/render/image.rs
  8. +1
    -1
      source/render/instance.rs
  9. +3
    -1
      source/render/properties.rs
  10. +2
    -2
      source/util/log/color.rs

+ 10
- 5
source/hal/context.rs View File

@@ -1,15 +1,20 @@
use crate::hal::ErrSdl;
use sdl2_sys as sdl;
use sdl2_sys::{
SDL_INIT_VIDEO,
SDL_Init,
SDL_Quit,
SDL_SetMainReady,
};

pub struct Context;

impl Context {
pub fn new() -> Result<Self, ErrSdl> {
let init_flags = sdl::SDL_INIT_VIDEO;
let init_flags = SDL_INIT_VIDEO;
unsafe {
sdl::SDL_SetMainReady();
SDL_SetMainReady();
}
if unsafe { sdl::SDL_Init(init_flags) } == 0 {
if unsafe { SDL_Init(init_flags) } == 0 {
Ok(Self)
} else {
Err(unsafe { ErrSdl::new() })
@@ -20,7 +25,7 @@ impl Context {
impl Drop for Context {
fn drop(&mut self) {
unsafe {
sdl::SDL_Quit();
SDL_Quit();
}
}
}


+ 2
- 3
source/hal/err.rs View File

@@ -1,4 +1,4 @@
use sdl2_sys as sdl;
use sdl2_sys::SDL_GetError;

#[derive(Debug)]
pub struct ErrSdl {
@@ -15,8 +15,7 @@ impl std::fmt::Display for ErrSdl {

impl ErrSdl {
pub unsafe fn new() -> Self {
let contents =
std::ffi::CStr::from_ptr(sdl::SDL_GetError()).to_string_lossy();
let contents = std::ffi::CStr::from_ptr(SDL_GetError()).to_string_lossy();
Self { contents }
}
}


+ 28
- 13
source/hal/event.rs View File

@@ -1,19 +1,17 @@
use sdl2_sys as sdl;
use sdl2_sys::{
SDL_Event,
SDL_EventType,
SDL_PollEvent,
SDL_WindowEvent,
SDL_WindowEventID,
};
use std::os::raw::c_int;

pub struct EventIterator;

pub enum Event {
Quit,
}

impl Event {
fn read_in(evt: sdl::SDL_Event) -> Option<Self> {
let evt_type = unsafe { std::mem::transmute(evt.type_) };
match evt_type {
sdl::SDL_EventType::SDL_QUIT => Some(Self::Quit),
_ => None,
}
}
Resize(i32, i32),
}

impl Iterator for EventIterator {
@@ -21,8 +19,8 @@ impl Iterator for EventIterator {

fn next(&mut self) -> Option<Self::Item> {
let mut event = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
while unsafe { sdl::SDL_PollEvent(&mut event) } == 1 {
if let Some(evt) = Event::read_in(event) {
while unsafe { SDL_PollEvent(&mut event) } == 1 {
if let Some(evt) = read_in(event) {
return Some(evt);
}
}
@@ -30,4 +28,21 @@ impl Iterator for EventIterator {
}
}

fn read_in_window(evt: SDL_WindowEvent) -> Option<Event> {
match unsafe { std::mem::transmute(c_int::from(evt.event)) } {
SDL_WindowEventID::SDL_WINDOWEVENT_RESIZED => {
Some(Event::Resize(evt.data1, evt.data2))
},
_ => None,
}
}

fn read_in(evt: SDL_Event) -> Option<Event> {
match unsafe { std::mem::transmute(evt.type_) } {
SDL_EventType::SDL_QUIT => Some(Event::Quit),
SDL_EventType::SDL_WINDOWEVENT => read_in_window(unsafe { evt.window }),
_ => None,
}
}

// EOF

+ 25
- 15
source/hal/window.rs View File

@@ -3,7 +3,16 @@ use ash::{
version::InstanceV1_0,
vk::{self, Handle},
};
use sdl2_sys as sdl;
use sdl2_sys::{
SDL_CreateWindow,
SDL_DestroyWindow,
SDL_Vulkan_CreateSurface,
SDL_Vulkan_GetInstanceExtensions,
SDL_WINDOWPOS_UNDEFINED_MASK,
SDL_Window,
SDL_WindowFlags,
SDL_bool,
};
use std::{
marker::PhantomData,
os::raw::{c_char, c_int},
@@ -11,7 +20,7 @@ use std::{

pub struct Window<'a> {
hal: PhantomData<&'a Context>,
handle: *mut sdl::SDL_Window,
handle: *mut SDL_Window,
}

impl<'a> Window<'a> {
@@ -22,17 +31,18 @@ impl<'a> Window<'a> {
h: i16,
) -> Result<Self, ErrSdl> {
let handle = unsafe {
sdl::SDL_CreateWindow(
SDL_CreateWindow(
title,
sdl::SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
sdl::SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
SDL_WINDOWPOS_UNDEFINED_MASK as c_int,
w.into(),
h.into(),
sdl::SDL_WindowFlags::SDL_WINDOW_SHOWN as u32 |
sdl::SDL_WindowFlags::SDL_WINDOW_VULKAN as u32,
SDL_WindowFlags::SDL_WINDOW_SHOWN as u32 |
SDL_WindowFlags::SDL_WINDOW_VULKAN as u32 |
SDL_WindowFlags::SDL_WINDOW_RESIZABLE as u32,
)
};
if handle != std::ptr::null_mut() {
if !handle.is_null() {
Ok(Self { hal: PhantomData, handle })
} else {
Err(unsafe { ErrSdl::new() })
@@ -44,22 +54,22 @@ impl<'a> Window<'a> {
) -> Result<Vec<*const c_char>, ErrSdl> {
let mut count = 0;
let res = unsafe {
sdl::SDL_Vulkan_GetInstanceExtensions(
SDL_Vulkan_GetInstanceExtensions(
self.handle,
&mut count,
std::ptr::null_mut(),
)
};
if res != sdl::SDL_bool::SDL_FALSE {
if res != SDL_bool::SDL_FALSE {
let mut names = vec![std::ptr::null(); count as usize];
let res = unsafe {
sdl::SDL_Vulkan_GetInstanceExtensions(
SDL_Vulkan_GetInstanceExtensions(
self.handle,
&mut count,
names.as_mut_ptr(),
)
};
if res != sdl::SDL_bool::SDL_FALSE {
if res != SDL_bool::SDL_FALSE {
Ok(names)
} else {
Err(unsafe { ErrSdl::new() })
@@ -75,13 +85,13 @@ impl<'a> Window<'a> {
) -> Result<vk::SurfaceKHR, ErrSdl> {
let mut surface = 0;
let res = unsafe {
sdl::SDL_Vulkan_CreateSurface(
SDL_Vulkan_CreateSurface(
self.handle,
(**instance).handle().as_raw() as usize,
&mut surface,
)
};
if res != sdl::SDL_bool::SDL_FALSE {
if res != SDL_bool::SDL_FALSE {
Ok(vk::SurfaceKHR::from_raw(surface))
} else {
Err(unsafe { ErrSdl::new() })
@@ -92,7 +102,7 @@ impl<'a> Window<'a> {
impl Drop for Window<'_> {
fn drop(&mut self) {
unsafe {
sdl::SDL_DestroyWindow(self.handle);
SDL_DestroyWindow(self.handle);
}
}
}


+ 3
- 2
source/main.rs View File

@@ -24,9 +24,9 @@ impl<'shd, 'lay, 'ren, 'dev, 'ins> Pipelines<'shd, 'lay, 'ren, 'dev, 'ins> {
}
}

const MAIN_VERT: &'static [u8] =
const MAIN_VERT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.vert.o"));
const MAIN_FRAG: &'static [u8] =
const MAIN_FRAG: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/main.frag.o"));

fn draw_frame<'fen, 'dev, 'ins>(
@@ -202,6 +202,7 @@ fn fallback_main(
for event in hal::EventIterator {
match event {
hal::Event::Quit => break 'main_loop,
hal::Event::Resize(_, _) => {},
}
}



+ 1
- 1
source/render/device.rs View File

@@ -38,7 +38,7 @@ fn enable_device_extensions(
phys_device: &PhysicalDevice,
extensions: &[*const c_char],
) -> Result<vk::DeviceCreateInfo, ErrDeviceCreate> {
if extensions.len() > 0 {
if !extensions.is_empty() {
let props = unsafe {
instance.enumerate_device_extension_properties(**phys_device)?
};


+ 0
- 1
source/render/image.rs View File

@@ -1,6 +1,5 @@
use crate::render::{Device, Swapchain};
use ash::{version::DeviceV1_0, vk};
use std::marker::PhantomData;

pub enum ImageOwner<'swpa, 'deva, 'insa, 'devb, 'insb> {
Swapchain(&'swpa Swapchain<'deva, 'insa>),


+ 1
- 1
source/render/instance.rs View File

@@ -31,7 +31,7 @@ fn enable_instance_layers(
layers: &[*const c_char],
entry: &ash::Entry,
) -> Result<vk::InstanceCreateInfo, ErrInstanceCreate> {
if layers.len() > 0 {
if !layers.is_empty() {
let props = entry.enumerate_instance_layer_properties()?;
ensure_properties(&props, layers, |x| x.layer_name.as_ptr())?;
}


+ 3
- 1
source/render/properties.rs View File

@@ -23,8 +23,10 @@ where
{
for name in names {
let name = unsafe { CStr::from_ptr(*name) };
let any =
props.iter().any(|x| unsafe { CStr::from_ptr(func(x)) == name });

if !props.iter().any(|x| unsafe { CStr::from_ptr(func(x)) == name }) {
if !any {
return Err(ErrNoProperty {
name: name.to_string_lossy().to_string(),
});


+ 2
- 2
source/util/log/color.rs View File

@@ -11,11 +11,11 @@ impl Color {
level: Level,
out: &mut impl WriteColor,
) -> std::io::Result<()> {
out.write(b"[")?;
out.write_all(b"[")?;
out.set_color(&level.color())?;
write!(out, "{:^8}", level.name())?;
out.reset()?;
out.write(b"]")?;
out.write_all(b"]")?;
Ok(())
}
}


Loading…
Cancel
Save