-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
185 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |