VSGL2 - Very Simple Graphic Library 2  2.0
 All Classes Namespaces Files Functions Variables Macros Pages
vsgl2.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 Alessandro Bugatti (alessandro.bugatti@istruzione.it)
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  as published by the Free Software Foundation; either version 2
7  of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #ifndef VSGL2_H_INCLUDED
20 #define VSGL2_H_INCLUDED
21 
40 #include <string>
41 #include <sstream>
42 #include <SDL2/SDL.h>
43 #include <SDL2/SDL_net.h>
44 #include <SDL2/SDL_image.h>
45 #include <SDL2/SDL_mixer.h>
46 #include <SDL2/SDL_ttf.h>
47 
48 using namespace std;
49 
50 namespace vsgl2
51 {
52 
53 #include "vsgl2_keycode.h"
54 
55 class Color
56 {
57 
58 public:
59  SDL_Color c;
60  Color (){}
61  Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
62  {
63  c.r = r;
64  c.g = g;
65  c.b = b;
66  c.a = a;
67  }
68 };
69 
70 namespace general
71 {
72 
73 const int FULLSCREEN = SDL_WINDOW_FULLSCREEN;
74 
78 void init();
79 
83 void close();
84 
93 void set_window(int w, int h, string title, int fullscreen = 0);
94 
100 void set_background_color(const Color& bg);
101 
106 int get_window_width();
107 
112 int get_window_height();
113 
114 }//closing namespace general
115 
116 namespace video
117 {
123 bool done();
124 
130 void undone();
131 
132 
137 void update();
138 
146 void draw_point(int x, int y, const Color &c);
147 
156 void draw_line(int x1, int y1, int x2, int y2, const Color &c);
157 
168 void draw_rect(int x, int y, int w, int h, const Color &c);
169 
180 void draw_filled_rect(int x, int y, int w, int h, const Color &c);
181 
199 void draw_image(string image, int x, int y, int w, int h, uint8_t alpha = 255);
200 
201 }//closing video namespace
202 
203 
204 namespace audio
205 {
214  void play_music(string music);
219  void pause_music();
224  void stop_music();
235  void play_sound(string sound);
236 }//closing namespace audio
237 
238 
239 namespace io
240 {
252  bool is_pressed(int key);
253 
267  string read_text(string font, int dim, int x, int y, Color c, uint8_t max_length = 0);
268 
274  int get_mouse_x();
275 
280  int get_mouse_y();
281 
288  int get_mouse_wheel_x();
289 
295  int get_mouse_wheel_y();
296 
303 
310 }//closing io namespaces
311 
312 namespace ttf_fonts
313 {
328  void draw_text(string font, int dim, string text, int x, int y, Color c);
329 
340  int text_width(string font, int dim, string text);
352  int text_height(string font, int dim, string text);
353 
354 }
355 
356 namespace utils
357 {
358 
366 
371 void delay(int milliseconds);
372 
378 unsigned int ms_time();
379 
386 int take_screenshot(string filename);
387 
388 }//closing namaspace utility
389 
390 }
391 
392 //This is here to avoid using of namespace by the user of the library
393 using namespace vsgl2;
394 using namespace vsgl2::general;
395 using namespace vsgl2::video;
396 using namespace vsgl2::audio;
397 using namespace vsgl2::io;
398 using namespace vsgl2::ttf_fonts;
399 using namespace vsgl2::utils;
400 #endif //VSGL2_H_INCLUDED
void delay(int milliseconds)
Use this function to stop the execution for a given amount of milliseconds.
Definition: vsgl2.cpp:546
unsigned int ms_time()
Use this function to get the time from SDL initialization in milliseconds.
Definition: vsgl2.cpp:551
void draw_line(int x1, int y1, int x2, int y2, const Color &c)
Use to draw a line.
Definition: vsgl2.cpp:247
void update()
Use this function to update the screen after drawing on it Typically called at the end of the main lo...
Definition: vsgl2.cpp:179
int get_mouse_wheel_y()
Use to get the mouse wheel vertical value,.
Definition: vsgl2.cpp:412
void play_sound(string sound)
Use to play a sound (WAVE, MOD, MIDI, OGG, MP3, FLAC, and any file that you use a command to play wit...
Definition: vsgl2.cpp:318
bool mouse_left_button_pressed()
Use to check if the mouse left button is pressed.
Definition: vsgl2.cpp:417
void draw_point(int x, int y, const Color &c)
Use to draw a point at x,y coordinates.
Definition: vsgl2.cpp:206
void draw_text(string font, int dim, string text, int x, int y, Color c)
Use this function to display a text on the screen with the desired font and dimension.
Definition: vsgl2.cpp:454
string read_text(string font, int dim, int x, int y, Color c, uint8_t max_length)
Use to read text from the user, the return key stops the input.
Definition: vsgl2.cpp:340
void pause_music()
Use to pause the music that is currently playing.
Definition: vsgl2.cpp:305
int get_mouse_x()
Use to get the mouse x coordinate.
Definition: vsgl2.cpp:397
void play_music(string file)
Use to play a music in a loop (WAVE, MOD, MIDI, OGG, MP3, FLAC, and any file that you use a command t...
Definition: vsgl2.cpp:288
void draw_filled_rect(int x, int y, int w, int h, const Color &c)
Use to draw a filled rectangle, use instead draw_rect to draw an empty rectangle with colored borders...
Definition: vsgl2.cpp:231
const int FULLSCREEN
Definition: vsgl2.h:73
void set_background_color(const Color &bg)
Use this function to change the background color.
Definition: vsgl2.cpp:144
Color()
Definition: vsgl2.h:60
void wait_for_button_pressed()
Use this function to wait for any button to be pressed.
Definition: vsgl2.cpp:524
void undone()
Use this function to reset the isDone glabal variable to its initial state (false) This can be useful...
Definition: vsgl2.cpp:174
int get_mouse_y()
Use to get the mouse y coordinate.
Definition: vsgl2.cpp:402
int take_screenshot(string filename)
Use this function to take a screenshot of the window.
Definition: vsgl2.cpp:556
void init()
Use this function to initiate the library, must be called first.
Definition: vsgl2.cpp:52
Definition: vsgl2.h:55
SDL_Color c
Definition: vsgl2.h:59
Color(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
Definition: vsgl2.h:61
bool mouse_right_button_pressed()
Use to check if the mouse right button is pressed.
Definition: vsgl2.cpp:422
void draw_image(string image, int x, int y, int w, int h, uint8_t alpha)
Use to draw an image (BMP, PNG, JPG)
Definition: vsgl2.cpp:257
int get_window_height()
Use this function to get the height of the current window.
Definition: vsgl2.cpp:159
void stop_music()
Use to stop the music that is currently playing.
Definition: vsgl2.cpp:312
int text_height(string font, int dim, string text)
Use this function to retrieve the height of a string, given the font, the dimension and the text...
Definition: vsgl2.cpp:503
void draw_rect(int x, int y, int w, int h, const Color &c)
Use to draw a rectangle (only the border), use instead draw_filled_rect to draw a rectangle filled wi...
Definition: vsgl2.cpp:215
int get_mouse_wheel_x()
Use to get the mouse wheel horizontal value,.
Definition: vsgl2.cpp:407
int get_window_width()
Use this function to get the width of the current window.
Definition: vsgl2.cpp:154
Mix_Music * music
Definition: vsgl2.cpp:47
bool is_pressed(int key)
Use to check if a button is pressed.
Definition: vsgl2.cpp:334
void set_window(int w, int h, string title, int fullscreen)
Use this function to create a window with the specified dimensions and a title, optionally in full sc...
Definition: vsgl2.cpp:113
bool done()
Use this function to check when to stop the main loop.
Definition: vsgl2.cpp:168
void close()
Close and free all the resources, must be called last.
Definition: vsgl2.cpp:86
int text_width(string font, int dim, string text)
Use this function to retrieve the width of a string, given the font, the dimension and the text...
Definition: vsgl2.cpp:487