citerate.h

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2000  Jeffrey S. Freedman
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 */
00024 
00025 #ifndef INCL_CITERATE
00026 #define INCL_CITERATE 1
00027 
00028 /*
00029  *  Here's an iterator that takes a rectangle of tiles, and sequentially
00030  *  returns the interesection of that rectangle with each chunk that it
00031  *  touches.
00032  */
00033 class Chunk_intersect_iterator
00034   {
00035   Rectangle tiles;    // Original rect, shifted -cx, -cy.
00036   int start_tx;     // Saves start of tx in tiles.
00037           // Chunk #'s covered:
00038   int startcx, stopcx, stopcy;
00039   int curcx, curcy;   // Next chunk to return.
00040 public:
00041   Chunk_intersect_iterator(Rectangle t) : tiles(t),
00042       startcx(t.x/c_tiles_per_chunk),
00043       stopcx(INCR_CHUNK((t.x + t.w - 1)/c_tiles_per_chunk)),
00044       stopcy(INCR_CHUNK((t.y + t.h - 1)/c_tiles_per_chunk)),
00045       curcy(t.y/c_tiles_per_chunk)
00046     {
00047     curcx = startcx;
00048     tiles.shift(-curcx*c_tiles_per_chunk,-curcy*c_tiles_per_chunk);
00049     start_tx = tiles.x;
00050     if (t.x < 0 || t.y < 0)
00051       {   // Empty to begin with.
00052       curcx = stopcx;
00053       curcy = stopcy;
00054       }
00055     }
00056           // Intersect is ranged within chunk.
00057   int get_next(Rectangle& intersect, int& cx, int& cy)
00058     {
00059     if (curcx == stopcx)  // End of row?
00060       if (curcy == stopcy)
00061         return (0);
00062       else
00063         {
00064         tiles.y -= c_tiles_per_chunk;
00065         tiles.x = start_tx;
00066         curcy = INCR_CHUNK(curcy);
00067         curcx = startcx;
00068         }
00069     Rectangle cr(0, 0, c_tiles_per_chunk, c_tiles_per_chunk);
00070           // Intersect given rect. with chunk.
00071     intersect = cr.intersect(tiles);
00072     cx = curcx;
00073     cy = curcy;
00074     curcx = INCR_CHUNK(curcx);
00075     tiles.x -= c_tiles_per_chunk;
00076     return (1);
00077     }
00078   };
00079 
00080 #endif

Generated on Mon Jul 9 14:42:49 2007 for ExultEngine by  doxygen 1.5.1