Skip to content

Commit 5fac223

Browse files
committed
use explicit common namespace, set framerate hint
1 parent 276aa03 commit 5fac223

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

examples/gpu-basic-triangle/src/main.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use sdl3_sys::everything::*;
1010

1111
#[path = "../../gpu-pull-sprite-batch/src/common.rs"]
1212
mod common;
13-
use common::{dbg_sdl_error, deinit_gpu_window, init_gpu_window, load_shader};
1413

1514
struct AppState {
1615
window: *mut SDL_Window,
@@ -32,7 +31,7 @@ impl Drop for AppState {
3231
SDL_ReleaseGPUGraphicsPipeline(self.device, self.line_pipeline);
3332
}
3433

35-
deinit_gpu_window(self.device, self.window);
34+
common::deinit_gpu_window(self.device, self.window);
3635
}
3736
}
3837
}
@@ -43,22 +42,26 @@ unsafe impl Send for AppState {}
4342
impl AppState {
4443
fn app_init() -> AppResultWithState<Box<Mutex<Self>>> {
4544
unsafe {
45+
if !SDL_SetHint(SDL_HINT_MAIN_CALLBACK_RATE, c"60".as_ptr()) {
46+
println!("failed to set framerate hint");
47+
}
48+
4649
const TITLE: &CStr = c"Basic Triangle Example";
4750
let Some((window, device)) =
48-
init_gpu_window(TITLE.as_ptr(), SDL_WindowFlags::default())
51+
common::init_gpu_window(TITLE.as_ptr(), SDL_WindowFlags::default())
4952
else {
5053
return AppResultWithState::Failure(None);
5154
};
5255

53-
let vert_shader = load_shader(device, "RawTriangle.vert");
56+
let vert_shader = common::load_shader(device, "RawTriangle.vert");
5457
if vert_shader.is_null() {
55-
dbg_sdl_error("failed to load vert shader");
58+
common::dbg_sdl_error("failed to load vert shader");
5659
return AppResultWithState::Failure(None);
5760
}
5861

59-
let frag_shader = load_shader(device, "SolidColor.frag");
62+
let frag_shader = common::load_shader(device, "SolidColor.frag");
6063
if frag_shader.is_null() {
61-
dbg_sdl_error("failed to load frag shader");
64+
common::dbg_sdl_error("failed to load frag shader");
6265
return AppResultWithState::Failure(None);
6366
}
6467

@@ -82,14 +85,14 @@ impl AppState {
8285
pipeline_create_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_FILL;
8386
let fill_pipeline = SDL_CreateGPUGraphicsPipeline(device, &pipeline_create_info);
8487
if fill_pipeline.is_null() {
85-
dbg_sdl_error("failed to create fill pipeline");
88+
common::dbg_sdl_error("failed to create fill pipeline");
8689
return AppResultWithState::Failure(None);
8790
}
8891

8992
pipeline_create_info.rasterizer_state.fill_mode = SDL_GPU_FILLMODE_LINE;
9093
let line_pipeline = SDL_CreateGPUGraphicsPipeline(device, &pipeline_create_info);
9194
if line_pipeline.is_null() {
92-
dbg_sdl_error("failed to create line pipeline");
95+
common::dbg_sdl_error("failed to create line pipeline");
9396
return AppResultWithState::Failure(None);
9497
}
9598

@@ -119,7 +122,7 @@ impl AppState {
119122

120123
let command_buffer = SDL_AcquireGPUCommandBuffer(self.device);
121124
if command_buffer.is_null() {
122-
dbg_sdl_error("failed to acquire command buffer");
125+
common::dbg_sdl_error("failed to acquire command buffer");
123126
return AppResult::Failure;
124127
}
125128

@@ -131,7 +134,7 @@ impl AppState {
131134
null_mut(),
132135
null_mut(),
133136
) {
134-
dbg_sdl_error("failed to acquire swapchain texture");
137+
common::dbg_sdl_error("failed to acquire swapchain texture");
135138
return AppResult::Failure;
136139
}
137140

examples/gpu-pull-sprite-batch/src/common.rs

+8
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,11 @@ impl Matrix4x4 {
222222
fn get_content_dir() -> String {
223223
format!("{MANIFEST_DIR}/../../content")
224224
}
225+
226+
pub fn set_framerate_hint(fps: usize) {
227+
let hint_value = CString::new(fps.to_string()).unwrap();
228+
let was_set = unsafe { SDL_SetHint(SDL_HINT_MAIN_CALLBACK_RATE, hint_value.as_ptr()) };
229+
if !was_set {
230+
println!("failed to set framerate hint");
231+
}
232+
}

examples/gpu-pull-sprite-batch/src/main.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use sdl3_main::{app_impl, AppResult};
1111
use sdl3_sys::everything::*;
1212

1313
mod common;
14-
use common::{dbg_sdl_error, deinit_gpu_window, init_gpu_window, load_bmp, load_shader, Matrix4x4};
1514

1615
const SPRITE_COUNT: u32 = 8192;
1716
const GPU_SPRITE_BUFFER_SIZE: u32 = SPRITE_COUNT * std::mem::size_of::<GPUSprite>() as u32;
@@ -50,7 +49,7 @@ impl Drop for AppState {
5049
SDL_ReleaseGPUBuffer(self.device, self.sprite_data_buffer);
5150
}
5251

53-
deinit_gpu_window(self.device, self.window);
52+
common::deinit_gpu_window(self.device, self.window);
5453
}
5554
}
5655
}
@@ -141,9 +140,11 @@ impl CPUSprite {
141140
impl AppState {
142141
fn app_init() -> Option<Box<Mutex<AppState>>> {
143142
unsafe {
143+
common::set_framerate_hint(60);
144+
144145
const TITLE: &CStr = c"Pull Sprite Batch Example";
145146
let Some((window, device)) =
146-
init_gpu_window(TITLE.as_ptr(), SDL_WindowFlags::default())
147+
common::init_gpu_window(TITLE.as_ptr(), SDL_WindowFlags::default())
147148
else {
148149
return None;
149150
};
@@ -167,15 +168,15 @@ impl AppState {
167168
present_mode,
168169
);
169170

170-
let vert_shader = load_shader(device, "PullSpriteBatch.vert");
171+
let vert_shader = common::load_shader(device, "PullSpriteBatch.vert");
171172
if vert_shader.is_null() {
172-
dbg_sdl_error("failed to load vert shader");
173+
common::dbg_sdl_error("failed to load vert shader");
173174
return None;
174175
}
175176

176-
let frag_shader = load_shader(device, "TexturedQuadColor.frag");
177+
let frag_shader = common::load_shader(device, "TexturedQuadColor.frag");
177178
if frag_shader.is_null() {
178-
dbg_sdl_error("failed to load frag shader");
179+
common::dbg_sdl_error("failed to load frag shader");
179180
return None;
180181
}
181182

@@ -210,9 +211,9 @@ impl AppState {
210211
SDL_ReleaseGPUShader(device, vert_shader);
211212
SDL_ReleaseGPUShader(device, frag_shader);
212213

213-
let image_ptr = load_bmp("ravioli_atlas.bmp");
214+
let image_ptr = common::load_bmp("ravioli_atlas.bmp");
214215
if image_ptr.is_null() {
215-
dbg_sdl_error("failed to load image");
216+
common::dbg_sdl_error("failed to load image");
216217
return None;
217218
}
218219
let image = &mut *image_ptr;
@@ -364,7 +365,7 @@ fn draw_sprites(app: &mut AppState) -> AppResult {
364365
unsafe {
365366
let command_buffer = SDL_AcquireGPUCommandBuffer(app.device);
366367
if command_buffer.is_null() {
367-
dbg_sdl_error("AcquireGPUCommandBuffer failed");
368+
common::dbg_sdl_error("AcquireGPUCommandBuffer failed");
368369
return AppResult::Failure;
369370
}
370371

@@ -376,7 +377,7 @@ fn draw_sprites(app: &mut AppState) -> AppResult {
376377
null_mut(),
377378
null_mut(),
378379
) {
379-
dbg_sdl_error("WaitAndAcquireGPUSwapchainTexture failed");
380+
common::dbg_sdl_error("WaitAndAcquireGPUSwapchainTexture failed");
380381
return AppResult::Failure;
381382
}
382383

@@ -386,7 +387,7 @@ fn draw_sprites(app: &mut AppState) -> AppResult {
386387
SDL_MapGPUTransferBuffer(app.device, app.sprite_data_transfer_buffer, true)
387388
as *mut GPUSprite;
388389
if data_ptr.is_null() {
389-
dbg_sdl_error("failed to map gpu transfer buffer");
390+
common::dbg_sdl_error("failed to map gpu transfer buffer");
390391
return AppResult::Failure;
391392
}
392393

@@ -445,8 +446,11 @@ fn draw_sprites(app: &mut AppState) -> AppResult {
445446
},
446447
1,
447448
);
449+
450+
use common::Matrix4x4;
448451
const CAMERA_MATRIX: Matrix4x4 =
449452
Matrix4x4::create_orthographic_off_center(0.0, 640.0, 480.0, 0.0, 0.0, -1.0);
453+
450454
SDL_PushGPUVertexUniformData(
451455
command_buffer,
452456
0,

0 commit comments

Comments
 (0)