/[svn]/gigedit/trunk/src/gigedit/global.h
ViewVC logotype

Diff of /gigedit/trunk/src/gigedit/global.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2967 by schoenebeck, Mon Jul 18 11:22:38 2016 UTC revision 3089 by schoenebeck, Sun Jan 15 19:18:39 2017 UTC
# Line 1  Line 1 
1  /*                                                         -*- c++ -*-  /*                                                         -*- c++ -*-
2   * Copyright (C) 2007-2015 Andreas Persson   * Copyright (C) 2007-2017 Andreas Persson
3   *   *
4   * This program is free software; you can redistribute it and/or   * This program is free software; you can redistribute it and/or
5   * modify it under the terms of the GNU General Public License as   * modify it under the terms of the GNU General Public License as
# Line 30  Line 30 
30  #endif  #endif
31    
32  #include <sstream>  #include <sstream>
33    #include <map>
34    
35    #ifdef LIBGIG_HEADER_FILE
36    # include LIBGIG_HEADER_FILE(gig.h)
37    #else
38    # include <gig.h>
39    #endif
40    
41  //FIXME: for some reason AC GETTEXT check fails on the Mac cross compiler?  //FIXME: for some reason AC GETTEXT check fails on the Mac cross compiler?
42  #if (HAVE_GETTEXT || defined(__APPLE__))  #if (HAVE_GETTEXT || defined(__APPLE__))
# Line 54  template<class T> inline std::string ToS Line 61  template<class T> inline std::string ToS
61      return ss.str();      return ss.str();
62  }  }
63    
64    inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) {
65        for (uint i = 0; i < rgn->Dimensions; ++i)
66            if (rgn->pDimensionDefinitions[i].dimension == type)
67                return i;
68        return -1;
69    }
70    
71    inline int getDimensionRegionIndex(gig::DimensionRegion* dr) {
72        if (!dr) return -1;
73        gig::Region* rgn = (gig::Region*)dr->GetParent();
74        for (uint i = 0; i < 256; ++i)
75            if (rgn->pDimensionRegions[i] == dr)
76                return i;
77        return -1;
78    }
79    
80  /// Find the number of bits required to hold the specified amount of zones.  /// Find the number of bits required to hold the specified amount of zones.
81  inline int zoneCountToBits(int nZones) {  inline int zoneCountToBits(int nZones) {
82      if (!nZones) return 0;      if (!nZones) return 0;
# Line 64  inline int zoneCountToBits(int nZones) { Line 87  inline int zoneCountToBits(int nZones) {
87      return iFinalBits;      return iFinalBits;
88  }  }
89    
90    /**
91     * Returns the sum of all bits of all dimensions defined before the given
92     * dimensions (@a type). This allows to access cases of that particular
93     * dimension directly. If the supplied dimension @a type does not exist in the
94     * the supplied @a region, then this function returns -1 instead!
95     *
96     * @param type - dimension that shall be used
97     * @param rgn - parent region of that dimension
98     */
99    inline int baseBits(gig::dimension_t type, gig::Region* rgn) {
100        int previousBits = 0;
101        for (uint i = 0; i < rgn->Dimensions; ++i) {
102            if (rgn->pDimensionDefinitions[i].dimension == type) return previousBits;
103            previousBits += rgn->pDimensionDefinitions[i].bits;
104        }
105        return -1;
106    }
107    
108    // key: dimension type, value: dimension's zone index
109    class DimensionCase : public std::map<gig::dimension_t,int> {
110    public:
111        bool isViolating(const DimensionCase& c) const {
112            for (DimensionCase::const_iterator it = begin(); it != end(); ++it) {
113                if (c.find(it->first) == c.end()) continue;
114                if (c.find(it->first)->second != it->second) return true;
115            }
116            return false;
117        }
118    };
119    
120    inline DimensionCase dimensionCaseOf(gig::DimensionRegion* dr) {
121        DimensionCase dimCase;
122        int idr = getDimensionRegionIndex(dr);
123        if (idr < 0) return dimCase;
124        gig::Region* rgn = (gig::Region*)dr->GetParent();
125        int bitpos = 0;
126        for (int d = 0; d < rgn->Dimensions; ++d) {
127            const gig::dimension_def_t& dimdef = rgn->pDimensionDefinitions[d];
128            const int zone = (idr >> bitpos) & ((1 << dimdef.bits) - 1);
129            dimCase[dimdef.dimension] = zone;
130            bitpos += rgn->pDimensionDefinitions[d].bits;
131        }
132        return dimCase;
133    }
134    
135    inline std::vector<gig::DimensionRegion*> dimensionRegionsMatching(const DimensionCase& dimCase, gig::Region* rgn) {
136        std::vector<gig::DimensionRegion*> v;
137        for (int idr = 0; idr < 256; ++idr) {
138            if (!rgn->pDimensionRegions[idr]) continue;
139            DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]);
140            if (!dimCase.isViolating(c)) v.push_back(rgn->pDimensionRegions[idr]);
141        }
142        return v;
143    }
144    
145  #endif // GIGEDIT_GLOBAL_H  #endif // GIGEDIT_GLOBAL_H

Legend:
Removed from v.2967  
changed lines
  Added in v.3089

  ViewVC Help
Powered by ViewVC