sm64

A Super Mario 64 decompilation
Log | Files | Refs | README | LICENSE

fps.patch (1743B)


      1 diff --git a/src/game/game_init.c b/src/game/game_init.c
      2 index b961ca52..531231cf 100644
      3 --- a/src/game/game_init.c
      4 +++ b/src/game/game_init.c
      5 @@ -82,6 +82,47 @@ struct DemoInput gRecordedDemoInput = { 0 };
      6  // Display
      7  // ----------------------------------------------------------------------------------------------------
      8  
      9 +// SDK states that 1 cycle takes about 21.33 nanoseconds
     10 +#define SECONDS_PER_CYCLE 0.00000002133f
     11 +
     12 +#define FPS_COUNTER_X_POS 24
     13 +#define FPS_COUNTER_Y_POS 190
     14 +
     15 +static OSTime gLastOSTime = 0;
     16 +static float gFrameTime = 0.0f;
     17 +static u16 gFrames = 0;
     18 +static u16 gFPS = 0;
     19 +static u8 gRenderFPS = FALSE;
     20 +
     21 +static void calculate_frameTime_from_OSTime(OSTime diff) {
     22 +    gFrameTime += diff * SECONDS_PER_CYCLE;
     23 +    gFrames++;
     24 +}
     25 +
     26 +static void render_fps(void) {
     27 +    // Toggle rendering framerate with the L button.
     28 +    if (gPlayer1Controller->buttonPressed & L_TRIG) {
     29 +        gRenderFPS ^= 1;
     30 +    }
     31 +
     32 +    if (gRenderFPS) {
     33 +        OSTime newTime = osGetTime();
     34 +
     35 +        calculate_frameTime_from_OSTime(newTime - gLastOSTime);
     36 +
     37 +        // If frame time is longer or equal to a second, update FPS counter.
     38 +        if (gFrameTime >= 1.0f) {
     39 +            gFPS = gFrames;
     40 +            gFrames = 0;
     41 +            gFrameTime -= 1.0f;
     42 +        }
     43 +
     44 +        print_text_fmt_int(FPS_COUNTER_X_POS, FPS_COUNTER_Y_POS, "FPS %d", gFPS);
     45 +
     46 +        gLastOSTime = newTime;
     47 +    }
     48 +}
     49 +
     50  /**
     51   * Sets the initial RDP (Reality Display Processor) rendering settings.
     52   */
     53 @@ -694,5 +735,7 @@ void thread5_game_loop(UNUSED void *arg) {
     54              // amount of free space remaining.
     55              print_text_fmt_int(180, 20, "BUF %d", gGfxPoolEnd - (u8 *) gDisplayListHead);
     56          }
     57 +
     58 +        render_fps();
     59      }
     60  }