Skip to content

Commit

Permalink
SWT example
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Feb 1, 2021
1 parent 4d06dfa commit 8df8c5e
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Backends:

- [x] Bitmap
- [x] OpenGL
- [x] Metal (example pending)
- [ ] Metal (example pending)
- [ ] Vulkan

APIs:
Expand Down Expand Up @@ -189,10 +189,11 @@ If Skija is missing a documentation for a particular method or class, check the

Finally, [LWJGL demo app](/examples/lwjgl) has examples of most of the APIs that are currently implemented.

## Talks
## Resources

- Overview [youtube.com/watch?v=t1X-Oln1u24](https://www.youtube.com/watch?v=t1X-Oln1u24)
- Coding game in Skija and Clojure [youtube.com/watch?v=KES-lKTq-3M](https://www.youtube.com/watch?v=KES-lKTq-3M)
- SWT and Skija on Raspberry Pi [spket.com/blog/native-gui-app-with-swt-and-skija-on-raspberry-pi](https://www.spket.com/blog/native-gui-app-with-swt-and-skija-on-raspberry-pi/)

## Building Skija from scratch

Expand Down
7 changes: 7 additions & 0 deletions examples/swt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A simple SWT example, inspired by [spket/skiawt](https://github.com/spket/skiawt).

How to run:

1. Build Skija with `../../script/build.sh`
2. Download and install [Babashka](https://github.com/babashka/babashka)
3. Run `./script/run.clj`
73 changes: 73 additions & 0 deletions examples/swt/script/run.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#! /usr/bin/env bb

(require '[babashka.process :as ps])

(def home (System/getProperty "user.home"))

(def working-dir (-> (io/file *file*) (.getCanonicalFile) (.getParentFile) (.getParent)))

(def os
(condp re-find (str/lower-case (System/getProperty "os.name"))
#"(mac|darwin)" :macos
#"windows" :windows
#"(unix|linux)" :linux))

(defn fetch-maven
([group name version]
(fetch-maven group name version {:repo "https://repo1.maven.org/maven2"}))
([group name version {:keys [repo classifier]}]
(let [path (str (str/replace group "." "/") "/" name "/" version "/" (str name "-" version (when classifier (str "-" classifier)) ".jar"))
file (io/file home ".m2" "repository" path)
url (str repo "/" path)]
(when-not (.exists file)
(println "Downloading" (.getName file))
(.mkdirs (.getParentFile file))
(io/copy (:body @(org.httpkit.client/get url {:as :stream})) file))
file)))

(def classpath
(->>
[(io/file working-dir ".." ".." "native" "build")
(io/file working-dir ".." ".." "shared" "target" "classes")
(fetch-maven "org.eclipse.platform"
(case os
:macos "org.eclipse.swt.cocoa.macosx.x86_64"
:windows "org.eclipse.swt.win32.win32.x86_64"
:linux "org.eclipse.swt.gtk.linux.x86_64")
"3.115.100")]
(map #(.getPath (.getCanonicalFile %)))
(str/join (System/getProperty "path.separator"))))

(def sources
(->> (io/file working-dir "src")
(file-seq)
(filter #(str/ends-with? (.getName %) ".java"))
(mapv #(.getPath %))))

;; compile
(->
(ps/process
(concat
["javac"
"-encoding" "UTF8"
"--release" "11"
"-cp" classpath
"-d" "target/classes"]
sources)
{:dir working-dir})
(ps/check))

;; run
@(ps/process
(concat
["java" "-cp" (str "target/classes" (System/getProperty "path.separator") classpath)]
(when (= :macos os)
["-XstartOnFirstThread"])
#_["-Djava.awt.headless=true"
"-ea"
"-esa"
"-Dskija.logLevel=DEBUG"]
["org.jetbrains.skija.examples.swt.Main"])
{:dir working-dir
:out *out*
:err *out*})
102 changes: 102 additions & 0 deletions examples/swt/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jetbrains.skija.examples.swt;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.opengl.*;
import org.eclipse.swt.widgets.*;
import org.jetbrains.skija.*;
import org.jetbrains.skija.Canvas;

public class Main {
public static void main(String[] args) throws Exception {
new Main().run();
}

private Display display;
private GLCanvas glCanvas;
private DirectContext context;
private Surface surface;
private BackendRenderTarget renderTarget;

protected void run() throws Exception {
display = new Display();
Shell shell = new Shell(display);
shell.setText("Skija SWT Demo");
shell.setLayout(new FillLayout());

GLData data = new GLData();
data.doubleBuffer = true;

glCanvas = new GLCanvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE, data);
glCanvas.setCurrent();
context = DirectContext.makeGL();

Listener listener = event -> {
switch (event.type) {
case SWT.Paint:
onPaint(event);
break;
case SWT.Resize:
onResize(event);
break;
case SWT.Dispose:
onDispose();
break;
}
};
glCanvas.addListener(SWT.Paint, listener);
glCanvas.addListener(SWT.Resize, listener);
shell.addListener(SWT.Dispose, listener);

shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();
}

protected void release() {
if (surface != null) {
surface.close();
surface = null;
}
if (renderTarget != null) {
renderTarget.close();
renderTarget = null;
}
}

protected void onResize(Event event) {
release();
Rectangle rect = glCanvas.getClientArea();
renderTarget = BackendRenderTarget.makeGL(rect.width, rect.height, /*samples*/0, /*stencil*/8, /*fbid*/0, FramebufferFormat.GR_GL_RGBA8);
surface = Surface.makeFromBackendRenderTarget(context, renderTarget, SurfaceOrigin.BOTTOM_LEFT, SurfaceColorFormat.RGBA_8888, ColorSpace.getDisplayP3());
}

protected void onPaint(Event event) {
if (surface == null)
return;
Canvas canvas = surface.getCanvas();
paint(canvas);
context.flush();
glCanvas.swapBuffers();
}

protected void onDispose() {
release();
context.close();
}

protected void paint(Canvas canvas) {
canvas.clear(0xFFFFFFFF);
canvas.save();
canvas.translate(100, 100);
canvas.rotate(System.currentTimeMillis() % 1000 / 1000f * 360f);
canvas.drawRect(Rect.makeLTRB(-100, -100, 100, 100), new Paint().setColor(0xFFCC3333));
canvas.restore();
}
}

0 comments on commit 8df8c5e

Please sign in to comment.