Btk
render.hpp
1 #if !defined(_BTK_RENDERER_HPP_)
2 #define _BTK_RENDERER_HPP_
3 #include "defs.hpp"
4 struct SDL_Window;
5 
6 #ifdef __cplusplus
7 #include "pixels.hpp"
8 #include "rect.hpp"
9 //Basic type defs
10 typedef Btk::Rect BtkRect;
11 typedef Btk::Vec2 BtkVec2;
12 extern "C"{
13 
14 #else
15 #include <SDL2/SDL_rect.h>
16 typedef SDL_Rect BtkRect;
17 typedef SDL_Point BtkVec2;
18 #endif
19 struct SDL_RWops;
24 typedef struct BtkTexture BtkTexture;
29 typedef struct BtkRenderer BtkRenderer;
34 struct BtkTable{
43  BtkRenderer *(*CreateRenderer)(SDL_Window *win,int index,Uint32 flags);
48  BtkTexture *(*CreateTexture)(BtkRenderer*,Uint32 fmt,int access,int w,int h);
49  BtkTexture *(*CreateTextureFrom)(BtkRenderer*,SDL_Surface*);
54  BtkTexture *(*LoadTextureFrom)(BtkRenderer*,SDL_RWops*);
59  void (*DestroyTexture)(BtkTexture *);
60  void (*DestroyRenderer)(BtkRenderer *);
61 
62  void (*RenderPresent)(BtkRenderer *);
63 
64  int (*RenderDrawRect) (BtkRenderer*,const BtkRect *);
65  int (*RenderDrawRects)(BtkRenderer*,const BtkRect *,int n);
66  int (*RenderDrawBox )(BtkRenderer*,const BtkRect *);
67  int (*RenderDrawBoxs)(BtkRenderer*,const BtkRect *,int n);
68  int (*RenderDrawLine)(BtkRenderer*,int x1,int y1,int x2,int y2);
69  int (*RenderDrawLines)(BtkRenderer*,const BtkVec2 *vec2s,int n);
70  int (*RenderDrawPoint)(BtkRenderer*,int x1,int y1);
71  int (*RenderDrawPoints)(BtkRenderer*,const BtkVec2 *points,int n);
72 
73  int (*RenderSetDrawColor)(BtkRenderer*,Uint8 r,Uint8 g,Uint8 b,Uint8 a);
74  int (*RenderGetDrawColor)(BtkRenderer*,Uint8 *r,Uint8 *g,Uint8 *b,Uint8 *a);
75 
76  void (*RenderGetClipRect)(BtkRenderer*,BtkRect *cliprect);
77  int (*RenderSetClipRect)(BtkRenderer*,const BtkRect *cliprect);
78 
79  void (*RenderGetViewPort)(BtkRenderer*,BtkRect *viewport);
80  int (*RenderSetViewPort)(BtkRenderer*,const BtkRect *viewport);
81 
82  int (*RenderCopy)(BtkRenderer*,BtkTexture *t,const BtkRect *src,const BtkRect *dst);
83  int (*RenderClear)(BtkRenderer*);
84 
85  int (*QueryTexture)(BtkTexture*,Uint32 *fmt,int *access,int *w,int *h);
86  int (*UpdateTexture)(BtkTexture*,const BtkRect *rect,const void *pixels,int pitch);
87 
88  int (*LockTexture)(BtkTexture*,const BtkRect *rect,void **pixels,int *pitch);
89  void (*UnlockTexture)(BtkTexture *);
90 
91 
92 
93  int (*SetError)(const char *fmt,...);
94  const char *(*GetError)();
95 };
96 extern BTKAPI struct BtkTable btk_rtbl;
97 
98 BTKAPI void Btk_ResetRITable();
99 #define Btk_CreateRenderer (btk_rtbl.CreateRenderer)
100 #define Btk_CreateTexture (btk_rtbl.CreateTexture)
101 #define Btk_DestroyTexture (btk_rtbl.DestroyTexture)
102 #define Btk_DestroyRenderer (btk_rtbl.DestroyRenderer)
103 
104 #define Btk_RenderDrawRect (btk_rtbl.RenderDrawRect)
105 #define Btk_RenderDrawRects (btk_rtbl.RenderDrawRects)
106 
107 #define Btk_RenderDrawBox (btk_rtbl.RenderDrawBox)
108 #define Btk_RenderDrawBoxs (btk_rtbl.RenderDrawBoxs)
109 
110 #define Btk_RenderDrawLine (btk_rtbl.RenderDrawLine)
111 #define Btk_RenderDrawLines (btk_rtbl.RenderDrawLines)
112 
113 #define Btk_RenderDrawPoint (btk_rtbl.RenderDrawPoint)
114 #define Btk_RenderDrawPoints (btk_rtbl.RenderDrawPoints)
115 
116 #define Btk_RenderSetClipRect (btk_rtbl.RenderSetClipRect)
117 #define Btk_RenderGetClipRect (btk_rtbl.RenderGetClipRect)
118 
119 #define Btk_RenderSetViewPort (btk_rtbl.RenderSetViewPort)
120 #define Btk_RenderGetViewPort (btk_rtbl.RenderGetViewPort)
121 
122 #define Btk_RenderSetDrawColor (btk_rtbl.RenderSetDrawColor)
123 
124 #define Btk_RenderCopy (btk_rtbl.RenderCopy)
125 #define Btk_RenderClear (btk_rtbl.RenderClear)
126 #define Btk_RenderPresent (btk_rtbl.RenderPresent)
127 
128 
129 #define Btk_QueryTexture (btk_rtbl.QueryTexture)
130 #define Btk_UpdateTexture (btk_rtbl.UpdateTexture)
131 
132 #define Btk_LockTexture (btk_rtbl.LockTexture)
133 #define Btk_UnlockTexture (btk_rtbl.UnlockTexture)
134 
135 #define Btk_CreateTextureFromSurface (btk_rtbl.CreateTextureFrom)
136 #define Btk_CreateTextureFrom (btk_rtbl.CreateTextureFrom)
137 #define Btk_LoadTexture (btk_rtbl.LoadTextureFrom)
138 
139 #define Btk_RISetError (btk_rtbl.SetError)
140 #define Btk_RIGetError (btk_rtbl.GetError)
141 #ifdef __cplusplus
142 }
143 #endif
144 
145 #ifdef __cplusplus
146 namespace Btk{
151  class BTKAPI Renderer{
152  public:
153  Renderer(SDL_Window *win);
154  Renderer(const Renderer &) = delete;
155  ~Renderer();
156  Rect get_cliprect(){
157  Rect rect;
158  Btk_RenderGetClipRect(render,&rect);
159  return rect;
160  }
161  int set_cliprect(){
162  return Btk_RenderSetClipRect(render,nullptr);
163  }
164  int set_cliprect(const Rect &r){
165  return Btk_RenderSetClipRect(render,&r);
166  }
167 
168  Rect get_viewport(){
169  Rect rect;
170  Btk_RenderGetViewPort(render,&rect);
171  return rect;
172  }
173  int set_viewport(){
174  return Btk_RenderSetViewPort(render,nullptr);
175  }
176  int set_viewport(const Rect &r){
177  return Btk_RenderSetViewPort(render,&r);
178  }
179 
180 
181  int copy(const Texture &t,const Rect *src,const Rect *dst){
182  return Btk_RenderCopy(render,t.get(),src,dst);
183  }
184  int copy(const Texture &t,const Rect &src,const Rect *dst){
185  return Btk_RenderCopy(render,t.get(),&src,dst);
186  }
187  int copy(const Texture &t,const Rect *src,const Rect &dst){
188  return Btk_RenderCopy(render,t.get(),src,&dst);
189  }
190  int copy(const Texture &t,const Rect &src,const Rect &dst){
191  return Btk_RenderCopy(render,t.get(),&src,&dst);
192  }
193 
194  int clear(){
195  return Btk_RenderClear(render);
196  }
197  void done(){
198  Btk_RenderPresent(render);
199  }
204  void destroy(){
205  Btk_DestroyRenderer(render);
206  render = nullptr;
207  }
212  int start(Color c);
213  int line(int x1,int y1,int x2,int y2,Color c);
214  int line(const Vec2 &beg,const Vec2 &end,Color c){
215  return line(beg.x,beg.y,end.x,end.y,c);
216  }
217  int aaline(int x1,int y1,int x2,int y2,Color c);
218 
219  int rect(const Rect &r,Color c);
220  int box(const Rect &r,Color c);
221 
222  int rounded_box(const Rect &r,int rad,Color c);
223  int rounded_rect(const Rect &r,int rad,Color c);
230  Texture create_from(const PixBuf &pixbuf);
240  Texture create(Uint32 fmt,TextureAccess access,int w,int h);
247  Texture load(std::string_view fname);
248  Texture load(RWops &);
258  int copy(const PixBuf &pixbuf,const Rect *src,const Rect *dst);
259  int copy(const PixBuf &pixbuf,const Rect *src,const Rect &dst){
260  return copy(pixbuf,src,&dst);
261  }
262  int copy(const PixBuf &pixbuf,const Rect &src,const Rect *dst){
263  return copy(pixbuf,&src,dst);
264  }
265  int copy(const PixBuf &pixbuf,const Rect &src,const Rect &dst){
266  return copy(pixbuf,&src,&dst);
267  }
268  public:
269  BtkRenderer *render = nullptr;
270  BtkTexture *cache = nullptr;//< Texture cache for pixbuf copying
271  };
272 
273  template<>
274  struct LockGuard<BtkTexture*>{
275  LockGuard(BtkTexture *t,const Rect *r = nullptr){
276  texture = t;
277  Btk_LockTexture(texture,r,&pixels,&pitch);
278  }
279  LockGuard(BtkTexture *t,const Rect &r){
280  texture = t;
281  Btk_LockTexture(texture,&r,&pixels,&pitch);
282  }
283  LockGuard(const LockGuard &) = delete;
284  ~LockGuard(){
285  Btk_UnlockTexture(texture);
286  }
287  BtkTexture*texture;
288  void * pixels;
289  int pitch;
290  };
291 };
292 #endif
293 
294 #endif // _BTK_RENDERER_HPP_
a SDL_Rect with methods
Definition: rect.hpp:10
Renderer Interface Table.
Definition: render.hpp:34
This header include many useful containers.
Definition: async.hpp:7
void(* DestroyTexture)(BtkTexture *)
Destroy a texture.
Definition: render.hpp:59
Definition: rect.hpp:81