Update Files

This commit is contained in:
2025-01-22 16:18:30 +01:00
parent ed4603cf95
commit a36294b518
16718 changed files with 2960346 additions and 0 deletions

158
Kha/Backends/Krom/Krom.hx Normal file
View File

@ -0,0 +1,158 @@
extern class Krom {
static inline var KROM_API: Int = 6;
static function clear(flags: Int, color: Int, depth: Float, stencil: Int): Void;
static function createVertexShader(data: haxe.io.BytesData, name: String): Dynamic;
static function createVertexShaderFromSource(source: String): Dynamic;
static function createFragmentShader(data: haxe.io.BytesData, name: String): Dynamic;
static function createFragmentShaderFromSource(source: String): Dynamic;
static function createGeometryShader(data: haxe.io.BytesData, name: String): Dynamic;
static function createTessellationControlShader(data: haxe.io.BytesData, name: String): Dynamic;
static function createTessellationEvaluationShader(data: haxe.io.BytesData, name: String): Dynamic;
static function deleteShader(shader: Dynamic): Dynamic;
static function createPipeline(): Dynamic;
static function deletePipeline(pipeline: Dynamic): Dynamic;
static function compilePipeline(pipeline: Dynamic, structure0: Dynamic, structure1: Dynamic, structure2: Dynamic, structure3: Dynamic, length: Int,
vertexShader: Dynamic, fragmentShader: Dynamic, geometryShader: Dynamic, tessellationControlShader: Dynamic, tessellationEvaluationShader: Dynamic,
state: Dynamic): Void;
static function setPipeline(pipeline: Dynamic): Void;
static function getConstantLocation(pipeline: Dynamic, name: String): Dynamic;
static function getTextureUnit(pipeline: Dynamic, name: String): Dynamic;
static function setTexture(stage: kha.graphics4.TextureUnit, texture: Dynamic): Void;
static function setRenderTarget(stage: kha.graphics4.TextureUnit, renderTarget: Dynamic): Void;
static function setTextureDepth(unit: kha.graphics4.TextureUnit, texture: Dynamic): Void;
static function setImageTexture(stage: kha.graphics4.TextureUnit, texture: Dynamic): Void;
static function setTextureParameters(texunit: kha.graphics4.TextureUnit, uAddressing: Int, vAddressing: Int, minificationFilter: Int,
magnificationFilter: Int, mipmapFilter: Int): Void;
static function setTexture3DParameters(texunit: kha.graphics4.TextureUnit, uAddressing: Int, vAddressing: Int, wAddressing: Int, minificationFilter: Int,
magnificationFilter: Int, mipmapFilter: Int): Void;
static function setTextureCompareMode(texunit: kha.graphics4.TextureUnit, enabled: Bool): Void;
static function setCubeMapCompareMode(texunit: kha.graphics4.TextureUnit, enabled: Bool): Void;
static function setBool(location: kha.graphics4.ConstantLocation, value: Bool): Void;
static function setInt(location: kha.graphics4.ConstantLocation, value: Int): Void;
static function setFloat(location: kha.graphics4.ConstantLocation, value: Float): Void;
static function setFloat2(location: kha.graphics4.ConstantLocation, value1: Float, value2: Float): Void;
static function setFloat3(location: kha.graphics4.ConstantLocation, value1: Float, value2: Float, value3: Float): Void;
static function setFloat4(location: kha.graphics4.ConstantLocation, value1: Float, value2: Float, value3: Float, value4: Float): Void;
static function setFloats(location: kha.graphics4.ConstantLocation, values: js.lib.ArrayBuffer): Void;
static function setMatrix(location: kha.graphics4.ConstantLocation, matrix: js.lib.ArrayBuffer): Void;
static function setMatrix3(location: kha.graphics4.ConstantLocation, matrix: js.lib.ArrayBuffer): Void;
static function begin(renderTarget: kha.Canvas, additionalRenderTargets: Array<kha.Canvas>): Void;
static function beginFace(renderTarget: kha.Canvas, face: Int): Void;
static function end(): Void;
static function renderTargetsInvertedY(): Bool;
static function viewport(x: Int, y: Int, width: Int, height: Int): Void;
static function scissor(x: Int, y: Int, width: Int, height: Int): Void;
static function disableScissor(): Void;
static function createRenderTarget(width: Int, height: Int, format: Int, depthBufferBits: Int, stencilBufferBits: Int): Dynamic;
static function createRenderTargetCubeMap(cubeMapSize: Int, format: Int, depthBufferBits: Int, stencilBufferBits: Int): Dynamic;
static function createTexture(width: Int, height: Int, format: Int): Dynamic;
static function createTexture3D(width: Int, height: Int, depth: Int, format: Int): Dynamic;
static function createTextureFromBytes(data: haxe.io.BytesData, width: Int, height: Int, format: Int, readable: Bool): Dynamic;
static function createTextureFromBytes3D(data: haxe.io.BytesData, width: Int, height: Int, depth: Int, format: Int, readable: Bool): Dynamic;
static function createTextureFromEncodedBytes(data: haxe.io.BytesData, format: String, readable: Bool): Dynamic;
static function getTexturePixels(texture: Dynamic): haxe.io.BytesData;
static function getRenderTargetPixels(renderTarget: Dynamic, data: haxe.io.BytesData): Void;
static function lockTexture(texture: Dynamic, level: Int): js.lib.ArrayBuffer;
static function unlockTexture(texture: Dynamic): Void;
static function generateTextureMipmaps(texture: Dynamic, levels: Int): Void;
static function generateRenderTargetMipmaps(renderTarget: Dynamic, levels: Int): Void;
static function setMipmaps(texture: Dynamic, mipmaps: Array<kha.Image>): Void;
static function setDepthStencilFrom(target: Dynamic, source: Dynamic): Void;
static function clearTexture(target: Dynamic, x: Int, y: Int, z: Int, width: Int, height: Int, depth: Int, color: Int): Void;
static function createIndexBuffer(count: Int): Dynamic;
static function deleteIndexBuffer(buffer: Dynamic): Dynamic;
static function lockIndexBuffer(buffer: Dynamic): kha.arrays.Uint32Array;
static function unlockIndexBuffer(buffer: Dynamic): Void;
static function setIndexBuffer(buffer: Dynamic): Void;
static function createVertexBuffer(count: Int, structure: Array<kha.graphics4.VertexElement>, usage: Int, instanceDataStepRate: Int): Dynamic;
static function deleteVertexBuffer(buffer: Dynamic): Dynamic;
static function lockVertexBuffer(buffer: Dynamic, start: Int, count: Int): js.lib.ArrayBuffer;
static function unlockVertexBuffer(buffer: Dynamic, count: Int): Void;
static function setVertexBuffer(buffer: Dynamic): Void;
static function setVertexBuffers(vertexBuffers: Array<kha.graphics4.VertexBuffer>): Void;
static function drawIndexedVertices(start: Int, count: Int): Void;
static function drawIndexedVerticesInstanced(instanceCount: Int, start: Int, count: Int): Void;
static function loadImage(file: String, readable: Bool): Dynamic;
static function unloadImage(image: kha.Image): Void;
static function loadSound(file: String): Dynamic;
static function writeAudioBuffer(buffer: js.lib.ArrayBuffer, samples: Int): Void;
static function loadBlob(file: String): js.lib.ArrayBuffer;
static function init(title: String, width: Int, height: Int, samplesPerPixel: Int, vSync: Bool, windowMode: Int, windowFeatures: Int, kromApi: Int): Void;
static function log(v: Dynamic): Void;
static function setCallback(callback: Void->Void): Void;
static function setDropFilesCallback(callback: String->Void): Void;
static function setCutCopyPasteCallback(cutCallback: Void->String, copyCallback: Void->String, pasteCallback: String->Void): Void;
static function setApplicationStateCallback(foregroundCallback: Void->Void, resumeCallback: Void->Void, pauseCallback: Void->Void,
backgroundCallback: Void->Void, shutdownCallback: Void->Void): Void;
static function setKeyboardDownCallback(callback: Int->Void): Void;
static function setKeyboardUpCallback(callback: Int->Void): Void;
static function setKeyboardPressCallback(callback: Int->Void): Void;
static function setMouseDownCallback(callback: Int->Int->Int->Void): Void;
static function setMouseUpCallback(callback: Int->Int->Int->Void): Void;
static function setMouseMoveCallback(callback: Int->Int->Int->Int->Void): Void;
static function setMouseWheelCallback(callback: Int->Void): Void;
static function setPenDownCallback(callback: Int->Int->Float->Void): Void;
static function setPenUpCallback(callback: Int->Int->Float->Void): Void;
static function setPenMoveCallback(callback: Int->Int->Float->Void): Void;
static function setGamepadAxisCallback(callback: Int->Int->Float->Void): Void;
static function setGamepadButtonCallback(callback: Int->Int->Float->Void): Void;
static function lockMouse(): Void;
static function unlockMouse(): Void;
static function canLockMouse(): Bool;
static function isMouseLocked(): Bool;
static function setMousePosition(windowId: Int, x: Int, y: Int): Void;
static function showMouse(show: Bool): Void;
static function setAudioCallback(callback: Int->Void): Void;
static function getTime(): Float;
static function windowWidth(id: Int): Int;
static function windowHeight(id: Int): Int;
static function setWindowTitle(id: Int, title: String): Void;
static function screenDpi(): Int;
static function systemId(): String;
static function requestShutdown(): Void;
static function displayCount(): Int;
static function displayWidth(index: Int): Int;
static function displayHeight(index: Int): Int;
static function displayX(index: Int): Int;
static function displayY(index: Int): Int;
static function displayIsPrimary(index: Int): Bool;
static function writeStorage(name: String, data: haxe.io.BytesData): Void;
static function readStorage(name: String): haxe.io.BytesData;
static function fileSaveBytes(path: String, bytes: haxe.io.BytesData): Void;
static function sysCommand(cmd: String, ?args: Array<String>): Int;
static function savePath(): String;
static function getArgCount(): Int;
static function getArg(index: Int): String;
static function getFilesLocation(): String;
static function setBoolCompute(location: kha.compute.ConstantLocation, value: Bool): Void;
static function setIntCompute(location: kha.compute.ConstantLocation, value: Int): Void;
static function setFloatCompute(location: kha.compute.ConstantLocation, value: Float): Void;
static function setFloat2Compute(location: kha.compute.ConstantLocation, value1: Float, value2: Float): Void;
static function setFloat3Compute(location: kha.compute.ConstantLocation, value1: Float, value2: Float, value3: Float): Void;
static function setFloat4Compute(location: kha.compute.ConstantLocation, value1: Float, value2: Float, value3: Float, value4: Float): Void;
static function setFloatsCompute(location: kha.compute.ConstantLocation, values: js.lib.ArrayBuffer): Void;
static function setMatrixCompute(location: kha.compute.ConstantLocation, matrix: js.lib.ArrayBuffer): Void;
static function setMatrix3Compute(location: kha.compute.ConstantLocation, matrix: js.lib.ArrayBuffer): Void;
static function setTextureCompute(unit: kha.compute.TextureUnit, texture: Dynamic, access: Int): Void;
static function setRenderTargetCompute(unit: kha.compute.TextureUnit, renderTarget: Dynamic, access: Int): Void;
static function setSampledTextureCompute(unit: kha.compute.TextureUnit, texture: Dynamic): Void;
static function setSampledRenderTargetCompute(unit: kha.compute.TextureUnit, renderTarget: Dynamic): Void;
static function setSampledDepthTextureCompute(unit: kha.compute.TextureUnit, texture: Dynamic): Void;
static function setTextureParametersCompute(texunit: kha.compute.TextureUnit, uAddressing: Int, vAddressing: Int, minificationFilter: Int,
magnificationFilter: Int, mipmapFilter: Int): Void;
static function setTexture3DParametersCompute(texunit: kha.compute.TextureUnit, uAddressing: Int, vAddressing: Int, wAddressing: Int,
minificationFilter: Int, magnificationFilter: Int, mipmapFilter: Int): Void;
static function setShaderCompute(shader: Dynamic): Void;
static function deleteShaderCompute(shader: Dynamic): Void;
static function createShaderCompute(bytes: haxe.io.BytesData): Dynamic;
static function getConstantLocationCompute(shader: Dynamic, name: String): Dynamic;
static function getTextureUnitCompute(shader: Dynamic, name: String): Dynamic;
static function compute(x: Int, y: Int, z: Int): Void;
}

