Shadowrun: Awakened 29 September 2011 - Build 871
rpolygon.h
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 // rpolygon.h
00030 //
00031 #ifndef _RPOLYGON_H_
00032 #define _RPOLYGON_H_
00033 #include <vector>
00034 #include <iostream>
00035 #include "pover_video.h"
00036 
00037 #include "tbb/scalable_allocator.h"
00038 
00039 using namespace std;
00040 
00041 using namespace tbb;
00042 
00043 class RPolygon;
00044 typedef scalable_allocator<RPolygon> RPolygon_allocator;
00045 DEFINE RPolygon_allocator rAlloc;
00046 
00047 enum MallocBehavior {
00048     UseMalloc,
00049     UseScalableAllocator
00050 };
00051 
00052 DEFINE MallocBehavior gMBehavior INIT(UseScalableAllocator);
00053 
00054 class RPolygon {
00055 public:
00056     RPolygon() {m_XMin = m_YMin = m_XMax = m_YMax = 0;
00057         m_r = m_g = m_b = 0;
00058     }
00059     RPolygon(int xMin, int yMin, int xMax, int yMax, int r=-1, int g=-1, int b=-1) : m_XMin(xMin), m_YMin(yMin), m_XMax(xMax), m_YMax(yMax) {
00060         if( r >= 0) {
00061             m_r=(colorcomp_t)r; m_g=(colorcomp_t)g; m_b=(colorcomp_t)b;
00062                         if(gDoDraw) drawPoly();
00063         }
00064     }
00065 
00066     static RPolygon *alloc_RPolygon(int xMin, int yMin, int xMax, int yMax, int r=-1, int g=-1, int b=-1) {
00067         switch(gMBehavior) {
00068             case UseScalableAllocator: {
00069                 RPolygon *my_p = rAlloc.allocate(1);
00070                 my_p->set_nodraw(xMin,yMin,xMax,yMax);
00071                 my_p->setColor(r,g,b);
00072                 if( r >= 0 && gDoDraw) {
00073                     my_p->drawPoly();
00074                 }
00075                 return my_p;
00076             }
00077             case UseMalloc: {
00078                 RPolygon *my_p = new RPolygon(xMin,yMin,xMax,yMax,r,g,b);
00079                 return my_p;
00080             }
00081         }
00082         return NULL;
00083     }
00084 
00085     static void free_RPolygon(RPolygon *p) { 
00086         switch(gMBehavior) {
00087             case UseScalableAllocator: {
00088                 rAlloc.deallocate(p, 1);
00089                 break;
00090             }
00091             case UseMalloc: {
00092                 delete p;
00093                 break;
00094             }
00095         }
00096     }
00097 
00098     void set_nodraw(int xMin, int yMin, int xMax, int yMax) {m_XMin=xMin; m_YMin=yMin; m_XMax=xMax; m_YMax=yMax;}
00099 
00100     RPolygon &intersect(RPolygon &otherPoly);
00101     void set(int xMin, int yMin, int xMax, int yMax) {
00102          set_nodraw(xMin,yMin,xMax,yMax);
00103          if(gDoDraw) {
00104             drawPoly();
00105          }
00106     }
00107     void get(int *xMin, int *yMin, int *xMax, int *yMax) const {*xMin=m_XMin;*yMin=m_YMin;*xMax=m_XMax;*yMax=m_YMax;}
00108     void setColor(colorcomp_t newr, colorcomp_t newg, colorcomp_t newb) {m_r = newr; m_g=newg; m_b=newb;}
00109     void getColor(int *myr, int *myg, int *myb) {*myr=m_r; *myg=m_g; *myb=m_b;}
00110     color_t myColor() {return gVideo->get_color(m_r, m_g, m_b);}
00111     void drawPoly() {
00112         if(gVideo->running) {
00113             if(g_next_frame()) {    // Shouldn't call next_frame each time
00114                 drawing_area ldrawing(
00115                     gDrawXOffset+m_XMin*gPolyXBoxSize,         //x
00116                     gDrawYOffset+m_YMin*gPolyYBoxSize,         //y
00117                     (m_XMax-m_XMin+1)*gPolyXBoxSize,           //sizex
00118                     (m_YMax-m_YMin+1)*gPolyYBoxSize);          //sizey
00119                 for(int y=0; y<ldrawing.size_y; y++) {
00120                     ldrawing.set_pos(0,y);
00121                     color_t my_color = myColor();
00122                     for(int x=0;x < ldrawing.size_x; x++) {
00123                          ldrawing.put_pixel(my_color);
00124                     }
00125                 }
00126             }
00127         }
00128     }
00129     int  area() {return ((m_XMax-m_XMin+1)*(m_YMax-m_YMin+1));}
00130     void print(int i) { cout << "RPolygon " << i << " (" << m_XMin << ", " << m_YMin << ")-(" << m_XMax << ", " << m_YMax << ") " << endl; fflush(stdout);}
00131 private:
00132     int m_XMin;
00133     int m_YMin;
00134     int m_XMax;
00135     int m_YMax;
00136     colorcomp_t m_r;
00137     colorcomp_t m_g;
00138     colorcomp_t m_b;
00139 };
00140 
00141 extern ostream& operator<<(ostream& s, const RPolygon &p);
00142 
00143 class RPolygon_flagged {
00144     RPolygon *myPoly;
00145     bool is_duplicate;
00146 public:
00147     RPolygon_flagged() {myPoly = NULL; is_duplicate = false;}
00148     bool isDuplicate() {return is_duplicate;}
00149     void setDuplicate(bool newValue) {is_duplicate = newValue;}
00150     RPolygon *p() {return myPoly;}
00151     void setp(RPolygon *newp) {myPoly = newp;}
00152 };
00153 
00154 typedef class vector<RPolygon *> Polygon_map_t;
00155 typedef class vector<RPolygon_flagged> Flagged_map_t; // we'll make shallow copies
00156 
00157 inline bool PolygonsOverlap(RPolygon *p1, RPolygon *p2, int &xl, int &yl, int &xh, int &yh) {
00158     int xl1, yl1, xh1, yh1, xl2, yl2, xh2, yh2;
00159 #if _DEBUG
00160      rt_sleep(1);   // slow down the process so we can see it.
00161 #endif
00162     p1->get(&xl1, &yl1, &xh1, &yh1);
00163     p2->get(&xl2, &yl2, &xh2, &yh2);
00164     if(xl1 > xh2) return false;
00165     if(xh1 < xl2) return false;
00166     if(yl1 > yh2) return false;
00167     if(yh1 < yl2) return false;
00168     xl = (xl1 < xl2) ? xl2 : xl1;
00169     xh = (xh1 < xh2) ? xh1 : xh2;
00170     yl = (yl1 < yl2) ? yl2 : yl1;
00171     yh = (yh1 < yh2) ? yh1 : yh2;
00172     return true;
00173 }
00174 
00175 #endif // _RPOLYGON_H_

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net