42 #include <SDL_image.h>
70 void init(
const char* name,
int w,
int h,
int posx=-1,
int posy=-1, SDL_WindowFlags flag = SDL_WindowFlags(0), std::string render_driver=
"");
77 void color(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a=255);
78 void colorf(
float r,
float g,
float b,
float a=1.f);
79 SDL_Color& getColor();
80 SDL_Color& getBackgroundColor();
81 void backgroundColor(
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a=255);
84 void setFont(
int s,
const char* ttf=NULL);
85 int keyHasBeenPressed(
unsigned int sc);
92 const SDL_Window* window()
const {
return m_window; }
93 SDL_Window* window() {
return m_window; }
94 SDL_Renderer * renderer() {
return m_renderer; }
95 const SDL_Renderer * renderer()
const {
return m_renderer; }
96 TTF_Font* font() {
return m_font; }
97 const TTF_Font* font()
const {
return m_font; }
98 bool hasFinished()
const {
return m_quit; }
99 bool isInit()
const {
return m_window; }
100 Uint32 textureFormat()
const {
return m_textureFormat; }
101 int winDimX()
const {
return m_width; }
102 int winDimY()
const {
return m_height; }
104 static Grapic& singleton(
bool secure=
true);
109 SDL_Window* m_window;
110 SDL_Renderer *m_renderer;
111 SDL_RendererInfo m_renderInfo;
112 Uint32 m_textureFormat;
114 std::string m_fontFile;
119 SDL_Color m_currentColor;
120 SDL_Color m_backgroundColor;
121 std::vector<int> m_keyStates;
122 bool m_keyRepeatMode;
123 int imagesSavedCount;
126 unsigned int m_frameTime[60];
127 float m_averageFramePerSecond;
128 unsigned int m_lastTime;
129 unsigned int m_frameNumber;
130 unsigned long m_frameSum;
134 bool manageOneEvent(SDL_Event event);
136 void initFrameCounter();
137 void updateFrameCounter();
139 static Grapic* currentGrapic;
140 friend void winInit(
const char* name,
int w,
int h,
int posx,
int posy);
151 Image(
const char* filename,
bool transparency,
unsigned char r,
unsigned char g,
unsigned b,
unsigned char a);
152 Image(
const Image& im);
156 Image& operator=(
const Image& im);
158 void copy(
const Image& im);
159 void savePNG(
const char* filename)
const;
161 unsigned char get(
int x,
int y,
int c)
const;
162 void set(
int x,
int y,
unsigned char r,
unsigned char g,
unsigned b,
unsigned char a);
163 void printInfo()
const;
165 const SDL_Surface* surface()
const;
167 void draw(
int x,
int y,
int w,
int h);
168 void draw(
int x,
int y,
int w,
int h,
float angle,
int flip);
171 SDL_Surface* m_surface;
172 SDL_Texture* m_texture;
176 void init(
int w,
int h);
188 void change(
int i,
const std::string& str);
190 void setSelect(
int s);
192 void draw(
int xmin,
int ymin,
int xmax,
int ymax);
193 void add(
const std::string& str);
197 std::vector<std::string> m_txt;
205 typedef std::vector< std::pair<float,float> > Curve;
211 void setSize(
const int n);
212 void add(
float x,
float y,
int curve_n);
213 void draw(
int xmin,
int ymin,
int xmax,
int ymax,
bool clearOrNot)
const;
214 void draw(
const Curve& cu,
int xmin,
int ymin,
int xmax,
int ymax,
float fxmin,
float fymin,
float fxmax,
float fymax)
const;
215 void minMax(
float& fxmin,
float& fymin,
float& fxmax,
float& fymax,
int& maxsize)
const;
218 std::vector< Curve > m_dat;
245 inline void winInit(
const char* name,
int w,
int h,
int posx=-1,
int posy=-1)
247 Grapic::currentGrapic =
new Grapic();
248 Grapic::singleton(
false).init(name,w,h,posx,posy);
264 grapic::Grapic::singleton().clear();
271 return Grapic::singleton().hasFinished();
278 grapic::Grapic::singleton().clearEvent();
302 return Grapic::singleton().display();
309 Grapic::singleton().quit();
310 if (Grapic::currentGrapic)
312 delete Grapic::currentGrapic;
313 Grapic::currentGrapic =
nullptr;
321 SDL_GL_SetSwapInterval(reduceFPS);
328 return Grapic::singleton().framesPerSecond();
336 return Grapic::singleton().lastFrameTime();
341 inline void color(
unsigned char _r = 255,
unsigned char _g = 255,
unsigned char _b = 255,
unsigned char _a = 255)
343 Grapic::singleton().color( _r, _g, _b, _a );
348 inline void colorf(
float _r = 1.f,
float _g = 1.f,
float _b = 1.f,
float _a = 1.f)
350 Grapic::singleton().colorf( _r, _g, _b, _a );
355 inline void backgroundColor(
unsigned char _r = 255,
unsigned char _g = 255,
unsigned char _b = 255,
unsigned char _a = 255)
357 Grapic::singleton().backgroundColor( _r, _g, _b, _a);
362 inline void backgroundColorf(
float _r = 1.f,
float _g = 1.f,
float _b = 1.f,
float _a = 1.f)
364 Grapic::singleton().backgroundColorf( _r, _g, _b, _a);
377 void ellipse(
int xc,
int yc,
int horizontal,
int vertical);
393 inline void line(
int x1,
int y1,
int x2,
int y2)
395 Grapic& g = Grapic::singleton();
396 SDL_RenderDrawLine( g.renderer(), x1, g.inverseY(y1), x2, g.inverseY(y2));
401 void put_pixel(
int x,
int y,
unsigned char r,
unsigned char g,
unsigned char b,
unsigned char a=255);
407 Grapic& g = Grapic::singleton();
408 SDL_RenderDrawPoint( g.renderer(), x, g.inverseY(y));
415 Grapic& g = Grapic::singleton();
416 SDL_RenderDrawPoints( g.renderer(), ((
const SDL_Point*)(p)), n);
421 void grid(
int xmin,
int ymin,
int xmax,
int ymax,
int nx,
int ny);
425 inline int irand(
int rmin=0,
int rmax=100)
427 return rmin + rand() % (rmax - rmin + 1);
432 inline float frand(
float rmin = 0.f,
float rmax = 1.f)
434 float r =
static_cast<float>(rand()) / RAND_MAX;
435 return rmin + r * (rmax - rmin);
442 return 0.001f * SDL_GetTicks();
455 Grapic& g = Grapic::singleton();
456 return (g.keyHasBeenPressed(key));
464 Grapic& g = Grapic::singleton();
465 g.setKeyRepeatMode(repeat);
488 return SDL_GetMouseState(
nullptr,
nullptr) & SDL_BUTTON(button);
514 return Grapic::singleton().manageEvent();
521 Grapic::singleton().setFont(s);
528 Grapic::singleton().setFont(Grapic::singleton().getFontSize(), path);
537 void print(
int x,
int y,
const char* txt);
544 inline void print(
int x,
int y,
int nb)
547 sprintf(txt,
"%d", nb);
557 inline void print(
int x,
int y,
float nb)
560 sprintf(txt,
"%.2f", nb);
587 inline Image
image(
const char* filename,
bool transparency=
false,
unsigned char r=255,
unsigned char g=255,
unsigned b=255,
unsigned char a=255)
589 return Image(filename, transparency, r, g, b, a);
608 im.savePNG(filename);
613 inline void image_draw(Image& im,
int x,
int y,
int w=-1,
int h=-1)
620 inline void image_draw(Image& im,
int x,
int y,
int w,
int h,
float angle,
float flip=SDL_FLIP_NONE)
622 im.draw(x,y,w,h,angle,flip);
628 inline unsigned char image_get(
const Image& im,
int x,
int y,
int c=0)
630 return im.get(x,y,c);
635 void image_set(Image& im,
int x,
int y,
unsigned char r,
unsigned char g,
unsigned b,
unsigned char a);
642 return im.surface()->w;
650 return im.surface()->h;
694 inline void menu_add(Menu& m,
const std::string& str)
721 inline void menu_draw(Menu& m,
int xmin=5,
int ymin=5,
int xmax=-1,
int ymax=-1)
723 m.draw(xmin,ymin,xmax,ymax);
741 return m.caseToPixel(c,ymin,ymax);
760 inline void plot_add(Plot& p,
float x,
float y,
int curve_n=0)
766 inline void plot_draw(
const Plot& p,
int xmin,
int ymin,
int xmax,
int ymax,
bool clearOrNot=
true)
768 p.draw(xmin,ymin,xmax,ymax,clearOrNot);
777 void triangle(
int x1,
int y1,
int x2,
int y2,
int x3,
int y3);
802 bool isInTriangle(
float px,
float py,
float ax,
float ay,
float bx,
float by,
float cx,
float cy);
833 void polygon(
int p[][2],
unsigned int number);
void ellipse(int xc, int yc, int horizontal, int vertical)
Draw an ellipse at (xc, yc), horizontal radius and vertical radius, rotation optional (Thanks to Anas...
void triangle(int x1, int y1, int x2, int y2, int x3, int y3)
Draw a triangle from the vertices (x1, y1), (x2, y2) and (x3, y3). (Code provided by Bastien DOIGNIES...
void menu_add(Menu &m, const std::string &str)
Add a line to the menu m with the text str.
Definition: Grapic.h:694
void backgroundColor(unsigned char _r=255, unsigned char _g=255, unsigned char _b=255, unsigned char _a=255)
Change the default background color (the color used to clear the screen)
Definition: Grapic.h:355
void regular_polygone(int x, int y, unsigned int apotheme, unsigned int line_number)
Draw a regular polygon with line_numbers edges centred on (x, y). (Code provided by Bastien DOIGNIES,...
void color(unsigned char _r=255, unsigned char _g=255, unsigned char _b=255, unsigned char _a=255)
Change the default color (unsigned char values between 0 and 255)
Definition: Grapic.h:341
void plot_setSize(Plot &p, const int n)
Define the size of the stored value of the funtion (<0 means infinity)
Definition: Grapic.h:754
void mousePosGlobal(int &x, int &y)
After this function (x,y) store the mouse position.
void plot_draw(const Plot &p, int xmin, int ymin, int xmax, int ymax, bool clearOrNot=true)
Draw the curve in the rectangle (xmin,ymin,xmax,ymax); clear the rectangle if clearOrNot is true.
Definition: Grapic.h:766
int image_width(const Image &im)
return the width of the image
Definition: Grapic.h:640
void ellipseFill(int xc, int yc, int horizontal, int vertical)
Draw a filled ellipse at (xc, yc), horizontal radius and vertical radius, rotation optional (a bit lo...
unsigned int lastFrameTime()
Returns the time elapsed between the last frame in milliseconds. It can be used to sync animations wi...
Definition: Grapic.h:334
void image_draw(Image &im, int x, int y, int w=-1, int h=-1)
Draw the image at position (x,y) with width=w and height=h (if w<0 or h<0 the original size of the im...
Definition: Grapic.h:613
float elapsedTime()
return the time elapsed since the beginning of the process in second
Definition: Grapic.h:440
void setKeyRepeatMode(bool repeat)
(de)Activate the repeat mode: when the user presses continuously on the key touch is repeated....
Definition: Grapic.h:462
void print(int x, int y, const char *txt)
Print the text txt , up left corner is (x,y)
bool winDisplay()
Display the window. All drawing is hidden until this function is not called.
Definition: Grapic.h:300
int isKeyPressed(int key)
return the number of time the key 'key' has been pressed since the last call to this function.
Definition: Grapic.h:453
void rectangleFill(int xmin, int ymin, int xmax, int ymax)
Draw a filled rectangle from (xmin,ymin) to (xmax,ymax)
void point(int x, int y)
Draw a point at (x,y)
Definition: Grapic.h:405
bool isMousePressed(int button)
return true if the mouse button 'button' is pressed
Definition: Grapic.h:486
Image image_copy(const Image &im)
Return a copy of the image. It duplicates the image. It is useful since an affectation shares the sam...
void delay(int d)
Stop the program during d milliseconds.
Definition: Grapic.h:476
void circleFill(int xc, int yc, int radius)
Draw a filled circle from (xmin,ymin) to (xmax,ymax)
void winClearEvent()
Clear the event queue of the window.
Definition: Grapic.h:276
void menu_draw(Menu &m, int xmin=5, int ymin=5, int xmax=-1, int ymax=-1)
Draw the menu on the screen. See menu_add for an example of usage.
Definition: Grapic.h:721
void winSetPosition(int w, int h, int ps, int py, bool fullscreen)
Change the size (w,h), the position(ps,py) or the fullscreen on/off Set a negative parameter to let h...
void winClear()
Clear the window with the default background color.
Definition: Grapic.h:262
int image_height(const Image &im)
return the height of the image
Definition: Grapic.h:648
void winQuit()
Quit and close all things.
Definition: Grapic.h:307
int caseToPixel(const Menu &m, int c, int ymin, int ymax)
return the pixel from a line of the menu
Definition: Grapic.h:739
int irand(int rmin=0, int rmax=100)
return a random number (integer) between rmin to rmax included
Definition: Grapic.h:425
bool winManageEvent()
Manage standard event like key 'ESC', quit, etc.
Definition: Grapic.h:512
void pressSpace(bool isPrint=true)
Stop the program until key 'space'is pressed.
void circle(int xc, int yc, int radius)
Draw a circle from (xmin,ymin) to (xmax,ymax)
bool image_isInit(const Image &im)
return true if the image is initialized
Definition: Grapic.h:660
void fontSize(int s)
Change the default size of the font.
Definition: Grapic.h:519
int menu_select(const Menu &m)
return the line selected in the menu. See menu_add for an example of usage.
Definition: Grapic.h:727
void image_savePNG(const Image &im, const char *filename)
Save the image into the file: format is PNG.
Definition: Grapic.h:606
void rectangle(int xmin, int ymin, int xmax, int ymax)
Draw a rectangle from (xmin,ymin) to (xmax,ymax)
void menu_setSelect(Menu &m, int s)
modifies selected line of the menu. See menu_add for an example of usage.
Definition: Grapic.h:733
void winInit(const char *name, int w, int h, int posx=-1, int posy=-1)
Initialize the window with a size w,h and a position (posx,posy). If posx<0 or posy<0,...
Definition: Grapic.h:245
void selectFont(const char *path)
Changes the default font to the a the given path (must be a .ttf file)
Definition: Grapic.h:526
void image_printInfo(const Image &im)
Print the informations of the image im.
Definition: Grapic.h:667
void triangleFill(int x1, int y1, int x2, int y2, int x3, int y3)
Draw a filled triangle from the vertices (x1, y1), (x2, y2), (x3, y3)
void plot_clear(Plot &p)
Clear the data stored.
Definition: Grapic.h:748
void polygon(int p[][2], unsigned int number)
Draw a polygon. (Code provided by Bastien DOIGNIES, many thanks)
void colorf(float _r=1.f, float _g=1.f, float _b=1.f, float _a=1.f)
Change the default color (float values between 0.f and 1.f)
Definition: Grapic.h:348
void qToQuit(bool enable)
Permet à l'utilisateur d'activer/désactiver la fermeture de la fenêtre sur l'appui de la touche 'q'.
float framesPerSecond()
Returns the average number of frames per second.
Definition: Grapic.h:326
void plot_add(Plot &p, float x, float y, int curve_n=0)
Add a point (x,y=f(x)) to the curve number curve_n.
Definition: Grapic.h:760
void mousePos(int &x, int &y)
After this function (x,y) store the mouse position.
void savePerformanceMode(bool reduceFPS)
If set to true, limit the framerate (to monitor framerate) to save ressources on low end pcs.
Definition: Grapic.h:319
void backgroundColorf(float _r=1.f, float _g=1.f, float _b=1.f, float _a=1.f)
Change the default background color (the color used to clear the screen)
Definition: Grapic.h:362
unsigned char image_get(const Image &im, int x, int y, int c=0)
return the color component c of the pixel (x,y) of the image im. c must be 0 for the red component,...
Definition: Grapic.h:628
float frand(float rmin=0.f, float rmax=1.f)
return a random number (float) between rmin to rmax included
Definition: Grapic.h:432
Image image(const char *filename, bool transparency=false, unsigned char r=255, unsigned char g=255, unsigned b=255, unsigned char a=255)
Return an image loaded from the file filename.
Definition: Grapic.h:587
void grid(int xmin, int ymin, int xmax, int ymax, int nx, int ny)
Draw a grid from (xmin,ymin) to (xmax,ymax) with nx columns and ny rows.
bool winHasFinished()
return true if the application should be closed (because of 'ESC' or 'q' key pressed for instance)
Definition: Grapic.h:269
bool isInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
Decide if a point (px, py) is inside the triangle (ax, ay), (bx, by), (cx, xy). (Code provided by Bas...
int menu_has_changed(Menu &m)
Check if the menu item was changed.
Definition: Grapic.h:710
void points(int p[][2], int n)
Draw an array of n points.
Definition: Grapic.h:413
void put_pixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a=255)
Draw a pixel on (x,y) with color (r,g,b,a)
void regular_polygonFill(int x, int y, unsigned int apotheme, unsigned int line_number)
Draw a filled regular polygon with line_numbers edges centred on (x, y). (Code provided by Bastien DO...
void image_set(Image &im, int x, int y, unsigned char r, unsigned char g, unsigned b, unsigned char a)
Set the pixel (x,y) of the image im with the color c.
void menu_change(Menu &m, int i, const std::string &str)
Change the text of a line in the menu.
Definition: Grapic.h:701
void line(int x1, int y1, int x2, int y2)
Draw a line from (x1,y1) to (x2,y2)
Definition: Grapic.h:393
void polygonFill(int p[][2], unsigned int number)
Draw a simple(no edge-crossing) and without holes filled polygon.