View File

@ -0,0 +1,3 @@
package kha;
typedef Blob = kha.internal.BytesBlob;

View File

@ -0,0 +1,96 @@
package kha;
class Display {
static var displays: Array<Display>;
var num: Int;
var isPrimary: Bool;
function new(num: Int, isPrimary: Bool) {
this.num = num;
this.isPrimary = isPrimary;
}
public static function init(): Void {
if (displays == null) {
// TODO: Krom.displayInit();
displays = [];
for (i in 0...Krom.displayCount()) {
displays.push(new Display(i, Krom.displayIsPrimary(i)));
}
}
}
public static var primary(get, never): Display;
static function get_primary(): Display {
init();
for (display in displays) {
if (display.isPrimary) {
return display;
}
}
return null;
}
public static var all(get, never): Array<Display>;
static function get_all(): Array<Display> {
init();
return displays;
}
public var available(get, never): Bool;
function get_available(): Bool {
return true;
}
public var name(get, never): String;
function get_name(): String {
return "Display";
}
public var x(get, never): Int;
function get_x(): Int {
return Krom.displayX(num);
}
public var y(get, never): Int;
function get_y(): Int {
return Krom.displayY(num);
}
public var width(get, never): Int;
function get_width(): Int {
return Krom.displayWidth(num);
}
public var height(get, never): Int;
function get_height(): Int {
return Krom.displayHeight(num);
}
public var frequency(get, never): Int;
function get_frequency(): Int {
return 60;
}
public var pixelsPerInch(get, never): Int;
function get_pixelsPerInch(): Int {
return Krom.screenDpi();
}
public var modes(get, never): Array<DisplayMode>;
function get_modes(): Array<DisplayMode> {
return [];
}
}

View File

@ -0,0 +1,3 @@
package kha;
typedef Font = kha.Kravur;

View File

