Super Mario 64s source code (from a leak on 4chan so be careful)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

6 years ago
  1. /*
  2. * Framerate counter
  3. *
  4. * Calculates the game's current framerate by using osGetTime() and
  5. * prints it out on the lower left side of the screen.
  6. *
  7. * HOW TO USE:
  8. *
  9. * Add this include statement to the top of game.c
  10. *
  11. * #include "../../enhancements/fps.inc.c"
  12. *
  13. * Then, at the end of the while(1) loop in the function thread5_game_loop()
  14. * add the function call render_fps()
  15. *
  16. * That's it! Build the rom file and press L when the game boots up
  17. * to toggle the FPS counter.
  18. */
  19. // SDK states that 1 cycle takes about 21.33 nanoseconds
  20. #define SECONDS_PER_CYCLE 0.00000002133f
  21. #define FPS_COUNTER_X_POS 24
  22. #define FPS_COUNTER_Y_POS 190
  23. OSTime gLastOSTime = 0;
  24. float gFrameTime = 0.0f;
  25. u16 gFrames = 0;
  26. u16 gFPS = 0;
  27. u8 gRenderFPS = FALSE;
  28. void calculate_frameTime_from_OSTime(OSTime diff)
  29. {
  30. gFrameTime += diff * SECONDS_PER_CYCLE;
  31. gFrames++;
  32. }
  33. void render_fps(void)
  34. {
  35. // Toggle rendering framerate with the L button.
  36. if (gPlayer1Controller->buttonPressed & L_TRIG)
  37. {
  38. gRenderFPS ^= 1;
  39. }
  40. if(gRenderFPS)
  41. {
  42. OSTime newTime = osGetTime();
  43. calculate_frameTime_from_OSTime(newTime - gLastOSTime);
  44. // If frame time is longer or equal to a second, update FPS counter.
  45. if(gFrameTime >= 1.0f)
  46. {
  47. gFPS = gFrames;
  48. gFrames = 0;
  49. gFrameTime -= 1.0f;
  50. }
  51. print_text_fmt_int(FPS_COUNTER_X_POS, FPS_COUNTER_Y_POS, "FPS %d", gFPS);
  52. gLastOSTime = newTime;
  53. }
  54. }