Skip to content

Commit

Permalink
fix: build in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Tengu712 committed Jun 7, 2023
1 parent f9123f7 commit 1098f36
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sstar"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
license = "CC0-1.0"
description = "A small frontend framework for 2D game."
Expand Down
76 changes: 58 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,70 @@

A small frontend framework for game development. This framework is supported on Windows and Linux.

## Preparation

Install dependencies:

* Linux
* X11
* vulkan
* Windows
* user32
* xinput
* vulkan-1

Write shaders:

```c
/* vertex shader */
#version 450
layout(push_constant) uniform PushConstant {
vec4 scl;
vec4 rot;
vec4 trs;
vec4 col;
vec4 uv;
ivec4 param;
} constant;
layout(binding=0) uniform UniformBuffer {
mat4 view;
mat4 perse;
mat4 ortho;
};
layout(location=0) in vec3 in_pos;
layout(location=1) in vec2 in_uv;
/* omit outputs */
void main() {
/* omit */
}
```
```c
/* fragment shader */
#version 450
layout(binding=1) uniform sampler2D diffuse_map;
/* omit inputs */
/* omit outputs */
void main() {
/* omit */
}
```

Compile shaders to `shader.vert.spv` and `shader.frag.spv` at runtime root directory:

```
$ glslc -o <runtime-root>/shader.vert.spv <vertex-shader>
$ glslc -o <runtime-root>/shader.frag.spv <fragment-shader>
```

## Example

Here is an example that creates a window and clears it default color:

```rust
use sstar::{
vulkan::*,
window::*,
};
use sstar::{vulkan::*, window::*};

fn main() {
let mut window_app = WindowApp::new("Sample Program\0", 640, 480);
let mut window_app = WindowApp::new("Sample Program", 640, 480);
let vulkan_app = VulkanApp::new(&window_app, 10);
while window_app.do_events() {
vulkan_app.render(None, &[]).unwrap();
Expand All @@ -25,20 +77,8 @@ fn main() {
}
```

## Dependencies

Following dependencies are required:

* Linux
* X11
* vulkan
* Windows
* user32
* xinput
* vulkan-1

## License

This software (shooting-star) is published under the CC0 public domain.
However, some dependencies may be linked when building an app using this software.
See [LICENSE-ALL](https://github.com/Tengu712/shooting-star/LICENSE-ALL) in detail.
See [LICENSE-ALL](./LICENSE-ALL) in detail.
13 changes: 5 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,13 @@ fn to_regx(s: &str) -> String {
fn linux() {
println!("cargo:rustc-link-lib=X11");
println!("cargo:rustc-link-lib=vulkan");
cc::Build::new()
.file("./src/tpl.c")
.include("./src")
.compile("libtpl.a");
}

#[cfg(target_os = "windows")]
fn windows() {
println!("cargo:rustc-link-lib=user32");
println!("cargo:rustc-link-lib=Xinput");
println!("cargo:rustc-link-lib=vulkan-1");
cc::Build::new()
.file("./src/tpl.c")
.include("./src")
.compile("./tpl.lib");
}

fn main() {
Expand All @@ -65,6 +57,11 @@ fn main() {
.write_to_file(out_path.join("bindings.rs"))
.expect("failed to write tpl.rs");

cc::Build::new()
.file("./src/tpl.c")
.include("./src")
.compile("tpl");

println!("cargo:rustc-link-lib=tpl");
#[cfg(target_os = "linux")]
linux();
Expand Down
2 changes: 1 addition & 1 deletion examples/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn check(p: Result<(), String>) {

fn main() {
// create sstar objects
let mut window_app = WindowApp::new("Sample Program\0", 640, 480);
let mut window_app = WindowApp::new("Sample Program", 640, 480);
let mut vulkan_app = VulkanApp::new(&window_app, 10);

// create and load an image texture
Expand Down
2 changes: 1 addition & 1 deletion examples/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ layout(location=1) in vec4 in_col;
layout(location=0) out vec4 out_col;

void main() {
out_col = texture(diffuse_map, in_uv);
out_col = texture(diffuse_map, in_uv) * in_col;
}
2 changes: 1 addition & 1 deletion examples/shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout(push_constant) uniform PushConstant {
vec4 trs;
vec4 col;
vec4 uv;
int param;
ivec4 param;
} constant;

layout(binding=0) uniform UniformBuffer {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use sstar::{vulkan::*, window::*};

fn main() {
let mut window_app = WindowApp::new("Sample Program\0", 640, 480);
let mut window_app = WindowApp::new("Sample Program", 640, 480);
let vulkan_app = VulkanApp::new(&window_app, 10);
while window_app.do_events() {
vulkan_app.render(None, &[]).unwrap();
Expand Down
21 changes: 0 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
//! Shooting Star is a front engine for a simple 2D game.
//!
//! # Example
//!
//! Here is an example that creates a window and clears it default color:
//!
//! ```rust
//! use sstar::{
//! vulkan::*,
//! window::*,
//! };
//!
//! fn main() {
//! let mut window_app = WindowApp::new("Sample Program\0", 640, 480);
//! let vulkan_app = VulkanApp::new(&window_app, 10);
//! while window_app.do_events() {
//! vulkan_app.render(None, &[]).unwrap();
//! }
//! vulkan_app.terminate();
//! window_app.terminate();
//! }
//! ```

pub mod bitmap;
pub mod log;
Expand Down
4 changes: 2 additions & 2 deletions src/vulkan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct PushConstant {
pub trs: [f32; 4],
pub col: [f32; 4],
pub uv: [f32; 4],
pub param: i32,
pub param: [i32; 4],
}
impl Default for PushConstant {
fn default() -> Self {
Expand All @@ -92,7 +92,7 @@ impl Default for PushConstant {
trs: [0.0; 4],
col: [1.0; 4],
uv: [0.0, 0.0, 1.0, 1.0],
param: 0,
param: [0; 4],
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/window/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ impl WindowApp {
};

// change the window title
let c_title = title.bytes().map(|c| c as c_char).collect::<Vec<c_char>>();
let mut c_title = title.bytes().map(|c| c as c_char).collect::<Vec<c_char>>();
c_title.push(0);
let c_title = c_title.as_ptr();
unsafe { XStoreName(display, window, c_title) };

Expand Down
4 changes: 3 additions & 1 deletion src/window/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ impl WindowApp {
unsafe { AdjustWindowRect(&mut rect, style, 0) };

// create a window
let title_utf16 = title.encode_utf16().collect::<Vec<u16>>().as_ptr();
let mut title_utf16 = title.encode_utf16().collect::<Vec<u16>>();
title_utf16.push(0);
let title_utf16 = title_utf16.as_ptr();
let window = unsafe {
CreateWindowExW(
0,
Expand Down

0 comments on commit 1098f36

Please sign in to comment.