Very Simple Graphic Library
|
00001 /* 00002 QuickCG 20071121 00003 00004 Copyright (c) 2004-2007, Lode Vandevenne 00005 00006 All rights reserved. 00007 00008 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 00009 00010 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 00011 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 00012 00013 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00014 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00015 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00016 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00017 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00018 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00019 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00020 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00021 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00022 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00023 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00026 /* 00027 QuickCG is an SDL codebase that wraps some of the SDL functionality. 00028 It's used by Lode's Computer Graphics Tutorial to work with simple function calls 00029 to demonstrate graphical programs. It may or may not be of industrial strength 00030 for games, though I've actually used it for some. 00031 00032 QuickCG can handle some things that standard C++ does not but that are useful, such as: 00033 -drawing graphics 00034 -a bitmap font 00035 -simplified saving and loading of files 00036 -reading keyboard and mouse input 00037 -playing sound 00038 -color models 00039 -loading images 00040 00041 Contact info: 00042 My email address is (puzzle the account and domain together with an @ symbol): 00043 Domain: gmail dot com. 00044 Account: lode dot vandevenne. 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> //std::min and std::max 00057 00058 namespace QuickCG 00059 { 00060 00062 //useful templates////////////////////////////////////////////////////////////// 00064 00065 //don't know why, but the standard C++ abs sometimes gives cryptic errors? if so use this :D 00066 template<typename T> 00067 const T template_abs(const T &a) 00068 { 00069 return (a < 0) ? -a : a; 00070 } 00071 00072 //usage: std::string str = valtostr(25454.91654654f); 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 //usage: double val = strtoval<double>("465498.654"); 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 //length is decimal precision of the floating point number 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 //COLOR STRUCTS///////////////////////////////////////////////////////////////// 00104 00105 struct ColorRGB8bit; 00106 //a color with 3 components: r, g and b 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 //a color with 3 components: r, g and b 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 //a color with 3 components: h, s and l 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 //a color with 3 components: h, s and v 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 //GLOBAL VARIABLES////////////////////////////////////////////////////////////// 00180 00181 extern int w; 00182 extern int h; 00183 00185 //KEYBOARD FUNCTIONS//////////////////////////////////////////////////////////// 00187 00188 bool keyDown(int key); //this checks if the key is held down, returns true all the time until the key is up 00189 bool keyPressed(int key); //this checks if the key is *just* pressed, returns true only once until the key is up again 00190 00192 //BASIC SCREEN FUNCTIONS//////////////////////////////////////////////////////// 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 //NON GRAPHICAL FUNCTIONS/////////////////////////////////////////////////////// 00208 00209 void sleep(); 00210 void waitFrame(double oldTime, double frameDuration); //in seconds 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(); //ticks in milliseconds 00217 inline double getTime() { return getTicks() / 1000.0; } //time in seconds 00218 00220 //2D SHAPES///////////////////////////////////////////////////////////////////// 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 //COLOR CONVERSIONS///////////////////////////////////////////////////////////// 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 //FILE FUNCTIONS//////////////////////////////////////////////////////////////// 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 //IMAGE FUNCTIONS/////////////////////////////////////////////////////////////// 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 //TEXT FUNCTIONS//////////////////////////////////////////////////////////////// 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 //print something (string, int, float, ...) 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 //print some floating point number, this one allows printing floating point numbers with limited length 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 //TEXT INPUT FUNCTIONS////////////////////////////////////////////////////////// 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 //SOUNDCARD FUNCTIONS/////////////////////////////////////////////////////////// 00297 00298 int audioOpen(int samplerate, int framesize); //always 16-bit mono sound for now; returns 0 if no error happened 00299 void audioClose(); 00300 int audioReOpen(); //closes and opens again with same parameters 00301 00302 /* 00303 push samples to the soundcard, making sure not to cause shortage or overflow 00304 pos and end are the range in the samples vector that you want to push to the audio card 00305 */ 00306 void audioPushSamples(const std::vector<double>& samples, size_t pos, size_t end); 00307 00308 size_t audioSamplesShortage(); //returns value > 0 if the soundcard is consuming more samples than you're producing 00309 size_t audioSamplesOverflow(); //returns value > 0 if you're producing more samples than the soundard is consuming - so take it easy a bit 00310 void audioSetBufferSamplesRange(size_t min_samples, size_t max_samples); //set shortage and overflow values. E.g. 4096 and 8192. 00311 00312 /* 00313 This plays the sound starting at this time, until it's done 00314 The difference with audioPushSamples is: 00315 audioPlay allows playing multiple sounds at the same time: it doesn't push at the end, 00316 but elementwise-adds or pushes back samples if needed. 00317 The duration depends on samplerate, make sure the samples in the vector have the correct samplerate. 00318 */ 00319 void audioPlay(const std::vector<double>& samples); 00320 00321 void audioSetMode(int mode); //0: silent, 1: full (no volume calculations ==> faster), 2: volume-controlled (= default value) 00322 void audioSetVolume(double volume); //multiplier used if mode is 2 (volume-controlled). Default value is 1.0. 00323 00324 } //end of namespace QuickCG 00325 00326 #endif 00327 00328