100 lines
2.2 KiB

  1. // Test stuff
  2. #include <SDL.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include "video.h"
  6. #include "tiler.h"
  7. /* Global objects */
  8. Video *video;
  9. Tiler *tiler;
  10. /* Storage for map layers */
  11. int *layers[128];
  12. int width = 32, height = 32;
  13. int nlayers = 0;
  14. void LoadMap(void)
  15. {
  16. FILE *fp = popen("grep '^ ' maps/testmap.tmx | while read i; do echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", "r");
  17. while (fp && !feof(fp))
  18. {
  19. layers[nlayers] = (int *)malloc(width * height * sizeof(int));
  20. fread(layers[nlayers], sizeof(int), width * height, fp);
  21. if (feof(fp))
  22. {
  23. free(layers[nlayers]);
  24. layers[nlayers] = 0;
  25. fclose(fp);
  26. break;
  27. }
  28. for (int n = 0; n < width * height; n++)
  29. {
  30. unsigned int i = layers[nlayers][n];
  31. //i = (i >> 24) | ((i >> 8) & 0xff00) | ((i << 8) & 0xff0000) | (i << 24);
  32. layers[nlayers][n] = i ? i - 1 : 0;
  33. }
  34. nlayers++;
  35. }
  36. }
  37. /* The main drawing function. */
  38. void DrawScene()
  39. {
  40. video->Clear();
  41. for (int i = 0; i < nlayers; i++)
  42. for (int y = 0; y < height; y++)
  43. for (int x = 0; x < width; x++)
  44. tiler->AddTile(layers[i][y * width + x], x * 32, y * 32, i);
  45. /* Test stuff */
  46. int playerx, playery;
  47. SDL_GetMouseState(&playerx, &playery);
  48. tiler->AddTile(50, playerx, playery, 1);
  49. tiler->Render();
  50. video->Refresh(33.33333f);
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. video = new Video("Deus Hax", 640, 480);
  55. tiler = new Tiler();
  56. int done;
  57. /* Loop, drawing and checking events */
  58. LoadMap();
  59. done = 0;
  60. while (!done)
  61. {
  62. DrawScene();
  63. /* This could go in a separate function */
  64. SDL_Event event;
  65. while (SDL_PollEvent(&event))
  66. {
  67. if (event.type == SDL_QUIT)
  68. done = 1;
  69. if (event.type == SDL_KEYDOWN)
  70. {
  71. if (event.key.keysym.sym == SDLK_RETURN)
  72. video->FullScreen();
  73. else if (event.key.keysym.sym == SDLK_ESCAPE)
  74. done = 1;
  75. }
  76. }
  77. }
  78. delete tiler;
  79. delete video;
  80. return EXIT_SUCCESS;
  81. }