-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix some sdl unsound usage and allowed the ppu layer windows to be cl…
…osed First of all allow the ppu layer windows to be closed by managing them in the main thread Also have been calling SDL_PumpEvents in the incorrect thread + managed the windows not in the video subsystem initializing thread.
- Loading branch information
Showing
7 changed files
with
151 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
use core::convert::TryInto; | ||
|
||
const VRAM_SIZE:usize = 0x4000; | ||
const VRAM_BANK_SIZE:usize = 0x2000; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,49 @@ | ||
use sdl2::sys::*; | ||
use magenboy_core::{keypad::{joypad::{Joypad, NUM_OF_KEYS}, joypad_provider::JoypadProvider, button::Button}, utils::create_array}; | ||
use magenboy_core::keypad::{joypad::{Joypad, NUM_OF_KEYS}, joypad_provider::JoypadProvider}; | ||
use magenboy_common::joypad_menu::MenuJoypadProvider; | ||
use super::utils::get_sdl_error_message; | ||
|
||
|
||
pub struct SdlJoypadProvider{ | ||
keyborad_state: [*const u8;NUM_OF_KEYS], | ||
mapping: [SDL_Scancode; NUM_OF_KEYS] | ||
} | ||
|
||
impl SdlJoypadProvider{ | ||
pub fn new<F:Fn(&Button)->SDL_Scancode>(mapper:F)->Self{ | ||
let keyboard_ptr = unsafe{SDL_GetKeyboardState(std::ptr::null_mut())}; | ||
let mut counter:u8 = 0; | ||
let init_lambda = ||{ | ||
let button:Button = unsafe{std::mem::transmute(counter)}; | ||
let result = unsafe{keyboard_ptr.offset(mapper(&button) as isize)}; | ||
counter += 1; | ||
return result; | ||
}; | ||
let state:[*const u8; NUM_OF_KEYS] = create_array(init_lambda); | ||
|
||
return Self{keyborad_state: state} | ||
pub fn new(mapping: [SDL_Scancode; NUM_OF_KEYS])->Self{ | ||
Self{mapping} | ||
} | ||
} | ||
|
||
impl JoypadProvider for SdlJoypadProvider{ | ||
// Events are pumped from the main thread (the thread that initializes SDL) | ||
// Its unsound to pump them from other threads | ||
fn provide(&mut self, joypad:&mut Joypad) { | ||
unsafe{ | ||
SDL_PumpEvents(); | ||
|
||
let state = SDL_GetKeyboardState(std::ptr::null_mut()); | ||
for i in 0..NUM_OF_KEYS{ | ||
joypad.buttons[i] = *self.keyborad_state[i] != 0; | ||
joypad.buttons[i] = *state.add(self.mapping[i] as usize) != 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl MenuJoypadProvider for SdlJoypadProvider{ | ||
fn poll(&mut self, mut joypad:&mut Joypad) { | ||
fn poll(&mut self, joypad:&mut Joypad) { | ||
joypad.buttons.fill(false); | ||
unsafe{ | ||
let mut event = std::mem::MaybeUninit::<SDL_Event>::uninit(); | ||
loop{ | ||
let mut event = std::mem::MaybeUninit::<SDL_Event>::uninit(); | ||
if SDL_WaitEvent(event.as_mut_ptr()) == 0{ | ||
std::panic!("SDL_Error: {}", get_sdl_error_message()); | ||
} | ||
let event = event.assume_init(); | ||
if event.type_ == SDL_EventType::SDL_KEYDOWN as u32 || event.type_ == SDL_EventType::SDL_KEYUP as u32 { | ||
if event.type_ == SDL_EventType::SDL_KEYUP as u32{ | ||
if let Some(index) = self.mapping.iter().position(|key|*key == event.key.keysym.scancode){ | ||
joypad.buttons[index] = true; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
self.provide(&mut joypad); | ||
} | ||
} |
Oops, something went wrong.