@ -0,0 +1,291 @@
package kha;
import haxe.io.Bytes;
import kha.graphics4.DepthStencilFormat;
import kha.graphics4.TextureFormat;
import kha.graphics4.Usage;
class Image implements Canvas implements Resource {
public var texture_: Dynamic;
public var renderTarget_: Dynamic;
var myFormat: TextureFormat;
var readable: Bool;
var graphics1: kha.graphics1.Graphics;
var graphics2: kha.graphics2.Graphics;
var graphics4: kha.graphics4.Graphics;
function new(texture: Dynamic) {
texture_ = texture;
}
static function getRenderTargetFormat(format: TextureFormat): Int {
switch (format) {
case RGBA32: // Target32Bit
return 0;
case RGBA64: // Target64BitFloat
return 1;
case A32: // Target32BitRedFloat
return 2;
case RGBA128: // Target128BitFloat
return 3;
case DEPTH16: // Target16BitDepth
return 4;
case L8:
return 5; // Target8BitRed
case A16:
return 6; // Target16BitRedFloat
default:
return 0;
}
}
static function getDepthBufferBits(depthAndStencil: DepthStencilFormat): Int {
return switch (depthAndStencil) {
case NoDepthAndStencil: -1;
case DepthOnly: 24;
case DepthAutoStencilAuto: 24;
case Depth24Stencil8: 24;
case Depth32Stencil8: 32;
case Depth16: 16;
}
}
static function getStencilBufferBits(depthAndStencil: DepthStencilFormat): Int {
return switch (depthAndStencil) {
case NoDepthAndStencil: -1;
case DepthOnly: -1;
case DepthAutoStencilAuto: 8;
case Depth24Stencil8: 8;
case Depth32Stencil8: 8;
case Depth16: 0;
}
}
static function getTextureFormat(format: TextureFormat): Int {
switch (format) {
case RGBA32:
return 0;
case RGBA128:
return 3;
case RGBA64:
return 4;
case A32:
return 5;
case A16:
return 7;
default:
return 1; // Grey8
}
}
public static function _fromTexture(texture: Dynamic): Image {
return new Image(texture);
}
public static function fromBytes(bytes: Bytes, width: Int, height: Int, format: TextureFormat = null, usage: Usage = null, readable: Bool = false): Image {
if (format == null)
format = TextureFormat.RGBA32;
var image = new Image(null);
image.myFormat = format;
image.texture_ = Krom.createTextureFromBytes(bytes.getData(), width, height, getTextureFormat(format), readable);
return image;
}
public static function fromBytes3D(bytes: Bytes, width: Int, height: Int, depth: Int, format: TextureFormat = null, usage: Usage = null,
readable: Bool = false): Image {
if (format == null)
format = TextureFormat.RGBA32;
var image = new Image(null);
image.myFormat = format;
image.texture_ = Krom.createTextureFromBytes3D(bytes.getData(), width, height, depth, getTextureFormat(format), readable);
return image;
}
public static function fromEncodedBytes(bytes: Bytes, format: String, doneCallback: Image->Void, errorCallback: String->Void,
readable: Bool = false): Void {
var image = new Image(null);
image.texture_ = Krom.createTextureFromEncodedBytes(bytes.getData(), format, readable);
doneCallback(image);
}
public static function create(width: Int, height: Int, format: TextureFormat = null, usage: Usage = null, readable: Bool = false): Image {
if (format == null)
format = TextureFormat.RGBA32;
var image = new Image(null);
image.myFormat = format;
image.texture_ = Krom.createTexture(width, height, getTextureFormat(format));
return image;
}
public static function create3D(width: Int, height: Int, depth: Int, format: TextureFormat = null, usage: Usage = null, readable: Bool = false): Image {
if (format == null)
format = TextureFormat.RGBA32;
var image = new Image(null);
image.myFormat = format;
image.texture_ = Krom.createTexture3D(width, height, depth, getTextureFormat(format));
return image;
}
public static function createRenderTarget(width: Int, height: Int, format: TextureFormat = null,
depthStencil: DepthStencilFormat = DepthStencilFormat.NoDepthAndStencil, antiAliasingSamples: Int = 1): Image {
if (format == null)
format = TextureFormat.RGBA32;
var image = new Image(null);
image.myFormat = format;
image.renderTarget_ = Krom.createRenderTarget(width, height, getRenderTargetFormat(format), getDepthBufferBits(depthStencil),
getStencilBufferBits(depthStencil));
return image;
}
public static var maxSize(get, never): Int;
static function get_maxSize(): Int {
return 4096;
}
public static var nonPow2Supported(get, never): Bool;
static function get_nonPow2Supported(): Bool {
return true;
}
public static function renderTargetsInvertedY(): Bool {
return Krom.renderTargetsInvertedY();
}
public function isOpaque(x: Int, y: Int): Bool {
return false;
}
public function at(x: Int, y: Int): Color {
return Color.Black;
}
public function unload(): Void {
Krom.unloadImage(this);
texture_ = null;
renderTarget_ = null;
}
public function lock(level: Int = 0): Bytes {
return Bytes.ofData(Krom.lockTexture(texture_, level));
}
public function unlock(): Void {
Krom.unlockTexture(texture_);
}
var pixels: Bytes = null;
public function getPixels(): Bytes {
if (renderTarget_ != null) {
if (pixels == null)
pixels = Bytes.alloc(formatByteSize(myFormat) * width * height);
Krom.getRenderTargetPixels(renderTarget_, pixels.getData());
return pixels;
}
else {
return Bytes.ofData(Krom.getTexturePixels(texture_));
}
}
static function formatByteSize(format: TextureFormat): Int {
return switch (format) {
case RGBA32: 4;
case L8: 1;
case RGBA128: 16;
case DEPTH16: 2;
case RGBA64: 8;
case A32: 4;
case A16: 2;
default: 4;
}
}
public function generateMipmaps(levels: Int): Void {
texture_ == null ? Krom.generateRenderTargetMipmaps(renderTarget_, levels) : Krom.generateTextureMipmaps(texture_, levels);
}
public function setMipmaps(mipmaps: Array<Image>): Void {
Krom.setMipmaps(texture_, mipmaps);
}
public function setDepthStencilFrom(image: Image): Void {
Krom.setDepthStencilFrom(renderTarget_, image.renderTarget_);
}
public function clear(x: Int, y: Int, z: Int, width: Int, height: Int, depth: Int, color: Color): Void {
Krom.clearTexture(texture_, x, y, z, width, height, depth, color);
}
public var width(get, never): Int;
function get_width(): Int {
return texture_ == null ? renderTarget_.width : texture_.width;
}
public var height(get, never): Int;
function get_height(): Int {
return texture_ == null ? renderTarget_.height : texture_.height;
}
public var depth(get, never): Int;
function get_depth(): Int {
return texture_ != null ? texture_.depth : 1;
}
public var format(get, never): TextureFormat;
function get_format(): TextureFormat {
return myFormat;
}
public var realWidth(get, never): Int;
function get_realWidth(): Int {
return texture_ == null ? renderTarget_.width : texture_.realWidth;
}
public var realHeight(get, never): Int;
function get_realHeight(): Int {
return texture_ == null ? renderTarget_.height : texture_.realHeight;
}
public var stride(get, never): Int;
function get_stride(): Int {
return texture_.stride;
}
public var g1(get, never): kha.graphics1.Graphics;
function get_g1(): kha.graphics1.Graphics {
if (graphics1 == null) {
graphics1 = new kha.graphics2.Graphics1(this);
}
return graphics1;
}
public var g2(get, never): kha.graphics2.Graphics;
function get_g2(): kha.graphics2.Graphics {
if (graphics2 == null) {
graphics2 = new kha.graphics4.Graphics2(this);
}
return graphics2;
}
public var g4(get, never): kha.graphics4.Graphics;
function get_g4(): kha.graphics4.Graphics {
if (graphics4 == null) {
graphics4 = new kha.krom.Graphics(this);
}
return graphics4;
}
}

View File

@ -0,0 +1,77 @@
package kha;
import kha.FontStyle;
import kha.Blob;
import kha.Kravur;
import haxe.io.Bytes;
import haxe.io.BytesData;
class LoaderImpl {
public static function getImageFormats(): Array<String> {
return ["png", "jpg"];
}
public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) {
var readable = Reflect.hasField(desc, "readable") ? desc.readable : false;
var texture = Krom.loadImage(desc.files[0], readable);
if (texture == null) {
failed({
url: desc.files.join(","),
error: "Could not load image(s)",
});
}
else {
done(Image._fromTexture(texture));
}
}
public static function getSoundFormats(): Array<String> {
return ["wav", "ogg"];
}
public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: AssetError->Void) {
var sound = Krom.loadSound(desc.files[0]);
if (sound == null) {
failed({
url: desc.files.join(","),
error: "Could not load sound(s)",
});
}
else {
done(new kha.krom.Sound(Bytes.ofData(sound)));
}
}
public static function getVideoFormats(): Array<String> {
return ["webm"];
}
public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void {
failed({
url: desc.files.join(","),
error: "Could not load video(s), Krom currently does not support loading videos",
});
}
public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) {
var blob = Krom.loadBlob(desc.files[0]);
if (blob == null) {
failed({
url: desc.files.join(","),
error: "Could not load blob(s)",
});
}
else {
done(new Blob(Bytes.ofData(blob)));
}
}
public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void {
loadBlobFromDescription(desc, function(blob: Blob) {
done(new Kravur(blob));
}, (a: AssetError) -> {
a.error = "Could not load font(s)";
failed(a);
});
}
}

View File

@ -0,0 +1,38 @@
package kha;
import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import haxe.io.BytesData;
using StringTools;
class KromStorageFile extends StorageFile {
var name: String;
public function new(name: String) {
this.name = name;
}
override public function read(): Blob {
var data: BytesData = Krom.readStorage(name);
return data != null ? Blob.fromBytes(Bytes.ofData(data)) : null;
}
override public function write(data: Blob): Void {
if (data != null) {
Krom.writeStorage(name, data.toBytes().getData());
}
}
}
class Storage {
public static function namedFile(name: String): StorageFile {
name = name.replace("\\", ".");
name = name.replace("/", ".");
return new KromStorageFile(name);
}
public static function defaultFile(): StorageFile {
return namedFile("default.kha");
}
}

View File

