GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/render/graphics.h Lines: 11 45 24.4 %
Date: 2017-11-29 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-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
71
#include "enums/render/rendertype.h"
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
104
/**
105
 * A central point of control for graphics.
106
 */
107
class Graphics notfinal
108
{
109
    public:
110
#ifdef USE_OPENGL
111
        friend class OpenGLScreenshotHelper;
112
#endif  // USE_SDL2
113
114
        friend class SdlScreenshotHelper;
115
116
        A_DELETE_COPY(Graphics)
117
118
        /**
119
         * Destructor.
120
         */
121
        virtual ~Graphics();
122
123
        static void cleanUp();
124
125
        /**
126
         * Alignments for text drawing.
127
         */
128
        enum Alignment
129
        {
130
            LEFT = 0,
131
            CENTER,
132
            RIGHT
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
143
        SDL_Window *getWindow() const restrict2 noexcept2
144
        { return mWindow; }
145
146
        /**
147
         * Sets whether vertical refresh syncing is enabled. Takes effect after
148
         * the next call to setVideoMode(). Only implemented on MacOS for now.
149
         */
150
        void setSync(const bool sync) restrict2;
151
152
        bool getSync() const restrict2 noexcept2 A_WARN_UNUSED
153
        { return mSync; }
154
155
        /**
156
         * Try to create a window with the given settings.
157
         */
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
167
        /**
168
         * Set fullscreen mode.
169
         */
170
        bool setFullscreen(const bool fs) restrict2;
171
172
        /**
173
         * Resize the window to the specified size.
174
         */
175
        virtual bool resizeScreen(const int width,
176
                                  const int height) restrict2;
177
178
        virtual void restoreContext() restrict2
179
        { }
180
181
        /**
182
         * Draws a resclaled version of the image
183
         */
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
193
        /**
194
         * Draw a pattern based on a rescaled version of the given image...
195
         */
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
251
        /**
252
         * Updates the screen. This is done by either copying the buffer to the
253
         * screen or swapping pages.
254
         */
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
264
        /**
265
         * Returns the width of the screen.
266
         */
267
        int getWidth() const restrict2 A_WARN_UNUSED;
268
269
        /**
270
         * Returns the height of the screen.
271
         */
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
14
        { return mClipStack.top(); }
282
283
        void setRedraw(const bool n) restrict2 noexcept2
284
196
        { mRedraw = n; }
285
286
        bool getRedraw() const restrict2 noexcept2 A_WARN_UNUSED
287
34
        { 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
16
        virtual void setColor(const Color &restrict color) restrict2
320
        {
321
16
            mColor = color;
322
16
            mAlpha = (color.a != 255);
323
16
        }
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
344
        /**
345
         * Blits an image onto the screen.
346
         *
347
         * @return <code>true</code> if the image was blitted properly
348
         *         <code>false</code> otherwise.
349
         */
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
152
        virtual bool isAllowScale() const restrict2 noexcept2
369
152
        { return false; }
370
371
        void setScale(int scale) restrict2;
372
373
        /**
374
         * Pushes a clip area onto the stack. The x and y coordinates in the
375
         * rectangle is  relative to the last pushed clip area.
376
         * If the new area falls outside the current clip area, it will be
377
         * clipped as necessary.
378
         *
379
         * If a clip area is outside of the top clip area a clip area with
380
         * zero width and height will be pushed.
381
         *
382
         * @param area The clip area to be pushed onto the stack.
383
         */
384
        virtual void pushClipArea(const Rect &restrict area) restrict2;
385
386
        /**
387
         * Removes the top most clip area from the stack.
388
         *
389
         * @throws Exception if the stack is empty.
390
         */
391
        virtual void popClipArea() restrict2;
392
393
        /**
394
         * Ddraws a line.
395
         *
396
         * @param x1 The first x coordinate.
397
         * @param y1 The first y coordinate.
398
         * @param x2 The second x coordinate.
399
         * @param y2 The second y coordinate.
400
         */
401
        virtual void drawLine(int x1, int y1,
402
                              int x2, int y2) restrict2 = 0;
403
404
        /**
405
         * Draws a simple, non-filled, rectangle with a one pixel width.
406
         *
407
         * @param rectangle The rectangle to draw.
408
         */
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
421
        /**
422
         * Draws a single point/pixel.
423
         *
424
         * @param x The x coordinate.
425
         * @param y The y coordinate.
426
         */
427
        virtual void drawPoint(int x, int y) restrict2 = 0;
428
429
        /**
430
          * Initializes drawing. Called by the Gui when Gui::draw() is called.
431
          * It is needed by some implementations of Graphics to perform
432
          * preparations before drawing. An example of such an implementation
433
          * is the OpenGLGraphics.
434
          *
435
          * NOTE: You will never need to call this function yourself, unless
436
          *       you use a Graphics object outside of Guichan.
437
          *
438
          * @see endDraw, Gui::draw
439
          */
440
        virtual void beginDraw() restrict2
441
        { }
442
443
        /**
444
          * Deinitializes drawing. Called by the Gui when a Gui::draw() is done.
445
          * done. It should reset any state changes made by beginDraw().
446
          *
447
          * NOTE: You will never need to call this function yourself, unless
448
          *       you use a Graphics object outside of Guichan.
449
          *
450
          * @see beginDraw, Gui::draw
451
          */
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
478
        virtual void finalize(ImageCollection *restrict const col A_UNUSED)
465
                              restrict2
466
478
        { }
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;
485
        int mActualWidth;
486
        int mActualHeight;
487
488
    protected:
489
        /**
490
         * Constructor.
491
         */
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
517
        /**
518
         * Holds the clip area stack.
519
         */
520
        MStack<ClipRect> mClipStack;
521
522
        SDL_Window *restrict mWindow;
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;
538
        bool mFullscreen;
539
        bool mHWAccel;
540
        bool mRedraw;
541
        bool mDoubleBuffer;
542
        SDL_Rect mRect;
543
        bool mSecure;
544
        RenderType mOpenGL;
545
        bool mEnableResize;
546
        bool mNoFrame;
547
        bool mAllowHighDPI;
548
        std::string mName;
549
        int mStartFreeMem;
550
        bool mSync;
551
        int mScale;
552
        Color mColor;
553
};
554
555
extern Graphics *mainGraphics A_NONNULLPOINTER;
556
557
#endif  // RENDER_GRAPHICS_H