Skip to content

Commit

Permalink
unload old texture when calling loadGraphic
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Dec 22, 2024
1 parent 96d690d commit a5f15a8
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 9 deletions.
71 changes: 66 additions & 5 deletions src/flixel/FlxObject.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package flixel;

import flixel.math.FlxVelocity;
import flixel.math.FlxPoint;
import flixel.util.FlxAxes;

class FlxObject extends FlxBasic {
Expand All @@ -13,13 +15,47 @@ class FlxObject extends FlxBasic {

public var angle:Float = 0;

public var velocity(default, null):FlxPoint;

public var maxVelocity(default, null):FlxPoint;

public var scrollFactor(default, null):FlxPoint;

public var acceleration(default, null):FlxPoint;

public var drag(default, null):FlxPoint;

public var angularVelocity:Float = 0;

public var angularAcceleration:Float = 0;

public var angularDrag:Float = 0;

public var maxAngular:Float = 10000;

public function new(x:Float = 0, y:Float, width:Float = 0, height:Float = 0) {
super();

this.x = x;
this.y = y;
this.width = width;
this.height = height;

initVars();
}

@:noCompletion
inline function initVars() {
scrollFactor = FlxPoint.create(1, 1);
initMotionVars();
}

@:noCompletion
inline function initMotionVars() {
velocity = FlxPoint.create();
acceleration = FlxPoint.create();
drag = FlxPoint.create();
maxVelocity = FlxPoint.create(10000, 10000);
}

public function screenCenter(axes:FlxAxes = XY):FlxObject {
Expand All @@ -32,14 +68,29 @@ class FlxObject extends FlxBasic {
return this;
}

@:noCompletion
function get_width():Float {
return width;
override public function update(elapsed:Float) {
updateMotion(elapsed);
}

@:noCompletion
function get_height():Float {
return height;
function updateMotion(elapsed:Float):Void {
var velocityDelta:Float = 0.5 * (FlxVelocity.computeVelocity(angularVelocity, angularAcceleration, angularDrag, maxAngular, elapsed)
- angularVelocity);
angularVelocity += velocityDelta;
angle += angularVelocity * elapsed;
angularVelocity += velocityDelta;

velocityDelta = 0.5 * (FlxVelocity.computeVelocity(velocity.x, acceleration.x, drag.x, maxVelocity.x, elapsed) - velocity.x);
velocity.x += velocityDelta;
var delta:Float = velocity.x * elapsed;
velocity.x += velocityDelta;
x += delta;

velocityDelta = 0.5 * (FlxVelocity.computeVelocity(velocity.y, acceleration.y, drag.y, maxVelocity.y, elapsed) - velocity.y);
velocity.y += velocityDelta;
delta = velocity.y * elapsed;
velocity.y += velocityDelta;
y += delta;
}

@:noCompletion
Expand All @@ -51,4 +102,14 @@ class FlxObject extends FlxBasic {
function set_height(value:Float):Float {
return height = value;
}

@:noCompletion
function get_height():Float {
return height;
}

@:noCompletion
function get_width():Float {
return width;
}
}
4 changes: 4 additions & 0 deletions src/flixel/FlxSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ class FlxSprite extends FlxObject {
}

public inline overload extern function loadGraphic(graphic:String):FlxSprite {
unloadTexture(texture);
texture = loadTexture(graphic);
return this;
}

public inline overload extern function loadGraphic(image:Image):FlxSprite {
unloadTexture(texture);
texture = loadTextureFromImage(image);
return this;
}
Expand Down Expand Up @@ -65,11 +67,13 @@ class FlxSprite extends FlxObject {
return texture;
}

@:noCompletion
override function set_width(value:Float) {
texture.width = Std.int(value);
return super.set_width(value);
}

@:noCompletion
override function set_height(value:Float) {
texture.height = Std.int(value);
return super.set_height(value);
Expand Down
5 changes: 5 additions & 0 deletions src/flixel/math/FlxPoint.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package flixel.math;

import raylib.Vector2;

typedef FlxPoint = Vector2;
26 changes: 26 additions & 0 deletions src/flixel/math/FlxVelocity.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package flixel.math;

class FlxVelocity {
public static function computeVelocity(velocity:Float, acceleration:Float, drag:Float, max:Float, elapsed:Float):Float {
if (acceleration != 0) {
velocity += acceleration * elapsed;
} else if (drag != 0) {
var _drag:Float = drag * elapsed;
if (velocity - _drag > 0) {
velocity -= _drag;
} else if (velocity + _drag < 0) {
velocity += _drag;
} else {
velocity = 0;
}
}
if ((velocity != 0) && (max != 0)) {
if (velocity > max) {
velocity = max;
} else if (velocity < -max) {
velocity = -max;
}
}
return velocity;
}
}
2 changes: 1 addition & 1 deletion src/raylib/Vector2.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern class Vector2 {

public var y:Float;

public static inline function create(x:Float, y:Float):Vector2 {
public static inline function create(x:Float = 0, y:Float = 0):Vector2 {
return untyped __cpp__("Vector2{(float){0}, (float){1}}", x, y);
}
}
2 changes: 1 addition & 1 deletion src/raylib/Vector3.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern class Vector3 {

public var z:Float;

public static inline function create(x:Float, y:Float, z:Float):Vector3 {
public static inline function create(x:Float = 0, y:Float = 0, z:Float = 0):Vector3 {
return untyped __cpp__("Vector3{(float){0}, (float){1}, (float){2}}", x, y, z);
}
}
2 changes: 1 addition & 1 deletion src/raylib/Vector4.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern class Vector4 {

public var w:Float;

public static inline function create(x:Float, y:Float, z:Float, w:Float):Vector4 {
public static inline function create(x:Float = 0, y:Float = 0, z:Float = 0, w:Float = 0):Vector4 {
return untyped __cpp__("Vector4{(float){0}, (float){1}, (float){2}, (float){3}}", x, y, z, w);
}
}
3 changes: 2 additions & 1 deletion test/src/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PlayState extends FlxState {

skateboard = new FlxSprite();
skateboard.loadGraphic("images/skateboard.png");
skateboard.velocity.x = 15;
add(skateboard);

scythe = new FlxSprite();
Expand All @@ -31,7 +32,7 @@ class PlayState extends FlxState {

scythe.x = RayMath.lerp(scythe.x, Raylib.getMousePosition().x, elapsed * 10);
scythe.y = RayMath.lerp(scythe.y, Raylib.getMousePosition().y, elapsed * 10);

if (Raylib.isKeyPressed(KEY_SPACE)) {
FlxG.switchState(new OtherState());
}
Expand Down

0 comments on commit a5f15a8

Please sign in to comment.