80 lines
1.7 KiB

  1. //
  2. // Deus Hax (working title)
  3. // Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
  4. //
  5. #if defined HAVE_CONFIG_H
  6. # include "config.h"
  7. #endif
  8. #include <cstdio>
  9. #include <cmath>
  10. #include <SDL.h>
  11. #include "sdlinput.h"
  12. #include "debugfps.h"
  13. #include "game.h"
  14. #include "ticker.h"
  15. #include "video.h"
  16. int main(int argc, char **argv)
  17. {
  18. /* Initialise SDL */
  19. if (SDL_Init(SDL_INIT_VIDEO) < 0)
  20. {
  21. fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError());
  22. return EXIT_FAILURE;
  23. }
  24. SDL_Surface *video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
  25. if (!video)
  26. {
  27. fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError());
  28. SDL_Quit();
  29. return EXIT_FAILURE;
  30. }
  31. SDL_WM_SetCaption("Deus Hax (SDL)", NULL);
  32. SDL_ShowCursor(0);
  33. SDL_WM_GrabInput(SDL_GRAB_ON);
  34. /* Initialise timer */
  35. Uint32 ticks = SDL_GetTicks();
  36. /* Initialise OpenGL */
  37. Video::Setup(video->w, video->h);
  38. /* Create a game */
  39. Game *game = new Game("maps/testmap.tmx");
  40. /* Register an input driver and some debug stuff */
  41. new SdlInput(game);
  42. new DebugFps();
  43. while (!game->Finished())
  44. {
  45. /* Compute delta time */
  46. Uint32 newticks = SDL_GetTicks();
  47. float delta_time = (float)(newticks - ticks);
  48. ticks = newticks;
  49. /* Tick the game */
  50. Ticker::TickGame(delta_time);
  51. /* Clear the screen, tick the renderer, and show the frame */
  52. Video::Clear();
  53. Ticker::TickRender(delta_time);
  54. SDL_GL_SwapBuffers();
  55. /* Clamp to desired framerate */
  56. while (SDL_GetTicks() < ticks + (33.33333f - 0.5f))
  57. SDL_Delay(1);
  58. }
  59. SDL_Quit();
  60. return EXIT_SUCCESS;
  61. }