@ -0,0 +1,343 @@
package kha;
import kha.graphics4.TextureFormat;
import kha.input.Gamepad;
import kha.input.Keyboard;
import kha.input.Mouse;
import kha.input.MouseImpl;
import kha.input.Pen;
import kha.input.Surface;
import kha.System;
import haxe.ds.Vector;
class SystemImpl {
static var start: Float;
static var framebuffer: Framebuffer;
static var keyboard: Keyboard;
static var mouse: Mouse;
static var pen: Pen;
static var maxGamepads: Int = 4;
static var gamepads: Array<Gamepad>;
static var mouseLockListeners: Array<Void->Void> = [];
static function renderCallback(): Void {
Scheduler.executeFrame();
System.render([framebuffer]);
}
static function dropFilesCallback(filePath: String): Void {
System.dropFiles(filePath);
}
static function copyCallback(): String {
if (System.copyListener != null) {
return System.copyListener();
}
else {
return null;
}
}
static function cutCallback(): String {
if (System.cutListener != null) {
return System.cutListener();
}
else {
return null;
}
}
static function pasteCallback(data: String): Void {
if (System.pasteListener != null) {
System.pasteListener(data);
}
}
static function foregroundCallback(): Void {
System.foreground();
}
static function resumeCallback(): Void {
System.resume();
}
static function pauseCallback(): Void {
System.pause();
}
static function backgroundCallback(): Void {
System.background();
}
static function shutdownCallback(): Void {
System.shutdown();
}
static function keyboardDownCallback(code: Int): Void {
keyboard.sendDownEvent(cast code);
}
static function keyboardUpCallback(code: Int): Void {
keyboard.sendUpEvent(cast code);
}
static function keyboardPressCallback(charCode: Int): Void {
keyboard.sendPressEvent(String.fromCharCode(charCode));
}
static function mouseDownCallback(button: Int, x: Int, y: Int): Void {
mouse.sendDownEvent(0, button, x, y);
}
static function mouseUpCallback(button: Int, x: Int, y: Int): Void {
mouse.sendUpEvent(0, button, x, y);
}
static function mouseMoveCallback(x: Int, y: Int, mx: Int, my: Int): Void {
mouse.sendMoveEvent(0, x, y, mx, my);
}
static function mouseWheelCallback(delta: Int): Void {
mouse.sendWheelEvent(0, delta);
}
static function penDownCallback(x: Int, y: Int, pressure: Float): Void {
pen.sendDownEvent(0, x, y, pressure);
}
static function penUpCallback(x: Int, y: Int, pressure: Float): Void {
pen.sendUpEvent(0, x, y, pressure);
}
static function penMoveCallback(x: Int, y: Int, pressure: Float): Void {
pen.sendMoveEvent(0, x, y, pressure);
}
static function gamepadAxisCallback(gamepad: Int, axis: Int, value: Float): Void {
gamepads[gamepad].sendAxisEvent(axis, value);
}
static function gamepadButtonCallback(gamepad: Int, button: Int, value: Float): Void {
gamepads[gamepad].sendButtonEvent(button, value);
}
static function audioCallback(samples: Int): Void {
kha.audio2.Audio._callCallback(samples);
var buffer = @:privateAccess kha.audio2.Audio.buffer;
Krom.writeAudioBuffer(buffer.data.buffer, samples);
}
public static function init(options: SystemOptions, callback: Window->Void): Void {
Krom.init(options.title, options.width, options.height, options.framebuffer.samplesPerPixel, options.framebuffer.verticalSync,
cast options.window.mode, options.window.windowFeatures, Krom.KROM_API);
start = Krom.getTime();
haxe.Log.trace = function(v: Dynamic, ?infos: haxe.PosInfos) {
var message = haxe.Log.formatOutput(v, infos);
Krom.log(message);
};
new Window(0);
Scheduler.init();
Shaders.init();
var g4 = new kha.krom.Graphics();
framebuffer = new Framebuffer(0, null, null, g4);
framebuffer.init(new kha.graphics2.Graphics1(framebuffer), new kha.graphics4.Graphics2(framebuffer), g4);
Krom.setCallback(renderCallback);
Krom.setDropFilesCallback(dropFilesCallback);
Krom.setCutCopyPasteCallback(cutCallback, copyCallback, pasteCallback);
Krom.setApplicationStateCallback(foregroundCallback, resumeCallback, pauseCallback, backgroundCallback, shutdownCallback);
keyboard = new Keyboard();
mouse = new MouseImpl();
pen = new Pen();
gamepads = new Array<Gamepad>();
for (i in 0...maxGamepads) {
gamepads[i] = new Gamepad(i);
}
Krom.setKeyboardDownCallback(keyboardDownCallback);
Krom.setKeyboardUpCallback(keyboardUpCallback);
Krom.setKeyboardPressCallback(keyboardPressCallback);
Krom.setMouseDownCallback(mouseDownCallback);
Krom.setMouseUpCallback(mouseUpCallback);
Krom.setMouseMoveCallback(mouseMoveCallback);
Krom.setMouseWheelCallback(mouseWheelCallback);
Krom.setPenDownCallback(penDownCallback);
Krom.setPenUpCallback(penUpCallback);
Krom.setPenMoveCallback(penMoveCallback);
Krom.setGamepadAxisCallback(gamepadAxisCallback);
Krom.setGamepadButtonCallback(gamepadButtonCallback);
kha.audio2.Audio._init();
kha.audio1.Audio._init();
Krom.setAudioCallback(audioCallback);
Scheduler.start();
callback(Window.get(0));
}
public static function initEx(title: String, options: Array<WindowOptions>, windowCallback: Int->Void, callback: Void->Void): Void {}
static function translateWindowMode(value: Null<WindowMode>): Int {
if (value == null) {
return 0;
}
return switch (value) {
case Windowed: 0;
case Fullscreen: 1;
case ExclusiveFullscreen: 2;
}
}
public static function getScreenRotation(): ScreenRotation {
return ScreenRotation.RotationNone;
}
public static function getTime(): Float {
return Krom.getTime() - start;
}
public static function getVsync(): Bool {
return true;
}
public static function getRefreshRate(): Int {
return 60;
}
public static function getSystemId(): String {
return Krom.systemId();
}
public static function vibrate(ms: Int): Void {
// TODO: Implement
}
public static function getLanguage(): String {
return "en"; // TODO: Implement
}
public static function requestShutdown(): Bool {
Krom.requestShutdown();
return true;
}
public static function getMouse(num: Int): Mouse {
return mouse;
}
public static function getPen(num: Int): Pen {
return pen;
}
public static function getKeyboard(num: Int): Keyboard {
return keyboard;
}
public static function lockMouse(): Void {
if (!isMouseLocked()) {
Krom.lockMouse();
for (listener in mouseLockListeners) {
listener();
}
}
}
public static function unlockMouse(): Void {
if (isMouseLocked()) {
Krom.unlockMouse();
for (listener in mouseLockListeners) {
listener();
}
}
}
public static function canLockMouse(): Bool {
return Krom.canLockMouse();
}
public static function isMouseLocked(): Bool {
return Krom.isMouseLocked();
}
public static function notifyOfMouseLockChange(func: Void->Void, error: Void->Void): Void {
if (canLockMouse() && func != null) {
mouseLockListeners.push(func);
}
}
public static function removeFromMouseLockChange(func: Void->Void, error: Void->Void): Void {
if (canLockMouse() && func != null) {
mouseLockListeners.remove(func);
}
}
public static function hideSystemCursor(): Void {
Krom.showMouse(false);
}
public static function showSystemCursor(): Void {
Krom.showMouse(true);
}
static function unload(): Void {}
public static function canSwitchFullscreen(): Bool {
return false;
}
public static function isFullscreen(): Bool {
return false;
}
public static function requestFullscreen(): Void {}
public static function exitFullscreen(): Void {}
public static function notifyOfFullscreenChange(func: Void->Void, error: Void->Void): Void {}
public static function removeFromFullscreenChange(func: Void->Void, error: Void->Void): Void {}
public static function changeResolution(width: Int, height: Int): Void {}
public static function setKeepScreenOn(on: Bool): Void {}
public static function loadUrl(url: String): Void {}
public static function getGamepadId(index: Int): String {
return "unknown";
}
public static function getGamepadVendor(index: Int): String {
return "unknown";
}
public static function setGamepadRumble(index: Int, leftAmount: Float, rightAmount: Float): Void {}
public static function safeZone(): Float {
return 1.0;
}
public static function login(): Void {}
public static function automaticSafeZone(): Bool {
return true;
}
public static function setSafeZone(value: Float): Void {}
public static function unlockAchievement(id: Int): Void {}
public static function waitingForLogin(): Bool {
return false;
}
public static function disallowUserChange(): Void {}
public static function allowUserChange(): Void {}
}

View File

@ -0,0 +1,119 @@
package kha;
class Window {
static var windows: Array<Window> = [];
var num: Int;
var windowTitle: String;
@:noCompletion
@:noDoc
public function new(num: Int) {
this.num = num;
windows.push(this);
}
public static function create(win: WindowOptions = null, frame: FramebufferOptions = null): Window {
return null;
}
public static function destroy(window: Window): Void {}
public static function get(index: Int): Window {
return windows[index];
}
public static var all(get, never): Array<Window>;
static function get_all(): Array<Window> {
return windows;
}
public function resize(width: Int, height: Int): Void {}
public function move(x: Int, y: Int): Void {}
public function changeWindowFeatures(features: Int): Void {}
public function changeFramebuffer(frame: FramebufferOptions): Void {}
public var x(get, set): Int;
function get_x(): Int {
return 0;
}
function set_x(value: Int): Int {
return 0;
}
public var y(get, set): Int;
function get_y(): Int {
return 0;
}
function set_y(value: Int): Int {
return 0;
}
public var width(get, set): Int;
function get_width(): Int {
return Krom.windowWidth(num);
}
function set_width(value: Int): Int {
return 800;
}
public var height(get, set): Int;
function get_height(): Int {
return Krom.windowHeight(num);
}
function set_height(value: Int): Int {
return 600;
}
public var mode(get, set): WindowMode;
function get_mode(): WindowMode {
return Windowed;
}
function set_mode(mode: WindowMode): WindowMode {
return Windowed;
}
public var visible(get, set): Bool;
function get_visible(): Bool {
return true;
}
function set_visible(value: Bool): Bool {
return true;
}
public var title(get, set): String;
function get_title(): String {
return windowTitle;
}
function set_title(value: String): String {
Krom.setWindowTitle(num, value);
windowTitle = value;
return windowTitle;
}
public function notifyOnResize(callback: Int->Int->Void): Void {}
public var vSynced(get, never): Bool;
function get_vSynced(): Bool {
return true;
}
}

View File

