Skip to content

Commit 89f8a65

Browse files
authored
Implement background method (#1308)
* Implement background. * Install wayland deps.
1 parent 2232a0d commit 89f8a65

File tree

11 files changed

+165
-18
lines changed

11 files changed

+165
-18
lines changed

.github/workflows/build-gradle.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ jobs:
1717
with:
1818
java-version: '17'
1919
distribution: 'temurin'
20+
- name: Install Wayland Dependencies
21+
run: |
22+
sudo apt-get update
23+
sudo apt-get install -y libwayland-dev libxkbcommon-dev
2024
- name: Setup Gradle
2125
uses: gradle/actions/setup-gradle@v4
2226

@@ -66,6 +70,11 @@ jobs:
6670
java-version: '17'
6771
distribution: 'temurin'
6872
architecture: ${{ matrix.arch }}
73+
- name: Install Wayland Dependencies
74+
if: runner.os == 'Linux'
75+
run: |
76+
sudo apt-get update
77+
sudo apt-get install -y libwayland-dev libxkbcommon-dev
6978
- name: Setup Gradle
7079
uses: gradle/actions/setup-gradle@v4
7180

.github/workflows/pull_request-gradle.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ jobs:
1818
with:
1919
java-version: '17'
2020
distribution: 'temurin'
21+
- name: Install Wayland Dependencies
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install -y libwayland-dev libxkbcommon-dev
2125
- name: Setup Gradle
2226
uses: gradle/actions/setup-gradle@v4
2327

@@ -67,6 +71,11 @@ jobs:
6771
java-version: '17'
6872
distribution: 'temurin'
6973
architecture: ${{ matrix.arch }}
74+
- name: Install Wayland Dependencies
75+
if: runner.os == 'Linux'
76+
run: |
77+
sudo apt-get update
78+
sudo apt-get install -y libwayland-dev libxkbcommon-dev
7079
- name: Setup Gradle
7180
uses: gradle/actions/setup-gradle@v4
7281

core/src/processing/webgpu/PGraphicsWebGPU.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public void setSize(int w, int h) {
2626
}
2727
}
2828

