Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
some more gpu assets util and fix hashlink *again*
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Dec 5, 2023
1 parent 29ea14d commit 4d2cf15
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 49 deletions.
9 changes: 5 additions & 4 deletions source/funkin/Preferences.hx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class Preferences {
fixOldPrefs();
}

inline public static function addPref(id:String, label:String, defaultValue:Dynamic):Void {
public static function addPref(id:String, label:String, defaultValue:Dynamic):Void {
id = id.toLowerCase().trim();
prefsArray.push(id);

prefsLabels.set(id, label);
if (defaultValue.array != null) arrayPrefs.set(id, defaultValue);
if (Reflect.hasField(defaultValue, "array")) arrayPrefs.set(id, defaultValue);
if (!preferences.exists(id)) preferences.set(id, defaultValue);
}

Expand All @@ -77,8 +77,9 @@ class Preferences {
}

inline public static function getPref(pref:String):Dynamic {
final pref:Dynamic = cast preferences.get(pref.toLowerCase().trim());
return pref?.value ?? pref;
final pref:Dynamic = preferences.get(pref.toLowerCase().trim());
#if !hl return pref?.value ?? pref;
#else return Reflect.hasField(pref, "value") ? pref.value : pref; #end
}

inline public static function setPref(pref:String, value:Dynamic):Void {
Expand Down
34 changes: 18 additions & 16 deletions source/funkin/Preloader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,30 @@ class Preloader extends flixel.FlxState {
return cachedGraphics.exists(key);
}

public static function uploadTexture(bmp:BitmapData, key:String) {
#if !hl
var _texture = null;
if (cachedTextures.exists(key)) _texture = cachedTextures.get(key);
else {
_texture = FlxG.stage.context3D.createTexture(bmp.width, bmp.height, BGR_PACKED, true);
_texture.uploadFromBitmapData(bmp);
cachedTextures.set(key, _texture);
}
AssetManager.disposeBitmap(bmp);
bmp = null;
final graphic = FlxGraphic.fromBitmapData(BitmapData.fromTexture(_texture));
#else
final graphic = FlxGraphic.fromBitmapData(bmp);
#end
public static inline function makeGraphic(bpm:BitmapData, key:String):FlxGraphic {
final graphic = FlxGraphic.fromBitmapData(makeBitmap(bpm, key));
graphic.persist = true;
graphic.destroyOnNoUse = false;
return graphic;
}

public static inline function makeBitmap(bmp:BitmapData, key:String):BitmapData {
return #if hl bmp #else BitmapData.fromTexture(uploadTexture(bmp, key))#end ;
}

#if !hl
public static function uploadTexture(bmp:BitmapData, key:String) {
if (cachedTextures.exists(key)) return cachedTextures.get(key);
final _texture = FlxG.stage.context3D.createTexture(bmp.width, bmp.height, BGR_PACKED, true);
_texture.uploadFromBitmapData(bmp);
AssetManager.disposeBitmap(bmp);
cachedTextures.set(key, _texture);
return _texture;
}
#end

public static function addFromBitmap(bmp:BitmapData, key:String) {
final graphic:FlxGraphic = uploadTexture(bmp, key);
final graphic:FlxGraphic = makeGraphic(bmp, key);
cachedGraphics.set(key, graphic);
return graphic;
}
Expand Down
16 changes: 8 additions & 8 deletions source/funkin/graphics/FlxFunkText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ class FlxFunkText extends FlxSprite {
}


public var startSelection:Null<Int> = null;
public var endSelection:Null<Int> = null;
public var startSelection:Int = -1;
public var endSelection:Int = -1;
public var selected(get, never):Bool;
function get_selected() {
return !(startSelection == null || endSelection == null);
return startSelection != -1 && endSelection != -1 && !(startSelection == 0 && endSelection == 0);//(startSelection != -1) && (endSelection ?? -1) != -1) && (startSelection != 0 && endSelection != 0);
}

inline public function setSelection(start:Int, end:Int) {
if (start != startSelection && end != endSelection) {
if ((start != startSelection) || (end != endSelection)) {
startSelection = start;
endSelection = end;
_regen = true;
Expand All @@ -72,8 +72,8 @@ class FlxFunkText extends FlxSprite {

inline public function deselect() {
if (selected) {
startSelection = null;
endSelection = null;
startSelection = -1;
endSelection = -1;
_regen = true;
}
}
Expand Down Expand Up @@ -105,10 +105,10 @@ class FlxFunkText extends FlxSprite {

if (selected) {
textField.setSelection(startSelection, endSelection);
textField.__dirty = true;
textField.__setRenderDirty();
}

@:privateAccess
textField.__textEngine.update();
pixels.fillRect(_fillRect, FlxColor.TRANSPARENT);
pixels.draw(textField, _textMatrix, null, null, null, antialiasing);
}
Expand Down
6 changes: 5 additions & 1 deletion source/funkin/graphics/FlxSpriteExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,13 @@ class FlxSpriteExt extends FlxSkewedSprite {
animDatas = null;
}

public function stampBitmap(Brush:BitmapData, X:Float = 0, Y:Float = 0) {
inline public function stampBitmap(Brush:BitmapData, X:Float = 0, Y:Float = 0) {
final matrix:FlxMatrix = new FlxMatrix();
matrix.translate(X,Y);
graphic.bitmap.draw(Brush, matrix);
}

inline public function uploadGpu(key:String) {
return AssetManager.uploadSpriteGpu(this, key);
}
}
70 changes: 55 additions & 15 deletions source/funkin/objects/funkui/FunkInputText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
__text = new FunkUIText(X, Y, text, width - 3, cast height - 2);
__text.offset.set(-3,-2);
__text.wordWrap = lines != 1;
__text.deselect();

this.text = text;
wordPos = text.length;
Expand All @@ -26,8 +27,8 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
_tmr = 0.0;
__text.text = text;
__text.deselect();
if (overlap) setColor(FlxColor.WHITE);
}
targetColor = selected ? FlxColor.WHITE : HIGHLIGHT_COLOR;
return selected = value;
}

Expand All @@ -38,7 +39,7 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
}

static final HIGHLIGHT_COLOR = 0xFFD8D8D8;
static final DESELECT_COLOR = 0xFFA3A3A3;
static final CLICK_COLOR = 0xFFA3A3A3;

var _tmr:Float = 0;
var _addBar(default,set):Bool = false;
Expand All @@ -51,9 +52,19 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
}

var targetColor = FlxColor.WHITE;
inline function setColor(c:FlxColor) {
__text.color = color = c;
}

var overlap:Bool = false;
var mouseClick:Bool = false;
var mousePress:Bool = false;

override function update(elapsed:Float) {
super.update(elapsed);
overlap = FlxG.mouse.overlaps(this);
mouseClick = FlxG.mouse.justPressed;
mousePress = FlxG.mouse.pressed;

if (selected) {
_tmr -= elapsed;
Expand All @@ -62,20 +73,30 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
_addBar = !_addBar;
}

textInput();

if (mouseClick && !overlap) { // DESELECT
selected = false;
}
else {
mouseInput();
textInput();
}
}
if (FlxG.mouse.overlaps(this) || selected) {
if (!selected) __text.color = color = HIGHLIGHT_COLOR;
if (FlxG.mouse.justPressed) {
selected = !selected;
if (!selected) {
__text.color = color = DESELECT_COLOR;
else {
if (overlap) {
targetColor = HIGHLIGHT_COLOR;
if (mouseClick) {
selected = true;
setColor(CLICK_COLOR);
}
}
else {
targetColor = FlxColor.WHITE;
}
}

if (color != targetColor) {
__text.color = color = FlxColor.interpolate(color, targetColor, elapsed * 10);
setColor(FlxColor.interpolate(color, targetColor, elapsed * 10));
}
}

Expand All @@ -90,6 +111,21 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
__pressAt = 0.0;
}

function mouseInput() {
if (mousePress) {
final mousePos = FlxG.mouse.getScreenPosition();
final fieldPos = __text.getScreenPosition();
var mouseIndex = __text.getTextField().getCharIndexAtPoint(mousePos.x - fieldPos.x, mousePos.y - fieldPos.y);

if (mouseClick)
__text.setSelection(mouseIndex, mouseIndex);
else {
if (mouseIndex > 0) mouseIndex++;
__text.setSelection(__text.startSelection, mouseIndex == -1 ? __text.startSelection : mouseIndex);
}
}
}

function textInput() {
// Just pressed keys
final key = FlxG.keys.firstJustPressed();
Expand Down Expand Up @@ -156,9 +192,8 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
}

inline function copySelection() {
if (__text.startSelection != null) {
if (__text.selected)
clipBoard = text.substring(__text.startSelection, __text.endSelection);
}
}

inline function removeSelection() {
Expand Down Expand Up @@ -186,7 +221,7 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
case SHIFT | CONTROL | TAB: // these aint do nothin
case CAPSLOCK: capsLock = !capsLock;
case BACKSPACE:
if (__text.startSelection != null) {
if (__text.selected) {
removeSelection();
}
else {
Expand All @@ -197,8 +232,12 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
updateCurLine();

case LEFT | RIGHT:
__text.deselect();
if (!ctrlPress) { // Normal scrolling
if (__text.selected) { // SELECTED jump scrolling
final start:Int = cast Math.min( __text.startSelection, __text.endSelection);
final end:Int = cast Math.max( __text.startSelection, __text.endSelection);
wordPos = (key == LEFT) ? start : end;
}
else if (!ctrlPress) { // Normal scrolling
wordPos += (key == LEFT ? -1 : 1);
wordPos = cast FlxMath.bound(wordPos, 0, text.length);
updateCurLine();
Expand All @@ -213,6 +252,7 @@ class FunkInputText extends FourSideSprite implements IFunkUIObject {
wordPos += lineLength;
}
}
__text.deselect();

case UP | DOWN:
__text.deselect();
Expand Down
3 changes: 1 addition & 2 deletions source/funkin/objects/note/Note.hx
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ class Note extends FlxSpriteExt implements INoteData {
pixels.fillRect(new Rectangle(0, endPos, width, susEnd.height), FlxColor.fromRGB(0,0,0,0));
stampBitmap(susEnd, 0, endPos);

final gpuGraphic = AssetManager.uploadGraphicGPU(key); // After this the sustain bitmap data wont be readable, sorry
frames = gpuGraphic.imageFrame;
frames = AssetManager.uploadGpuFromKey(key).imageFrame; // After this the sustain bitmap data wont be readable, sorry
origin.set(width * 0.5, 0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/util/NoteUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class NoteUtil {
//susPieceMap.set(i, createLoopBitmap(refSprite.framePixels.clone(), 'sus-bitmap-$skin-hold-$i'));
susPieceMap.set(i, AssetManager.addGraphicFromBitmap(refSprite.framePixels.clone(), 'sus-bitmap-$skin-hold-$i', true).bitmap);

refSprite.animation.play('hold$i-end', true);
refSprite.animation.play('hold' + i + '-end', true);
refSprite.updateHitbox();
refSprite.drawFrame();
susEndMap.set(i, AssetManager.addGraphicFromBitmap(refSprite.framePixels.clone(), 'sus-bitmap-$skin-holdend-$i', true).bitmap);
Expand Down
16 changes: 14 additions & 2 deletions source/funkin/util/backend/AssetManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,27 @@ class AssetManager {
return getGraphic(key, cache).bitmap;
}

static public function uploadGraphicGPU(key:String) {
static public function uploadGpuFromKey(key:String) {
if (!existsGraphic(key)) return null;
final graphic = getGraphic(key);
final gpuGraphic = Preloader.uploadTexture(graphic.bitmap, key);
final gpuGraphic = Preloader.makeGraphic(graphic.bitmap, key);
removeGraphicByKey(key);
cachedGraphics.set(key, gpuGraphic);
return gpuGraphic;
}

static public inline function uploadSpriteGpu(sprite:FlxSprite, key:String):FlxGraphic {
if (#if hl true #else sprite.frames.parent.bitmap.readable == false #end) return null; // Already uploaded bitmap

final gpuGraphic = Preloader.makeGraphic(sprite.frames.parent.bitmap, key);
for (i in 0...sprite.frames.frames.length) {
final frame = sprite.frames.frames[i];
frame.parent = gpuGraphic;
}

return sprite.frames.parent = gpuGraphic;
}

public static function clearSoundCache(forced:Bool = false) {
for (key in cachedSounds.keys()) {
if (key.contains(Conductor._loadedSong) && !forced) continue;
Expand Down

0 comments on commit 4d2cf15

Please sign in to comment.