@ -0,0 +1,187 @@
package kha.arrays;
import js.lib.DataView;
import kha.FastFloat;
@:forward
abstract ByteArray(DataView) to DataView {
static final LITTLE_ENDIAN: Bool = js.Syntax.code("new Uint8Array(new Uint32Array([0x12345678]).buffer)[0] === 0x78");
public var buffer(get, never): ByteBuffer;
inline function get_buffer(): ByteBuffer {
return cast this.buffer;
}
public function new(buffer: ByteBuffer, ?byteOffset: Int, ?byteLength: Int) {
this = new DataView(buffer, byteOffset, byteLength);
}
static public function make(byteLength: Int): ByteArray {
return new ByteArray(ByteBuffer.create(byteLength));
}
public inline function getInt8(byteOffset: Int): Int {
return this.getInt8(byteOffset);
}
public inline function getUint8(byteOffset: Int): Int {
return this.getUint8(byteOffset);
}
public inline function getInt16(byteOffset: Int): Int {
return this.getInt16(byteOffset, LITTLE_ENDIAN);
}
public inline function getUint16(byteOffset: Int): Int {
return this.getUint16(byteOffset, LITTLE_ENDIAN);
}
public inline function getInt32(byteOffset: Int): Int {
return this.getInt32(byteOffset, LITTLE_ENDIAN);
}
public inline function getUint32(byteOffset: Int): Int {
return this.getUint32(byteOffset, LITTLE_ENDIAN);
}
public inline function getFloat32(byteOffset: Int): FastFloat {
return this.getFloat32(byteOffset, LITTLE_ENDIAN);
}
public inline function getFloat64(byteOffset: Int): Float {
return this.getFloat64(byteOffset, LITTLE_ENDIAN);
}
public inline function setInt8(byteOffset: Int, value: Int): Void {
this.setInt8(byteOffset, value);
}
public inline function setUint8(byteOffset: Int, value: Int): Void {
this.setUint8(byteOffset, value);
}
public inline function setInt16(byteOffset: Int, value: Int): Void {
this.setInt16(byteOffset, value, LITTLE_ENDIAN);
}
public inline function setUint16(byteOffset: Int, value: Int): Void {
this.setUint16(byteOffset, value, LITTLE_ENDIAN);
}
public inline function setInt32(byteOffset: Int, value: Int): Void {
this.setInt32(byteOffset, value, LITTLE_ENDIAN);
}
public inline function setUint32(byteOffset: Int, value: Int): Void {
this.setUint32(byteOffset, value, LITTLE_ENDIAN);
}
public inline function setFloat32(byteOffset: Int, value: FastFloat): Void {
this.setFloat32(byteOffset, value, true);
}
public inline function setFloat64(byteOffset: Int, value: Float): Void {
this.setFloat64(byteOffset, value, LITTLE_ENDIAN);
}
public inline function getInt16LE(byteOffset: Int): Int {
return this.getInt16(byteOffset, true);
}
public inline function getUint16LE(byteOffset: Int): Int {
return this.getUint16(byteOffset, true);
}
public inline function getInt32LE(byteOffset: Int): Int {
return this.getInt32(byteOffset, true);
}
public inline function getUint32LE(byteOffset: Int): Int {
return this.getUint32(byteOffset, true);
}
public inline function getFloat32LE(byteOffset: Int): FastFloat {
return this.getFloat32(byteOffset, true);
}
public inline function getFloat64LE(byteOffset: Int): Float {
return this.getFloat64(byteOffset, true);
}
public inline function setInt16LE(byteOffset: Int, value: Int): Void {
this.setInt16(byteOffset, value, true);
}
public inline function setUint16LE(byteOffset: Int, value: Int): Void {
this.setUint16(byteOffset, value, true);
}
public inline function setInt32LE(byteOffset: Int, value: Int): Void {
this.setInt32(byteOffset, value, true);
}
public inline function setUint32LE(byteOffset: Int, value: Int): Void {
this.setUint32(byteOffset, value, true);
}
public inline function setFloat32LE(byteOffset: Int, value: FastFloat): Void {
this.setFloat32(byteOffset, value, true);
}
public inline function setFloat64LE(byteOffset: Int, value: Float): Void {
this.setFloat64(byteOffset, value, true);
}
public inline function getInt16BE(byteOffset: Int): Int {
return this.getInt16(byteOffset);
}
public inline function getUint16BE(byteOffset: Int): Int {
return this.getUint16(byteOffset);
}
public inline function getInt32BE(byteOffset: Int): Int {
return this.getInt32(byteOffset);
}
public inline function getUint32BE(byteOffset: Int): Int {
return this.getUint32(byteOffset);
}
public inline function getFloat32BE(byteOffset: Int): FastFloat {
return this.getFloat32(byteOffset);
}
public inline function getFloat64BE(byteOffset: Int): Float {
return this.getFloat64(byteOffset);
}
public inline function setInt16BE(byteOffset: Int, value: Int): Void {
this.setInt16(byteOffset, value);
}
public inline function setUint16BE(byteOffset: Int, value: Int): Void {
this.setUint16(byteOffset, value);
}
public inline function setInt32BE(byteOffset: Int, value: Int): Void {
this.setInt32(byteOffset, value);
}
public inline function setUint32BE(byteOffset: Int, value: Int): Void {
this.setUint32(byteOffset, value);
}
public inline function setFloat32BE(byteOffset: Int, value: FastFloat): Void {
this.setFloat32(byteOffset, value);
}
public inline function setFloat64BE(byteOffset: Int, value: Float): Void {
this.setFloat64(byteOffset, value);
}
public inline function subarray(start: Int, ?end: Int): ByteArray {
return new ByteArray(buffer, start, end != null ? end - start : null);
}
}

View File

@ -0,0 +1,14 @@
package kha.arrays;
import js.lib.ArrayBuffer;
@:forward
abstract ByteBuffer(ArrayBuffer) from ArrayBuffer to ArrayBuffer {
public static function create(length: Int): ByteBuffer {
return new ByteBuffer(length);
}
function new(length: Int) {
this = new ArrayBuffer(length);
}
}

View File

@ -0,0 +1,3 @@
package kha.audio1;
typedef Audio = kha.audio2.Audio1;

View File

@ -0,0 +1,57 @@
package kha.audio2;
import kha.Sound;
import kha.internal.IntBox;
class Audio {
public static var disableGcInteractions = false;
static var intBox: IntBox = new IntBox(0);
static var buffer: Buffer;
public static function _init() {
var bufferSize = 1024 * 2;
buffer = new Buffer(bufferSize * 4, 2, 44100);
Audio.samplesPerSecond = 44100;
}
public static function _callCallback(samples: Int): Void {
if (buffer == null)
return;
if (audioCallback != null) {
intBox.value = samples;
audioCallback(intBox, buffer);
}
else {
for (i in 0...samples) {
buffer.data.set(buffer.writeLocation, 0);
buffer.writeLocation += 1;
if (buffer.writeLocation >= buffer.size) {
buffer.writeLocation = 0;
}
}
}
}
public static function _readSample(): Float {
if (buffer == null)
return 0;
var value = buffer.data.get(buffer.readLocation);
buffer.readLocation += 1;
if (buffer.readLocation >= buffer.size) {
buffer.readLocation = 0;
}
return value;
}
public static var samplesPerSecond: Int;
public static var audioCallback: IntBox->Buffer->Void;
public static function play(sound: Sound, loop: Bool = false): kha.audio1.AudioChannel {
return null;
}
public static function stream(sound: Sound, loop: Bool = false): kha.audio1.AudioChannel {
return null;
}
}

View File

