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.
 
 
 

121 lines
2.4 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 <cstdlib>
  9. #include <cmath>
  10. #ifdef WIN32
  11. # define WIN32_LEAN_AND_MEAN
  12. # include <windows.h>
  13. #endif
  14. #if defined __APPLE__ && defined __MACH__
  15. # include <OpenGL/gl.h>
  16. #else
  17. # define GL_GLEXT_PROTOTYPES
  18. # include <GL/gl.h>
  19. #endif
  20. #include "core.h"
  21. struct Tile
  22. {
  23. uint32_t prio, code;
  24. int x, y, z, o;
  25. };
  26. /*
  27. * Scene implementation class
  28. */
  29. class SceneData
  30. {
  31. friend class Scene;
  32. private:
  33. static int Compare(void const *p1, void const *p2)
  34. {
  35. Tile const *t1 = (Tile const *)p1;
  36. Tile const *t2 = (Tile const *)p2;
  37. return t2->prio - t1->prio;
  38. }
  39. Tile *tiles;
  40. int ntiles;
  41. };
  42. /*
  43. * Public Scene class
  44. */
  45. Scene::Scene()
  46. {
  47. data = new SceneData();
  48. data->tiles = 0;
  49. data->ntiles = 0;
  50. }
  51. Scene::~Scene()
  52. {
  53. delete data;
  54. }
  55. void Scene::AddTile(uint32_t code, int x, int y, int z, int o)
  56. {
  57. if ((data->ntiles % 1024) == 0)
  58. data->tiles = (Tile *)realloc(data->tiles,
  59. (data->ntiles + 1024) * sizeof(Tile));
  60. data->tiles[data->ntiles].prio = -y - 2 * 32 * z + (o ? 0 : 32);
  61. data->tiles[data->ntiles].code = code;
  62. data->tiles[data->ntiles].x = x;
  63. data->tiles[data->ntiles].y = y;
  64. data->tiles[data->ntiles].z = z;
  65. data->tiles[data->ntiles].o = o;
  66. data->ntiles++;
  67. }
  68. void Scene::Render() // XXX: rename to Blit()
  69. {
  70. #if 0
  71. // Randomise, then sort.
  72. for (int i = 0; i < data->ntiles; i++)
  73. {
  74. Tile tmp = data->tiles[i];
  75. int j = rand() % data->ntiles;
  76. data->tiles[i] = data->tiles[j];
  77. data->tiles[j] = tmp;
  78. }
  79. #endif
  80. qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
  81. // XXX: debug stuff
  82. glPushMatrix();
  83. static float f = 0.0f;
  84. f += 0.05f;
  85. glTranslatef(320.0f, 240.0f, 0.0f);
  86. glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
  87. #if 0
  88. glRotatef(3.0f * sinf(f), 1.0f, 0.0f, 0.0f);
  89. glRotatef(8.0f * cosf(f), 0.0f, 0.0f, 1.0f);
  90. #endif
  91. glTranslatef(-320.0f, -240.0f, 0.0f);
  92. for (int i = 0; i < data->ntiles; i++)
  93. Tiler::BlitTile(data->tiles[i].code, data->tiles[i].x,
  94. data->tiles[i].y, data->tiles[i].z, data->tiles[i].o);
  95. glPopMatrix();
  96. // XXX: end of debug stuff
  97. free(data->tiles);
  98. data->tiles = 0;
  99. data->ntiles = 0;
  100. }