GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/render/graphics.h Lines: 11 45 24.4 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
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
72
#include "enums/render/rendertype.h"
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
105
/**
106
 * A central point of control for graphics.
107
 */
108
class Graphics notfinal
109
{
110
    public:
111
#ifdef USE_OPENGL
112
        friend class OpenGLScreenshotHelper;
113
#endif  // USE_SDL2
114
115
        friend class SdlScreenshotHelper;
116
117
        A_DELETE_COPY(Graphics)
118
119
        /**
120
         * Destructor.
121
         */
122
        virtual ~Graphics();
123
124
        static void cleanUp();
125
126
        /**
127
         * Alignments for text drawing.
128
         */
129
        enum Alignment
130
        {
131
            LEFT = 0,
132
            CENTER,
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
144
        SDL_Window *getWindow() const restrict2 noexcept2
145
        { return mWindow; }
146
147
        /**
148
         * Sets whether vertical refresh syncing is enabled. Takes effect after
149
         * the next call to setVideoMode(). Only implemented on MacOS for now.
150
         */
151
        void setSync(const bool sync) restrict2;
152
153
        bool getSync() const restrict2 noexcept2 A_WARN_UNUSED
154
        { return mSync; }
155
156
        /**
157
         * Try to create a window with the given settings.
158
         */
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
168
        /**
169
         * Set fullscreen mode.
170
         */
171
        bool setFullscreen(const bool fs) restrict2;
172
173
        /**
174
         * Resize the window to the specified size.
175
         */
176
        virtual bool resizeScreen(const int width,
177
                                  const int height) restrict2;
178
179
        virtual void restoreContext() restrict2
180
        { }
181
182
        /**
183
         * Draws a resclaled version of the image
184
         */
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
194
        /**
195
         * Draw a pattern based on a rescaled version of the given image...
196
         */
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
233
        virtual void drawTileCollection(const ImageCollection
234
                                        *restrict const vertCol) restrict2
235
                                        A_NONNULL(2) = 0;
236
237
        virtual void calcTileCollection(ImageCollection *restrict const
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
252
        /**
253
         * Updates the screen. This is done by either copying the buffer to the
254
         * screen or swapping pages.
255
         */
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
265
        /**
266
         * Returns the width of the screen.
267
         */
268
        int getWidth() const restrict2 A_WARN_UNUSED;
269
270
        /**
271
         * Returns the height of the screen.
272
         */
273
        int getHeight() const restrict2 A_WARN_UNUSED;
274
275
        int getMemoryUsage() const restrict2 A_WARN_UNUSED;
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
281
        ClipRect &getTopClip() const restrict2 A_WARN_UNUSED
282
7
        { return mClipStack.top(); }
283
284
        void setRedraw(const bool n) restrict2 noexcept2
285
98
        { mRedraw = n; }
286
287
        bool getRedraw() const restrict2 noexcept2 A_WARN_UNUSED
288
17
        { return mRedraw; }
289
290
        void setSecure(const bool n) restrict2 noexcept2
291
        { mSecure = n; }
292
293
        bool getSecure() const restrict2 noexcept2 A_WARN_UNUSED
294
        { return mSecure; }
295
296
        int getBpp() const restrict2 noexcept2 A_WARN_UNUSED
297
        { return mBpp; }
298
299
        bool getFullScreen() const restrict2 noexcept2 A_WARN_UNUSED
300
        { return mFullscreen; }
301
302
        bool getHWAccel() const restrict2 noexcept2 A_WARN_UNUSED
303
        { return mHWAccel; }
304
305
        bool getDoubleBuffer() const restrict2 noexcept2 A_WARN_UNUSED
306
        { return mDoubleBuffer; }
307
308
        RenderType getOpenGL() const restrict2 noexcept2 A_WARN_UNUSED
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
8
        virtual void setColor(const Color &restrict color) restrict2
321
        {
322
8
            mColor = color;
323
8
            mAlpha = (color.a != 255);
324
8
        }
325
326
        const Color &getColor() const restrict2 noexcept2
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
345
        /**
346
         * Blits an image onto the screen.
347
         *
348
         * @return <code>true</code> if the image was blitted properly
349
         *         <code>false</code> otherwise.
350
         */
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
366
        int getScale() const restrict2 noexcept2
367
        { return mScale; }
368
369
77
        virtual bool isAllowScale() const restrict2 noexcept2
370
77
        { return false; }
371
372
        void setScale(int scale) restrict2;
373
374
        /**
375
         * Pushes a clip area onto the stack. The x and y coordinates in the
376
         * rectangle is  relative to the last pushed clip area.
377
         * If the new area falls outside the current clip area, it will be
378
         * clipped as necessary.
379
         *
380
         * If a clip area is outside of the top clip area a clip area with
381
         * zero width and height will be pushed.
382
         *
383
         * @param area The clip area to be pushed onto the stack.
384
         */
385
        virtual void pushClipArea(const Rect &restrict area) restrict2;
386
387
        /**
388
         * Removes the top most clip area from the stack.
389
         *
390
         * @throws Exception if the stack is empty.
391
         */
392
        virtual void popClipArea() restrict2;
393
394
        /**
395
         * Ddraws a line.
396
         *
397
         * @param x1 The first x coordinate.
398
         * @param y1 The first y coordinate.
399
         * @param x2 The second x coordinate.
400
         * @param y2 The second y coordinate.
401
         */
402
        virtual void drawLine(int x1, int y1,
403
                              int x2, int y2) restrict2 = 0;
404
405
        /**
406
         * Draws a simple, non-filled, rectangle with a one pixel width.
407
         *
408
         * @param rectangle The rectangle to draw.
409
         */
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
422
        /**
423
         * Draws a single point/pixel.
424
         *
425
         * @param x The x coordinate.
426
         * @param y The y coordinate.
427
         */
428
        virtual void drawPoint(int x, int y) restrict2 = 0;
429
430
        /**
431
          * Initializes drawing. Called by the Gui when Gui::draw() is called.
432
          * It is needed by some implementations of Graphics to perform
433
          * preparations before drawing. An example of such an implementation
434
          * is the OpenGLGraphics.
435
          *
436
          * NOTE: You will never need to call this function yourself, unless
437
          *       you use a Graphics object outside of Guichan.
438
          *
439
          * @see endDraw, Gui::draw
440
          */
441
        virtual void beginDraw() restrict2
442
        { }
443
444
        /**
445
          * Deinitializes drawing. Called by the Gui when a Gui::draw() is done.
446
          * done. It should reset any state changes made by beginDraw().
447
          *
448
          * NOTE: You will never need to call this function yourself, unless
449
          *       you use a Graphics object outside of Guichan.
450
          *
451
          * @see beginDraw, Gui::draw
452
          */
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
241
        virtual void finalize(ImageCollection *restrict const col A_UNUSED)
466
                              restrict2
467
241
        { }
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;
486
        int mActualWidth;
487
        int mActualHeight;
488
489
    protected:
490
        /**
491
         * Constructor.
492
         */
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
504
        int getOpenGLFlags() const restrict2 A_WARN_UNUSED;
505
506
        int getSoftwareFlags() const restrict2 A_WARN_UNUSED;
507
508
        bool setOpenGLMode() restrict2;
509
510
        void updateMemoryInfo() restrict2;
511
512
        bool videoInfo() restrict2;
513
514
#ifdef USE_OPENGL
515
        void setOpenGLFlags() restrict2;
516
#endif  // USE_OPENGL
517
518
        /**
519
         * Holds the clip area stack.
520
         */
521
        MStack<ClipRect> mClipStack;
522
523
        SDL_Window *restrict mWindow;
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;
539
        bool mFullscreen;
540
        bool mHWAccel;
541
        bool mRedraw;
542
        bool mDoubleBuffer;
543
        SDL_Rect mRect;
544
        bool mSecure;
545
        RenderType mOpenGL;
546
        bool mEnableResize;
547
        bool mNoFrame;
548
        bool mAllowHighDPI;
549
        std::string mName;
550
        int mStartFreeMem;
551
        bool mSync;
552
        int mScale;
553
        Color mColor;
554
};
555
556
extern Graphics *mainGraphics A_NONNULLPOINTER;
557
558
#endif  // RENDER_GRAPHICS_H