Skip to content

Commit

Permalink
feat: add fps counter
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Sep 14, 2024
1 parent 94e6bb5 commit 27e9e37
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/flame/ricochlime_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class RicochlimeGame extends Forge2DGame
/// and only update the game when the sum is greater than 1/30.
double groupedUpdateDt = 0;
static const maxDt = 0.5;
static final lastDt = ValueNotifier(maxDt);

@override
// ignore: must_call_super (super.update is called in [updateNow])
Expand Down Expand Up @@ -349,6 +350,7 @@ class RicochlimeGame extends Forge2DGame
}

void updateNow(double dt, double timeDilation) {
if (Prefs.showFpsCounter.value) lastDt.value = dt;
dt = min(dt * timeDilation, maxDt);
ticker.tick(dt);
super.update(dt);
Expand Down
3 changes: 3 additions & 0 deletions lib/i18n/_missing_translations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ es:
showReflectionInAimGuide(OUTDATED): Show reflection in aim guide
biggerBullets(OUTDATED): Bigger bullets
maxFps(OUTDATED): Max FPS
showFpsCounter(OUTDATED): Show FPS counter
appInfo(OUTDATED): App info
licenseNotice(OUTDATED): |-
Ricochlime Copyright (C) 2023-$buildYear Adil Hanney
Expand Down Expand Up @@ -100,6 +101,7 @@ kk:
showReflectionInAimGuide(OUTDATED): Show reflection in aim guide
biggerBullets(OUTDATED): Bigger bullets
maxFps(OUTDATED): Max FPS
showFpsCounter(OUTDATED): Show FPS counter
ageDialog:
guessNumber(OUTDATED): Guess \#$n
howOldAreYou(OUTDATED): "How old are you?"
Expand Down Expand Up @@ -137,6 +139,7 @@ ru:
settingsPage:
removeAdsInShop(OUTDATED): "Want to remove ads? Check out the options in the shop!"
stylizedPageTransitions(OUTDATED): Stylized page transitions
showFpsCounter(OUTDATED): Show FPS counter
adWarning:
getCoins(rich)(OUTDATED): Watch an optional ad to get $c 100 ($t)
shopPage:
Expand Down
8 changes: 6 additions & 2 deletions lib/i18n/strings.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
/// To regenerate, run: `dart run slang`
///
/// Locales: 4
/// Strings: 276 (69 per locale)
/// Strings: 280 (70 per locale)
///
/// Built on 2024-09-07 at 01:44 UTC
/// Built on 2024-09-14 at 03:52 UTC
// coverage:ignore-file
// ignore_for_file: type=lint
Expand Down Expand Up @@ -201,6 +201,7 @@ class _StringsSettingsPageEn {
String get showReflectionInAimGuide => 'Show reflection in aim guide';
String get biggerBullets => 'Bigger bullets';
String get maxFps => 'Max FPS';
String get showFpsCounter => 'Show FPS counter';
String get appInfo => 'App info';
String licenseNotice({required Object buildYear}) => 'Ricochlime Copyright (C) 2023-${buildYear} Adil Hanney\nThis program comes with absolutely no warranty. This is free software, and you are welcome to redistribute it under certain conditions.';
}
Expand Down Expand Up @@ -391,6 +392,7 @@ class _StringsSettingsPageEs extends _StringsSettingsPageEn {
@override final _StringsEs _root; // ignore: unused_field

// Translations
@override String get showFpsCounter => 'Mostrar contador de FPS';
@override String get stylizedPageTransitions => 'Transiciones de página estilizadas';
@override String get removeAdsInShop => '¿Quieres eliminar los anuncios? ¡Consulta las opciones en la tienda!';
@override String get showReflectionInAimGuide => 'Mostrar reflejo en la guía de objetivos.';
Expand Down Expand Up @@ -596,6 +598,7 @@ class _StringsSettingsPageKk extends _StringsSettingsPageEn {
// Translations
@override String get title => 'Баптау';
@override String get adConsent => 'Жарнамаға келісімді баптау';
@override String get showFpsCounter => 'FPS есептегішін көрсету';
@override String get appInfo => 'Қолданба ақпары';
@override String licenseNotice({required Object buildYear}) => 'Ricochlime Copyright (C) 2023-${buildYear} Adil Hanney\nБұл бағдарлама еш кепілдіксіз жеткізіледі. Ол еркін екенін ескере отырып, сіз оны кейбір шарттардың аясында еркін тарата аласыз.';
@override String get stylizedPageTransitions => 'Стильденген беттердің ауысуы';
Expand Down Expand Up @@ -807,6 +810,7 @@ class _StringsSettingsPageRu extends _StringsSettingsPageEn {
@override String get showReflectionInAimGuide => 'Показывать отскок в прицельной траектории';
@override String get biggerBullets => 'Большие мячики';
@override String get maxFps => 'Максимальный FPS';
@override String get showFpsCounter => 'Показать счетчик FPS';
@override String get appInfo => 'О программе';
@override String licenseNotice({required Object buildYear}) => 'Ricochlime Copyright (C) 2023-${buildYear} Adil Hanney\nThis program comes with absolutely no warranty. This is free software, and you are welcome to redistribute it under certain conditions.';
@override String get stylizedPageTransitions => 'Стилизованные переходы страниц';
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/strings.i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ settingsPage:
showReflectionInAimGuide: Show reflection in aim guide
biggerBullets: Bigger bullets
maxFps: Max FPS
showFpsCounter: Show FPS counter
appInfo: App info
licenseNotice: |-
Ricochlime Copyright (C) 2023-$buildYear Adil Hanney
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/strings_es.i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ playPage:
undo(OUTDATED): Deshacer movimiento
highScore(OUTDATED): "Mejor: $p"
settingsPage:
showFpsCounter(OUTDATED): Mostrar contador de FPS
stylizedPageTransitions(OUTDATED): Transiciones de página estilizadas
removeAdsInShop(OUTDATED): "¿Quieres eliminar los anuncios? ¡Consulta las opciones en la tienda!"
showReflectionInAimGuide(OUTDATED): Mostrar reflejo en la guía de objetivos.
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/strings_kk.i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ settingsPage:
licenseNotice: |-
Ricochlime Copyright (C) 2023-$buildYear Adil Hanney
Бұл бағдарлама еш кепілдіксіз жеткізіледі. Ол еркін екенін ескере отырып, сіз оны кейбір шарттардың аясында еркін тарата аласыз.
showFpsCounter(OUTDATED): FPS есептегішін көрсету
stylizedPageTransitions(OUTDATED): Стильденген беттердің ауысуы
removeAdsInShop(OUTDATED): "Жарнамаларды өшіргіңіз келе ме? Дүкенде опцияларды тексеріңіз!"
showReflectionInAimGuide(OUTDATED): Мақсат нұсқаулығында рефлексияны көрсетіңіз
Expand Down
1 change: 1 addition & 0 deletions lib/i18n/strings_ru.i18n.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ settingsPage:
licenseNotice: |-
Ricochlime Copyright (C) 2023-$buildYear Adil Hanney
This program comes with absolutely no warranty. This is free software, and you are welcome to redistribute it under certain conditions.
showFpsCounter(OUTDATED): Показать счетчик FPS
stylizedPageTransitions(OUTDATED): Стилизованные переходы страниц
removeAdsInShop(OUTDATED): "Хотите удалить рекламу? Посмотрите варианты в магазине!"
ageDialog:
Expand Down
28 changes: 27 additions & 1 deletion lib/pages/play.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class _PlayPageState extends State<PlayPage> {
backgroundColor: Colors.transparent,
elevation: 0,
systemOverlayStyle: SystemUiOverlayStyle.light,
leadingWidth: 80,
leadingWidth: Prefs.showFpsCounter.value ? 136 : 80,
leading: Row(
mainAxisSize: MainAxisSize.min,
children: [
Expand Down Expand Up @@ -184,6 +184,10 @@ class _PlayPageState extends State<PlayPage> {
size: const Size.square(20),
),
),
if (Prefs.showFpsCounter.value) ...[
const SizedBox(width: 16),
const FpsCounter(),
],
],
),
centerTitle: true,
Expand Down Expand Up @@ -351,3 +355,25 @@ class _PlayPageState extends State<PlayPage> {
);
}
}

class FpsCounter extends StatelessWidget {
const FpsCounter({super.key});

static double _lastLastDt = 1 / 30;

@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
valueListenable: RicochlimeGame.lastDt,
builder: (context, dt, _) {
dt = (dt + _lastLastDt) / 2;
_lastLastDt = dt;
final fps = (1 / dt).round();
return Text(
fps.toString(),
style: const TextStyle(fontSize: 24),
);
},
);
}
}
39 changes: 38 additions & 1 deletion lib/pages/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class SettingsPage extends StatelessWidget {
} else {
return Text(
maxFps.toString(),
style: const TextStyle(fontSize: 20),
style: const TextStyle(fontSize: 24),
);
}
},
Expand All @@ -294,6 +294,43 @@ class SettingsPage extends StatelessWidget {
),
),

