Skip to content

Commit

Permalink
some minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Dec 15, 2024
1 parent 0168e6a commit e6d0a69
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test/bin/cpp/*
.vscode/
.zed/
!test/bin/cpp/images/
!test/bin/cpp/images/
!test/bin/cpp/sound/
12 changes: 9 additions & 3 deletions src/flixel/FlxG.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>')
class FlxG {
public static var elapsed(get, null):Float;

Expand All @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/flixel/FlxGame.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -22,8 +24,7 @@ class FlxGame {
endMode2D();
}
}
FlxG.sound.update(getFrameTime());
FlxG.state.update(getFrameTime());
drawFPS(1, 1);
endDrawing();
}
closeAudioDevice();
Expand Down
14 changes: 14 additions & 0 deletions src/flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
95 changes: 69 additions & 26 deletions src/flixel/group/FlxGroup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ class FlxTypedGroup<T:FlxBasic> 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();
}
}
}
Expand All @@ -35,15 +34,15 @@ class FlxTypedGroup<T:FlxBasic> extends FlxBasic {

if (maxSize > 0 && length >= maxSize)
return basic;

members.push(basic);

return basic;
}

override public function destroy() {
super.destroy();
if(members != null){
if (members != null) {
for (member in members) {
member?.destroy();
member = null;
Expand All @@ -54,25 +53,69 @@ class FlxTypedGroup<T:FlxBasic> 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<T> {
return getFirstHelper((basic) -> basic.exists && basic.alive);
}

function getFirstHelper(func:T->Bool):Null<T> {
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;
}
}
25 changes: 14 additions & 11 deletions src/flixel/sound/FlxSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}

Binary file not shown.
Binary file not shown.
Binary file not shown.
17 changes: 8 additions & 9 deletions test/src/SoundState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}

0 comments on commit e6d0a69

Please sign in to comment.