common.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * common.c
  3. * written by Holmes Futrell
  4. * use however you want
  5. */
  6. #include "common.h"
  7. #include "SDL.h"
  8. #include <stdlib.h>
  9. /*
  10. Produces a random int x, min <= x <= max
  11. following a uniform distribution
  12. */
  13. int
  14. randomInt(int min, int max)
  15. {
  16. return min + rand() % (max - min + 1);
  17. }
  18. /*
  19. Produces a random float x, min <= x <= max
  20. following a uniform distribution
  21. */
  22. float
  23. randomFloat(float min, float max)
  24. {
  25. return rand() / (float) RAND_MAX *(max - min) + min;
  26. }
  27. void
  28. fatalError(const char *string)
  29. {
  30. printf("%s: %s\n", string, SDL_GetError());
  31. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, string, SDL_GetError(), NULL);
  32. exit(1);
  33. }
  34. static Uint64 prevTime = 0;
  35. double
  36. updateDeltaTime(void)
  37. {
  38. Uint64 curTime;
  39. double deltaTime;
  40. if (prevTime == 0) {
  41. prevTime = SDL_GetPerformanceCounter();
  42. }
  43. curTime = SDL_GetPerformanceCounter();
  44. deltaTime = (double) (curTime - prevTime) / (double) SDL_GetPerformanceFrequency();
  45. prevTime = curTime;
  46. return deltaTime;
  47. }