@ -0,0 +1,141 @@
package kha.compute;
import kha.Image;
import kha.FastFloat;
import kha.arrays.Float32Array;
import kha.math.FastMatrix3;
import kha.math.FastMatrix4;
import kha.math.FastVector2;
import kha.math.FastVector3;
import kha.math.FastVector4;
import kha.graphics4.CubeMap;
import kha.graphics4.TextureAddressing;
import kha.graphics4.TextureFilter;
import kha.graphics4.MipMapFilter;
class Compute {
public static function setBool(location: ConstantLocation, value: Bool) {
Krom.setBoolCompute(location, value);
}
public static function setInt(location: ConstantLocation, value: Int) {
Krom.setIntCompute(location, value);
}
public static function setFloat(location: ConstantLocation, value: FastFloat) {
Krom.setFloatCompute(location, value);
}
public static function setFloat2(location: ConstantLocation, value1: FastFloat, value2: FastFloat) {
Krom.setFloat2Compute(location, value1, value2);
}
public static function setFloat3(location: ConstantLocation, value1: FastFloat, value2: FastFloat, value3: FastFloat) {
Krom.setFloat3Compute(location, value1, value2, value3);
}
public static function setFloat4(location: ConstantLocation, value1: FastFloat, value2: FastFloat, value3: FastFloat, value4: FastFloat) {
Krom.setFloat4Compute(location, value1, value2, value3, value4);
}
public static function setFloats(location: ConstantLocation, values: Float32Array) {
Krom.setFloatsCompute(location, values.buffer);
}
public static function setVector2(location: ConstantLocation, value: FastVector2): Void {
Compute.setFloat2(location, value.x, value.y);
}
public static function setVector3(location: ConstantLocation, value: FastVector3): Void {
Compute.setFloat3(location, value.x, value.y, value.z);
}
public static function setVector4(location: ConstantLocation, value: FastVector4): Void {
Compute.setFloat4(location, value.x, value.y, value.z, value.w);
}
static var mat = new kha.arrays.Float32Array(16);
public static function setMatrix(location: ConstantLocation, matrix: FastMatrix4): Void {
mat[0] = matrix._00;
mat[1] = matrix._01;
mat[2] = matrix._02;
mat[3] = matrix._03;
mat[4] = matrix._10;
mat[5] = matrix._11;
mat[6] = matrix._12;
mat[7] = matrix._13;
mat[8] = matrix._20;
mat[9] = matrix._21;
mat[10] = matrix._22;
mat[11] = matrix._23;
mat[12] = matrix._30;
mat[13] = matrix._31;
mat[14] = matrix._32;
mat[15] = matrix._33;
Krom.setMatrixCompute(location, mat.buffer);
}
public static function setMatrix3(location: ConstantLocation, matrix: FastMatrix3): Void {
mat[0] = matrix._00;
mat[1] = matrix._01;
mat[2] = matrix._02;
mat[3] = matrix._10;
mat[4] = matrix._11;
mat[5] = matrix._12;
mat[6] = matrix._20;
mat[7] = matrix._21;
mat[8] = matrix._22;
Krom.setMatrix3Compute(location, mat.buffer);
}
public static function setBuffer(buffer: ShaderStorageBuffer, index: Int) {}
public static function setTexture(unit: TextureUnit, texture: Image, access: Access) {
if (texture == null)
return;
texture.texture_ != null ? Krom.setTextureCompute(unit, texture.texture_, access) : Krom.setRenderTargetCompute(unit, texture.renderTarget_, access);
}
public static function setSampledTexture(unit: TextureUnit, texture: Image) {
if (texture == null)
return;
texture.texture_ != null ? Krom.setSampledTextureCompute(unit, texture.texture_) : Krom.setSampledRenderTargetCompute(unit, texture.renderTarget_);
}
public static function setSampledDepthTexture(unit: TextureUnit, texture: Image) {
if (texture == null)
return;
Krom.setSampledDepthTextureCompute(unit, texture);
}
public static function setSampledCubeMap(unit: TextureUnit, cubeMap: CubeMap) {
if (cubeMap == null)
return;
cubeMap.texture_ != null ? Krom.setSampledTextureCompute(unit, cubeMap.texture_) : Krom.setSampledRenderTargetCompute(unit, cubeMap.renderTarget_);
}
public static function setSampledDepthCubeMap(unit: TextureUnit, cubeMap: CubeMap) {
if (cubeMap == null)
return;
Krom.setSampledDepthTextureCompute(unit, cubeMap);
}
public static function setTextureParameters(unit: TextureUnit, uAddressing: TextureAddressing, vAddressing: TextureAddressing,
minificationFilter: TextureFilter, magnificationFilter: TextureFilter, mipmapFilter: MipMapFilter): Void {
Krom.setTextureParametersCompute(unit, uAddressing, vAddressing, minificationFilter, magnificationFilter, mipmapFilter);
}
public static function setTexture3DParameters(unit: TextureUnit, uAddressing: TextureAddressing, vAddressing: TextureAddressing,
wAddressing: TextureAddressing, minificationFilter: TextureFilter, magnificationFilter: TextureFilter, mipmapFilter: MipMapFilter): Void {
Krom.setTexture3DParametersCompute(unit, uAddressing, vAddressing, wAddressing, minificationFilter, magnificationFilter, mipmapFilter);
}
public static function setShader(shader: Shader) {
Krom.setShaderCompute(shader.shader_);
}
public static function compute(x: Int, y: Int, z: Int) {
Krom.compute(x, y, z);
}
}

View File

@ -0,0 +1,5 @@
package kha.compute;
class ConstantLocation {
public function new() {}
}

View File

@ -0,0 +1,25 @@
package kha.compute;
import haxe.io.Bytes;
import kha.Blob;
class Shader {
public var shader_: Dynamic;
public function new(sources: Array<Blob>, files: Array<String>) {
shader_ = Krom.createShaderCompute(sources[0].toBytes().getData());
}
public function delete(): Void {
Krom.deleteShaderCompute(shader_);
shader_ = null;
}
public function getConstantLocation(name: String): ConstantLocation {
return Krom.getConstantLocationCompute(shader_, name);
}
public function getTextureUnit(name: String): TextureUnit {
return Krom.getTextureUnitCompute(shader_, name);
}
}

View File

@ -0,0 +1,33 @@
package kha.compute;
import kha.graphics4.VertexData;
class ShaderStorageBuffer {
var data: Array<Int>;
var myCount: Int;
public function new(indexCount: Int, type: VertexData) {
myCount = indexCount;
data = new Array<Int>();
data[myCount - 1] = 0;
init(indexCount, type);
}
function init(indexCount: Int, type: VertexData) {
myCount = indexCount;
data = new Array<Int>();
data[myCount - 1] = 0;
}
public function delete(): Void {}
public function lock(): Array<Int> {
return data;
}
public function unlock(): Void {}
public function count(): Int {
return myCount;
}
}

View File

@ -0,0 +1,5 @@
package kha.compute;
class TextureUnit {
public function new() {}
}

View File

@ -0,0 +1,119 @@
package kha.graphics4;
import haxe.io.Bytes;
class CubeMap implements Canvas implements Resource {
public var texture_: Dynamic;
public var renderTarget_: Dynamic;
var format: TextureFormat;
var graphics4: kha.graphics4.Graphics;
function new(texture: Dynamic) {
texture_ = texture;
}
static function getRenderTargetFormat(format: TextureFormat): Int {
switch (format) {
case RGBA32: // Target32Bit
return 0;
case RGBA64: // Target64BitFloat
return 1;
case RGBA128: // Target128BitFloat
return 3;
case DEPTH16: // Target16BitDepth
return 4;
default:
return 0;
}
}
static function getDepthBufferBits(depthAndStencil: DepthStencilFormat): Int {
return switch (depthAndStencil) {
case NoDepthAndStencil: -1;
case DepthOnly: 24;
case DepthAutoStencilAuto: 24;
case Depth24Stencil8: 24;
case Depth32Stencil8: 32;
case Depth16: 16;
}
}
static function getStencilBufferBits(depthAndStencil: DepthStencilFormat): Int {
return switch (depthAndStencil) {
case NoDepthAndStencil: -1;
case DepthOnly: -1;
case DepthAutoStencilAuto: 8;
case Depth24Stencil8: 8;
case Depth32Stencil8: 8;
case Depth16: 0;
}
}
static function getTextureFormat(format: TextureFormat): Int {
switch (format) {
case RGBA32:
return 0;
case RGBA128:
return 3;
case RGBA64:
return 4;
case A32:
return 5;
default:
return 1; // Grey8
}
}
public static function createRenderTarget(size: Int, format: TextureFormat = null, depthStencil: DepthStencilFormat = NoDepthAndStencil): CubeMap {
if (format == null)
format = TextureFormat.RGBA32;
var cubeMap = new CubeMap(null);
cubeMap.format = format;
cubeMap.renderTarget_ = Krom.createRenderTargetCubeMap(size, getRenderTargetFormat(format), getDepthBufferBits(depthStencil),
getStencilBufferBits(depthStencil));
return cubeMap;
}
public function unload(): Void {}
public function lock(level: Int = 0): Bytes {
return null;
}
public function unlock(): Void {}
public var width(get, never): Int;
function get_width(): Int {
return texture_ == null ? renderTarget_.width : texture_.width;
}
public var height(get, never): Int;
function get_height(): Int {
return texture_ == null ? renderTarget_.height : texture_.height;
}
public var g1(get, never): kha.graphics1.Graphics;
function get_g1(): kha.graphics1.Graphics {
return null;
}
public var g2(get, never): kha.graphics2.Graphics;
function get_g2(): kha.graphics2.Graphics {
return null;
}
public var g4(get, never): kha.graphics4.Graphics;
function get_g4(): kha.graphics4.Graphics {
if (graphics4 == null) {
graphics4 = new kha.krom.Graphics(this);
}
return graphics4;
}
}

View File

@ -0,0 +1,22 @@
package kha.graphics4;
class FragmentShader {
public var shader: Dynamic;
public function new(sources: Array<Blob>, names: Array<String>) {
if (sources != null) {
shader = Krom.createFragmentShader(sources[0].bytes.getData(), names[0]);
}
}
public static function fromSource(source: String): FragmentShader {
var shader = new FragmentShader(null, null);
shader.shader = Krom.createFragmentShaderFromSource(source);
return shader;
}
public function delete() {
Krom.deleteShader(shader);
shader = null;
}
}

View File

@ -0,0 +1,14 @@
package kha.graphics4;
class GeometryShader {
public var shader: Dynamic;
public function new(sources: Array<Blob>, names: Array<String>) {
shader = Krom.createGeometryShader(sources[0].bytes.getData(), names[0]);
}
public function delete() {
Krom.deleteShader(shader);
shader = null;
}
}

View File

