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
20 #ifndef __COLLISION_GRID_ITERATOR_H__
21 #define __COLLISION_GRID_ITERATOR_H__
23 #include "math/rect.hpp"
28 class CollisionGridIterator
31 CollisionGridIterator(CollisionGrid& newgrid, const Rect& bbox)
34 start_x = int(bbox.p1.x / grid.cell_width) - 2;
39 y = int(bbox.p1.y / grid.cell_height) - 2;
43 end_x = int(bbox.p2.x / grid.cell_width) + 2;
44 if(end_x > (int) grid.cells_x)
47 end_y = int(bbox.p2.y / grid.cell_height) + 2;
48 if(end_y > (int) grid.cells_y)
51 timestamp = grid.iterator_timestamp++;
54 if(start_x >= end_x) {
55 msg_debug << "bad region" << std::endl;
64 CollisionGrid::ObjectWrapper* wrapper = next_wrapper();
68 return wrapper->object;
72 friend class CollisionGrid;
74 CollisionGrid::ObjectWrapper* next_wrapper()
76 CollisionGrid::ObjectWrapper* wrapper;
83 entry = grid.grid[y*grid.cells_x + x];
91 wrapper = entry->object_wrapper;
93 } while(wrapper->timestamp == timestamp);
95 wrapper->timestamp = timestamp;
101 CollisionGrid::GridEntry* entry;
103 int start_x, end_x, end_y;