Very Simple Graphic Library
 Tutto Classi File Funzioni Variabili Pagine
quickcg.h
1 /*
2 QuickCG 20071121
3 
4 Copyright (c) 2004-2007, Lode Vandevenne
5 
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11  * 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.
12 
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
14 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
15 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
16 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 
26 /*
27 QuickCG is an SDL codebase that wraps some of the SDL functionality.
28 It's used by Lode's Computer Graphics Tutorial to work with simple function calls
29 to demonstrate graphical programs. It may or may not be of industrial strength
30 for games, though I've actually used it for some.
31 
32 QuickCG can handle some things that standard C++ does not but that are useful, such as:
33 -drawing graphics
34 -a bitmap font
35 -simplified saving and loading of files
36 -reading keyboard and mouse input
37 -playing sound
38 -color models
39 -loading images
40 
41 Contact info:
42 My email address is (puzzle the account and domain together with an @ symbol):
43 Domain: gmail dot com.
44 Account: lode dot vandevenne.
45 */
46 
47 #ifndef _quickcg_h_included
48 #define _quickcg_h_included
49 
50 #include <SDL/SDL.h>
51 
52 #include <string>
53 #include <sstream>
54 #include <iomanip>
55 #include <vector>
56 #include <algorithm> //std::min and std::max
57 
58 namespace QuickCG
59 {
60 
62 //useful templates//////////////////////////////////////////////////////////////
64 
65 //don't know why, but the standard C++ abs sometimes gives cryptic errors? if so use this :D
66 template<typename T>
67 const T template_abs(const T &a)
68 {
69  return (a < 0) ? -a : a;
70 }
71 
72 //usage: std::string str = valtostr(25454.91654654f);
73 template<typename T>
74 std::string valtostr(const T& val)
75 {
76  std::ostringstream sstream;
77  sstream << val;
78  return sstream.str();
79 }
80 
81 //usage: double val = strtoval<double>("465498.654");
82 template<typename T>
83 T strtoval(const std::string& s)
84 {
85  std::istringstream sstream(s);
86  T val;
87  sstream >> val;
88  return val;
89 }
90 
91 //length is decimal precision of the floating point number
92 template<typename T>
93 std::string valtostr(const T& val, int length, bool fixed = true)
94 {
95  std::ostringstream sstream;
96  if(fixed) sstream << std::fixed;
97  sstream << std::setprecision(length) << val;
98  return sstream.str();
99 }
100 
102 //COLOR STRUCTS/////////////////////////////////////////////////////////////////
104 
105 struct ColorRGB8bit;
106 //a color with 3 components: r, g and b
107 struct ColorRGB
108 {
109  int r;
110  int g;
111  int b;
112 
113  ColorRGB(Uint8 r, Uint8 g, Uint8 b);
114  ColorRGB(const ColorRGB8bit& color);
115  ColorRGB();
116 };
117 
118 ColorRGB operator+(const ColorRGB& color, const ColorRGB& color2);
119 ColorRGB operator-(const ColorRGB& color, const ColorRGB& color2);
120 ColorRGB operator*(const ColorRGB& color, int a);
121 ColorRGB operator*(int a, const ColorRGB& color);
122 ColorRGB operator/(const ColorRGB& color, int a);
123 bool operator==(const ColorRGB& color, const ColorRGB& color2);
124 bool operator!=(const ColorRGB& color, const ColorRGB& color2);
125 
126 static const ColorRGB RGB_Black ( 0, 0, 0);
127 static const ColorRGB RGB_Red (255, 0, 0);
128 static const ColorRGB RGB_Green ( 0, 255, 0);
129 static const ColorRGB RGB_Blue ( 0, 0, 255);
130 static const ColorRGB RGB_Cyan ( 0, 255, 255);
131 static const ColorRGB RGB_Magenta (255, 0, 255);
132 static const ColorRGB RGB_Yellow (255, 255, 0);
133 static const ColorRGB RGB_White (255, 255, 255);
134 static const ColorRGB RGB_Gray (128, 128, 128);
135 static const ColorRGB RGB_Grey (192, 192, 192);
136 static const ColorRGB RGB_Maroon (128, 0, 0);
137 static const ColorRGB RGB_Darkgreen( 0, 128, 0);
138 static const ColorRGB RGB_Navy ( 0, 0, 128);
139 static const ColorRGB RGB_Teal ( 0, 128, 128);
140 static const ColorRGB RGB_Purple (128, 0, 128);
141 static const ColorRGB RGB_Olive (128, 128, 0);
142 
143 //a color with 3 components: r, g and b
145 {
146  Uint8 r;
147  Uint8 g;
148  Uint8 b;
149 
150  ColorRGB8bit(Uint8 r, Uint8 g, Uint8 b);
151  ColorRGB8bit(const ColorRGB& color);
152  ColorRGB8bit();
153 };
154 
155 //a color with 3 components: h, s and l
156 struct ColorHSL
157 {
158  int h;
159  int s;
160  int l;
161 
162  ColorHSL(Uint8 h, Uint8 s, Uint8 l);
163  ColorHSL();
164 };
165 
166 //a color with 3 components: h, s and v
167 struct ColorHSV
168 {
169  int h;
170  int s;
171  int v;
172 
173  ColorHSV(Uint8 h, Uint8 s, Uint8 v);
174  ColorHSV();
175 };
176 
178 //GLOBAL VARIABLES//////////////////////////////////////////////////////////////
180 
181 extern int w;
182 extern int h;
183 
185 //KEYBOARD FUNCTIONS////////////////////////////////////////////////////////////
187 
188 bool keyDown(int key); //this checks if the key is held down, returns true all the time until the key is up
189 bool keyPressed(int key); //this checks if the key is *just* pressed, returns true only once until the key is up again
190 
192 //BASIC SCREEN FUNCTIONS////////////////////////////////////////////////////////
194 
195 void screen(int width = 640, int height = 400, bool fullscreen = 0, const std::string& text = " ");
196 void lock();
197 void unlock();
198 void redraw();
199 void cls(const ColorRGB& color = RGB_Black);
200 void pset(int x, int y, const ColorRGB& color);
201 ColorRGB pget(int x, int y);
202 void drawBuffer(Uint32 *buffer);
203 bool onScreen(int x, int y);
204 SDL_Surface* getScreenSurface();
205 
207 //NON GRAPHICAL FUNCTIONS///////////////////////////////////////////////////////
209 
210 void sleep();
211 void waitFrame(double oldTime, double frameDuration); //in seconds
212 bool done(bool quit_if_esc = true, bool delay = true);
213 void end();
214 void readKeys();
215 void getMouseState(int& mouseX, int& mouseY);
216 void getMouseState(int& mouseX, int& mouseY, bool& LMB, bool& RMB);
217 unsigned long getTicks(); //ticks in milliseconds
218 inline double getTime() { return getTicks() / 1000.0; } //time in seconds
219 
221 //2D SHAPES/////////////////////////////////////////////////////////////////////
223 
224 bool horLine(int y, int x1, int x2, const ColorRGB& color);
225 bool verLine(int x, int y1, int y2, const ColorRGB& color);
226 bool drawLine(int x1, int y1, int x2, int y2, const ColorRGB& color);
227 bool drawCircle(int xc, int yc, int radius, const ColorRGB& color);
228 bool drawDisk(int xc, int yc, int radius, const ColorRGB& color);
229 bool drawRect(int x1, int y1, int x2, int y2, const ColorRGB& color);
230 bool clipLine(int x1,int y1,int x2, int y2, int & x3, int & y3, int & x4, int & y4);
231 
233 //COLOR CONVERSIONS/////////////////////////////////////////////////////////////
235 ColorHSL RGBtoHSL(const ColorRGB& colorRGB);
236 ColorRGB HSLtoRGB(const ColorHSL& colorHSL);
237 ColorHSV RGBtoHSV(const ColorRGB& colorRGB);
238 ColorRGB HSVtoRGB(const ColorHSV& colorHSV);
239 Uint32 RGBtoINT(const ColorRGB& colorRGB);
240 ColorRGB INTtoRGB(Uint32 colorINT);
241 
243 //FILE FUNCTIONS////////////////////////////////////////////////////////////////
245 
246 void loadFile(std::vector<unsigned char>& buffer, const std::string& filename);
247 void saveFile(const std::vector<unsigned char>& buffer, const std::string& filename);
248 
250 //IMAGE FUNCTIONS///////////////////////////////////////////////////////////////
252 
253 int loadImage(std::vector<ColorRGB>& out, unsigned long& w, unsigned long& h, const std::string& filename);
254 int loadImage(std::vector<Uint32>& out, unsigned long& w, unsigned long& h, const std::string& filename);
255 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);
256 int decodePNG(std::vector<unsigned char>& out_image_32bit, unsigned long& image_width, unsigned long& image_height, const std::vector<unsigned char>& in_png);
257 
259 //TEXT FUNCTIONS////////////////////////////////////////////////////////////////
261 extern bool font[256][8][8];
262 void drawLetter(unsigned char n, int x, int y, const ColorRGB& color = RGB_White, bool bg = 0, const ColorRGB& color2 = RGB_Black);
263 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);
264 
265 //print something (string, int, float, ...)
266 template<typename T>
267 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)
268 {
269  std::string text = valtostr(val);
270  return printString(text, x, y, color, bg, color2, forceLength);
271 }
272 
273 //print some floating point number, this one allows printing floating point numbers with limited length
274 template<typename T>
275 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)
276 {
277  std::string text = valtostr(val, length, true);
278  return printString(text, x, y, color, bg, color2, forceLength);
279 }
280 
282 //TEXT INPUT FUNCTIONS//////////////////////////////////////////////////////////
284 Uint8 getInputCharacter();
285 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);
286 
287 template<typename T>
288 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)
289 {
290  std::string text;
291  getInputString(text, message, clear, x, y, color, bg, color2);
292  return strtoval<T>(text);
293 }
294 
296 //SOUNDCARD FUNCTIONS///////////////////////////////////////////////////////////
298 
299 int audioOpen(int samplerate, int framesize); //always 16-bit mono sound for now; returns 0 if no error happened
300 void audioClose();
301 int audioReOpen(); //closes and opens again with same parameters
302 
303 /*
304 push samples to the soundcard, making sure not to cause shortage or overflow
305 pos and end are the range in the samples vector that you want to push to the audio card
306 */
307 void audioPushSamples(const std::vector<double>& samples, size_t pos, size_t end);
308 
309 size_t audioSamplesShortage(); //returns value > 0 if the soundcard is consuming more samples than you're producing
310 size_t audioSamplesOverflow(); //returns value > 0 if you're producing more samples than the soundard is consuming - so take it easy a bit
311 void audioSetBufferSamplesRange(size_t min_samples, size_t max_samples); //set shortage and overflow values. E.g. 4096 and 8192.
312 
313 /*
314 This plays the sound starting at this time, until it's done
315 The difference with audioPushSamples is:
316 audioPlay allows playing multiple sounds at the same time: it doesn't push at the end,
317 but elementwise-adds or pushes back samples if needed.
318 The duration depends on samplerate, make sure the samples in the vector have the correct samplerate.
319 */
320 void audioPlay(const std::vector<double>& samples);
321 
322 void audioSetMode(int mode); //0: silent, 1: full (no volume calculations ==> faster), 2: volume-controlled (= default value)
323 void audioSetVolume(double volume); //multiplier used if mode is 2 (volume-controlled). Default value is 1.0.
324 
325 } //end of namespace QuickCG
326 
327 #endif
328 
329 
Definition: quickcg.h:107
int mouseX
Variabile globale che contiene le coordinate X del mouse.
Definition: vsgl.cpp:26
int mouseY
Variabile globale che contiene le coordinate Y del mouse.
Definition: vsgl.cpp:26
Definition: quickcg.h:156
Definition: quickcg.h:144
Definition: quickcg.h:167