@ -0,0 +1,42 @@
package kha.graphics4;
import kha.arrays.Uint32Array;
import kha.graphics4.Usage;
class IndexBuffer {
public var _data: Uint32Array;
var buffer: Dynamic;
var indexCount: Int;
public function new(indexCount: Int, usage: Usage, canRead: Bool = false) {
this.indexCount = indexCount;
buffer = Krom.createIndexBuffer(indexCount);
}
public function delete() {
Krom.deleteIndexBuffer(buffer);
buffer = null;
}
public function lock(?start: Int, ?count: Int): Uint32Array {
_data = Krom.lockIndexBuffer(buffer);
if (start == null)
start = 0;
if (count == null)
count = indexCount;
return _data.subarray(start, start + count);
}
public function unlock(?count: Int): Void {
Krom.unlockIndexBuffer(buffer);
}
public function set(): Void {
Krom.setIndexBuffer(buffer);
}
public function count(): Int {
return indexCount;
}
}

View File

@ -0,0 +1,95 @@
package kha.graphics4;
import kha.graphics4.FragmentShader;
import kha.graphics4.VertexData;
import kha.graphics4.VertexShader;
import kha.graphics4.VertexStructure;
class PipelineState extends PipelineStateBase {
var pipeline: Dynamic;
public function new() {
super();
pipeline = Krom.createPipeline();
}
public function delete() {
Krom.deletePipeline(pipeline);
pipeline = null;
}
public function compile(): Void {
var structure0 = inputLayout.length > 0 ? inputLayout[0] : null;
var structure1 = inputLayout.length > 1 ? inputLayout[1] : null;
var structure2 = inputLayout.length > 2 ? inputLayout[2] : null;
var structure3 = inputLayout.length > 3 ? inputLayout[3] : null;
var gs = geometryShader != null ? geometryShader.shader : null;
var tcs = tessellationControlShader != null ? tessellationControlShader.shader : null;
var tes = tessellationEvaluationShader != null ? tessellationEvaluationShader.shader : null;
var stencilReferenceValue = 0;
switch (this.stencilReferenceValue) {
case Static(value):
stencilReferenceValue = value;
default:
}
Krom.compilePipeline(pipeline, structure0, structure1, structure2, structure3, inputLayout.length, vertexShader.shader, fragmentShader.shader, gs,
tcs, tes, {
cullMode: cullMode,
depthWrite: this.depthWrite,
depthMode: depthMode,
stencilMode: stencilFrontMode,
stencilBothPass: stencilFrontBothPass,
stencilDepthFail: stencilFrontDepthFail,
stencilFail: stencilFrontFail,
stencilReferenceValue: stencilReferenceValue,
stencilReadMask: stencilReadMask,
stencilWriteMask: stencilWriteMask,
blendSource: convertBlendingFactor(blendSource),
blendDestination: convertBlendingFactor(blendDestination),
alphaBlendSource: convertBlendingFactor(alphaBlendSource),
alphaBlendDestination: convertBlendingFactor(alphaBlendDestination),
colorWriteMaskRed: colorWriteMasksRed,
colorWriteMaskGreen: colorWriteMasksGreen,
colorWriteMaskBlue: colorWriteMasksBlue,
colorWriteMaskAlpha: colorWriteMasksAlpha,
conservativeRasterization: conservativeRasterization
});
}
public function set(): Void {
Krom.setPipeline(pipeline);
}
public function getConstantLocation(name: String): kha.graphics4.ConstantLocation {
return Krom.getConstantLocation(pipeline, name);
}
public function getTextureUnit(name: String): kha.graphics4.TextureUnit {
return Krom.getTextureUnit(pipeline, name);
}
static function convertBlendingFactor(factor: BlendingFactor): Int {
switch (factor) {
case Undefined, BlendOne:
return 0;
case BlendZero:
return 1;
case SourceAlpha:
return 2;
case DestinationAlpha:
return 3;
case InverseSourceAlpha:
return 4;
case InverseDestinationAlpha:
return 5;
case SourceColor:
return 6;
case DestinationColor:
return 7;
case InverseSourceColor:
return 8;
case InverseDestinationColor:
return 9;
}
}
}

View File

@ -0,0 +1,14 @@
package kha.graphics4;
class TessellationControlShader {
public var shader: Dynamic;
public function new(sources: Array<Blob>, names: Array<String>) {
shader = Krom.createTessellationControlShader(sources[0].bytes.getData(), names[0]);
}
public function delete() {
Krom.deleteShader(shader);
shader = null;
}
}

View File

@ -0,0 +1,14 @@
package kha.graphics4;
class TessellationEvaluationShader {
public var shader: Dynamic;
public function new(sources: Array<Blob>, names: Array<String>) {
shader = Krom.createTessellationEvaluationShader(sources[0].bytes.getData(), names[0]);
}
public function delete() {
Krom.deleteShader(shader);
shader = null;
}
}

View File

@ -0,0 +1,61 @@
package kha.graphics4;
import kha.arrays.Float32Array;
import kha.arrays.Int16Array;
import kha.graphics4.Usage;
import kha.graphics4.VertexStructure;
import kha.graphics4.VertexData;
class VertexBuffer {
public var buffer: Dynamic;
public var _data: Float32Array;
var vertexCount: Int;
var structure: VertexStructure;
var mySize: Int;
var lockStart: Int = 0;
var lockEnd: Int = 0;
public function new(vertexCount: Int, structure: VertexStructure, usage: Usage, instanceDataStepRate: Int = 0, canRead: Bool = false) {
this.vertexCount = vertexCount;
this.structure = structure;
mySize = vertexCount;
buffer = Krom.createVertexBuffer(vertexCount, structure.elements, usage, instanceDataStepRate);
}
public function delete() {
Krom.deleteVertexBuffer(buffer);
buffer = null;
}
public function lock(?start: Int, ?count: Int): Float32Array {
lockStart = start != null ? start : 0;
lockEnd = count != null ? start + count : mySize;
_data = new kha.arrays.ByteArray(Krom.lockVertexBuffer(buffer, lockStart, lockEnd));
return _data;
}
public function lockInt16(?start: Int, ?count: Int): Int16Array {
return new Int16Array(untyped lock(start, count).buffer);
}
public function unlock(?count: Int): Void {
if (count != null) {
lockEnd = lockStart + count;
}
Krom.unlockVertexBuffer(buffer, lockEnd);
}
public function stride(): Int {
return structure.byteSize();
}
public function count(): Int {
return vertexCount;
}
public function set(offset: Int): Int {
Krom.setVertexBuffer(buffer);
return 0;
}
}

View File

@ -0,0 +1,22 @@
package kha.graphics4;
class VertexShader {
public var shader: Dynamic;
public function new(sources: Array<Blob>, names: Array<String>) {
if (sources != null) {
shader = Krom.createVertexShader(sources[0].bytes.getData(), names[0]);
}
}
public static function fromSource(source: String): VertexShader {
var shader = new VertexShader(null, null);
shader.shader = Krom.createVertexShaderFromSource(source);
return shader;
}
public function delete() {
Krom.deleteShader(shader);
shader = null;
}
}

View File

@ -0,0 +1,41 @@
package kha.input;
import kha.SystemImpl;
class MouseImpl extends kha.input.Mouse {
public function new() {
super();
}
override public function lock(): Void {
SystemImpl.lockMouse();
}
override public function unlock(): Void {
SystemImpl.unlockMouse();
}
override public function canLock(): Bool {
return SystemImpl.canLockMouse();
}
override public function isLocked(): Bool {
return SystemImpl.isMouseLocked();
}
override public function notifyOnLockChange(func: Void->Void, error: Void->Void): Void {
SystemImpl.notifyOfMouseLockChange(func, error);
}
override public function removeFromLockChange(func: Void->Void, error: Void->Void): Void {
SystemImpl.removeFromMouseLockChange(func, error);
}
override public function hideSystemCursor(): Void {
SystemImpl.hideSystemCursor();
}
override public function showSystemCursor(): Void {
SystemImpl.showSystemCursor();
}
}

View File

