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.

75 lines
1.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <set>
  2. #include "tile_collection.h"
  3. bool Range::contains(int x, int y, int d) const {
  4. assert(d <= depth);
  5. int dz = depth - d;
  6. //printf("@%i (%i,%i) <= (%i,%i) <= (%i,%i)\n", d, minx >> dz, miny >> dz, x, y, maxx >> dz, maxy >> dz);
  7. return x >= minx >> dz && y >= miny >> dz && x <= maxx >> dz && y <= maxy >> dz;
  8. }
  9. Quad::Quad(int x, int y, int depth): x(x), y(y), depth(depth){
  10. tile = NULL;
  11. for(int i = 0; i < 4; i++)
  12. children[i] = NULL;
  13. }
  14. void Quad::queryRange(std::list<Tile *> &l, const Range &r){
  15. if(!r.contains(x, y, depth))
  16. return;
  17. if(!tile)
  18. tile = new Tile(x, y, depth);
  19. if(depth < r.depth){
  20. buildChildren();
  21. for(int i = 0; i < 4; i++)
  22. children[i]->queryRange(l, r);
  23. }else{
  24. l.push_back(tile);
  25. }
  26. }
  27. void Quad::buildChildren(){
  28. if(!children[0])
  29. children[0] = new Quad(x*2, y*2, depth+1);
  30. if(!children[1])
  31. children[1] = new Quad(x*2+1, y*2, depth+1);
  32. if(!children[2])
  33. children[2] = new Quad(x*2, y*2+1, depth+1);
  34. if(!children[3])
  35. children[3] = new Quad(x*2+1, y*2+1, depth+1);
  36. }
  37. void TileCollection::set_bounds(int _minx, int _miny, int _maxx, int _maxy, int _zoom){
  38. range.minx = _minx;
  39. range.maxx = _maxx;
  40. range.miny = _miny;
  41. range.maxy = _maxy;
  42. range.depth = _zoom;
  43. }
  44. bool TileCollection::work(){
  45. bool working = !!transfers.active();
  46. transfers.work();
  47. std::list<Tile *> l;
  48. quad.queryRange(l, range);
  49. for(std::list<Tile *>::iterator it = l.begin(); transfers.active() < 2 && it != l.end(); ++it){
  50. Tile *t = *it;
  51. if(t->state != Tile::EMPTY)
  52. continue;
  53. transfers.queue(t);
  54. working = true;
  55. }
  56. return working;
  57. }
  58. void TileCollection::render(SDL_Surface *screen, int offsetx, int offsety){
  59. std::list<Tile *> l;
  60. quad.queryRange(l, range);
  61. for(std::list<Tile *>::iterator it = l.begin(); it != l.end(); ++it){
  62. (*it)->render(screen, offsetx, offsety);
  63. }
  64. }