Skip to content

Commit

Permalink
Add Application.GetTargetFrameRate and Application.SetTargetFrameRate
Browse files Browse the repository at this point in the history
  • Loading branch information
Lactozilla committed Sep 22, 2024
1 parent 1af42ff commit 3e5cf80
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
19 changes: 10 additions & 9 deletions source/Engine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Application {

static XMLNode* GameConfig;

static float FPS;
static int TargetFPS;
static float CurrentFPS;
static bool Running;
static bool GameStart;

Expand Down Expand Up @@ -108,8 +109,8 @@ char Application::SettingsFile[4096];

XMLNode* Application::GameConfig = NULL;

float Application::FPS = DEFAULT_TARGET_FRAMERATE;
int TargetFPS = DEFAULT_TARGET_FRAMERATE;
int Application::TargetFPS = DEFAULT_TARGET_FRAMERATE;
float Application::CurrentFPS = DEFAULT_TARGET_FRAMERATE;
bool Application::Running = false;
bool Application::GameStart = false;

Expand Down Expand Up @@ -151,7 +152,7 @@ double BenchmarkTickStart = 0.0;

double Overdelay = 0.0;
double FrameTimeStart = 0.0;
double FrameTimeDesired = 1000.0 / TargetFPS;
double FrameTimeDesired = 1000.0 / Application::TargetFPS;

int KeyBinds[(int)KeyBind::Max];

Expand Down Expand Up @@ -292,8 +293,8 @@ PUBLIC STATIC void Application::Init(int argc, char* args[]) {
PUBLIC STATIC void Application::SetTargetFrameRate(int targetFPS) {
if (targetFPS < 1)
TargetFPS = 1;
else if (targetFPS > 240)
TargetFPS = 240;
else if (targetFPS > MAX_TARGET_FRAMERATE)
TargetFPS = MAX_TARGET_FRAMERATE;
else
TargetFPS = targetFPS;

Expand Down Expand Up @@ -359,7 +360,7 @@ PUBLIC STATIC void Application::GetPerformanceSnapshot() {
MetricPresentTime,
0.0,
MetricFrameTime,
FPS,
CurrentFPS
};
const char* typeNames[] = {
"Event Polling: %8.3f ms",
Expand Down Expand Up @@ -978,7 +979,7 @@ PRIVATE STATIC void Application::RunFrame(void* p) {
Graphics::Save();
Graphics::Translate(infoW - infoPadding - (8 * 16.0 * 0.85), infoPadding, 0.0);
Graphics::Scale(0.85, 0.85, 1.0);
snprintf(textBuffer, 256, "FPS: %03.1f", FPS);
snprintf(textBuffer, 256, "FPS: %03.1f", TargetFPS);
DEBUG_DrawText(textBuffer, 0.0, 0.0);
Graphics::Restore();

Expand Down Expand Up @@ -1175,7 +1176,7 @@ PUBLIC STATIC void Application::Run(int argc, char* args[]) {
BenchmarkFrameCount++;
if (BenchmarkFrameCount == TargetFPS) {
double measuredSecond = Clock::GetTicks() - BenchmarkTickStart;
FPS = 1000.0 / floor(measuredSecond) * TargetFPS;
CurrentFPS = 1000.0 / floor(measuredSecond) * TargetFPS;
BenchmarkFrameCount = 0;
}

Expand Down
34 changes: 31 additions & 3 deletions source/Engine/Bytecode/StandardLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,15 +1002,41 @@ VMValue Application_GetEngineVersionCodename(int argCount, VMValue* args, Uint32
return NULL_VAL;
#endif
}
/***
* Application.GetTargetFrameRate
* \desc Gets the target frame rate.
* \return Returns an Integer value.
* \ns Application
*/
VMValue Application_GetTargetFrameRate(int argCount, VMValue* args, Uint32 threadID) {
CHECK_ARGCOUNT(0);
return INTEGER_VAL(Application::TargetFPS);
}
/***
* Application.SetTargetFrameRate
* \desc Sets the target frame rate.
* \param framerate (Integer): The target frame rate.
* \ns Application
*/
VMValue Application_SetTargetFrameRate(int argCount, VMValue* args, Uint32 threadID) {
CHECK_ARGCOUNT(1);
int framerate = GET_ARG(0, GetInteger);
if (framerate < 1 || framerate > MAX_TARGET_FRAMERATE) {
OUT_OF_RANGE_ERROR("Framerate", framerate, 1, MAX_TARGET_FRAMERATE);
return NULL_VAL;
}
Application::SetTargetFrameRate(framerate);
return NULL_VAL;
}
/***
* Application.GetFPS
* \desc Gets the current FPS.
* \desc Gets the current FPS (frames per second).
* \return Returns a Decimal value.
* \ns Application
*/
VMValue Application_GetFPS(int argCount, VMValue* args, Uint32 threadID) {
CHECK_ARGCOUNT(0);
return DECIMAL_VAL(Application::FPS);
return DECIMAL_VAL(Application::CurrentFPS);
}
/***
* Application.GetKeyBind
Expand Down Expand Up @@ -16395,10 +16421,11 @@ PUBLIC STATIC void StandardLibrary::Link() {
DEF_NATIVE(Application, GetEngineVersionPatch);
DEF_NATIVE(Application, GetEngineVersionPrerelease);
DEF_NATIVE(Application, GetEngineVersionCodename);
DEF_NATIVE(Application, GetTargetFrameRate);
DEF_NATIVE(Application, SetTargetFrameRate);
DEF_NATIVE(Application, GetFPS);
DEF_NATIVE(Application, GetKeyBind);
DEF_NATIVE(Application, SetKeyBind);
DEF_NATIVE(Application, Quit);
DEF_NATIVE(Application, GetGameTitle);
DEF_NATIVE(Application, GetGameTitleShort);
DEF_NATIVE(Application, GetGameVersion);
Expand All @@ -16409,6 +16436,7 @@ PUBLIC STATIC void StandardLibrary::Link() {
DEF_NATIVE(Application, SetGameDescription);
DEF_NATIVE(Application, SetCursorVisible);
DEF_NATIVE(Application, GetCursorVisible);
DEF_NATIVE(Application, Quit);
/***
* \enum KeyBind_Fullscreen
* \desc Fullscreen keybind.
Expand Down
2 changes: 2 additions & 0 deletions source/Engine/Includes/Standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ enum class KeyBind {

#define DEFAULT_TARGET_FRAMERATE 60

#define MAX_TARGET_FRAMERATE 240

#define MAX_SCENE_VIEWS 8
#define MAX_PALETTE_COUNT 256
#define MAX_DEFORM_LINES 0x400
Expand Down

0 comments on commit 3e5cf80

Please sign in to comment.