29+
@Override
30+
public void beginDraw() {
31+
super.beginDraw();
32+
checkSettings();
33+
}
34+
2935
@Override
3036
public void endDraw() {
3137
super.endDraw();
@@ -35,7 +41,18 @@ public void endDraw() {
3541
@Override
3642
public void dispose() {
3743
super.dispose();
38-
44+
if (windowId != 0) {
45+
PWebGPU.destroySurface(windowId);
46+
windowId = 0;
47+
}
3948
PWebGPU.exit();
4049
}
50+
51+
@Override
52+
protected void backgroundImpl() {
53+
if (windowId == 0) {
54+
return;
55+
}
56+
PWebGPU.backgroundColor(windowId, backgroundR, backgroundG, backgroundB, backgroundA);
57+
}
4158
}

core/src/processing/webgpu/PWebGPU.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import processing.core.NativeLibrary;
44

5+
import java.lang.foreign.Arena;
56
import java.lang.foreign.MemorySegment;
67

78
import static java.lang.foreign.MemorySegment.NULL;
89
import static processing.ffi.processing_h.*;
10+
import processing.ffi.Color;
911

1012
/**
1113
* PWebGPU provides the native interface layer for libProcessing's WebGPU support.
@@ -47,6 +49,16 @@ public static long createSurface(long windowHandle, int width, int height, float
4749
return windowId;
4850
}
4951

52+
/**
53+
* Destroys a WebGPU surface.
54+
*
55+
* @param windowId The window ID returned from createSurface
56+
*/
57+
public static void destroySurface(long windowId) {
58+
processing_destroy_surface(windowId);
59+
checkError();
60+
}
61+
5062
/**
5163
* Updates a window's size.
5264
*
@@ -55,7 +67,7 @@ public static long createSurface(long windowHandle, int width, int height, float
5567
* @param height New physical window height in pixels
5668
*/
5769
public static void windowResized(long windowId, int width, int height) {
58-
processing_window_resized(windowId, width, height);
70+
processing_resize_surface(windowId, width, height);
5971
checkError();
6072
}
6173

@@ -75,6 +87,20 @@ public static void exit() {
7587
checkError();
7688
}
7789

90+
public static void backgroundColor(long windowId, float r, float g, float b, float a) {
91+
try (Arena arena = Arena.ofConfined()) {
92+
MemorySegment color = Color.allocate(arena);
93+
94+
Color.r(color, r);
95+
Color.g(color, g);
96+
Color.b(color, b);
97+
Color.a(color, a);
98+
99+
processing_background_color(windowId, color);
100+
checkError();
101+
}
102+
}
103+
78104
/**
79105
* Checks for errors from the native library and throws a PWebGPUException if an error occurred.
80106
*/

libProcessing/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libProcessing/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
[workspace]
22
resolver = "3"
33
members = ["ffi","renderer"]
4+
5+
[workspace.dependencies]
6+
bevy = { version = "0.17", no-default-features = true, features = [
7+
"bevy_render",
8+
"bevy_color",
9+
] }

libProcessing/ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ crate-type = ["cdylib"]
99

1010
[dependencies]
1111
renderer = { path = "../renderer" }
12+
bevy = { workspace = true }
1213

1314
[build-dependencies]
1415
cbindgen = "0.29"

libProcessing/ffi/src/color.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// A sRGB (?) color
2+
#[repr(C)]
3+
pub struct Color {
4+
pub r: f32,
5+
pub g: f32,
6+
pub b: f32,
7+
pub a: f32,
8+
}
9+
10+
impl From<Color> for bevy::color::Color {
11+
fn from(color: Color) -> Self {
12+
bevy::color::Color::srgba(color.r, color.g, color.b, color.a)
13+
}
14+
}

libProcessing/ffi/src/lib.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use crate::color::Color;
2+
use bevy::prelude::Entity;
3+
4+
mod color;
15
mod error;
26

37
/// Initialize libProcessing.
@@ -31,16 +35,41 @@ pub extern "C" fn processing_create_surface(
3135
.unwrap_or(0)
3236
}
3337

38+
/// Destroy the surface associated with the given window ID.
39+
///
40+
/// SAFETY:
41+
/// - Init and create_surface have been called.
42+
/// - window_id is a valid ID returned from create_surface.
43+
/// - This is called from the same thread as init.
44+
#[unsafe(no_mangle)]
45+
pub extern "C" fn processing_destroy_surface(window_id: u64) {
46+
error::clear_error();
47+
let window_entity = Entity::from_bits(window_id);
48+
error::check(|| renderer::destroy_surface(window_entity));
49+
}
50+
3451
/// Update window size when resized.
3552
///
3653
/// SAFETY:
3754
/// - Init and create_surface have been called.
3855
/// - window_id is a valid ID returned from create_surface.
3956
/// - This is called from the same thread as init.
4057
#[unsafe(no_mangle)]
41-
pub extern "C" fn processing_window_resized(window_id: u64, width: u32, height: u32) {
58+
pub extern "C" fn processing_resize_surface(window_id: u64, width: u32, height: u32) {
59+
error::clear_error();
60+
let window_entity = Entity::from_bits(window_id);
61+
error::check(|| renderer::resize_surface(window_entity, width, height));
62+
}
63+
64+
/// Set the background color for the given window.
65+
///
66+
/// SAFETY:
67+
/// - This is called from the same thread as init.
68+
#[unsafe(no_mangle)]
69+
pub extern "C" fn processing_background_color(window_id: u64, color: Color) {
4270
error::clear_error();
43-
error::check(|| renderer::window_resized(window_id, width, height));
71+
let window_entity = Entity::from_bits(window_id);
72+
error::check(|| renderer::background_color(window_entity, color.into()));
4473
}
4574

4675
/// Step the application forward.

libProcessing/renderer/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ edition = "2024"
66
[dependencies]
77
tracing = "0.1"
88
tracing-subscriber = "0.3"
9-
bevy = { version = "0.17", no-default-features = true, features = [
10-
"bevy_render"
11-
] }
9+
bevy = { workspace = true }
1210
thiserror = "2"
1311
raw-window-handle = "0.6"
1412

0 commit comments

Comments
 (0)