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.h"
27 class CollisionGridIterator
30 CollisionGridIterator(CollisionGrid& newgrid, const Rect& bbox)
33 start_x = int(bbox.p1.x / grid.cell_width) - 2;
38 y = int(bbox.p1.y / grid.cell_height) - 2;
42 end_x = int(bbox.p2.x / grid.cell_width) + 2;
43 if(end_x > (int) grid.cells_x)
46 end_y = int(bbox.p2.y / grid.cell_height) + 2;
47 if(end_y > (int) grid.cells_y)
50 if(start_x >= end_x) {
51 printf("bad region.\n");
57 timestamp = grid.iterator_timestamp++;
63 CollisionGrid::ObjectWrapper* wrapper = next_wrapper();
67 return wrapper->object;
71 friend class CollisionGrid;
73 CollisionGrid::ObjectWrapper* next_wrapper()
75 CollisionGrid::ObjectWrapper* wrapper;
82 entry = grid.grid[y*grid.cells_x + x];
90 wrapper = entry->object_wrapper;
92 } while(wrapper->timestamp == timestamp);
94 wrapper->timestamp = timestamp;
100 CollisionGrid::GridEntry* entry;
102 int start_x, end_x, end_y;