Skip to content

Commit

Permalink
Merge branch 'waywardmonkeys-use-cstr-literals'
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Mar 30, 2024
2 parents be5e41b + d01406e commit f44517b
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 161 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ extern "C" fn cleanup() {
}

fn main() {
let window_title = b"clear\0".as_ptr() as _;

sapp::run(&sapp::Desc {
init_cb: Some(init),
cleanup_cb: Some(cleanup),
frame_cb: Some(frame),
window_title,
window_title: c"clear".as_ptr(),
width: 800,
height: 600,
sample_count: 4,
Expand All @@ -77,14 +75,16 @@ fn main() {
## Dependencies
The Rust compiler and cargo can be installed using [rustup](https://rustup.rs/)

The same dependencies apply as with sokol normally for each platform
The same dependencies apply as with sokol normally for each platform.

## Building with cargo
Cargo will compile and link the sokol headers automatically during compilation thanks to the buildscript `build.rs`

## Examples
Not all examples have been translated to Rust yet, but you can check the ones that have been in the `examples` directory.

Rust 1.77 or later is required to build the examples.

You can compile all examples using the following command:
```console
cargo build --all-targets
Expand Down
2 changes: 1 addition & 1 deletion examples/audio/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn main() {
width: 640,
height: 480,
icon: sapp::IconDesc { sokol_default: true, ..Default::default() },
window_title: b"saudio\0".as_ptr() as _,
window_title: c"saudio".as_ptr(),
logger: sapp::Logger { func: Some(slog::slog_func), ..Default::default() },
..Default::default()
});
Expand Down
4 changes: 1 addition & 3 deletions examples/blend/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,14 @@ extern "C" fn cleanup() {
}

fn main() {
let window_title = b"blend\0".as_ptr() as _;

sapp::run(&sapp::Desc {
init_cb: Some(init),
frame_cb: Some(frame),
cleanup_cb: Some(cleanup),
width: 800,
height: 600,
sample_count: 4,
window_title,
window_title: c"blend".as_ptr(),
logger: sapp::Logger { func: Some(sokol::log::slog_func), ..Default::default() },
icon: sapp::IconDesc { sokol_default: true, ..Default::default() },
..Default::default()
Expand Down
60 changes: 30 additions & 30 deletions examples/blend/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,39 +660,39 @@ pub fn bg_shader_desc(backend: sg::Backend) -> sg::ShaderDesc {
let mut desc = sg::ShaderDesc::new();
match backend {
sg::Backend::Glcore33 => {
desc.attrs[0].name = b"position\0".as_ptr() as *const _;
desc.attrs[0].name = c"position".as_ptr();
desc.vs.source = &VS_BG_SOURCE_GLSL330 as *const _ as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.entry = c"main".as_ptr();
desc.fs.source = &FS_BG_SOURCE_GLSL330 as *const _ as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.fs.entry = c"main".as_ptr();
desc.fs.uniform_blocks[0].size = 16;
desc.fs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.uniform_blocks[0].uniforms[0].name = b"bg_fs_params\0".as_ptr() as *const _;
desc.fs.uniform_blocks[0].uniforms[0].name = c"bg_fs_params".as_ptr();
desc.fs.uniform_blocks[0].uniforms[0]._type = sg::UniformType::Float4;
desc.fs.uniform_blocks[0].uniforms[0].array_count = 1;
desc.label = b"bg_shader\0".as_ptr() as *const _;
desc.label = c"bg_shader".as_ptr();
},
sg::Backend::D3d11 => {
desc.attrs[0].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[0].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[0].sem_index = 0;
desc.vs.source = &VS_BG_SOURCE_HLSL4 as *const _ as *const _;
desc.vs.d3d11_target = b"vs_4_0\0".as_ptr() as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.d3d11_target = c"vs_4_0".as_ptr();
desc.vs.entry = c"main".as_ptr();
desc.fs.source = &FS_BG_SOURCE_HLSL4 as *const _ as *const _;
desc.fs.d3d11_target = b"ps_4_0\0".as_ptr() as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.fs.d3d11_target = c"ps_4_0".as_ptr();
desc.fs.entry = c"main".as_ptr();
desc.fs.uniform_blocks[0].size = 16;
desc.fs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.label = b"bg_shader\0".as_ptr() as *const _;
desc.label = c"bg_shader".as_ptr();
},
sg::Backend::MetalMacos => {
desc.vs.source = &VS_BG_SOURCE_METAL_MACOS as *const _ as *const _;
desc.vs.entry = b"main0\0".as_ptr() as *const _;
desc.vs.entry = c"main0".as_ptr();
desc.fs.source = &FS_BG_SOURCE_METAL_MACOS as *const _ as *const _;
desc.fs.entry = b"main0\0".as_ptr() as *const _;
desc.fs.entry = c"main0".as_ptr();
desc.fs.uniform_blocks[0].size = 16;
desc.fs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.label = b"bg_shader\0".as_ptr() as *const _;
desc.label = c"bg_shader".as_ptr();
},
_ => {},
}
Expand All @@ -702,42 +702,42 @@ pub fn quad_shader_desc(backend: sg::Backend) -> sg::ShaderDesc {
let mut desc = sg::ShaderDesc::new();
match backend {
sg::Backend::Glcore33 => {
desc.attrs[0].name = b"position\0".as_ptr() as *const _;
desc.attrs[1].name = b"color0\0".as_ptr() as *const _;
desc.attrs[0].name = c"position".as_ptr();
desc.attrs[1].name = c"color0".as_ptr();
desc.vs.source = &VS_QUAD_SOURCE_GLSL330 as *const _ as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.entry = c"main".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.vs.uniform_blocks[0].uniforms[0].name = b"quad_vs_params\0".as_ptr() as *const _;
desc.vs.uniform_blocks[0].uniforms[0].name = c"quad_vs_params".as_ptr();
desc.vs.uniform_blocks[0].uniforms[0]._type = sg::UniformType::Float4;
desc.vs.uniform_blocks[0].uniforms[0].array_count = 4;
desc.fs.source = &FS_QUAD_SOURCE_GLSL330 as *const _ as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.label = b"quad_shader\0".as_ptr() as *const _;
desc.fs.entry = c"main".as_ptr();
desc.label = c"quad_shader".as_ptr();
},
sg::Backend::D3d11 => {
desc.attrs[0].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[0].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[0].sem_index = 0;
desc.attrs[1].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[1].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[1].sem_index = 1;
desc.vs.source = &VS_QUAD_SOURCE_HLSL4 as *const _ as *const _;
desc.vs.d3d11_target = b"vs_4_0\0".as_ptr() as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.d3d11_target = c"vs_4_0".as_ptr();
desc.vs.entry = c"main".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.source = &FS_QUAD_SOURCE_HLSL4 as *const _ as *const _;
desc.fs.d3d11_target = b"ps_4_0\0".as_ptr() as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.label = b"quad_shader\0".as_ptr() as *const _;
desc.fs.d3d11_target = c"ps_4_0".as_ptr();
desc.fs.entry = c"main".as_ptr();
desc.label = c"quad_shader".as_ptr();
},
sg::Backend::MetalMacos => {
desc.vs.source = &VS_QUAD_SOURCE_METAL_MACOS as *const _ as *const _;
desc.vs.entry = b"main0\0".as_ptr() as *const _;
desc.vs.entry = c"main0".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.source = &FS_QUAD_SOURCE_METAL_MACOS as *const _ as *const _;
desc.fs.entry = b"main0\0".as_ptr() as *const _;
desc.label = b"quad_shader\0".as_ptr() as *const _;
desc.fs.entry = c"main0".as_ptr();
desc.label = c"quad_shader".as_ptr();
},
_ => {},
}
Expand Down
4 changes: 1 addition & 3 deletions examples/clear/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,11 @@ extern "C" fn cleanup() {
}

fn main() {
let window_title = b"clear\0".as_ptr() as _;

sapp::run(&sapp::Desc {
init_cb: Some(init),
cleanup_cb: Some(cleanup),
frame_cb: Some(frame),
window_title,
window_title: c"clear".as_ptr(),
width: 800,
height: 600,
sample_count: 4,
Expand Down
4 changes: 1 addition & 3 deletions examples/cube/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,14 @@ extern "C" fn cleanup() {
}

fn main() {
let window_title = b"cube\0".as_ptr() as _;

sapp::run(&sapp::Desc {
init_cb: Some(init),
frame_cb: Some(frame),
cleanup_cb: Some(cleanup),
width: 800,
height: 600,
sample_count: 4,
window_title,
window_title: c"cube".as_ptr(),
logger: sapp::Logger { func: Some(sokol::log::slog_func), ..Default::default() },
icon: sapp::IconDesc { sokol_default: true, ..Default::default() },

Expand Down
32 changes: 16 additions & 16 deletions examples/cube/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,42 +342,42 @@ pub fn cube_shader_desc(backend: sg::Backend) -> sg::ShaderDesc {
let mut desc = sg::ShaderDesc::new();
match backend {
sg::Backend::Glcore33 => {
desc.attrs[0].name = b"position\0".as_ptr() as *const _;
desc.attrs[1].name = b"color0\0".as_ptr() as *const _;
desc.attrs[0].name = c"position".as_ptr();
desc.attrs[1].name = c"color0".as_ptr();
desc.vs.source = &VS_SOURCE_GLSL330 as *const _ as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.entry = c"main".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.vs.uniform_blocks[0].uniforms[0].name = b"vs_params\0".as_ptr() as *const _;
desc.vs.uniform_blocks[0].uniforms[0].name = c"vs_params".as_ptr();
desc.vs.uniform_blocks[0].uniforms[0]._type = sg::UniformType::Float4;
desc.vs.uniform_blocks[0].uniforms[0].array_count = 4;
desc.fs.source = &FS_SOURCE_GLSL330 as *const _ as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.label = b"cube_shader\0".as_ptr() as *const _;
desc.fs.entry = c"main".as_ptr();
desc.label = c"cube_shader".as_ptr();
},
sg::Backend::D3d11 => {
desc.attrs[0].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[0].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[0].sem_index = 0;
desc.attrs[1].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[1].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[1].sem_index = 1;
desc.vs.source = &VS_SOURCE_HLSL4 as *const _ as *const _;
desc.vs.d3d11_target = b"vs_4_0\0".as_ptr() as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.d3d11_target = c"vs_4_0".as_ptr();
desc.vs.entry = c"main".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.source = &FS_SOURCE_HLSL4 as *const _ as *const _;
desc.fs.d3d11_target = b"ps_4_0\0".as_ptr() as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.label = b"cube_shader\0".as_ptr() as *const _;
desc.fs.d3d11_target = c"ps_4_0".as_ptr();
desc.fs.entry = c"main".as_ptr();
desc.label = c"cube_shader".as_ptr();
},
sg::Backend::MetalMacos => {
desc.vs.source = &VS_SOURCE_METAL_MACOS as *const _ as *const _;
desc.vs.entry = b"main0\0".as_ptr() as *const _;
desc.vs.entry = c"main0".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.source = &FS_SOURCE_METAL_MACOS as *const _ as *const _;
desc.fs.entry = b"main0\0".as_ptr() as *const _;
desc.label = b"cube_shader\0".as_ptr() as *const _;
desc.fs.entry = c"main0".as_ptr();
desc.label = c"cube_shader".as_ptr();
},
_ => {},
}
Expand Down
4 changes: 1 addition & 3 deletions examples/debugtext/debugtext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ extern "C" fn cleanup() {
}

fn main() {
let window_title = b"debugtext\0".as_ptr() as *const _;

sapp::run(&sapp::Desc {
init_cb: Some(init),
frame_cb: Some(frame),
cleanup_cb: Some(cleanup),
width: 1024,
height: 600,
window_title,
window_title: c"debugtext".as_ptr(),
logger: sapp::Logger { func: Some(sokol::log::slog_func), ..Default::default() },
icon: sapp::IconDesc { sokol_default: true, ..Default::default() },
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion examples/instancing/instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn main() {
width: 800,
height: 600,
sample_count: 4,
window_title: b"instancing\0".as_ptr() as _,
window_title: c"instancing".as_ptr(),
icon: sapp::IconDesc { sokol_default: true, ..Default::default() },
logger: sapp::Logger { func: Some(slog::slog_func), ..Default::default() },
..Default::default()
Expand Down
36 changes: 18 additions & 18 deletions examples/instancing/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,45 +362,45 @@ pub fn instancing_shader_desc(backend: sg::Backend) -> sg::ShaderDesc {
let mut desc = sg::ShaderDesc::new();
match backend {
sg::Backend::Glcore33 => {
desc.attrs[0].name = b"pos\0".as_ptr() as *const _;
desc.attrs[1].name = b"color0\0".as_ptr() as *const _;
desc.attrs[2].name = b"inst_pos\0".as_ptr() as *const _;
desc.attrs[0].name = c"pos".as_ptr();
desc.attrs[1].name = c"color0".as_ptr();
desc.attrs[2].name = c"inst_pos".as_ptr();
desc.vs.source = &VS_SOURCE_GLSL330 as *const _ as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.entry = c"main".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.vs.uniform_blocks[0].uniforms[0].name = b"vs_params\0".as_ptr() as *const _;
desc.vs.uniform_blocks[0].uniforms[0].name = c"vs_params".as_ptr();
desc.vs.uniform_blocks[0].uniforms[0]._type = sg::UniformType::Float4;
desc.vs.uniform_blocks[0].uniforms[0].array_count = 4;
desc.fs.source = &FS_SOURCE_GLSL330 as *const _ as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.label = b"instancing_shader\0".as_ptr() as *const _;
desc.fs.entry = c"main".as_ptr();
desc.label = c"instancing_shader".as_ptr();
},
sg::Backend::D3d11 => {
desc.attrs[0].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[0].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[0].sem_index = 0;
desc.attrs[1].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[1].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[1].sem_index = 1;
desc.attrs[2].sem_name = b"TEXCOORD\0".as_ptr() as *const _;
desc.attrs[2].sem_name = c"TEXCOORD".as_ptr();
desc.attrs[2].sem_index = 2;
desc.vs.source = &VS_SOURCE_HLSL4 as *const _ as *const _;
desc.vs.d3d11_target = b"vs_4_0\0".as_ptr() as *const _;
desc.vs.entry = b"main\0".as_ptr() as *const _;
desc.vs.d3d11_target = c"vs_4_0".as_ptr();
desc.vs.entry = c"main".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.source = &FS_SOURCE_HLSL4 as *const _ as *const _;
desc.fs.d3d11_target = b"ps_4_0\0".as_ptr() as *const _;
desc.fs.entry = b"main\0".as_ptr() as *const _;
desc.label = b"instancing_shader\0".as_ptr() as *const _;
desc.fs.d3d11_target = c"ps_4_0".as_ptr();
desc.fs.entry = c"main".as_ptr();
desc.label = c"instancing_shader".as_ptr();
},
sg::Backend::MetalMacos => {
desc.vs.source = &VS_SOURCE_METAL_MACOS as *const _ as *const _;
desc.vs.entry = b"main0\0".as_ptr() as *const _;
desc.vs.entry = c"main0".as_ptr();
desc.vs.uniform_blocks[0].size = 64;
desc.vs.uniform_blocks[0].layout = sg::UniformLayout::Std140;
desc.fs.source = &FS_SOURCE_METAL_MACOS as *const _ as *const _;
desc.fs.entry = b"main0\0".as_ptr() as *const _;
desc.label = b"instancing_shader\0".as_ptr() as *const _;
desc.fs.entry = c"main0".as_ptr();
desc.label = c"instancing_shader".as_ptr();
},
_ => {},
}
Expand Down
4 changes: 1 addition & 3 deletions examples/mrt/mrt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ extern "C" fn cleanup() {
}

fn main() {
let window_title = b"mrt\0".as_ptr() as _;

sapp::run(&sapp::Desc {
init_cb: Some(init),
frame_cb: Some(frame),
Expand All @@ -352,7 +350,7 @@ fn main() {
width: 800,
height: 600,
sample_count: 1,
window_title,
window_title: c"mrt".as_ptr(),
logger: sapp::Logger { func: Some(sokol::log::slog_func), ..Default::default() },
icon: sapp::IconDesc { sokol_default: true, ..Default::default() },

Expand Down
Loading

0 comments on commit f44517b

Please sign in to comment.