Skip to content

Commit c3005f9

Browse files
committed
add experimental LWJGL support
1 parent 8724871 commit c3005f9

File tree

10 files changed

+80
-7
lines changed

10 files changed

+80
-7
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "static/cheerpj-natives"]
2+
path = static/cheerpj-natives
3+
url = https://github.com/leaningtech/cheerpj-natives.git

src/app.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ declare global {
1818
}
1919
// interface Platform {}
2020
}
21+
22+
interface Window {
23+
lwjglCanvasElement: HTMLCanvasElement;
24+
}
2125
}
2226

2327
export {};

src/lib/CheerpJ.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
}
2929
3030
await cheerpjInit({
31-
status: 'none'
31+
status: 'none',
32+
javaProperties: ['java.library.path=/app/cheerpj-natives/natives']
3233
});
3334
cheerpjCreateDisplay(-1, -1, display);
3435
dispatch('ready');

src/lib/repl/Output.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
export let console: HTMLPreElement;
55
export let display: HTMLElement;
6+
export let lwjglCanvas: HTMLCanvasElement;
67
export let showLink: boolean;
78
</script>
89

@@ -26,7 +27,10 @@
2627
</section>
2728
<section class="flex flex-col">
2829
<div class="p-3 text-stone-500 text-sm select-none">Result</div>
29-
<div class="grow" bind:this={display} />
30+
<div class="grow relative" bind:this={display}>
31+
<canvas bind:this={lwjglCanvas} class="absolute inset-0 w-full h-full" />
32+
<!-- #cheerpjDisplay will be inserted here -->
33+
</div>
3034
</section>
3135
</div>
3236

src/lib/repl/sidebar/Examples.svelte

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@
2222
]
2323
},
2424
{
25-
title: 'Hello world with Swing',
25+
title: 'GUI with Swing',
2626
files: [
2727
{
2828
path: 'Main.java',
2929
content: await fetchExampleFile('hello-world-swing/Main.java')
3030
}
3131
]
32-
}
32+
},
33+
/*{
34+
title: '3D with LWJGL',
35+
files: [
36+
{
37+
path: 'Main.java',
38+
content: await fetchExampleFile('lwjgl/Main.java')
39+
}
40+
]
41+
},*/
3342
];
3443
});
3544
</script>

src/routes/[[id]]/output/+page.svelte

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
4646
let consoleEl: HTMLPreElement;
4747
let display: HTMLElement;
48+
let lwjglCanvas: HTMLCanvasElement;
49+
50+
$: if (lwjglCanvas) window.lwjglCanvasElement = lwjglCanvas;
4851
4952
async function compileAndRun() {
5053
if (!browser) return;
@@ -53,10 +56,12 @@
5356
5457
consoleEl.innerHTML = '';
5558
59+
const classPath = '/app/tools.jar:/app/lwjgl-2.9.0.jar:/app/lwjgl_util-2.9.0.jar:/files/';
60+
5661
const sourceFiles = $files.map((file) => '/str/' + file.path);
5762
const code = await cheerpjRunMain(
5863
'com.sun.tools.javac.Main',
59-
'/app/tools.jar:/files/',
64+
classPath,
6065
...sourceFiles,
6166
'-d',
6267
'/files/',
@@ -70,7 +75,7 @@
7075
}
7176
7277
consoleEl.innerHTML = '';
73-
cheerpjRunMain(deriveMainClass($files[0]), '/app/tools.jar:/files/');
78+
cheerpjRunMain(deriveMainClass($files[0]), classPath);
7479
loading = false;
7580
window.top?.postMessage({ action: 'running', compileLog }, window.location.origin);
7681
}
@@ -94,7 +99,7 @@
9499
</div>
95100

96101
<div class="flex flex-col w-screen h-screen overflow-hidden" class:hidden={loading}>
97-
<Output bind:console={consoleEl} bind:display showLink={!isTop && isShared} />
102+
<Output bind:console={consoleEl} bind:display bind:lwjglCanvas showLink={!isTop && isShared} />
98103
</div>
99104

100105
<CheerpJ on:ready={ready} {display} />

static/cheerpj-natives

Submodule cheerpj-natives added at 1b43167

static/examples/lwjgl/Main.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Example using LWJGL 2.9.0
2+
// Warning: experimental!
3+
4+
import org.lwjgl.LWJGLException;
5+
import org.lwjgl.opengl.Display;
6+
import org.lwjgl.opengl.DisplayMode;
7+
import org.lwjgl.opengl.GL11;
8+
9+
public class Main {
10+
public static void main(String[] args) {
11+
try {
12+
Display.setDisplayMode(new DisplayMode(800, 600));
13+
Display.setTitle("Triangle Example");
14+
Display.create();
15+
} catch (LWJGLException e) {
16+
e.printStackTrace();
17+
System.exit(1);
18+
}
19+
20+
// Initialize OpenGL
21+
GL11.glMatrixMode(GL11.GL_PROJECTION);
22+
GL11.glLoadIdentity();
23+
GL11.glOrtho(0, 800, 0, 600, 1, -1);
24+
GL11.glMatrixMode(GL11.GL_MODELVIEW);
25+
26+
while (!Display.isCloseRequested()) {
27+
// Clear the screen
28+
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
29+
30+
// Draw a triangle
31+
GL11.glBegin(GL11.GL_TRIANGLES);
32+
GL11.glColor3f(1.0f, 0.0f, 0.0f); // Red
33+
GL11.glVertex2f(400, 100);
34+
GL11.glColor3f(0.0f, 1.0f, 0.0f); // Green
35+
GL11.glVertex2f(100, 500);
36+
GL11.glColor3f(0.0f, 0.0f, 1.0f); // Blue
37+
GL11.glVertex2f(700, 500);
38+
GL11.glEnd();
39+
40+
Display.update();
41+
}
42+
43+
Display.destroy();
44+
}
45+
46+
}

static/lwjgl-2.9.0.jar

971 KB
Binary file not shown.

static/lwjgl_util-2.9.0.jar

169 KB
Binary file not shown.

0 commit comments

Comments
 (0)