libAlgAudio  v1.99-440-g08538e5-dirty
The development library for AlgAudio framework.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DrawContext.hpp
Go to the documentation of this file.
1 #ifndef DRAWCONTEXT_HPP
2 #define DRAWCONTEXT_HPP
3 /*
4 This file is part of AlgAudio.
5 
6 AlgAudio, Copyright (C) 2015 CeTA - Audiovisual Technology Center
7 
8 AlgAudio is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12 
13 AlgAudio is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License
19 along with AlgAudio. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <stack>
23 #include <memory>
24 #include "Color.hpp"
25 #include "Utilities.hpp"
26 
27 struct SDL_Renderer;
28 struct SDL_Texture;
29 struct SDL_Color;
30 struct SDL_Rect;
31 struct SDL_Window;
32 typedef void* SDL_GLContext;
33 
34 namespace AlgAudio{
35 
36 class SDLTexture;
37 class SDLTextTexture;
38 
66 public:
70  DrawContext(SDL_Window* w, SDL_Renderer* renderer, SDL_GLContext cont, int x, int y, int width, int height);
71 
73 
74  void SetColor(short r, short g, short b, short a = 255);
75  void SetColor(const Color&);
77 
79 
80  inline void DrawLine(int x1, int y1, int x2, int y2) {DrawLine(Point2D(x1,y1),Point2D(x2,y2));}
81  void DrawLine(Point2D from, Point2D to, bool smooth=false);
82  void DrawLineEx(float x1, float y1, float x2, float y2, float width = 1.0);
84 
89  void DrawCubicBezier(Point2D p1, Point2D p2, Point2D p3, Point2D p4, float width, unsigned int segments = 15);
90 
92  void DrawTexture(std::shared_ptr<SDLTexture> texture, Point2D p = Point2D(0,0));
94  void DrawText(std::shared_ptr<SDLTextTexture> text, Color c, Point2D p = Point2D(0,0));
96 
97  void DrawRect(int x, int y, int w, int h);
98  inline void DrawRect(Rect r){DrawRect(r.a.x, r.a.y, r.Size().width, r.Size().height);}
99  void DrawRectBorder(Rect r);
101 
102  void Fill();
104  void Clear();
106  void Clear(Color);
108  Size2D Size() {return Size2D(width,height);}
110  bool HasZeroArea();
111 
113 
115  void Push(Point2D p, Size2D s);
116  inline void Push(Rect r) { Push(r.a, r.Size()); }
118 
122  void Push(std::shared_ptr<SDLTexture>, int width, int height);
125  void Pop();
126 
128  void SetOffset(Point2D off){offset = off;}
130  void ResetOffset(){offset = Point2D(0,0);}
132  void SetScale(float s){scale = s;}
133 
141  void Restore();
142 
143  /* Does some pointless GL calls to set a visible mark in gl api trace with
144  * the provided number. */
145  void MarkApiTrace(int no);
146 private:
147  // Current DrawContext state.
148  int width, height;
149  int x,y;
150 
151  Point2D offset;
156  float base_scale = 1.0, scale = 1.0;
157  inline Point2D_<float> Transform(Point2D_<float> p){return p*TotalScale() + offset;}
158  inline float TotalScale(){return base_scale * scale;}
159 
160  // Internal variables
161  SDL_Window* window;
162  SDL_Renderer* renderer;
163  SDL_GLContext context;
164 
166  std::shared_ptr<SDLTexture> current_target = nullptr;
168  struct DCLevel{
169  DCLevel(std::shared_ptr<SDLTexture> t, int x, int y, int w, int h, Point2D off, float bs, float s) :
170  target(t), xoffset(x), yoffset(y), width(w), height(h), offset(off), base_scale(bs), scale(s) {}
171  std::shared_ptr<SDLTexture> target;
172  int xoffset, yoffset, width, height;
173  Point2D offset;
174  float base_scale, scale;
175  };
177  std::stack<DCLevel> context_stack;
179  void SwitchToTarget(std::shared_ptr<SDLTexture>);
181  void UpdateClipRect();
182 };
183 
184 } // namespace AlgAudio
185 
186 #endif // DRAWCONTEXT_HPP
void DrawRect(Rect r)
Definition: DrawContext.hpp:98
Size2D Size()
Definition: DrawContext.hpp:108
Definition: Color.hpp:28
void SetScale(float s)
Definition: DrawContext.hpp:132
Definition: Utilities.hpp:104
void DrawCubicBezier(Point2D p1, Point2D p2, Point2D p3, Point2D p4, float width, unsigned int segments=15)
void DrawText(std::shared_ptr< SDLTextTexture > text, Color c, Point2D p=Point2D(0, 0))
Definition: DrawContext.hpp:65
void DrawLine(int x1, int y1, int x2, int y2)
Definition: DrawContext.hpp:80
Point2D_< int > Point2D
Definition: Utilities.hpp:35
int height
Definition: Utilities.hpp:46
void ResetOffset()
Definition: DrawContext.hpp:130
void SetOffset(Point2D off)
Definition: DrawContext.hpp:128
void Push(Rect r)
Definition: DrawContext.hpp:116
void Push(Point2D p, Size2D s)
Definition: Utilities.hpp:34
int width
Definition: Utilities.hpp:46
void DrawRectBorder(Rect r)
T x
Definition: Utilities.hpp:62
Definition: Alertable.hpp:26
void DrawLineEx(float x1, float y1, float x2, float y2, float width=1.0)
Point2D a
Definition: Utilities.hpp:105
Size2D Size() const
Definition: Utilities.hpp:118
void * SDL_GLContext
Definition: DrawContext.hpp:31
T y
Definition: Utilities.hpp:62
void DrawTexture(std::shared_ptr< SDLTexture > texture, Point2D p=Point2D(0, 0))
void DrawRect(int x, int y, int w, int h)
void MarkApiTrace(int no)
void SetColor(short r, short g, short b, short a=255)
DrawContext()
Definition: DrawContext.hpp:67
Definition: Utilities.hpp:40