* Fixed a few own mistakes from a previous commit
[supertux.git] / src / collision_grid_iterator.hpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 // 
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
19 //  02111-1307, USA.
20 #ifndef __COLLISION_GRID_ITERATOR_H__
21 #define __COLLISION_GRID_ITERATOR_H__
22
23 #include "math/rect.hpp"
24
25 class CollisionGrid;
26
27 class CollisionGridIterator
28 {
29 public:
30   CollisionGridIterator(CollisionGrid& newgrid, const Rect& bbox)
31     : grid(newgrid)
32   {
33     start_x = int(bbox.p1.x / grid.cell_width) - 2;
34     if(start_x < 0)
35       start_x = 0;
36     x = start_x;
37         
38     y = int(bbox.p1.y / grid.cell_height) - 2;
39     if(y < 0)
40       y = 0;
41     
42     end_x = int(bbox.p2.x / grid.cell_width) + 2;
43     if(end_x > (int) grid.cells_x)
44       end_x = grid.cells_x;
45     
46     end_y = int(bbox.p2.y / grid.cell_height) + 2;
47     if(end_y > (int) grid.cells_y)
48       end_y = grid.cells_y;
49     
50     timestamp = grid.iterator_timestamp++;
51     entry = 0;
52
53     if(start_x >= end_x) {
54       printf("bad region.\n");
55       y = 0;
56       end_y = 0;
57       return;
58     }
59   }
60
61   MovingObject* next()
62   {
63     CollisionGrid::ObjectWrapper* wrapper = next_wrapper();
64     if(wrapper == 0)
65       return 0;
66         
67     return wrapper->object;
68   }
69
70 private:
71   friend class CollisionGrid;
72
73   CollisionGrid::ObjectWrapper* next_wrapper() 
74   {
75     CollisionGrid::ObjectWrapper* wrapper;
76     
77     do {
78       while(entry == 0) {
79         if(y >= end_y)
80           return 0;
81         
82         entry = grid.grid[y*grid.cells_x + x];
83         x++;
84         if(x >= end_x) {
85           x = start_x;
86           y++;
87         }
88       }
89       
90       wrapper = entry->object_wrapper;
91       entry = entry->next;
92     } while(wrapper->timestamp == timestamp);
93     
94     wrapper->timestamp = timestamp;
95     
96     return wrapper;
97   }
98     
99   CollisionGrid& grid;
100   CollisionGrid::GridEntry* entry;
101   int x, y;
102   int start_x, end_x, end_y;
103   int timestamp;
104 };
105
106 #endif
107