diff --git a/.gitignore b/.gitignore index 440905c..fd2c5b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ test/bin/cpp/* .vscode/ .zed/ -!test/bin/cpp/images/ \ No newline at end of file +!test/bin/cpp/images/ +!test/bin/cpp/sound/ \ No newline at end of file diff --git a/src/flixel/FlxG.hx b/src/flixel/FlxG.hx index 5611a29..99f8faa 100644 --- a/src/flixel/FlxG.hx +++ b/src/flixel/FlxG.hx @@ -3,7 +3,10 @@ package flixel; import flixel.FlxCamera; import flixel.system.frontEnds.CameraFrontEnd; import flixel.system.frontEnds.SoundFrontEnd; +import haxe.Log; +import haxe.PosInfos; +@:cppFileCode('#include ') class FlxG { public static var elapsed(get, null):Float; @@ -23,20 +26,23 @@ class FlxG { public static var sound(default, null):SoundFrontEnd = new SoundFrontEnd(); - @:noCompletion static function get_width() { + @:noCompletion + static inline function get_width() { return getScreenWidth(); } - @:noCompletion static function get_height() { + @:noCompletion + static inline function get_height() { return getScreenHeight(); } @:noCompletion - static function get_elapsed() { + static inline function get_elapsed() { return getFrameTime(); } @:allow(flixel.FlxGame.new) static function init(game:FlxGame, width:Int, height:Int) { + Log.trace = (v:Dynamic, ?infos:PosInfos) -> untyped __cpp__("std::cout << {0}", '${Log.formatOutput(v, infos)}\n'); initialWidth = width; initialHeight = height; cameras.reset(); diff --git a/src/flixel/FlxGame.hx b/src/flixel/FlxGame.hx index 24ba8bc..26cd34a 100644 --- a/src/flixel/FlxGame.hx +++ b/src/flixel/FlxGame.hx @@ -10,6 +10,8 @@ class FlxGame { while (!windowShouldClose()) { beginDrawing(); clearBackground(FlxG.state.bgColor); + FlxG.sound.update(getFrameTime()); + FlxG.state.update(getFrameTime()); for (camera in FlxG.cameras.list) { if (camera != null && camera.exists && camera.active) { beginMode2D(camera._camera); @@ -22,8 +24,7 @@ class FlxGame { endMode2D(); } } - FlxG.sound.update(getFrameTime()); - FlxG.state.update(getFrameTime()); + drawFPS(1, 1); endDrawing(); } closeAudioDevice(); diff --git a/src/flixel/FlxSprite.hx b/src/flixel/FlxSprite.hx index 887b908..dac3af5 100644 --- a/src/flixel/FlxSprite.hx +++ b/src/flixel/FlxSprite.hx @@ -12,6 +12,8 @@ class FlxSprite extends FlxObject { public var color:Color = Colors.WHITE; + public var alpha(default, set):Float = 1.0; + public function new(x:Float = 0, y:Float = 0, ?graphic:String) { super(x, y); if (graphic != null) { @@ -29,8 +31,15 @@ class FlxSprite extends FlxObject { return this; } + public inline function isOnScreen():Bool { + return !((y + height < 0 || y > FlxG.height) || (x + width < 0 || x > FlxG.width)); + } + override public function draw() { super.draw(); + if (!visible || alpha < 0.001 || !camera.visible || !isOnScreen()){ + return; + } drawTexturePro(texture, Rectangle.create(0, 0, texture.width, texture.height), Rectangle.create((texture.width / 2) + x, (texture.height / 2) + y, texture.width, texture.height), Vector2.create(texture.width / 2, texture.height / 2), angle, color); @@ -58,4 +67,9 @@ class FlxSprite extends FlxObject { texture.height = Std.int(value); return super.set_height(value); } + + @:noCompletion + inline function set_alpha(value:Float):Float { + return alpha = value = color.a = Std.int(value * 255); + } } diff --git a/src/flixel/group/FlxGroup.hx b/src/flixel/group/FlxGroup.hx index f8ece6e..f6c1dc1 100644 --- a/src/flixel/group/FlxGroup.hx +++ b/src/flixel/group/FlxGroup.hx @@ -9,19 +9,18 @@ class FlxTypedGroup extends FlxBasic { public var maxSize(default, set):Int; - public var length(default, null):Int = 0; + public var length(default, null):Int = 0; - public function new(maxSize:Int = 0) { - super(); + super(); members = []; this.maxSize = Std.int(Math.abs(maxSize)); } - + override public function draw():Void { - for (basic in members) { - if (basic != null && basic.exists && basic.visible) { - basic.draw(); + for (basic in members) { + if (basic != null && basic.exists && basic.visible) { + basic.draw(); } } } @@ -35,7 +34,7 @@ class FlxTypedGroup extends FlxBasic { if (maxSize > 0 && length >= maxSize) return basic; - + members.push(basic); return basic; @@ -43,7 +42,7 @@ class FlxTypedGroup extends FlxBasic { override public function destroy() { super.destroy(); - if(members != null){ + if (members != null) { for (member in members) { member?.destroy(); member = null; @@ -54,25 +53,69 @@ class FlxTypedGroup extends FlxBasic { override public function update(elapsed:Float) { super.update(elapsed); for (basic in members) { - if (basic != null && basic.exists && basic.active) { - basic.update(elapsed); + if (basic != null && basic.exists && basic.active) { + basic.update(elapsed); } } } - @:noCompletion - function set_maxSize(size:Int):Int { - maxSize = Std.int(Math.abs(size)); - - if (maxSize == 0 || members == null || maxSize >= length) - return maxSize; - - // If the max size has shrunk, we need to get rid of some objects - while (length > maxSize) { - members.splice(maxSize - 1, 1)[0]?.destroy(); - length--; - } - - return maxSize; - } + @:noCompletion + function set_maxSize(size:Int):Int { + maxSize = Std.int(Math.abs(size)); + + if (maxSize == 0 || members == null || maxSize >= length) + return maxSize; + + // If the max size has shrunk, we need to get rid of some objects + while (length > maxSize) { + members.splice(maxSize - 1, 1)[0]?.destroy(); + length--; + } + + return maxSize; + } + + public function remove(basic:T, splice = false):T { + if (members == null) + return null; + + final index:Int = members.indexOf(basic); + + if (index < 0) + return null; + + if (splice) { + members.splice(index, 1); + length--; + } else + members[index] = null; + + return basic; + } + + public inline function getFirstAlive():Null { + return getFirstHelper((basic) -> basic.exists && basic.alive); + } + + function getFirstHelper(func:T->Bool):Null { + for (basic in members) { + if (basic != null && func(basic)) { + return basic; + } + } + return null; + } + + public function countLiving():Int { + var count:Int = 0; + + for (basic in members) { + if (basic != null) { + if (basic.exists && basic.alive) + count++; + } + } + + return count; + } } diff --git a/src/flixel/sound/FlxSound.hx b/src/flixel/sound/FlxSound.hx index 4e05cec..9ea5a9e 100644 --- a/src/flixel/sound/FlxSound.hx +++ b/src/flixel/sound/FlxSound.hx @@ -9,11 +9,11 @@ class FlxSound extends FlxBasic { public var playing(get, null):Bool = false; - public var pitch(default, set):Float = 1.0; + public var pitch(default, set):Float = 1.0; - public var volume(default, set):Float = 1.0; + public var volume(default, set):Float = 1.0; - public var pan(default, set):Float = 0.5; + public var pan(default, set):Float = 0.5; public var persist:Bool; @@ -48,23 +48,26 @@ class FlxSound extends FlxBasic { updateMusicStream(music); } - @:noCompletion inline function get_playing():Bool { + @:noCompletion + inline function get_playing():Bool { return isMusicStreamPlaying(music); } - @:noCompletion function set_pitch(pitch:Float):Float { - setMusicPitch(music, pitch); + @:noCompletion + function set_pitch(pitch:Float):Float { + setMusicPitch(music, pitch); return this.pitch = pitch; } - @:noCompletion function set_volume(volume:Float):Float { - setMusicVolume(music, volume); + @:noCompletion + function set_volume(volume:Float):Float { + setMusicVolume(music, volume); return this.volume = volume; } - @:noCompletion function set_pan(pan:Float):Float { - setMusicPan(music, pan); + @:noCompletion + function set_pan(pan:Float):Float { + setMusicPan(music, pan); return this.pan = pan; } } - diff --git a/test/bin/cpp/sound/BACKSTREET BOUNCE (RAP Version) - Key After Key.mp3 b/test/bin/cpp/sound/BACKSTREET BOUNCE (RAP Version) - Key After Key.mp3 new file mode 100644 index 0000000..208fcf8 Binary files /dev/null and b/test/bin/cpp/sound/BACKSTREET BOUNCE (RAP Version) - Key After Key.mp3 differ diff --git a/test/bin/cpp/sound/BICYCLE KICK - CYBERVORTEX PHIGHTING OST - aidn.wav b/test/bin/cpp/sound/BICYCLE KICK - CYBERVORTEX PHIGHTING OST - aidn.wav new file mode 100644 index 0000000..98d6240 Binary files /dev/null and b/test/bin/cpp/sound/BICYCLE KICK - CYBERVORTEX PHIGHTING OST - aidn.wav differ diff --git a/test/bin/cpp/sound/Hades II - Mountain of the Gods - Supergiant Games.ogg b/test/bin/cpp/sound/Hades II - Mountain of the Gods - Supergiant Games.ogg new file mode 100644 index 0000000..74616c0 Binary files /dev/null and b/test/bin/cpp/sound/Hades II - Mountain of the Gods - Supergiant Games.ogg differ diff --git a/test/src/SoundState.hx b/test/src/SoundState.hx index 1cce832..3a535fe 100644 --- a/test/src/SoundState.hx +++ b/test/src/SoundState.hx @@ -21,16 +21,15 @@ class SoundState extends FlxState { FlxG.switchState(new TextState()); } - if(Raylib.isKeyDown(263)){ - sound.pitch -= elapsed; - } + if (Raylib.isKeyDown(KEY_LEFT)) { + sound.pitch -= elapsed; + } - if(Raylib.isKeyDown(262)){ - sound.pitch += elapsed; - } + if (Raylib.isKeyDown(KEY_RIGHT)) { + sound.pitch += elapsed; + } - //sound.volume = Math.abs(Math.sin(Raylib.getTime())); - sound.pan = Math.sin(Raylib.getTime()); + // sound.volume = Math.abs(Math.sin(Raylib.getTime())); + // sound.pan = Math.sin(Raylib.getTime()); } - }