4 // Copyright (C) 2005 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 #ifndef __COLLISION_GRID_ITERATOR_H__
22 #define __COLLISION_GRID_ITERATOR_H__
24 #include "math/rectangle.h"
26 using namespace SuperTux;
30 class CollisionGridIterator
33 CollisionGridIterator(CollisionGrid& newgrid, const Rectangle& bbox)
36 start_x = int(bbox.p1.x / grid.cell_width) - 2;
41 y = int(bbox.p1.y / grid.cell_height) - 2;
45 end_x = int(bbox.p2.x / grid.cell_width) + 2;
46 if(end_x > (int) grid.cells_x)
49 end_y = int(bbox.p2.y / grid.cell_height) + 2;
50 if(end_y > (int) grid.cells_y)
53 if(start_x >= end_x) {
54 printf("bad region.\n");
60 timestamp = grid.iterator_timestamp++;
66 CollisionGrid::ObjectWrapper* wrapper = next_wrapper();
70 return wrapper->object;
74 friend class CollisionGrid;
76 CollisionGrid::ObjectWrapper* next_wrapper()
78 CollisionGrid::ObjectWrapper* wrapper;
85 entry = grid.grid[y*grid.cells_x + x];
93 wrapper = entry->object_wrapper;
95 } while(wrapper->timestamp == timestamp);
97 wrapper->timestamp = timestamp;
103 CollisionGrid::GridEntry* entry;
105 int start_x, end_x, end_y;