ManaPlus
graphics.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2017 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /* _______ __ __ __ ______ __ __ _______ __ __
24  * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
25  * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
26  * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
27  * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
28  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
29  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
30  *
31  * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
32  *
33  *
34  * Per Larsson a.k.a finalman
35  * Olof Naessén a.k.a jansem/yakslem
36  *
37  * Visit: http://guichan.sourceforge.net
38  *
39  * License: (BSD)
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  * notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  * notice, this list of conditions and the following disclaimer in
47  * the documentation and/or other materials provided with the
48  * distribution.
49  * 3. Neither the name of Guichan nor the names of its contributors may
50  * be used to endorse or promote products derived from this software
51  * without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
59  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #ifndef RENDER_GRAPHICS_H
67 #define RENDER_GRAPHICS_H
68 
69 #include "sdlshared.h"
70 
72 
73 #include "gui/color.h"
74 #include "gui/cliprect.h"
75 
76 #include "resources/mstack.h"
77 
78 PRAGMA48(GCC diagnostic push)
79 PRAGMA48(GCC diagnostic ignored "-Wshadow")
80 #ifdef USE_SDL2
81 #include <SDL_render.h>
82 #else // USE_SDL2
83 #include <SDL_video.h>
84 #endif // USE_SDL2
85 PRAGMA48(GCC diagnostic pop)
86 
87 #include "localconsts.h"
88 
89 #ifdef USE_SDL2
90 #define RectPos int32_t
91 #define RectSize int32_t
92 #else // USE_SDL2
93 #define RectPos int16_t
94 #define RectSize uint16_t
95 #endif // USE_SDL2
96 
97 class Image;
98 class ImageCollection;
99 class ImageRect;
100 class ImageVertexes;
101 
102 struct SDL_Window;
103 
108 {
109  public:
110 #ifdef USE_OPENGL
112 #endif // USE_SDL2
113 
114  friend class SdlScreenshotHelper;
115 
117 
118 
121  virtual ~Graphics();
122 
123  static void cleanUp();
124 
129  {
130  LEFT = 0,
133  };
134 
135  void setWindow(SDL_Window *restrict const window,
136  const int width, const int height) restrict2 noexcept2
137  {
138  mWindow = window;
139  mRect.w = static_cast<RectSize>(width);
140  mRect.h = static_cast<RectSize>(height);
141  }
142 
144  { return mWindow; }
145 
150  void setSync(const bool sync) restrict2;
151 
152  bool getSync() const restrict2 noexcept2 A_WARN_UNUSED
153  { return mSync; }
154 
158  virtual bool setVideoMode(const int w, const int h,
159  const int scale,
160  const int bpp,
161  const bool fs,
162  const bool hwaccel,
163  const bool resize,
164  const bool noFrame,
165  const bool allowHighDPI) restrict2 = 0;
166 
170  bool setFullscreen(const bool fs) restrict2;
171 
175  virtual bool resizeScreen(const int width,
176  const int height) restrict2;
177 
178  virtual void restoreContext() restrict2
179  { }
180 
184  virtual void drawRescaledImage(const Image *restrict const image,
185  int dstX, int dstY,
186  const int desiredWidth,
187  const int desiredHeight) restrict2 = 0;
188 
189  virtual void drawPattern(const Image *restrict const image,
190  const int x, const int y,
191  const int w, const int h) restrict2 = 0;
192 
196  virtual void drawRescaledPattern(const Image *restrict const image,
197  const int x, const int y,
198  const int w, const int h,
199  const int scaledWidth,
200  const int scaledHeight) restrict2 = 0;
201 
202  virtual void drawImageRect(const int x, const int y,
203  const int w, const int h,
204  const ImageRect &restrict imgRect)
205  restrict2 = 0;
206 
207  virtual void calcPattern(ImageVertexes *restrict const vert,
208  const Image *restrict const image,
209  const int x, const int y,
210  const int w, const int h) const restrict2 = 0;
211 
212  virtual void calcPattern(ImageCollection *restrict const vert,
213  const Image *restrict const image,
214  const int x, const int y,
215  const int w, const int h) const restrict2 = 0;
216 
217  virtual void calcTileVertexes(ImageVertexes *restrict const vert,
218  const Image *restrict const image,
219  int x,
220  int y) const restrict2 A_NONNULL(2, 3)
221  = 0;
222 
223  virtual void calcTileSDL(ImageVertexes *restrict const vert A_UNUSED,
224  int x A_UNUSED,
225  int y A_UNUSED) const restrict2
226  {
227  }
228 
229  virtual void drawTileVertexes(const ImageVertexes *restrict const vert)
230  restrict2 = 0;
231 
232  virtual void drawTileCollection(const ImageCollection
233  *restrict const vertCol) restrict2
234  A_NONNULL(2) = 0;
235 
236  virtual void calcTileCollection(ImageCollection *restrict const
237  vertCol,
238  const Image *restrict const image,
239  int x,
240  int y) restrict2 = 0;
241 
242  virtual void calcWindow(ImageCollection *restrict const vertCol,
243  const int x, const int y,
244  const int w, const int h,
245  const ImageRect &restrict imgRect)
246  restrict2 A_NONNULL(2) = 0;
247 
248  virtual void fillRectangle(const Rect &restrict rectangle)
249  restrict2 = 0;
250 
255  virtual void updateScreen() restrict2 = 0;
256 
257  void setWindowSize(const int width,
258 #ifdef USE_SDL2
259  const int height) restrict2;
260 #else // USE_SDL2
261  const int height) restrict2 A_CONST;
262 #endif // USE_SDL2
263 
267  int getWidth() const restrict2 A_WARN_UNUSED;
268 
272  int getHeight() const restrict2 A_WARN_UNUSED;
273 
274  int getMemoryUsage() const restrict2 A_WARN_UNUSED;
275 
276  virtual void drawNet(const int x1, const int y1,
277  const int x2, const int y2,
278  const int width, const int height) restrict2;
279 
280  ClipRect &getTopClip() const restrict2 A_WARN_UNUSED
281  { return mClipStack.top(); }
282 
283  void setRedraw(const bool n) restrict2 noexcept2
284  { mRedraw = n; }
285 
286  bool getRedraw() const restrict2 noexcept2 A_WARN_UNUSED
287  { return mRedraw; }
288 
289  void setSecure(const bool n) restrict2 noexcept2
290  { mSecure = n; }
291 
292  bool getSecure() const restrict2 noexcept2 A_WARN_UNUSED
293  { return mSecure; }
294 
295  int getBpp() const restrict2 noexcept2 A_WARN_UNUSED
296  { return mBpp; }
297 
298  bool getFullScreen() const restrict2 noexcept2 A_WARN_UNUSED
299  { return mFullscreen; }
300 
301  bool getHWAccel() const restrict2 noexcept2 A_WARN_UNUSED
302  { return mHWAccel; }
303 
304  bool getDoubleBuffer() const restrict2 noexcept2 A_WARN_UNUSED
305  { return mDoubleBuffer; }
306 
307  RenderType getOpenGL() const restrict2 noexcept2 A_WARN_UNUSED
308  { return mOpenGL; }
309 
310  void setNoFrame(const bool n) restrict2 noexcept2
311  { mNoFrame = n; }
312 
313  const std::string &getName() const restrict2 noexcept2 A_WARN_UNUSED
314  { return mName; }
315 
316  virtual void initArrays(const int vertCount A_UNUSED) restrict2
317  { }
318 
319  virtual void setColor(const Color &restrict color) restrict2
320  {
321  mColor = color;
322  mAlpha = (color.a != 255);
323  }
324 
325  const Color &getColor() const restrict2 noexcept2
326  { return mColor; }
327 
328 #ifdef DEBUG_DRAW_CALLS
329  virtual unsigned int getDrawCalls() const restrict2
330  { return 0; }
331 #endif // DEBUG_DRAW_CALLS
332 #ifdef DEBUG_BIND_TEXTURE
333  virtual unsigned int getBinds() const restrict2
334  { return 0; }
335 #endif // DEBUG_BIND_TEXTURE
336 #ifdef USE_SDL2
337  void dumpRendererInfo(const char *restrict const str,
338  const SDL_RendererInfo &restrict info) restrict2;
339 
340  virtual void setRendererFlags(const uint32_t flags A_UNUSED) restrict2
341  { }
342 #endif // USE_SDL2
343 
350  virtual void drawImage(const Image *restrict const image,
351  int dstX, int dstY) restrict2 = 0;
352 
353  virtual void copyImage(const Image *restrict const image,
354  int dstX, int dstY) restrict2 = 0;
355 
356  virtual void drawImageCached(const Image *restrict const image,
357  int srcX, int srcY) restrict2 = 0;
358 
359  virtual void drawPatternCached(const Image *restrict const image,
360  const int x, const int y,
361  const int w, const int h) restrict2 = 0;
362 
363  virtual void completeCache() restrict2 = 0;
364 
365  int getScale() const restrict2 noexcept2
366  { return mScale; }
367 
368  virtual bool isAllowScale() const restrict2 noexcept2
369  { return false; }
370 
371  void setScale(int scale) restrict2;
372 
384  virtual void pushClipArea(const Rect &restrict area) restrict2;
385 
391  virtual void popClipArea() restrict2;
392 
401  virtual void drawLine(int x1, int y1,
402  int x2, int y2) restrict2 = 0;
403 
409  virtual void drawRectangle(const Rect &restrict rectangle)
410  restrict2 = 0;
411 
412 #ifdef USE_OPENGL
413 #ifdef USE_SDL2
414  virtual void createGLContext(const bool custom) restrict2;
415 #else // USE_SDL2
416 
417  virtual void createGLContext(const bool custom) restrict2 A_CONST;
418 #endif // USE_SDL2
419 #endif // USE_OPENGL
420 
427  virtual void drawPoint(int x, int y) restrict2 = 0;
428 
440  virtual void beginDraw() restrict2
441  { }
442 
452  virtual void endDraw() restrict2
453  { }
454 
455  virtual void clearScreen() const restrict2
456  { }
457 
458  virtual void deleteArrays() restrict2
459  { }
460 
461  virtual void postInit() restrict2
462  { }
463 
464  virtual void finalize(ImageCollection *restrict const col A_UNUSED)
465  restrict2
466  { }
467 
468  virtual void finalize(ImageVertexes *restrict const vert A_UNUSED)
469  restrict2
470  { }
471 
472  virtual void testDraw() restrict2
473  { }
474 
475  virtual void removeArray(const uint32_t sz A_UNUSED,
476  uint32_t *restrict const arr A_UNUSED)
477  restrict2
478  { }
479 
480  virtual void screenResized() restrict2
481  { }
482 
483  int mWidth;
484  int mHeight;
487 
488  protected:
492  Graphics();
493 
494  void setMainFlags(const int w, const int h,
495  const int scale,
496  const int bpp,
497  const bool fs,
498  const bool hwaccel,
499  const bool resize,
500  const bool noFrame,
501  const bool allowHighDPI) restrict2;
502 
503  int getOpenGLFlags() const restrict2 A_WARN_UNUSED;
504 
505  int getSoftwareFlags() const restrict2 A_WARN_UNUSED;
506 
507  bool setOpenGLMode() restrict2;
508 
509  void updateMemoryInfo() restrict2;
510 
511  bool videoInfo() restrict2;
512 
513 #ifdef USE_OPENGL
514  void setOpenGLFlags() restrict2;
515 #endif // USE_OPENGL
516 
521 
523 
524 #ifdef USE_SDL2
525  static SDL_Renderer *restrict mRenderer;
526 #endif // USE_SDL2
527 #ifdef USE_OPENGL
528 #ifdef USE_SDL2
529  static SDL_GLContext mGLContext;
530 #else // USE_SDL2
531 
532  static void *restrict mGLContext;
533 #endif // USE_SDL2
534 #endif // USE_OPENGL
535 
536  int mBpp;
537  bool mAlpha;
539  bool mHWAccel;
540  bool mRedraw;
542  SDL_Rect mRect;
543  bool mSecure;
546  bool mNoFrame;
548  std::string mName;
550  bool mSync;
551  int mScale;
553 };
554 
556 
557 #endif // RENDER_GRAPHICS_H
#define A_DELETE_COPY(func)
Definition: localconsts.h:52
virtual void drawRescaledPattern(const Image *const image, const int x, const int y, const int w, const int h, const int scaledWidth, const int scaledHeight)=0
virtual void finalize(ImageVertexes *const vert)
Definition: graphics.h:468
virtual void endDraw()
Definition: graphics.h:452
void setMainFlags(const int w, const int h, const int scale, const int bpp, const bool fs, const bool hwaccel, const bool resize, const bool noFrame, const bool allowHighDPI)
Definition: graphics.cpp:181
virtual bool isAllowScale() const
Definition: graphics.h:368
std::string mName
Definition: graphics.h:548
bool mRedraw
Definition: graphics.h:540
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
ClipRect & getTopClip() const
Definition: graphics.h:280
SDL_Surface * mWindow
Definition: graphics.h:522
virtual void drawImageCached(const Image *const image, int srcX, int srcY)=0
#define A_NONNULLPOINTER
Definition: localconsts.h:294
RenderType mOpenGL
Definition: graphics.h:544
Graphics * mainGraphics
Definition: graphics.cpp:108
void setWindow(SDL_Surface *const window, const int width, const int height)
Definition: graphics.h:135
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
virtual ~Graphics()
Definition: graphics.cpp:153
virtual void calcTileSDL(ImageVertexes *const vert, int x, int y) const
Definition: graphics.h:223
Definition: rect.h:72
#define A_WARN_UNUSED
Definition: localconsts.h:172
virtual void copyImage(const Image *const image, int dstX, int dstY)=0
int mWidth
Definition: graphics.h:483
void setWindowSize(const int width, const int height) A_CONST
Definition: graphics.cpp:670
bool mHWAccel
Definition: graphics.h:539
virtual void updateScreen()=0
virtual void popClipArea()
Definition: graphics.cpp:738
bool mNoFrame
Definition: graphics.h:546
bool getSync() const
Definition: graphics.h:152
#define A_NONNULL(...)
Definition: localconsts.h:179
virtual void drawRectangle(const Rect &rectangle)=0
int getWidth() const
Definition: graphics.cpp:642
virtual void drawPatternCached(const Image *const image, const int x, const int y, const int w, const int h)=0
virtual void drawTileVertexes(const ImageVertexes *const vert)=0
virtual void calcTileVertexes(ImageVertexes *const vert, const Image *const image, int x, int y) const =0
virtual void drawNet(const int x1, const int y1, const int x2, const int y2, const int width, const int height)
Definition: graphics.cpp:652
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:464
Color mColor
Definition: graphics.h:552
virtual void screenResized()
Definition: graphics.h:480
virtual void deleteArrays()
Definition: graphics.h:458
virtual void initArrays(const int vertCount)
Definition: graphics.h:316
virtual void setColor(const Color &color)
Definition: graphics.h:319
virtual void beginDraw()
Definition: graphics.h:440
bool getSecure() const
Definition: graphics.h:292
bool mSecure
Definition: graphics.h:543
static void cleanUp()
Definition: graphics.cpp:158
static void * mGLContext
Definition: graphics.h:532
virtual void removeArray(const uint32_t sz, uint32_t *const arr)
Definition: graphics.h:475
bool info(InputEvent &event)
Definition: commands.cpp:56
int getOpenGLFlags() const
Definition: graphics.cpp:241
int getMemoryUsage() const
Definition: graphics.cpp:440
virtual void drawRescaledImage(const Image *const image, int dstX, int dstY, const int desiredWidth, const int desiredHeight)=0
const std::string & getName() const
Definition: graphics.h:313
bool mAlpha
Definition: graphics.h:537
int getHeight() const
Definition: graphics.cpp:647
void setSecure(const bool n)
Definition: graphics.h:289
void setSync(const bool sync)
Definition: graphics.cpp:176
void setScale(int scale)
Definition: graphics.cpp:205
RenderType getOpenGL() const
Definition: graphics.h:307
#define notfinal
Definition: localconsts.h:289
virtual void createGLContext(const bool custom) A_CONST
Definition: graphics.cpp:417
virtual void clearScreen() const
Definition: graphics.h:455
int getSoftwareFlags() const
Definition: graphics.cpp:388
#define PRAGMA48(str)
Definition: localconsts.h:214
int mActualHeight
Definition: graphics.h:486
bool getRedraw() const
Definition: graphics.h:286
int mHeight
Definition: graphics.h:484
virtual void testDraw()
Definition: graphics.h:472
#define SDL_Window
Definition: sdlshared.h:59
bool mDoubleBuffer
Definition: graphics.h:541
bool mSync
Definition: graphics.h:550
SDL_Rect mRect
Definition: graphics.h:542
void setOpenGLFlags()
Definition: graphics.cpp:747
bool getDoubleBuffer() const
Definition: graphics.h:304
void setNoFrame(const bool n)
Definition: graphics.h:310
virtual void restoreContext()
Definition: graphics.h:178
int getScale() const
Definition: graphics.h:365
virtual void calcPattern(ImageVertexes *const vert, const Image *const image, const int x, const int y, const int w, const int h) const =0
#define RectSize
Definition: graphics.h:94
bool mEnableResize
Definition: graphics.h:545
virtual void fillRectangle(const Rect &rectangle)=0
#define A_UNUSED
Definition: localconsts.h:171
virtual bool resizeScreen(const int width, const int height)
Definition: graphics.cpp:557
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
bool mAllowHighDPI
Definition: graphics.h:547
bool mFullscreen
Definition: graphics.h:538
virtual void drawLine(int x1, int y1, int x2, int y2)=0
int mScale
Definition: graphics.h:551
int mStartFreeMem
Definition: graphics.h:549
MStack< ClipRect > mClipStack
Definition: graphics.h:520
Definition: image.h:61
virtual void calcTileCollection(ImageCollection *const vertCol, const Image *const image, int x, int y)=0
SDL_Surface * getWindow() const
Definition: graphics.h:143
virtual bool setVideoMode(const int w, const int h, const int scale, const int bpp, const bool fs, const bool hwaccel, const bool resize, const bool noFrame, const bool allowHighDPI)=0
Definition: color.h:74
virtual void pushClipArea(const Rect &area)
Definition: graphics.cpp:676
#define restrict
Definition: localconsts.h:176
virtual void postInit()
Definition: graphics.h:461
bool videoInfo()
Definition: graphics.cpp:482
const Color & getColor() const
Definition: graphics.h:325
virtual void drawPattern(const Image *const image, const int x, const int y, const int w, const int h)=0
void setRedraw(const bool n)
Definition: graphics.h:283
int mActualWidth
Definition: graphics.h:485
bool setFullscreen(const bool fs)
Definition: graphics.cpp:541
T & top() const
Definition: mstack.h:72
#define SDL_GLContext
Definition: sdlshared.h:62
void updateMemoryInfo()
Definition: graphics.cpp:425
virtual void drawPoint(int x, int y)=0
bool getHWAccel() const
Definition: graphics.h:301
virtual void completeCache()=0
#define noexcept2
Definition: localconsts.h:49
int getBpp() const
Definition: graphics.h:295
#define restrict2
Definition: localconsts.h:177
bool getFullScreen() const
Definition: graphics.h:298
int mBpp
Definition: graphics.h:536
bool setOpenGLMode()
Definition: graphics.cpp:282
virtual void calcWindow(ImageCollection *const vertCol, const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
RenderType
Definition: rendertype.h:24