00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef COMMON_H
00023 #define COMMON_H
00024
00025 #include <glibmm/ustring.h>
00026 #include <iostream>
00027 #include <signal.h>
00028 #include <vector>
00029 #include "SDL.h"
00030
00031 class Color;
00032 class ValueRange;
00033
00035 typedef Glib::ustring ustring;
00036
00038 typedef gunichar uchar;
00039
00041 const ustring STR_NULL="null";
00042
00044 typedef std::pair<ustring, ustring> StringPair;
00045
00047 typedef std::vector<std::pair<ValueRange, Color> > ColorRangeVector;
00048
00049
00050 static void onSigSegv(int sig) { };
00051
00057 class Color {
00058 public:
00065 Color(char r=255, char g=255, char b=255, char a=255) {
00066 m_R=r;
00067 m_G=g;
00068 m_B=b;
00069 m_A=a;
00070 }
00071
00075 SDL_Color toSDLColor() const {SDL_Color color={ m_R, m_G, m_B }; return color; }
00076
00078
00079 char r() const { return m_R; }
00080 char g() const { return m_G; }
00081 char b() const { return m_B; }
00082 char a() const { return m_A; }
00084
00085 private:
00086 char m_R, m_G, m_B, m_A;
00087 };
00088
00091 class ValueRange {
00092 public:
00097 ValueRange(int low, int high) {
00098 m_Low=low;
00099 m_High=high;
00100 }
00101
00105 bool operator<(ValueRange r) const {
00106 return (m_High!=r.getHighValue() || m_Low!=r.getLowValue());
00107 }
00108
00112 int difference() const { return m_High-m_Low; }
00113
00119 bool inRange(int val, bool inclusive=true) const {
00120 if (inclusive)
00121 return (val<=m_High && val>=m_Low);
00122 else
00123 return (m_High>val && val<m_Low);
00124 }
00125
00129 int getLowValue() const { return m_Low; }
00130
00134 int getHighValue() const { return m_High; }
00135
00136 private:
00137 int m_Low;
00138 int m_High;
00139 };
00140
00141
00142
00143 static std::ostream& operator<<(std::ostream &s, const ValueRange &range) {
00144 s << range.getLowValue() << " to " << range.getHighValue() << std::endl;
00145 return s;
00146 }
00147
00153 class Point {
00154 public:
00159 Point(int x=0, int y=0) {
00160 m_X=x;
00161 m_Y=y;
00162 }
00163
00168 Point operator+(const Point &p) {
00169 return Point(m_X+p.x(), m_Y+p.y());
00170 }
00171
00173 void invert() {
00174 m_X=-m_X;
00175 m_Y=-m_Y;
00176 }
00177
00181 void setX(int x) { m_X=x; }
00182
00186 void setY(int y) { m_Y=y; }
00187
00189
00190 int x() const { return m_X; }
00191 int y() const { return m_Y; }
00193
00194 private:
00195
00196 int m_X;
00197 int m_Y;
00198 };
00199
00200
00201 static std::ostream& operator<<(std::ostream &s, const Point &p) {
00202 s << "(" << p.x() << "," << p.y() << ")\n";
00203 return s;
00204 }
00205
00211 class Rect {
00212 public:
00214 Rect() {
00215 m_Corner=Point(0, 0);
00216 m_Width=m_Height=0;
00217 }
00218
00224 Rect(const Point &p, int w, int h) {
00225 m_Corner=p;
00226 m_Width=w;
00227 m_Height=h;
00228 }
00229
00233 Point getPoint() const { return m_Corner; }
00234
00236
00237 int getWidth() const { return m_Width; }
00238 int getHeight() const { return m_Height; }
00240
00246 void getGeometry(Point &p, int &w, int &h) const {
00247 p=m_Corner;
00248 w=m_Width;
00249 h=m_Height;
00250 }
00251
00252 private:
00253
00254 Point m_Corner;
00255
00256
00257 int m_Width;
00258 int m_Height;
00259 };
00260
00261
00262 static std::ostream& operator<<(std::ostream &s, const Rect &rect) {
00263 Point p;
00264 int w, h;
00265 rect.getGeometry(p, w, h);
00266
00267 s << "(" << p.x() << "," << p.y() << "), " << w << "x" << h << std::endl;
00268 return s;
00269 }
00270
00271 #endif