Update Files
This commit is contained in:
91
Kha/Backends/Java/kha/java/Font.hx
Normal file
91
Kha/Backends/Java/kha/java/Font.hx
Normal file
@ -0,0 +1,91 @@
|
||||
package kha.java;
|
||||
|
||||
import kha.FontStyle;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Font in JFont;
|
||||
|
||||
// @:classCode('
|
||||
// private static java.awt.image.BufferedImage testImage = new java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB);
|
||||
// private static java.awt.Graphics2D testGraphics;
|
||||
// public java.awt.Font font;
|
||||
// static {
|
||||
// testGraphics = testImage.createGraphics();
|
||||
// }
|
||||
// ')
|
||||
class Font implements Resource {
|
||||
static var testImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
|
||||
static var testGraphics: Graphics2D;
|
||||
|
||||
public var font: JFont;
|
||||
public var myName: String;
|
||||
public var myStyle: FontStyle;
|
||||
public var mySize: Float;
|
||||
|
||||
public function new(name: String, style: FontStyle, size: Float) {
|
||||
if (testGraphics == null)
|
||||
testGraphics = testImage.createGraphics();
|
||||
init(name, style, size);
|
||||
}
|
||||
|
||||
public var name(get, never): String;
|
||||
|
||||
function get_name(): String {
|
||||
return myName;
|
||||
}
|
||||
|
||||
public var style(get, never): FontStyle;
|
||||
|
||||
function get_style(): FontStyle {
|
||||
return myStyle;
|
||||
}
|
||||
|
||||
public var size(get, never): Float;
|
||||
|
||||
function get_size(): Float {
|
||||
return mySize;
|
||||
}
|
||||
|
||||
// @:functionCode('
|
||||
// font = new java.awt.Font(name, 0, (int)size);
|
||||
// myName = name;
|
||||
// myStyle = style;
|
||||
// mySize = size;
|
||||
// ')
|
||||
function init(name: String, style: FontStyle, size: Float) {
|
||||
font = new JFont(name, 0, Std.int(size));
|
||||
myName = name;
|
||||
myStyle = style;
|
||||
mySize = size;
|
||||
}
|
||||
|
||||
// @:functionCode('
|
||||
// return testGraphics.getFontMetrics(font).getHeight();
|
||||
// ')
|
||||
public function height(fontSize: Int): Float {
|
||||
return testGraphics.getFontMetrics(font).getHeight();
|
||||
}
|
||||
|
||||
// @:functionCode('
|
||||
// return testGraphics.getFontMetrics(font).stringWidth(str);
|
||||
// ')
|
||||
public function width(fontSize: Int, str: String): Float {
|
||||
return testGraphics.getFontMetrics(font).stringWidth(str);
|
||||
// return 0;
|
||||
}
|
||||
|
||||
// @:functionCode('
|
||||
// return testGraphics.getFontMetrics(font).getHeight() - testGraphics.getFontMetrics(font).getLeading();
|
||||
// ')
|
||||
public function baseline(fontSize: Int): Float {
|
||||
return testGraphics.getFontMetrics(font).getHeight() - testGraphics.getFontMetrics(font).getLeading();
|
||||
// return 0;
|
||||
}
|
||||
|
||||
// @:functionCode('
|
||||
// font = null;
|
||||
// ')
|
||||
public function unload(): Void {
|
||||
font = null;
|
||||
}
|
||||
}
|
9
Kha/Backends/Java/kha/java/Graphics.hx
Normal file
9
Kha/Backends/Java/kha/java/Graphics.hx
Normal file
@ -0,0 +1,9 @@
|
||||
package kha.java;
|
||||
|
||||
class Graphics {
|
||||
public function new() {}
|
||||
|
||||
public function maxTextureSize(): Int {
|
||||
return 4096;
|
||||
}
|
||||
}
|
120
Kha/Backends/Java/kha/java/Music.hx
Normal file
120
Kha/Backends/Java/kha/java/Music.hx
Normal file
@ -0,0 +1,120 @@
|
||||
package kha.java;
|
||||
|
||||
import java.lang.Runnable;
|
||||
|
||||
@:classCode('
|
||||
java.io.File file;
|
||||
javax.sound.sampled.AudioInputStream in;
|
||||
javax.sound.sampled.SourceDataLine line;
|
||||
int frameSize;
|
||||
byte[] buffer = new byte [32 * 1024]; // 32k is arbitrary
|
||||
Thread playThread;
|
||||
boolean playing;
|
||||
boolean notYetEOF;
|
||||
')
|
||||
class Music implements Runnable {
|
||||
var loop: Bool = false;
|
||||
|
||||
public function new(filename: String) {
|
||||
init(filename);
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
try {
|
||||
//instance = this;
|
||||
java.io.File f = new java.io.File(filename);
|
||||
file = f;
|
||||
in = javax.sound.sampled.AudioSystem.getAudioInputStream (f);
|
||||
javax.sound.sampled.AudioFormat format = in.getFormat();
|
||||
javax.sound.sampled.AudioFormat.Encoding formatEncoding = format.getEncoding();
|
||||
if (!(formatEncoding.equals(javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED) || formatEncoding.equals(javax.sound.sampled.AudioFormat.Encoding.PCM_UNSIGNED)))
|
||||
throw new javax.sound.sampled.UnsupportedAudioFileException(file.getName() + " is not PCM audio");
|
||||
//System.out.println ("got PCM format");
|
||||
frameSize = format.getFrameSize();
|
||||
javax.sound.sampled.DataLine.Info info = new javax.sound.sampled.DataLine.Info(javax.sound.sampled.SourceDataLine.class, format);
|
||||
//System.out.println ("got info");
|
||||
line = (javax.sound.sampled.SourceDataLine)javax.sound.sampled.AudioSystem.getLine(info);
|
||||
//System.out.println ("got line");
|
||||
line.open();
|
||||
//System.out.println ("opened line");
|
||||
playThread = new Thread(this);
|
||||
playing = false;
|
||||
notYetEOF = true;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
')
|
||||
function init(filename: String): Void {}
|
||||
|
||||
@:functionCode('
|
||||
int readPoint = 0;
|
||||
int bytesRead = 0;
|
||||
|
||||
try {
|
||||
while (playing) {
|
||||
while (notYetEOF) {
|
||||
if (playing) {
|
||||
bytesRead = in.read(buffer, readPoint, buffer.length - readPoint);
|
||||
if (bytesRead == -1) {
|
||||
notYetEOF = false;
|
||||
break;
|
||||
}
|
||||
// how many frames did we get,
|
||||
// and how many are left over?
|
||||
//int frames = bytesRead / frameSize;
|
||||
int leftover = bytesRead % frameSize;
|
||||
// send to line
|
||||
line.write(buffer, readPoint, bytesRead-leftover);
|
||||
// save the leftover bytes
|
||||
System.arraycopy(buffer, bytesRead, buffer, 0, leftover);
|
||||
readPoint = leftover;
|
||||
}
|
||||
else {
|
||||
// if not playing
|
||||
// Thread.yield();
|
||||
try { Thread.sleep (10);}
|
||||
catch (InterruptedException ie) {}
|
||||
}
|
||||
}
|
||||
//System.out.println ("reached eof");
|
||||
try {
|
||||
in = javax.sound.sampled.AudioSystem.getAudioInputStream(file);
|
||||
}
|
||||
catch (javax.sound.sampled.UnsupportedAudioFileException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (loop) notYetEOF = true;
|
||||
else playing = false;
|
||||
}
|
||||
line.drain();
|
||||
line.stop();
|
||||
}
|
||||
catch (java.io.IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
// line.close();
|
||||
}
|
||||
')
|
||||
public function run(): Void {}
|
||||
|
||||
@:functionCode('
|
||||
this.loop = loop;
|
||||
playing = true;
|
||||
if (!playThread.isAlive())
|
||||
playThread.start();
|
||||
line.start();
|
||||
')
|
||||
function play2(loop: Bool): Void {}
|
||||
|
||||
public function play(loop: Bool = false): Void {
|
||||
play2(loop);
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
playing = false;
|
||||
line.stop();
|
||||
')
|
||||
public function stop(): Void {}
|
||||
}
|
131
Kha/Backends/Java/kha/java/Painter.hx
Normal file
131
Kha/Backends/Java/kha/java/Painter.hx
Normal file
@ -0,0 +1,131 @@
|
||||
package kha.java;
|
||||
|
||||
import kha.Color;
|
||||
import kha.Font;
|
||||
import kha.Image;
|
||||
import kha.math.FastMatrix3;
|
||||
import kha.math.Matrix3;
|
||||
import kha.Rotation;
|
||||
import java.awt.Graphics2D;
|
||||
|
||||
@:classCode('
|
||||
public float tx = 0;
|
||||
public float ty = 0;
|
||||
|
||||
private static int round(double value) {
|
||||
return (int)Math.round(value);
|
||||
}
|
||||
')
|
||||
class Painter extends kha.graphics2.Graphics {
|
||||
public var graphics: Graphics2D;
|
||||
|
||||
// public var tx: Float = 0;
|
||||
// public var ty: Float = 0;
|
||||
var myColor: Color;
|
||||
var myFont: Font;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
// private static inline function round(value:Float):Int {
|
||||
// return Math.round(value);
|
||||
// }
|
||||
|
||||
@:functionCode('
|
||||
graphics.setBackground(new java.awt.Color(color));
|
||||
graphics.clearRect(0, 0, 2048, 2048);
|
||||
')
|
||||
function clear2(color: Int): Void {}
|
||||
|
||||
override public function clear(color: Color = null): Void {
|
||||
clear2(color != null ? color.value : Color.Black.value);
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
graphics.setRenderingHint(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
||||
')
|
||||
public function setRenderHint(): Void {}
|
||||
|
||||
@:functionCode('
|
||||
graphics.drawImage(img.image, round(tx + x), round(ty + y), null);
|
||||
')
|
||||
override public function drawImage(img: Image, x: Float, y: Float): Void {}
|
||||
|
||||
@:functionCode('
|
||||
graphics.drawImage(image.image, round(tx + dx), round(ty + dy), round(tx + dx + dw), round(ty + dy + dh), round(sx), round(sy), round(sx + sw), round(sy + sh), null);
|
||||
')
|
||||
override public function drawScaledSubImage(image: Image, sx: Float, sy: Float, sw: Float, sh: Float, dx: Float, dy: Float, dw: Float, dh: Float): Void {}
|
||||
|
||||
override function get_color(): kha.Color {
|
||||
return myColor;
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
graphics.setColor(new java.awt.Color(color));
|
||||
')
|
||||
function setColorInternal(color: kha.Color): Void {}
|
||||
|
||||
override function set_color(color: kha.Color): kha.Color {
|
||||
setColorInternal(color);
|
||||
return myColor = color;
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
java.awt.Stroke oldStroke = graphics.getStroke();
|
||||
graphics.setStroke(new java.awt.BasicStroke((float)strength));
|
||||
graphics.drawRect(round(tx + x), round(ty + y), round(width), round(height));
|
||||
graphics.setStroke(oldStroke);
|
||||
')
|
||||
function drawRect2(x: Float, y: Float, width: Float, height: Float, strength: Float): Void {}
|
||||
|
||||
override public function drawRect(x: Float, y: Float, width: Float, height: Float, strength: Float = 1.0): Void {
|
||||
drawRect2(x, y, width, height, strength);
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
graphics.fillRect(round(tx + x), round(ty + y), round(width), round(height));
|
||||
')
|
||||
override public function fillRect(x: Float, y: Float, width: Float, height: Float): Void {}
|
||||
|
||||
@:functionCode('
|
||||
graphics.setFont(((kha.java.Font)font).font);
|
||||
')
|
||||
function setFontInternal(font: Font): Void {}
|
||||
|
||||
override function get_font(): kha.Font {
|
||||
return myFont;
|
||||
}
|
||||
|
||||
override function set_font(font: kha.Font): kha.Font {
|
||||
setFontInternal(font);
|
||||
return myFont = font;
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
graphics.drawString(text, round(tx + x), round(ty + y));
|
||||
')
|
||||
override public function drawString(text: String, x: Float, y: Float): Void {}
|
||||
|
||||
@:functionCode('
|
||||
java.awt.Stroke oldStroke = graphics.getStroke();
|
||||
graphics.setStroke(new java.awt.BasicStroke((Float)strength));
|
||||
graphics.drawLine(round(tx + x1), round(ty + y1), round(tx + x2), round(ty + y2));
|
||||
graphics.setStroke(oldStroke);
|
||||
')
|
||||
override public function drawLine(x1: Float, y1: Float, x2: Float, y2: Float, strength: Float = 1.0): Void {}
|
||||
|
||||
@:functionCode('
|
||||
int[] xPoints = new int[]{round(tx + x1), round(tx + x2), round(tx + x3)};
|
||||
int[] yPoints = new int[]{round(ty + y1), round(ty + y2), round(ty + y3)};
|
||||
graphics.fillPolygon(xPoints, yPoints, 3);
|
||||
')
|
||||
override public function fillTriangle(x1: Float, y1: Float, x2: Float, y2: Float, x3: Float, y3: Float): Void {}
|
||||
|
||||
@:functionCode('
|
||||
graphics.setTransform(new java.awt.geom.AffineTransform(
|
||||
((Number)transformation._00).floatValue(), ((Number)transformation._01).floatValue(), ((Number)transformation._10).floatValue(),
|
||||
((Number)transformation._11).floatValue(), ((Number)transformation._20).floatValue(), ((Number)transformation._21).floatValue()));
|
||||
')
|
||||
override function setTransformation(transformation: FastMatrix3): Void {}
|
||||
}
|
161
Kha/Backends/Java/kha/java/Sound.hx
Normal file
161
Kha/Backends/Java/kha/java/Sound.hx
Normal file
@ -0,0 +1,161 @@
|
||||
package kha.java;
|
||||
|
||||
import kha.audio1.AudioChannel;
|
||||
|
||||
@:classCode('
|
||||
class SoundThread implements Runnable {
|
||||
private java.util.ArrayList<Sound> sounds = new java.util.ArrayList<Sound>();
|
||||
private java.util.Queue<Integer> queue = new java.util.ArrayDeque<Integer>();
|
||||
private java.util.ArrayDeque<Sound> soundqueue = new java.util.ArrayDeque<Sound>();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
for (;;) {
|
||||
boolean notempty;
|
||||
synchronized (this) {
|
||||
notempty = !queue.isEmpty();
|
||||
}
|
||||
while (notempty) {
|
||||
Sound sound;
|
||||
synchronized (this) {
|
||||
int index = queue.remove();
|
||||
sound = sounds.get(index);
|
||||
notempty = !queue.isEmpty();
|
||||
}
|
||||
if (soundqueue.contains(sound)) soundqueue.remove(sound);
|
||||
soundqueue.push(sound);
|
||||
if (!sound.isLoaded()) {
|
||||
if (soundqueue.size() < 30) {
|
||||
try {
|
||||
javax.sound.sampled.Clip clip = javax.sound.sampled.AudioSystem.getClip();
|
||||
sound.load(clip);
|
||||
}
|
||||
catch (javax.sound.sampled.LineUnavailableException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else {
|
||||
java.util.Iterator<Sound> it = soundqueue.descendingIterator();
|
||||
while (true) {
|
||||
Sound last = it.next();
|
||||
if (last.isLoaded()) {
|
||||
javax.sound.sampled.Clip clip = last.unloadit();
|
||||
sound.load(clip);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sound.realplay();
|
||||
}
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized int addSound(Sound sound) {
|
||||
sounds.add(sound);
|
||||
return sounds.size() - 1;
|
||||
}
|
||||
|
||||
public synchronized void play(int index) {
|
||||
queue.add(index);
|
||||
notify();
|
||||
}
|
||||
|
||||
public synchronized void stop(int index) {
|
||||
queue.remove(index);
|
||||
notify();
|
||||
}
|
||||
}
|
||||
|
||||
private static SoundThread thread;
|
||||
private javax.sound.sampled.Clip clip;
|
||||
|
||||
public javax.sound.sampled.Clip unloadit() {
|
||||
javax.sound.sampled.Clip clip = this.clip;
|
||||
this.clip = null;
|
||||
return clip;
|
||||
}
|
||||
|
||||
public void load(javax.sound.sampled.Clip clip) {
|
||||
this.clip = clip;
|
||||
javax.sound.sampled.AudioInputStream stream;
|
||||
try {
|
||||
stream = javax.sound.sampled.AudioSystem.getAudioInputStream(new java.io.File(filename));
|
||||
clip.close();
|
||||
clip.open(stream);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
')
|
||||
@:nativeGen
|
||||
@:keep
|
||||
@:native("kha.java.Sound")
|
||||
class Sound extends kha.Sound {
|
||||
var index: Int;
|
||||
var filename: String;
|
||||
|
||||
public function new(filename: String) {
|
||||
super();
|
||||
init(filename);
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
this.filename = filename;
|
||||
if (thread == null) {
|
||||
thread = new SoundThread();
|
||||
Thread realthread = new Thread(thread);
|
||||
realthread.start();
|
||||
}
|
||||
index = thread.addSound(this);
|
||||
')
|
||||
function init(filename: String) {}
|
||||
|
||||
@:functionCode('
|
||||
thread.play(index);
|
||||
return null;
|
||||
')
|
||||
public function play(): AudioChannel {
|
||||
return null;
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
if (clip.isRunning()) {
|
||||
clip.stop();
|
||||
}
|
||||
thread.stop(index);
|
||||
')
|
||||
public function stop(): Void {}
|
||||
|
||||
@:functionCode('
|
||||
return clip != null;
|
||||
')
|
||||
public function isLoaded(): Bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
@:functionCode('
|
||||
if (!clip.isRunning()) {
|
||||
clip.setFramePosition(0);
|
||||
clip.loop(0);
|
||||
}
|
||||
else clip.setFramePosition(0);
|
||||
')
|
||||
public function realplay(): Void {}
|
||||
|
||||
@:functionCode('
|
||||
return clip.isRunning();
|
||||
')
|
||||
public function isRunning(): Bool {
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user