Skip to content

Commit 1a9e821

Browse files
committed
reduce scope of unsafe
1 parent ec47fa1 commit 1a9e821

File tree

4 files changed

+179
-163
lines changed

4 files changed

+179
-163
lines changed

examples/tetris/src/audio.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,35 @@ pub const MAX_SAMPLES: usize = 65472;
1515
/// # Return Value
1616
///
1717
/// `(restlen, start_pos)`
18-
pub unsafe fn process_audio_loop(channel: i32, mut start_pos: usize, mut restlen: i32) -> (i32, usize) {
19-
if (start_pos+MAX_SAMPLES*2) < TETRIS_SONG.len() {
20-
if restlen == 0 {
18+
pub fn process_audio_loop(channel: i32, mut start_pos: usize, mut restlen: i32) -> (i32, usize) {
19+
unsafe {
20+
if (start_pos+MAX_SAMPLES*2) < TETRIS_SONG.len() {
21+
if restlen == 0 {
22+
sys::sceAudioOutput(
23+
channel,
24+
MAX_VOL,
25+
TETRIS_SONG.as_ptr().add(start_pos) as *mut _
26+
);
27+
start_pos += MAX_SAMPLES*2;
28+
}
29+
} else {
30+
let remainder: i32 = (((TETRIS_SONG.len() % (MAX_SAMPLES*2)/2)+63) & !63) as i32;
31+
if restlen == 0 {
32+
sys::sceAudioSetChannelDataLen(channel, remainder);
2133
sys::sceAudioOutput(
2234
channel,
2335
MAX_VOL,
2436
TETRIS_SONG.as_ptr().add(start_pos) as *mut _
2537
);
26-
start_pos += MAX_SAMPLES*2;
38+
start_pos += (remainder*2) as usize;
39+
}
40+
if start_pos >= TETRIS_SONG.len() {
41+
start_pos = 0;
42+
sys::sceAudioSetChannelDataLen(channel, MAX_SAMPLES as i32);
43+
}
2744
}
28-
} else {
29-
let remainder: i32 = (((TETRIS_SONG.len() % (MAX_SAMPLES*2)/2)+63) & !63) as i32;
30-
if restlen == 0 {
31-
sys::sceAudioSetChannelDataLen(channel, remainder);
32-
sys::sceAudioOutput(
33-
channel,
34-
MAX_VOL,
35-
TETRIS_SONG.as_ptr().add(start_pos) as *mut _
36-
);
37-
start_pos += (remainder*2) as usize;
38-
}
39-
if start_pos >= TETRIS_SONG.len() {
40-
start_pos = 0;
41-
sys::sceAudioSetChannelDataLen(channel, MAX_SAMPLES as i32);
42-
}
43-
}
4445

45-
restlen = sys::sceAudioGetChannelRestLen(channel);
46-
(restlen, start_pos)
46+
restlen = sys::sceAudioGetChannelRestLen(channel);
47+
(restlen, start_pos)
48+
}
4749
}

examples/tetris/src/game.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,29 @@ impl Game {
5959
let mut pad_data = SceCtrlData::default();
6060
unsafe {
6161
sys::sceCtrlReadBufferPositive(&mut pad_data, 1);
62-
if self.last_input.bits() == pad_data.buttons.bits() {
63-
// no change in input, and I don't feel like doing held down buttons
64-
return;
65-
}
66-
if pad_data.buttons.contains(CtrlButtons::LEFT) && !self.last_input.contains(CtrlButtons::LEFT) {
67-
self.attempt_move(-1, 0);
68-
}
69-
if pad_data.buttons.contains(CtrlButtons::RIGHT) && !self.last_input.contains(CtrlButtons::RIGHT) {
70-
self.attempt_move(1, 0);
71-
}
72-
if pad_data.buttons.contains(CtrlButtons::DOWN) && !self.last_input.contains(CtrlButtons::DOWN) {
73-
self.drop();
74-
self.current_shape.lock_to_gameboard(&mut self.board);
75-
self.shape_placed = true;
76-
}
77-
if pad_data.buttons.contains(CtrlButtons::CROSS) && !self.last_input.contains(CtrlButtons::CROSS) {
78-
self.attempt_rotate_ccw();
79-
}
80-
if pad_data.buttons.contains(CtrlButtons::CIRCLE) && !self.last_input.contains(CtrlButtons::CIRCLE) {
81-
self.attempt_rotate_cw();
82-
}
83-
self.last_input = pad_data.buttons;
8462
}
63+
if self.last_input.bits() == pad_data.buttons.bits() {
64+
// no change in input, and I don't feel like doing held down buttons
65+
return;
66+
}
67+
if pad_data.buttons.contains(CtrlButtons::LEFT) && !self.last_input.contains(CtrlButtons::LEFT) {
68+
self.attempt_move(-1, 0);
69+
}
70+
if pad_data.buttons.contains(CtrlButtons::RIGHT) && !self.last_input.contains(CtrlButtons::RIGHT) {
71+
self.attempt_move(1, 0);
72+
}
73+
if pad_data.buttons.contains(CtrlButtons::DOWN) && !self.last_input.contains(CtrlButtons::DOWN) {
74+
self.drop();
75+
self.current_shape.lock_to_gameboard(&mut self.board);
76+
self.shape_placed = true;
77+
}
78+
if pad_data.buttons.contains(CtrlButtons::CROSS) && !self.last_input.contains(CtrlButtons::CROSS) {
79+
self.attempt_rotate_ccw();
80+
}
81+
if pad_data.buttons.contains(CtrlButtons::CIRCLE) && !self.last_input.contains(CtrlButtons::CIRCLE) {
82+
self.attempt_rotate_cw();
83+
}
84+
self.last_input = pad_data.buttons;
8585
}
8686

8787
/// Called once per loop of the game, does all the biz.
@@ -181,12 +181,10 @@ impl Game {
181181
(*vertex_buffer)[402..410].copy_from_slice(&self.current_shape.as_vertices());
182182
(*vertex_buffer)[410..418].copy_from_slice(&self.next_shape.as_vertices());
183183

184-
unsafe {
185-
graphics::draw_vertices(vertex_buffer, texture_buffer, BLOCK_SIZE, BLOCK_SIZE, 0.75, 0.75);
186-
let score_string = alloc::format!("Score: {}", self.score);
187-
graphics::draw_text_at(327, 40, 0xffff_ffff, score_string.as_str());
188-
graphics::draw_text_at(327, 60, 0xffff_ffff, "Next Shape:");
189-
}
184+
graphics::draw_vertices(vertex_buffer, texture_buffer, BLOCK_SIZE, BLOCK_SIZE, 0.75, 0.75);
185+
let score_string = alloc::format!("Score: {}", self.score);
186+
graphics::draw_text_at(327, 40, 0xffff_ffff, score_string.as_str());
187+
graphics::draw_text_at(327, 60, 0xffff_ffff, "Next Shape:");
190188
}
191189

192190
/// Attempts to add to the `current_shape` position, returns true if successful.

examples/tetris/src/graphics/mod.rs

Lines changed: 80 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,58 @@ static mut LIST: Align16<[u32; 0x40000]> = Align16([0; 0x40000]);
2626
/// # Parameters
2727
///
2828
/// - `allocator`: A reference to a `SimpleVramAllocator`.
29-
pub unsafe fn setup(allocator: &mut psp::vram_alloc::SimpleVramAllocator) {
30-
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
31-
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
32-
33-
sys::sceGumLoadIdentity();
34-
sys::sceGuInit();
35-
36-
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
37-
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0 as _, BUF_WIDTH as i32);
38-
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1 as _, BUF_WIDTH as i32);
39-
sys::sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
40-
sys::sceGuViewport(2048, 2048, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
41-
sys::sceGuScissor(0, 0, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
42-
sys::sceGuEnable(GuState::ScissorTest);
43-
sys::sceGuEnable(GuState::Texture2D);
44-
45-
sys::sceGuTexMode(TexturePixelFormat::Psm8888, 0, 0, 0);
46-
sys::sceGuTexFunc(TextureEffect::Modulate, TextureColorComponent::Rgb);
47-
sys::sceGuTexWrap(GuTexWrapMode::Repeat, GuTexWrapMode::Repeat);
48-
49-
sys::sceGuEnable(GuState::Blend);
50-
sys::sceGuBlendFunc(BlendOp::Add, BlendFactor::SrcAlpha, BlendFactor::OneMinusSrcAlpha, 0, 0);
51-
sys::sceGuAlphaFunc(AlphaFunc::Greater, 0, 0xff);
52-
53-
sys::sceGumMatrixMode(MatrixMode::View);
54-
sys::sceGumLoadIdentity();
55-
56-
sys::sceGumMatrixMode(MatrixMode::Projection);
57-
sys::sceGumLoadIdentity();
58-
sys::sceGumOrtho(0.0,480.0,272.0,0.0,-30.0,30.0);
59-
60-
sys::sceDisplayWaitVblankStart();
61-
sys::sceGuFinish();
62-
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
63-
sys::sceGuDisplay(true);
29+
pub fn setup(allocator: &mut psp::vram_alloc::SimpleVramAllocator) {
30+
unsafe {
31+
let fbp0 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
32+
let fbp1 = allocator.alloc_texture_pixels(BUF_WIDTH, SCREEN_HEIGHT, TexturePixelFormat::Psm8888).as_mut_ptr_from_zero();
33+
34+
sys::sceGumLoadIdentity();
35+
sys::sceGuInit();
36+
37+
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
38+
sys::sceGuDrawBuffer(DisplayPixelFormat::Psm8888, fbp0 as _, BUF_WIDTH as i32);
39+
sys::sceGuDispBuffer(SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32, fbp1 as _, BUF_WIDTH as i32);
40+
sys::sceGuOffset(2048 - (SCREEN_WIDTH / 2), 2048 - (SCREEN_HEIGHT / 2));
41+
sys::sceGuViewport(2048, 2048, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
42+
sys::sceGuScissor(0, 0, SCREEN_WIDTH as i32, SCREEN_HEIGHT as i32);
43+
sys::sceGuEnable(GuState::ScissorTest);
44+
sys::sceGuEnable(GuState::Texture2D);
45+
46+
sys::sceGuTexMode(TexturePixelFormat::Psm8888, 0, 0, 0);
47+
sys::sceGuTexFunc(TextureEffect::Modulate, TextureColorComponent::Rgb);
48+
sys::sceGuTexWrap(GuTexWrapMode::Repeat, GuTexWrapMode::Repeat);
49+
50+
sys::sceGuEnable(GuState::Blend);
51+
sys::sceGuBlendFunc(BlendOp::Add, BlendFactor::SrcAlpha, BlendFactor::OneMinusSrcAlpha, 0, 0);
52+
sys::sceGuAlphaFunc(AlphaFunc::Greater, 0, 0xff);
53+
54+
sys::sceGumMatrixMode(MatrixMode::View);
55+
sys::sceGumLoadIdentity();
56+
57+
sys::sceGumMatrixMode(MatrixMode::Projection);
58+
sys::sceGumLoadIdentity();
59+
sys::sceGumOrtho(0.0,480.0,272.0,0.0,-30.0,30.0);
60+
61+
sys::sceDisplayWaitVblankStart();
62+
sys::sceGuFinish();
63+
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
64+
sys::sceGuDisplay(true);
65+
}
6466
}
6567

6668
/// Clear the screen a particular colour.
6769
///
6870
/// # Parameters
6971
///
7072
/// - `color`: The colour to clear with, in big-endian ABGR, little endian RGBA.
71-
pub unsafe fn clear_color(color: u32) {
72-
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
73-
sys::sceGuClearColor(color);
74-
sys::sceGuClear(ClearBuffer::COLOR_BUFFER_BIT | ClearBuffer::FAST_CLEAR_BIT);
75-
sys::sceGuFinish();
76-
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
73+
pub fn clear_color(color: u32) {
74+
unsafe {
75+
sys::sceGuStart(GuContextType::Direct, &mut LIST.0 as *mut [u32; 0x40000] as *mut _);
76+
sys::sceGuClearColor(color);
77+
sys::sceGuClear(ClearBuffer::COLOR_BUFFER_BIT | ClearBuffer::FAST_CLEAR_BIT);
78+
sys::sceGuFinish();
79+
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
80+
}
7781
}
7882

7983
/// Draw vertices to the screen.
@@ -87,36 +91,38 @@ pub unsafe fn clear_color(color: u32) {
8791
/// - `texture_height`: Height of texture, must be power of 2.
8892
/// - `scale_x`: Horizontal scale factor.
8993
/// - `scale_y`: Vertical scale factor.
90-
pub unsafe fn draw_vertices(
94+
pub fn draw_vertices(
9195
vertices: &[Align4<Vertex>],
9296
texture: &[u8],
9397
texture_width: u32,
9498
texture_height: u32,
9599
scale_x: f32,
96100
scale_y: f32,
97101
) {
98-
sys::sceGuStart(GuContextType::Direct, LIST.0.as_mut_ptr() as *mut _);
99-
100-
sys::sceGumMatrixMode(MatrixMode::Model);
101-
sys::sceGumLoadIdentity();
102-
sys::sceGumScale(&ScePspFVector3 { x: scale_x, y: scale_y, z: 1.0 });
103-
104-
// setup texture
105-
sys::sceGuTexImage(MipmapLevel::None, texture_width as i32, texture_height as i32, texture_width as i32, (*texture).as_ptr() as _);
106-
sys::sceGuTexScale(1.0/texture_width as f32, 1.0/texture_height as f32);
107-
108-
sys::sceKernelDcacheWritebackInvalidateAll();
109-
110-
// draw
111-
sys::sceGumDrawArray(
112-
GuPrimitive::Sprites,
113-
VertexType::TEXTURE_32BITF | VertexType::COLOR_8888 | VertexType::VERTEX_32BITF | VertexType::TRANSFORM_3D,
114-
(*vertices).len() as i32,
115-
ptr::null_mut(),
116-
(*vertices).as_ptr() as _,
117-
);
118-
sys::sceGuFinish();
119-
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
102+
unsafe {
103+
sys::sceGuStart(GuContextType::Direct, LIST.0.as_mut_ptr() as *mut _);
104+
105+
sys::sceGumMatrixMode(MatrixMode::Model);
106+
sys::sceGumLoadIdentity();
107+
sys::sceGumScale(&ScePspFVector3 { x: scale_x, y: scale_y, z: 1.0 });
108+
109+
// setup texture
110+
sys::sceGuTexImage(MipmapLevel::None, texture_width as i32, texture_height as i32, texture_width as i32, (*texture).as_ptr() as _);
111+
sys::sceGuTexScale(1.0/texture_width as f32, 1.0/texture_height as f32);
112+
113+
sys::sceKernelDcacheWritebackInvalidateAll();
114+
115+
// draw
116+
sys::sceGumDrawArray(
117+
GuPrimitive::Sprites,
118+
VertexType::TEXTURE_32BITF | VertexType::COLOR_8888 | VertexType::VERTEX_32BITF | VertexType::TRANSFORM_3D,
119+
(*vertices).len() as i32,
120+
ptr::null_mut(),
121+
(*vertices).as_ptr() as _,
122+
);
123+
sys::sceGuFinish();
124+
sys::sceGuSync(GuSyncMode::Finish, GuSyncBehavior::Wait);
125+
}
120126
}
121127

122128
/// Draws text at a given point on the screen in a given colour.
@@ -127,14 +133,18 @@ pub unsafe fn draw_vertices(
127133
/// - `y`: vertical position
128134
/// - `color`: Colour of text, in big-endian ABGR, little-endian RGBA.
129135
/// - `text`: ASCII text as an &str.
130-
pub unsafe fn draw_text_at(x: i32, y: i32, color: u32, text: &str) {
131-
sys::sceGuDebugPrint(x, y, color, (text.to_string() + "\0").as_bytes().as_ptr());
132-
sys::sceGuDebugFlush();
136+
pub fn draw_text_at(x: i32, y: i32, color: u32, text: &str) {
137+
unsafe {
138+
sys::sceGuDebugPrint(x, y, color, (text.to_string() + "\0").as_bytes().as_ptr());
139+
sys::sceGuDebugFlush();
140+
}
133141
}
134142

135143
/// Finishes drawing by waiting for VBlank and swapping the Draw and Display buffer
136144
/// pointers.
137-
pub unsafe fn finish_frame() {
138-
sys::sceDisplayWaitVblankStart();
139-
sys::sceGuSwapBuffers();
145+
pub fn finish_frame() {
146+
unsafe {
147+
sys::sceDisplayWaitVblankStart();
148+
sys::sceGuSwapBuffers();
149+
}
140150
}

0 commit comments

Comments
 (0)