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