00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef _quickcg_h_included
00048 #define _quickcg_h_included
00049
00050 #include <SDL/SDL.h>
00051
00052 #include <string>
00053 #include <sstream>
00054 #include <iomanip>
00055 #include <vector>
00056 #include <algorithm>
00057
00058 namespace QuickCG
00059 {
00060
00062
00064
00065
00066 template<typename T>
00067 const T template_abs(const T &a)
00068 {
00069 return (a < 0) ? -a : a;
00070 }
00071
00072
00073 template<typename T>
00074 std::string valtostr(const T& val)
00075 {
00076 std::ostringstream sstream;
00077 sstream << val;
00078 return sstream.str();
00079 }
00080
00081
00082 template<typename T>
00083 T strtoval(const std::string& s)
00084 {
00085 std::istringstream sstream(s);
00086 T val;
00087 sstream >> val;
00088 return val;
00089 }
00090
00091
00092 template<typename T>
00093 std::string valtostr(const T& val, int length, bool fixed = true)
00094 {
00095 std::ostringstream sstream;
00096 if(fixed) sstream << std::fixed;
00097 sstream << std::setprecision(length) << val;
00098 return sstream.str();
00099 }
00100
00102
00104
00105 struct ColorRGB8bit;
00106
00107 struct ColorRGB
00108 {
00109 int r;
00110 int g;
00111 int b;
00112
00113 ColorRGB(Uint8 r, Uint8 g, Uint8 b);
00114 ColorRGB(const ColorRGB8bit& color);
00115 ColorRGB();
00116 };
00117
00118 ColorRGB operator+(const ColorRGB& color, const ColorRGB& color2);
00119 ColorRGB operator-(const ColorRGB& color, const ColorRGB& color2);
00120 ColorRGB operator*(const ColorRGB& color, int a);
00121 ColorRGB operator*(int a, const ColorRGB& color);
00122 ColorRGB operator/(const ColorRGB& color, int a);
00123 bool operator==(const ColorRGB& color, const ColorRGB& color2);
00124 bool operator!=(const ColorRGB& color, const ColorRGB& color2);
00125
00126 static const ColorRGB RGB_Black ( 0, 0, 0);
00127 static const ColorRGB RGB_Red (255, 0, 0);
00128 static const ColorRGB RGB_Green ( 0, 255, 0);
00129 static const ColorRGB RGB_Blue ( 0, 0, 255);
00130 static const ColorRGB RGB_Cyan ( 0, 255, 255);
00131 static const ColorRGB RGB_Magenta (255, 0, 255);
00132 static const ColorRGB RGB_Yellow (255, 255, 0);
00133 static const ColorRGB RGB_White (255, 255, 255);
00134 static const ColorRGB RGB_Gray (128, 128, 128);
00135 static const ColorRGB RGB_Grey (192, 192, 192);
00136 static const ColorRGB RGB_Maroon (128, 0, 0);
00137 static const ColorRGB RGB_Darkgreen( 0, 128, 0);
00138 static const ColorRGB RGB_Navy ( 0, 0, 128);
00139 static const ColorRGB RGB_Teal ( 0, 128, 128);
00140 static const ColorRGB RGB_Purple (128, 0, 128);
00141 static const ColorRGB RGB_Olive (128, 128, 0);
00142
00143
00144 struct ColorRGB8bit
00145 {
00146 Uint8 r;
00147 Uint8 g;
00148 Uint8 b;
00149
00150 ColorRGB8bit(Uint8 r, Uint8 g, Uint8 b);
00151 ColorRGB8bit(const ColorRGB& color);
00152 ColorRGB8bit();
00153 };
00154
00155
00156 struct ColorHSL
00157 {
00158 int h;
00159 int s;
00160 int l;
00161
00162 ColorHSL(Uint8 h, Uint8 s, Uint8 l);
00163 ColorHSL();
00164 };
00165
00166
00167 struct ColorHSV
00168 {
00169 int h;
00170 int s;
00171 int v;
00172
00173 ColorHSV(Uint8 h, Uint8 s, Uint8 v);
00174 ColorHSV();
00175 };
00176
00178
00180
00181 extern int w;
00182 extern int h;
00183
00185
00187
00188 bool keyDown(int key);
00189 bool keyPressed(int key);
00190
00192
00194
00195 void screen(int width = 640, int height = 400, bool fullscreen = 0, const std::string& text = " ");
00196 void lock();
00197 void unlock();
00198 void redraw();
00199 void cls(const ColorRGB& color = RGB_Black);
00200 void pset(int x, int y, const ColorRGB& color);
00201 ColorRGB pget(int x, int y);
00202 void drawBuffer(Uint32 *buffer);
00203 bool onScreen(int x, int y);
00204
00206
00208
00209 void sleep();
00210 void waitFrame(double oldTime, double frameDuration);
00211 bool done(bool quit_if_esc = true, bool delay = true);
00212 void end();
00213 void readKeys();
00214 void getMouseState(int& mouseX, int& mouseY);
00215 void getMouseState(int& mouseX, int& mouseY, bool& LMB, bool& RMB);
00216 unsigned long getTicks();
00217 inline double getTime() { return getTicks() / 1000.0; }
00218
00220
00222
00223 bool horLine(int y, int x1, int x2, const ColorRGB& color);
00224 bool verLine(int x, int y1, int y2, const ColorRGB& color);
00225 bool drawLine(int x1, int y1, int x2, int y2, const ColorRGB& color);
00226 bool drawCircle(int xc, int yc, int radius, const ColorRGB& color);
00227 bool drawDisk(int xc, int yc, int radius, const ColorRGB& color);
00228 bool drawRect(int x1, int y1, int x2, int y2, const ColorRGB& color);
00229 bool clipLine(int x1,int y1,int x2, int y2, int & x3, int & y3, int & x4, int & y4);
00230
00232
00234 ColorHSL RGBtoHSL(const ColorRGB& colorRGB);
00235 ColorRGB HSLtoRGB(const ColorHSL& colorHSL);
00236 ColorHSV RGBtoHSV(const ColorRGB& colorRGB);
00237 ColorRGB HSVtoRGB(const ColorHSV& colorHSV);
00238 Uint32 RGBtoINT(const ColorRGB& colorRGB);
00239 ColorRGB INTtoRGB(Uint32 colorINT);
00240
00242
00244
00245 void loadFile(std::vector<unsigned char>& buffer, const std::string& filename);
00246 void saveFile(const std::vector<unsigned char>& buffer, const std::string& filename);
00247
00249
00251
00252 int loadImage(std::vector<ColorRGB>& out, unsigned long& w, unsigned long& h, const std::string& filename);
00253 int loadImage(std::vector<Uint32>& out, unsigned long& w, unsigned long& h, const std::string& filename);
00254 int decodePNG(std::vector<unsigned char>& out_image_32bit, unsigned long& image_width, unsigned long& image_height, const unsigned char* in_png, unsigned long in_size);
00255 int decodePNG(std::vector<unsigned char>& out_image_32bit, unsigned long& image_width, unsigned long& image_height, const std::vector<unsigned char>& in_png);
00256
00258
00260 extern bool font[256][8][8];
00261 void drawLetter(unsigned char n, int x, int y, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black);
00262 int printString(const std::string& text, int x = 0, int y = 0, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black, int forceLength = 0);
00263
00264
00265 template<typename T>
00266 int print(const T& val, int x = 0, int y = 0, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black, int forceLength = 0)
00267 {
00268 std::string text = valtostr(val);
00269 return printString(text, x, y, color, bg, color2, forceLength);
00270 }
00271
00272
00273 template<typename T>
00274 int fprint(const T& val, int length, int x = 0, int y = 0, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black, int forceLength = 0)
00275 {
00276 std::string text = valtostr(val, length, true);
00277 return printString(text, x, y, color, bg, color2, forceLength);
00278 }
00279
00281
00283 Uint8 getInputCharacter();
00284 void getInputString(std::string& text, const std::string& message = "", bool clear = false, int x = 0, int y = 0, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black);
00285
00286 template<typename T>
00287 T getInput(const std::string& message = "", bool clear = false, int x = 0, int y = 0, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black)
00288 {
00289 std::string text;
00290 getInputString(text, message, clear, x, y, color, bg, color2);
00291 return strtoval<T>(text);
00292 }
00293
00295
00297
00298 int audioOpen(int samplerate, int framesize);
00299 void audioClose();
00300 int audioReOpen();
00301
00302
00303
00304
00305
00306 void audioPushSamples(const std::vector<double>& samples, size_t pos, size_t end);
00307
00308 size_t audioSamplesShortage();
00309 size_t audioSamplesOverflow();
00310 void audioSetBufferSamplesRange(size_t min_samples, size_t max_samples);
00311
00312
00313
00314
00315
00316
00317
00318
00319 void audioPlay(const std::vector<double>& samples);
00320
00321 void audioSetMode(int mode);
00322 void audioSetVolume(double volume);
00323
00324 }
00325
00326 #endif
00327
00328