@ -0,0 +1,256 @@
package kha.krom;
import kha.arrays.Float32Array;
import kha.Blob;
import kha.graphics4.BlendingFactor;
import kha.graphics4.BlendingOperation;
import kha.graphics4.CompareMode;
import kha.graphics4.CubeMap;
import kha.graphics4.CullMode;
import kha.graphics4.FragmentShader;
import kha.graphics4.IndexBuffer;
import kha.graphics4.MipMapFilter;
import kha.graphics4.PipelineState;
import kha.graphics4.StencilAction;
import kha.graphics4.TexDir;
import kha.graphics4.TextureAddressing;
import kha.graphics4.TextureFilter;
import kha.graphics4.TextureFormat;
import kha.graphics4.Usage;
import kha.graphics4.VertexBuffer;
import kha.graphics4.VertexStructure;
import kha.graphics4.VertexShader;
import kha.Image;
import kha.math.FastMatrix3;
import kha.math.FastMatrix4;
import kha.math.FastVector2;
import kha.math.FastVector3;
import kha.math.FastVector4;
import kha.math.Matrix4;
import kha.math.Vector2;
import kha.math.Vector3;
import kha.math.Vector4;
class Graphics implements kha.graphics4.Graphics {
var renderTarget: kha.Canvas;
public function new(renderTarget: kha.Canvas = null) {
this.renderTarget = renderTarget;
}
public function begin(additionalRenderTargets: Array<kha.Canvas> = null): Void {
Krom.begin(renderTarget, additionalRenderTargets);
}
public function beginFace(face: Int): Void {
Krom.beginFace(renderTarget, face);
}
public function beginEye(eye: Int): Void {}
public function end(): Void {
Krom.end();
}
public function flush(): Void {}
public function vsynced(): Bool {
return true;
}
public function refreshRate(): Int {
return 60;
}
public function clear(?color: Color, ?depth: Float, ?stencil: Int): Void {
var flags: Int = 0;
if (color != null)
flags |= 1;
if (depth != null)
flags |= 2;
if (stencil != null)
flags |= 4;
Krom.clear(flags, color == null ? 0 : color.value, depth, stencil);
}
public function viewport(x: Int, y: Int, width: Int, height: Int): Void {
Krom.viewport(x, y, width, height);
}
public function setVertexBuffer(vertexBuffer: kha.graphics4.VertexBuffer): Void {
vertexBuffer.set(0);
}
public function setVertexBuffers(vertexBuffers: Array<kha.graphics4.VertexBuffer>): Void {
Krom.setVertexBuffers(vertexBuffers);
}
public function setIndexBuffer(indexBuffer: kha.graphics4.IndexBuffer): Void {
indexBuffer.set();
}
public function setCubeMap(unit: kha.graphics4.TextureUnit, cubeMap: kha.graphics4.CubeMap): Void {
if (cubeMap == null)
return;
cubeMap.texture_ != null ? Krom.setTexture(unit, cubeMap.texture_) : Krom.setRenderTarget(unit, cubeMap.renderTarget_);
}
public function setCubeMapDepth(unit: kha.graphics4.TextureUnit, cubeMap: kha.graphics4.CubeMap): Void {
if (cubeMap == null)
return;
Krom.setTextureDepth(unit, cubeMap.renderTarget_);
}
public function setTexture(unit: kha.graphics4.TextureUnit, texture: kha.Image): Void {
if (texture == null)
return;
texture.texture_ != null ? Krom.setTexture(unit, texture.texture_) : Krom.setRenderTarget(unit, texture.renderTarget_);
}
public function setTextureDepth(unit: kha.graphics4.TextureUnit, texture: kha.Image): Void {
if (texture == null)
return;
Krom.setTextureDepth(unit, texture.renderTarget_);
}
public function setTextureArray(unit: kha.graphics4.TextureUnit, texture: kha.Image): Void {}
public function setVideoTexture(unit: kha.graphics4.TextureUnit, texture: kha.Video): Void {}
public function setImageTexture(unit: kha.graphics4.TextureUnit, texture: kha.Image): Void {
if (texture == null)
return;
Krom.setImageTexture(unit, texture.texture_);
}
public function setTextureParameters(texunit: kha.graphics4.TextureUnit, uAddressing: TextureAddressing, vAddressing: TextureAddressing,
minificationFilter: TextureFilter, magnificationFilter: TextureFilter, mipmapFilter: MipMapFilter): Void {
Krom.setTextureParameters(texunit, uAddressing, vAddressing, minificationFilter, magnificationFilter, mipmapFilter);
}
public function setTexture3DParameters(texunit: kha.graphics4.TextureUnit, uAddressing: TextureAddressing, vAddressing: TextureAddressing,
wAddressing: TextureAddressing, minificationFilter: TextureFilter, magnificationFilter: TextureFilter, mipmapFilter: MipMapFilter): Void {
Krom.setTexture3DParameters(texunit, uAddressing, vAddressing, wAddressing, minificationFilter, magnificationFilter, mipmapFilter);
}
public function setTextureCompareMode(texunit: kha.graphics4.TextureUnit, enabled: Bool): Void {
Krom.setTextureCompareMode(texunit, enabled);
}
public function setCubeMapCompareMode(texunit: kha.graphics4.TextureUnit, enabled: Bool): Void {
Krom.setCubeMapCompareMode(texunit, enabled);
}
public function maxBoundTextures(): Int {
return 8;
}
public function setPipeline(pipeline: PipelineState): Void {
pipeline.set();
}
public function setStencilReferenceValue(value: Int): Void {}
public function setBool(location: kha.graphics4.ConstantLocation, value: Bool): Void {
Krom.setBool(location, value);
}
public function setInt(location: kha.graphics4.ConstantLocation, value: Int): Void {
Krom.setInt(location, value);
}
public function setInt2(location: kha.graphics4.ConstantLocation, value1: Int, value2: Int): Void {}
public function setInt3(location: kha.graphics4.ConstantLocation, value1: Int, value2: Int, value3: Int): Void {}
public function setInt4(location: kha.graphics4.ConstantLocation, value1: Int, value2: Int, value3: Int, value4: Int): Void {}
public function setInts(location: kha.graphics4.ConstantLocation, values: kha.arrays.Int32Array): Void {}
public function setFloat(location: kha.graphics4.ConstantLocation, value: Float): Void {
Krom.setFloat(location, value);
}
public function setFloat2(location: kha.graphics4.ConstantLocation, value1: Float, value2: Float): Void {
Krom.setFloat2(location, value1, value2);
}
public function setFloat3(location: kha.graphics4.ConstantLocation, value1: Float, value2: Float, value3: Float): Void {
Krom.setFloat3(location, value1, value2, value3);
}
public function setFloat4(location: kha.graphics4.ConstantLocation, value1: Float, value2: Float, value3: Float, value4: Float): Void {
Krom.setFloat4(location, value1, value2, value3, value4);
}
public function setFloats(location: kha.graphics4.ConstantLocation, values: Float32Array): Void {
Krom.setFloats(location, values.buffer);
}
public function setVector2(location: kha.graphics4.ConstantLocation, value: FastVector2): Void {
Krom.setFloat2(location, value.x, value.y);
}
public function setVector3(location: kha.graphics4.ConstantLocation, value: FastVector3): Void {
Krom.setFloat3(location, value.x, value.y, value.z);
}
public function setVector4(location: kha.graphics4.ConstantLocation, value: FastVector4): Void {
Krom.setFloat4(location, value.x, value.y, value.z, value.w);
}
static var mat = new kha.arrays.Float32Array(16);
public inline function setMatrix(location: kha.graphics4.ConstantLocation, matrix: FastMatrix4): Void {
mat[0] = matrix._00;
mat[1] = matrix._01;
mat[2] = matrix._02;
mat[3] = matrix._03;
mat[4] = matrix._10;
mat[5] = matrix._11;
mat[6] = matrix._12;
mat[7] = matrix._13;
mat[8] = matrix._20;
mat[9] = matrix._21;
mat[10] = matrix._22;
mat[11] = matrix._23;
mat[12] = matrix._30;
mat[13] = matrix._31;
mat[14] = matrix._32;
mat[15] = matrix._33;
Krom.setMatrix(location, mat.buffer);
}
public inline function setMatrix3(location: kha.graphics4.ConstantLocation, matrix: FastMatrix3): Void {
mat[0] = matrix._00;
mat[1] = matrix._01;
mat[2] = matrix._02;
mat[3] = matrix._10;
mat[4] = matrix._11;
mat[5] = matrix._12;
mat[6] = matrix._20;
mat[7] = matrix._21;
mat[8] = matrix._22;
Krom.setMatrix3(location, mat.buffer);
}
public function drawIndexedVertices(start: Int = 0, count: Int = -1): Void {
Krom.drawIndexedVertices(start, count);
}
public function drawIndexedVerticesInstanced(instanceCount: Int, start: Int = 0, count: Int = -1): Void {
Krom.drawIndexedVerticesInstanced(instanceCount, start, count);
}
public function instancedRenderingAvailable(): Bool {
return true;
}
public function scissor(x: Int, y: Int, width: Int, height: Int): Void {
Krom.scissor(x, y, width, height);
}
public function disableScissor(): Void {
Krom.disableScissor();
}
}

View File

@ -0,0 +1,26 @@
package kha.krom;
import haxe.io.Bytes;
class Sound extends kha.Sound {
public function new(bytes: Bytes) {
super();
sampleRate = 44100;
var count = Std.int(bytes.length / 4);
uncompressedData = new kha.arrays.Float32Array(count);
for (i in 0...count) {
uncompressedData[i] = bytes.getFloat(i * 4);
}
compressedData = null;
}
override public function uncompress(done: Void->Void): Void {
done();
}
override public function unload(): Void {
super.unload();
}
}

View File

@ -0,0 +1,8 @@
package kha.network;
import haxe.io.Bytes;
class Http {
public static function request(url: String, path: String, data: String, port: Int, secure: Bool, method: HttpMethod, contentType: String,
callback: Int->Int->String->Void /*error, response, body*/): Void {}
}

View File

@ -0,0 +1,11 @@
package kha.network;
import haxe.io.Bytes;
class Network {
public function new(url: String, port: Int, errorCallback: Void->Void, closeCallback: Void->Void) {}
public function send(bytes: Bytes, mandatory: Bool): Void {}
public function listen(listener: Bytes->Void): Void {}
}