Skip to content

Commit

Permalink
use game ticks instead of timer for sound time interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Apr 7, 2024
1 parent 44564fc commit 080edd3
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 9 deletions.
1 change: 0 additions & 1 deletion source/funkin/MobileTouch.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class MobileTouch extends Sprite
super();

addEventListener(Event.ADDED_TO_STAGE, (e) -> {

var w = FlxG.stage.fullScreenWidth;
var h = FlxG.stage.fullScreenHeight;

Expand Down
16 changes: 8 additions & 8 deletions source/funkin/sound/FlxFunkSound.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package funkin.sound;

import openfl.events.Event;
import openfl.net.URLRequest;
import openfl.Lib;
import openfl.media.SoundMixer;
import lime.media.AudioSource;
import openfl.media.SoundTransform;
Expand All @@ -17,6 +16,7 @@ import openfl.media.Sound;
@:access(lime.media.AudioSource)
@:access(openfl.media.SoundMixer)
@:access(openfl.media.Sound)
@:access(flixel.FlxGame)
class FlxFunkSound extends FlxBasic
{
@:noCompletion
Expand Down Expand Up @@ -59,7 +59,7 @@ class FlxFunkSound extends FlxBasic
function set_sound(value:Sound):Sound {
if (value != null)
{
var init:()->Void = function () {
var init:()->Void = () -> {
length = value.length;
source.buffer = value.__buffer;
source.init();
Expand All @@ -74,7 +74,7 @@ class FlxFunkSound extends FlxBasic

if (value.__urlLoading)
{
var onLoad:Event->Void = function (e:Event) {
var onLoad:Event->Void = (e:Event) -> {
if (value == e.target)
init();
}
Expand Down Expand Up @@ -122,7 +122,7 @@ class FlxFunkSound extends FlxBasic
{
this.onLoad = onLoad;

if (!path.startsWith("./"))
if(!path.startsWith("https://")) if (!path.startsWith("./"))
path = './$path';

final sound = new Sound();
Expand Down Expand Up @@ -235,20 +235,20 @@ class FlxFunkSound extends FlxBasic
source.position = position;
}

var __lastLibTime:Float = 0;
var __lastTick:Float = 0;
var __lastTime:Float = 0;

// Quick interpolate fix until the ninjamuffin lime pr gets merged
public function getTime():Float
{
final time = source.currentTime;
final time:Int = source.currentTime;

if (time != __lastTime) {
__lastTime = time;
__lastLibTime = Lib.getTimer();
__lastTick = Main.game._total;
return time;
}

return time + Lib.getTimer() - __lastLibTime;
return time + Main.game._total - __lastTick;
}
}
1 change: 1 addition & 0 deletions source/funkin/states/menus/FreeplayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class FreeplayState extends MusicBeatState {
function loadSong(toChart:Bool):Void {
var songData = songs[curSelected];
var diff = curWeekDiffs[curDifficulty];
#if mobile MobileTouch.setMode(NONE); #end
WeekSetup.loadSong(songData.week, songData.song, diff, false, false, #if DEV_TOOLS toChart ? ChartingState : #end null);
}

Expand Down
1 change: 1 addition & 0 deletions source/funkin/states/menus/StoryMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class StoryMenuState extends MusicBeatState {
CoolUtil.playSound('confirmMenu');
grpWeekText.members[curWeek].startFlashing();
grpWeekCharacters.members[1].playAnim('confirm', true);
#if mobile MobileTouch.setMode(NONE); #end

var playlist = getCurData().songList.songs;
var diff = curWeekDiffs[curDifficulty];
Expand Down
29 changes: 29 additions & 0 deletions source/funkin/states/newchart/ChartEditor.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package funkin.states.newchart;

class ChartEditor extends MusicBeatState
{
public static var SONG:SwagSong;

var bg:FlxSpriteExt;
var grid:ChartGrid;

override function create()
{
super.create();

setupSong("bopeebo", "hard");

bg = new FlxSpriteExt().loadImage("menuDesat");
bg.scrollFactor.set();
bg.color = 0xFF242424;
add(bg);

grid = new ChartGrid();
add(grid);
}

public static function setupSong(song:String, diff:String, ?input:SwagSong):SwagSong
{
return SONG = input ?? Song.loadFromFile(diff, song);
}
}
82 changes: 82 additions & 0 deletions source/funkin/states/newchart/ChartGrid.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package funkin.states.newchart;

import openfl.events.MouseEvent;
import flixel.addons.display.FlxGridOverlay;

@:access(flixel.input.mouse.FlxMouse)
class ChartGrid extends Group
{
inline static var TILE:Int = 40;

var notesGrid:FlxSpriteExt;
var eventsGrid:FlxSpriteExt;

public function new() {
super();

// Use the same bitmap for batch rendering on both grids
var bitmap = FlxGridOverlay.createGrid(1, 1, 8, 16, true, 0xff7c7c7c, 0xff6e6e6e);

notesGrid = new FlxSpriteExt(0, 0, bitmap);
notesGrid.setScale(TILE);
add(notesGrid);

eventsGrid = new FlxSpriteExt(0, 0, bitmap);
eventsGrid.setScale(TILE);
add(eventsGrid);

// Adjust events grid shit
eventsGrid.frame.frame.x = 1;
eventsGrid.frame.frame.width = 1;
eventsGrid.frame = eventsGrid.frame;
eventsGrid.width = TILE;

notesGrid.screenCenter(X);
eventsGrid.x = notesGrid.x - TILE - 10;

FlxG.mouse._stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}

// Save doing the mouse stuff on update
function onMouseMove(e)
{
// TODO: add tile logic and all that bullshit
}

// Use inst time when possible to avoid needing to sync all the time lol
inline function getTime():Float {
return Conductor.playing ? Conductor.inst.time + Conductor.offset[0] + Conductor.latency : Conductor.songPosition;
}

inline function getTimeY():Float {
return inline FlxMath.remapToRange(getTime(), 0, Conductor.sectionCrochet, 0, TILE * 16); // TODO: Change the 16 with the snap later
}

public var sectionObjects:Array<Array<FlxObject>> = [];

var curSection(default, set):Int = 0;
inline function set_curSection(value:Int):Int {
return curSection = cast FlxMath.bound(value, 0, ChartEditor.SONG.notes.length - 1);
}

override function draw() {
super.draw();

// Render the 3 current visible sections, maybe make this higher depending on the snap?
for (i in 0...3)
{
if (sectionObjects[curSection - 1 + i] != null)
{
sectionObjects[curSection - 1 + i].fastForEach((object, i) -> {
if (object != null) if (object.exists) if (object.visible)
object.draw();
});
}
}
}

override function destroy() {
super.destroy();
FlxG.mouse._stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
}

0 comments on commit 080edd3

Please sign in to comment.