Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #1496

Closed
wants to merge 3 commits into from
Closed

Cleanup #1496

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Backends/HTML5/kha/LoaderImpl.hx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package kha;

import js.html.FileReader;
import js.Syntax;
import haxe.io.Bytes;
import js.Browser;
import js.Syntax;
import js.html.FileReader;
import js.html.ImageElement;
import js.html.XMLHttpRequest;
import haxe.io.Bytes;
import kha.Blob;
import kha.js.WebAudioSound;
import kha.js.MobileWebAudioSound;
import kha.graphics4.TextureFormat;
import kha.graphics4.Usage;
import kha.js.MobileWebAudioSound;
import kha.js.WebAudioSound;

using StringTools;

Expand All @@ -22,7 +22,7 @@ class LoaderImpl {
return ["png", "jpg", "hdr"];
}

public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) {
public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: (err: AssetError) -> Void) {
var readable = Reflect.hasField(desc, "readable") ? desc.readable : false;
if (StringTools.endsWith(desc.files[0], ".hdr")) {
loadBlobFromDescription(desc, function(blob) {
Expand Down Expand Up @@ -56,7 +56,7 @@ class LoaderImpl {
return formats;
}

public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: AssetError->Void) {
public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: (err: AssetError) -> Void) {
if (SystemImpl._hasWebAudio) {
#if !kha_debug_html5
var element = Browser.document.createAudioElement();
Expand Down Expand Up @@ -154,11 +154,11 @@ class LoaderImpl {
#end
}

public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void {
public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: (err: AssetError) -> Void): Void {
kha.js.Video.fromFile(desc.files, done);
}

public static function loadRemote(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) {
public static function loadRemote(desc: Dynamic, done: Blob->Void, failed: (err: AssetError) -> Void) {
var request = untyped new XMLHttpRequest();
request.open("GET", desc.files[0], true);
request.responseType = "arraybuffer";
Expand Down Expand Up @@ -194,7 +194,7 @@ class LoaderImpl {
request.send(null);
}

public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) {
public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: (err: AssetError) -> Void) {
#if kha_debug_html5
var file: String = desc.files[0];

Expand Down Expand Up @@ -228,7 +228,7 @@ class LoaderImpl {
#end
}

public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void {
public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: (err: AssetError) -> Void): Void {
loadBlobFromDescription(desc, function(blob: Blob) {
done(new Font(blob));
}, failed);
Expand Down
2 changes: 1 addition & 1 deletion Backends/HTML5/kha/SystemImpl.hx
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ class SystemImpl {
}
// canvas.getContext("2d").scale(transform, transform);

if (!mobile && kha.audio2.Audio._init()) {
if ((!mobile || options.audio.allowMobileWebAudio) && kha.audio2.Audio._init()) {
SystemImpl._hasWebAudio = true;
kha.audio2.Audio1._init();
}
Expand Down
6 changes: 3 additions & 3 deletions Backends/HTML5/kha/audio2/Audio.hx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package kha.audio2;

import js.Syntax;
import js.Browser;
import js.Syntax;
import js.html.URL;
import js.html.audio.AudioContext;
import js.html.audio.AudioProcessingEvent;
import js.html.audio.ScriptProcessorNode;
import kha.Sound;
import kha.internal.IntBox;
import kha.js.AEAudioChannel;
import kha.Sound;

class Audio {
public static var disableGcInteractions = false;
Expand Down Expand Up @@ -70,7 +70,7 @@ class Audio {

public static var samplesPerSecond: Int;

public static var audioCallback: kha.internal.IntBox->Buffer->Void;
public static var audioCallback: (outputBufferLength: IntBox, buffer: Buffer) -> Void;

static var virtualChannels: Array<VirtualStreamChannel> = [];

Expand Down
49 changes: 20 additions & 29 deletions Sources/kha/Assets.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kha;

import haxe.io.Bytes;
import haxe.Unserializer;
import haxe.io.Bytes;

using StringTools;

Expand Down Expand Up @@ -144,16 +144,14 @@ class Assets {
onLoaded(bytes);
}

function loadFunc(desc: Dynamic, done: (bytes: Int) -> Void, failure: (err: AssetError, bytes: Int) -> Void): Void {
function loadFunc(desc: AssetData, done: (bytes: Int) -> Void, failure: (err: AssetError, bytes: Int) -> Void): Void {
final name = desc.name;
final size = desc.file_sizes[0];
switch (desc.type) {
case "image":
Assets.loadImage(name, function(image: Image) done(size), function(err: AssetError) {
onError(err, size);
});
Assets.loadImage(name, image -> done(size), err -> onError(err, size));
case "sound":
Assets.loadSound(name, function(sound: Sound) {
Assets.loadSound(name, sound -> {
if (uncompressSoundsFilter == null || uncompressSoundsFilter(desc)) {
sound.uncompress(function() {
done(size);
Expand All @@ -162,21 +160,13 @@ class Assets {
else {
done(size);
}
}, function(err: AssetError) {
onError(err, size);
});
}, err -> onError(err, size));
case "blob":
Assets.loadBlob(name, function(blob: Blob) done(size), function(err: AssetError) {
onError(err, size);
});
Assets.loadBlob(name, blob -> done(size), err -> onError(err, size));
case "font":
Assets.loadFont(name, function(font: Font) done(size), function(err: AssetError) {
onError(err, size);
});
Assets.loadFont(name, font -> done(size), err -> onError(err, size));
case "video":
Assets.loadVideo(name, function(video: Video) done(size), function(err: AssetError) {
onError(err, size);
});
Assets.loadVideo(name, video -> done(size), err -> onError(err, size));
}
}

Expand All @@ -203,7 +193,7 @@ class Assets {
* @param name The name as defined by the khafile.
* @param done A callback.
*/
public static function loadImage(name: String, done: Image->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadImage(name: String, done: (image: Image) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = Reflect.field(images, name + "Description");
if (description == null) {
reporter(failed, pos)({url: name, error: "Name not found"});
Expand All @@ -222,7 +212,8 @@ class Assets {
* @param readable If true, a copy of the image will be kept in main memory for image read operations.
* @param done A callback.
*/
public static function loadImageFromPath(path: String, readable: Bool, done: Image->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadImageFromPath(path: String, readable: Bool, done: (image: Image) -> Void, ?failed: (err: AssetError) -> Void,
?pos: haxe.PosInfos): Void {
var description = {files: [path], readable: readable};
LoaderImpl.loadImageFromDescription(description, done, reporter(failed, pos));
}
Expand All @@ -233,7 +224,7 @@ class Assets {
return LoaderImpl.getImageFormats();
}

public static function loadBlob(name: String, done: Blob->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadBlob(name: String, done: (blob: Blob) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = Reflect.field(blobs, name + "Description");
if (description == null) {
reporter(failed, pos)({url: name, error: "Name not found"});
Expand All @@ -245,12 +236,12 @@ class Assets {
}, reporter(failed, pos));
}

public static function loadBlobFromPath(path: String, done: Blob->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadBlobFromPath(path: String, done: (blob: Blob) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = {files: [path]};
LoaderImpl.loadBlobFromDescription(description, done, reporter(failed, pos));
}

public static function loadSound(name: String, done: Sound->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadSound(name: String, done: (sound: Sound) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = Reflect.field(sounds, name + "Description");
if (description == null) {
reporter(failed, pos)({url: name, error: "Name not found"});
Expand All @@ -262,7 +253,7 @@ class Assets {
}, reporter(failed, pos));
}

public static function loadSoundFromPath(path: String, done: Sound->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadSoundFromPath(path: String, done: (sound: Sound) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = {files: [path]};
return LoaderImpl.loadSoundFromDescription(description, done, reporter(failed, pos));
}
Expand All @@ -273,7 +264,7 @@ class Assets {
return LoaderImpl.getSoundFormats();
}

public static function loadFont(name: String, done: Font->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadFont(name: String, done: (font: Font) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = Reflect.field(fonts, name + "Description");
if (description == null) {
reporter(failed, pos)({url: name, error: "Name not found"});
Expand All @@ -285,7 +276,7 @@ class Assets {
}, reporter(failed, pos));
}

public static function loadFontFromPath(path: String, done: Font->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadFontFromPath(path: String, done: (font: Font) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = {files: [path]};
return LoaderImpl.loadFontFromDescription(description, done, reporter(failed, pos));
}
Expand All @@ -296,7 +287,7 @@ class Assets {
return ["ttf"];
}

public static function loadVideo(name: String, done: Video->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadVideo(name: String, done: (video: Video) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = Reflect.field(videos, name + "Description");
if (description == null) {
reporter(failed, pos)({url: name, error: "Name not found"});
Expand All @@ -308,7 +299,7 @@ class Assets {
}, reporter(failed, pos));
}

public static function loadVideoFromPath(path: String, done: Video->Void, ?failed: AssetError->Void, ?pos: haxe.PosInfos): Void {
public static function loadVideoFromPath(path: String, done: (video: Video) -> Void, ?failed: (err: AssetError) -> Void, ?pos: haxe.PosInfos): Void {
var description = {files: [path]};
return LoaderImpl.loadVideoFromDescription(description, done, reporter(failed, pos));
}
Expand All @@ -319,7 +310,7 @@ class Assets {
return LoaderImpl.getVideoFormats();
}

public static function reporter(custom: AssetError->Void, ?pos: haxe.PosInfos) {
public static function reporter(custom: (err: AssetError) -> Void, ?pos: haxe.PosInfos) {
return custom != null ? custom : haxe.Log.trace.bind(_, pos);
}
}
38 changes: 23 additions & 15 deletions Sources/kha/System.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ package kha;

import kha.WindowOptions;

@:structInit
private class AudioOptions {
/** Allow `audio2.Audio` api initialization on mobile browsers (only for HTML5 target) **/
public var allowMobileWebAudio = false;
}

@:structInit
class SystemOptions {
@:optional public var title: String = "Kha";
@:optional public var width: Int = -1;
@:optional public var height: Int = -1;
@:optional public var window: WindowOptions = null;
@:optional public var framebuffer: FramebufferOptions = null;
public var title: String = "Kha";
public var width: Int = -1;
public var height: Int = -1;
public var window: WindowOptions = null;
public var framebuffer: FramebufferOptions = null;
public var audio: AudioOptions = null;

/**
* Used to provide parameters for System.start
* Used to provide parameters for `System.start`
* @param title The application title is the default window title (unless the window parameter provides a title of its own)
* and is used for various other purposes - for example for save data locations
* @param width Just a shortcut which overwrites window.width if set
* @param height Just a shortcut which overwrites window.height if set
* @param width Just a shortcut which overwrites `window.width` if set
* @param height Just a shortcut which overwrites `window.height` if set
* @param window Optionally provide window options
* @param framebuffer Optionally provide framebuffer options
* @param audio Optionally provide audio options
*/
public function new(title: String = "Kha", ?width: Int = -1, ?height: Int = -1, window: WindowOptions = null, framebuffer: FramebufferOptions = null) {
public function new(title: String = "Kha", ?width: Int = -1, ?height: Int = -1, ?window: WindowOptions, ?framebuffer: FramebufferOptions,
?audio: AudioOptions) {
this.title = title;
this.window = window == null ? {} : window;
this.window = window ?? {};

if (width > 0) {
this.window.width = width;
Expand All @@ -39,11 +48,10 @@ class SystemOptions {
this.height = this.window.height;
}

if (this.window.title == null) {
this.window.title = title;
}
this.window.title ??= title;

this.framebuffer = framebuffer == null ? {} : framebuffer;
this.framebuffer = framebuffer ?? {};
this.audio = audio ?? {};
}
}

Expand Down Expand Up @@ -103,7 +111,7 @@ class System {
});
}

public static function start(options: SystemOptions, callback: Window->Void): Void {
public static function start(options: SystemOptions, callback: (window: Window) -> Void): Void {
theTitle = options.title;
SystemImpl.init(options, callback);
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/kha/audio2/Audio.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kha.audio2;

import kha.internal.IntBox;

extern class Audio {
/**
* The samples per second natively used by the target system.
Expand All @@ -11,7 +13,7 @@ extern class Audio {
* Beware: This is called from a separate audio thread on some targets.
* See kha.audio2.Audio1 for sample code.
*/
public static var audioCallback: Int->Buffer->Void;
public static var audioCallback: (outputBufferLength:IntBox, buffer:Buffer)->Void;

/**
* Similar to kha.audio1.Audio.stream, but only for hardware accelerated audio playback.
Expand Down
Loading
Loading