Skip to content

Commit

Permalink
fix lod for flixel update
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Apr 17, 2024
1 parent 87158ce commit 52cf320
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 275 deletions.
274 changes: 12 additions & 262 deletions source/funkin/graphics/TypedGroup.hx
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
package funkin.graphics;

import flixel.FlxBasic;
#if (flixel >= "5.7.0")
import flixel.group.FlxContainer.FlxTypedContainer;
#end

// Just flixel groups with unsafe gets for performance

typedef Group = TypedGroup<FlxBasic>;

class TypedGroup<T:FlxBasic> extends FlxTypedGroup<T>
class TypedGroup<T:FlxBasic> extends #if (flixel >= "5.7.0") FlxTypedContainer<T> #else FlxTypedGroup<T> #end
{
@:noCompletion
override function get_camera():FlxCamera @:privateAccess {
if (_cameras != null) if (_cameras.length != 0) return _cameras[0];
return FlxCamera._defaultCameras[0];
}

@:noCompletion
override inline function set_camera(Value:FlxCamera):FlxCamera {
if (_cameras == null) _cameras = [Value];
else _cameras.unsafeSet(0, Value);
return Value;
}

@:noCompletion
override inline function get_cameras():Array<FlxCamera> {
@:privateAccess
return (_cameras == null) ? FlxCamera._defaultCameras : _cameras;
}

@:noCompletion
override inline function set_cameras(Value:Array<FlxCamera>):Array<FlxCamera> {
return _cameras = Value;
}

public inline function setNull(object:T) {
var index:Int = members.indexOf(object);
if (index != -1) {
Expand Down Expand Up @@ -60,11 +39,11 @@ class TypedGroup<T:FlxBasic> extends FlxTypedGroup<T>
var index:Int = 0;
final l:Int = members.length;
while (index < l) {
index++;
if (members[index] == null) {
members.unsafeSet(index, object);
return;
}
index++;
}

members.unshift(object);
Expand Down Expand Up @@ -104,247 +83,18 @@ typedef SpriteGroup = TypedSpriteGroup<FlxSprite>;

class TypedSpriteGroup<T:FlxSprite> extends FlxTypedSpriteGroup<T>
{
#if (flixel < "5.7.0")
public function new(x:Float = 0, y:Float = 0, maxSize:Int = 0) {
super(x, y, maxSize);
group.destroy();

group = new TypedGroup<T>(maxSize);
#if (flixel < "5.7.0")
_sprites = cast group.members;
#end
}
}

/*class TypedSpriteGroup<T:FlxSprite> extends FlxObject {
public var group:TypedGroup<T>; // Group containing everything
override inline function get_camera():FlxCamera return group.camera;
override inline function set_camera(Value:FlxCamera):FlxCamera return group.camera = Value;
override inline function get_cameras():Array<FlxCamera> return group.cameras;
override inline function set_cameras(Value:Array<FlxCamera>):Array<FlxCamera> return group.cameras = Value;
public var members(get, never):Array<T>;
inline function get_members():Array<T> return group.members;
inline public function add(basic:T):T return group.add(basic);
inline public function recycle(?basicClass:Class<T>):T return group.recycle(basicClass);
public var offset(default, null):FlxPoint;
public var origin(default, null):FlxPoint;
var _cos(default, null):Float = 1.0;
var _sin(default, null):Float = 0.0;
override function set_angle(value:Float):Float {
if (angle != value) {
var rads:Float = value * CoolUtil.TO_RADS;
_cos = CoolUtil.cos(rads);
_sin = CoolUtil.sin(rads);
}
return angle = value;
}
public var alpha:Float = 1.0;
public var color(default, set):FlxColor = FlxColor.WHITE;
function set_color(value:FlxColor):FlxColor {
if (value != color) {
for (basic in members)
basic.color = value;
}
return color = value;
}
override function get_width():Float {
var w:Float = 0.0;
for (member in members) {
if (member == null || !member.alive) continue;
var value = member.x + member.width;
if (value > w) w = value;
}
return w;
}
override function get_height():Float {
var h:Float = 0.0;
for (member in members) {
if (member == null || !member.alive) continue;
var value = member.y + member.height;
if (value > h) h = value;
}
return h;
}
public function new (X:Float = 0, Y:Float = 0, ?maxSize:Int) {
super();
#else
override function initGroup(maxSize:Int):Void
{
@:bypassAccessor
group = new TypedGroup<T>(maxSize);
offset = FlxPoint.get();
origin = FlxPoint.get();
}
override function destroy() {
super.destroy();
group = FlxDestroyUtil.destroy(group);
offset = FlxDestroyUtil.put(offset);
origin = FlxDestroyUtil.put(origin);
}
override function update(elapsed:Float) {
group.update(elapsed);
}
override function draw() {
@:privateAccess {
final oldDefaultCameras = FlxCamera._defaultCameras;
if (group.cameras != null) FlxCamera._defaultCameras = group.cameras;
var point = CoolUtil.point;
point.set(x, y);
point.subtract(offset.x, offset.y);
for (basic in members) {
var basicX = basic.x; var basicY = basic.y; var basicAngle = basic.angle; var basicAlpha = basic.alpha;
CoolUtil.positionWithTrig(basic, basic.x - origin.x, basic.y - origin.y, _cos, _sin);
basic.x += point.x + origin.x;
basic.y += point.y + origin.y;
basic.angle += angle;
basic.alpha *= alpha;
if (basic != null && basic.exists && basic.visible) {
basic.draw();
}
basic.x = basicX;
basic.y = basicY;
basic.angle = basicAngle;
basic.alpha = basicAlpha;
}
FlxCamera._defaultCameras = oldDefaultCameras;
}
}
}*/

/*
class BaseTypedSpriteGroup<T:FlxSprite> extends TypedGroup<T>
{
public var x(default, set):Float = 0.0;
public var y(default, set):Float = 0.0;
public var offset(default, null):FlxPoint;
public var scrollFactor(default, null):FlxPoint;
public inline function setPosition(X:Float = 0, Y:Float = 0) {
x = X;
y = Y;
}
public inline function screenCenter(axes:FlxAxes = XY) {
if (axes.x) x = (FlxG.width - width) * .5;
if (axes.y) y = (FlxG.height - height) * .5;
return this;
}
public var alpha:Float = 1.0;
public var color(default, set):FlxColor = FlxColor.WHITE;
function set_color(value:FlxColor):FlxColor {
if (value != color) {
for (basic in members)
basic.color = value;
}
return color = value;
}
public var angle(default, set):Float = 0.0;
public var origin(default, null):FlxPoint;
var _cos(default, null):Float = 1.0;
var _sin(default, null):Float = 0.0;
function set_angle(value:Float):Float {
if (angle != value) {
var rads:Float = value * CoolUtil.TO_RADS;
_cos = CoolUtil.cos(rads);
_sin = CoolUtil.sin(rads);
}
return angle = value;
}
public var width(get, set):Float;
public var height(get, set):Float;
function get_width():Float {
var w:Float = 0.0;
for (member in members) {
if (member == null || !member.alive) continue;
var value = member.x + member.width;
if (value > w) w = value;
}
return w;
}
function get_height():Float {
var h:Float = 0.0;
for (member in members) {
if (member == null || !member.alive) continue;
var value = member.y + member.height;
if (value > h) h = value;
}
return h;
}
public function new(X:Float = 0.0, Y:Float = 0.0, ?maxSize:Int):Void {
super(maxSize);
setPosition(X, Y);
offset = FlxPoint.get();
origin = FlxPoint.get();
scrollFactor = new FlxCallbackPoint(function (point:FlxPoint) {
for (basic in members) {
basic.scrollFactor.set(point.x, point.y);
}
});
scrollFactor.set(1, 1);
}
override function destroy() {
super.destroy();
offset = FlxDestroyUtil.put(offset);
origin = FlxDestroyUtil.put(origin);
scrollFactor = FlxDestroyUtil.destroy(scrollFactor);
}
override function draw():Void {
@:privateAccess {
final oldDefaultCameras = FlxCamera._defaultCameras;
if (cameras != null) FlxCamera._defaultCameras = cameras;
var point = CoolUtil.point;
point.set(x, y);
point.subtract(offset.x, offset.y);
for (basic in members) {
var basicX = basic.x; var basicY = basic.y; var basicAngle = basic.angle; var basicAlpha = basic.alpha;
CoolUtil.positionWithTrig(basic, basic.x - origin.x, basic.y - origin.y, _cos, _sin);
basic.x += point.x;
basic.y += point.y;
basic.angle += angle;
basic.alpha *= alpha;
if (basic != null && basic.exists && basic.visible) {
basic.draw();
}
basic.x = basicX;
basic.y = basicY;
basic.angle = basicAngle;
basic.alpha = basicAlpha;
}
FlxCamera._defaultCameras = oldDefaultCameras;
}
}
}*/
#end
}
3 changes: 2 additions & 1 deletion source/funkin/states/menus/FreeplayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ typedef SongMetaData = {
var mod:String;
}

class FreeplayState extends MusicBeatState {
class FreeplayState extends MusicBeatState
{
var bg:FunkinSprite;

var songs:Array<SongMetaData> = [];
Expand Down
26 changes: 16 additions & 10 deletions source/funkin/util/Paths.hx
Original file line number Diff line number Diff line change
Expand Up @@ -244,31 +244,37 @@ class Paths
return [dirParts[1], dirParts[dirParts.length-1].split('.')[0]];
}

inline static public function getSparrowAtlas(key:String, ?library:String, ?useTexture:Bool, ?lodLevel:LodLevel):FlxAtlasFrames {
// Gotta do this to make sure FlxAtlasFrames doesnt lose his shit when the graphic is smaller than the data
private static function getFrames(image:LodGraphic, getter:()->FlxAtlasFrames) {
image.setSize(image.lodWidth, image.lodHeight);
var frames = getter();
image.setSize(image.bitmap.width, image.bitmap.height);
return frames;
}

inline static public function getSparrowAtlas(key:String, ?library:String, ?useTexture:Bool, ?lodLevel:LodLevel):FlxAtlasFrames
{
var image = image(key, library, false, useTexture, lodLevel);
var xml = CoolUtil.getFileContent(file('images/$key.xml', library));
return __checkLodFrames(FlxAtlasFrames.fromSparrow(image, xml));

return __checkLodFrames(getFrames(image, () -> return FlxAtlasFrames.fromSparrow(image, xml)));
}

inline static public function getSpriteSheetAtlas(key:String, ?library:String, ?useTexture:Bool, ?lodLevel:LodLevel):FlxAtlasFrames {
inline static public function getSpriteSheetAtlas(key:String, ?library:String, ?useTexture:Bool, ?lodLevel:LodLevel):FlxAtlasFrames
{
var image = image(key, library, false, useTexture, lodLevel);
var txt = CoolUtil.getFileContent(file('images/$key.txt', library));

return __checkLodFrames(FlxAtlasFrames.fromSpriteSheetPacker(image, txt));
return __checkLodFrames(getFrames(image, () -> return FlxAtlasFrames.fromSpriteSheetPacker(image, txt)));
}

inline static public function getAsepriteAtlas(key:String, ?library:String, ?useTexture:Bool, ?lodLevel:LodLevel):FlxAtlasFrames {
var image = image(key, library, false, useTexture, lodLevel);
var json = CoolUtil.getFileContent(file('images/$key.json', library));

return __checkLodFrames(JsonUtil.getAsepritePacker(image, json));
return __checkLodFrames(getFrames(image, () -> return JsonUtil.getAsepritePacker(image, json)));
}

//inline static public function getAnimateAtlas(key:String, ?library:String):FlxAtlasFrames {
// return null;
//}

@:noCompletion
inline private static function __checkLodFrames(frames:FlxAtlasFrames):FlxAtlasFrames {
var parent:LodGraphic = cast(frames.parent, LodGraphic);
Expand Down
13 changes: 11 additions & 2 deletions source/funkin/util/backend/AssetManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,22 @@ class LodGraphic extends FlxGraphic
imageFrame.frame.sourceSize *= lodScale;
}

public var lodWidth(default, null):Int;
public var lodHeight(default, null):Int;

public inline function setSize(width:Int, height:Int):Void {
this.width = width;
this.height = height;
}

override function set_bitmap(value:BitmapData):BitmapData
{
if (value != null)
{
bitmap = value;
width = bitmap.width << lodLevel;
height = bitmap.height << lodLevel;
setSize(bitmap.width, bitmap.height);
lodWidth = bitmap.width << lodLevel;
lodHeight = bitmap.height << lodLevel;
}

return value;
Expand Down

0 comments on commit 52cf320

Please sign in to comment.