// Show FPS counter
Padding(
padding: listTilePadding,
child: NesContainer(
padding: EdgeInsets.zero,
child: ValueListenableBuilder(
valueListenable: Prefs.showFpsCounter,
builder: (context, _, child) {
final fps = Prefs.maxFps.value < 0 ? 60 : Prefs.maxFps.value;
return MergeSemantics(
child: ListTile(
title: child,
leading: BlockSemantics(
child: Text(
fps.toString(),
style: const TextStyle(fontSize: 24),
),
),
tileColor: listTileColor,
shape: listTileShape,
contentPadding: listTileContentPadding,
trailing: NesCheckBox(
value: Prefs.showFpsCounter.value,
onChange: (value) {
Prefs.showFpsCounter.value = value;
},
),
onTap: () => Prefs.showFpsCounter.value =
!Prefs.showFpsCounter.value,
),
);
},
child: Text(t.settingsPage.showFpsCounter),
),
),
),

Padding(
padding: subtitlePadding,
child: Text(
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/prefs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ abstract class Prefs {
static late final PlainPref<String> bulletShape;

static late final PlainPref<int> maxFps;
static late final PlainPref<bool> showFpsCounter;

static late final PlainPref<int> totalCoinsGained,
totalBulletsGained,
Expand Down Expand Up @@ -89,6 +90,7 @@ abstract class Prefs {
bulletShape = PlainPref('bulletShape', ShopItems.bulletShapes.first.id);

maxFps = PlainPref('maxFps', -1);
showFpsCounter = PlainPref('showFpsCounter', false);

totalCoinsGained = PlainPref('totalCoinsGained', 0);
totalBulletsGained = PlainPref('totalBulletsGained', 0);
Expand Down

0 comments on commit 27e9e37

Please sign in to comment.