summaryrefslogtreecommitdiff
path: root/src/render/graphics.h
blob: 82bf220ab40f9710410ea6c2cdf15a404666c688 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2013  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef RENDER_GRAPHICS_H
#define RENDER_GRAPHICS_H

#include "SDL_video.h"

#include "sdlshared.h"

#include "render/renderers.h"

#include <guichan/color.hpp>
#include <guichan/graphics.hpp>

#ifdef USE_SDL2
#include <SDL_render.h>
#endif

#include "localconsts.h"

class Image;
class ImageCollection;
class ImageVertexes;

struct SDL_Window;

static const int defaultScreenWidth = 800;
static const int defaultScreenHeight = 600;

#define DRAW_IMAGE(graphics, image, x, y) \
    { \
        if (image) \
        { \
            (graphics)->drawImage2(image, 0, 0, x, y, \
                (image)->mBounds.w, (image)->mBounds.h, false); \
        } \
    }

/**
 * 9 images defining a rectangle. 4 corners, 4 sides and a middle area. The
 * topology is as follows:
 *
 * <pre>
 *  !-----!-----------------!-----!
 *  !  0  !        1        !  2  !
 *  !-----!-----------------!-----!
 *  !  3  !        4        !  5  !
 *  !-----!-----------------!-----!
 *  !  6  !        7        !  8  !
 *  !-----!-----------------!-----!
 * </pre>
 *
 * Sections 0, 2, 6 and 8 will remain as is. 1, 3, 4, 5 and 7 will be
 * repeated to fit the size of the widget.
 */
class ImageRect final
{
    public:
        ImageRect()
        {
            for (int f = 0; f < 9; f ++)
                grid[f] = nullptr;
        }

        A_DELETE_COPY(ImageRect)

        enum ImagePosition
        {
            UPPER_LEFT = 0,
            UPPER_CENTER = 1,
            UPPER_RIGHT = 2,
            LEFT = 3,
            CENTER = 4,
            RIGHT = 5,
            LOWER_LEFT = 6,
            LOWER_CENTER = 7,
            LOWER_RIGHT = 8
        };

        Image *grid[9];
};

/**
 * A central point of control for graphics.
 */
class Graphics : public gcn::Graphics
{
    public:
        A_DELETE_COPY(Graphics)

        /**
         * Destructor.
         */
        virtual ~Graphics();

        void setWindow(SDL_Window *const window,
                       const int width, const int height)
        {
            mWindow = window;
            mRect.w = width;
            mRect.h = height;
        }

        SDL_Window *getWindow() const
        { return mWindow; }

        /**
         * Sets whether vertical refresh syncing is enabled. Takes effect after
         * the next call to setVideoMode(). Only implemented on MacOS for now.
         */
        void setSync(const bool sync);

        bool getSync() const A_WARN_UNUSED
        { return mSync; }

        /**
         * Try to create a window with the given settings.
         */
        virtual bool setVideoMode(const int w, const int h, const int bpp,
                                  const bool fs, const bool hwaccel,
                                  const bool resize, const bool noFrame) = 0;

        /**
         * Set fullscreen mode.
         */
        bool setFullscreen(const bool fs);

        /**
         * Resize the window to the specified size.
         */
        virtual bool resizeScreen(const int width, const int height);

        //  override unused abstract function
        void drawImage(const gcn::Image* image A_UNUSED,
                       int srcX A_UNUSED, int srcY A_UNUSED,
                       int dstX A_UNUSED, int dstY A_UNUSED,
                       int width A_UNUSED, int height A_UNUSED) override
        {
        }

        /**
         * Draws a resclaled version of the image
         */
        virtual bool drawRescaledImage(const Image *const image, int srcX,
                                       int srcY, int dstX, int dstY,
                                       const int width, const int height,
                                       const int desiredWidth,
                                       const int desiredHeight,
                                       const bool useColor = false) = 0;

        virtual void drawPattern(const Image *const image,
                                 const int x, const int y,
                                 const int w, const int h) = 0;

        /**
         * Draw a pattern based on a rescaled version of the given image...
         */
        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;

        /**
         * Draws a rectangle using images. 4 corner images, 4 side images and 1
         * image for the inside.
         */
        void drawImageRect(const int x, const int y, const int w, const int h,
                           const Image *const topLeft,
                           const Image *const topRight,
                           const Image *const bottomLeft,
                           const Image *const bottomRight,
                           const Image *const top,
                           const Image *const right,
                           const Image *const bottom,
                           const Image *const left,
                           const Image *const center);

        /**
         * Draws a rectangle using images. 4 corner images, 4 side images and 1
         * image for the inside.
         */
        void drawImageRect(int x, int y, int w, int h,
                           const ImageRect &imgRect);

        bool calcImageRect(ImageVertexes *const vert,
                           const int x, const int y,
                           const int w, const int h,
                           const Image *const topLeft,
                           const Image *const topRight,
                           const Image *const bottomLeft,
                           const Image *const bottomRight,
                           const Image *const top,
                           const Image *const right,
                           const Image *const bottom,
                           const Image *const left,
                           const Image *const center);

        virtual void calcPattern(ImageVertexes *const vert,
                                 const Image *const image,
                                 const int x, const int y,
                                 const int w, const int h) const = 0;

        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 calcTileVertexes(ImageVertexes *const vert,
                                      const Image *const image,
                                      int x, int y) const = 0;

        virtual void calcTileSDL(ImageVertexes *const vert A_UNUSED,
                                 int x A_UNUSED, int y A_UNUSED) const
        {
        }

        virtual void drawTileVertexes(const ImageVertexes *const vert) = 0;

        virtual void drawTileCollection(const ImageCollection
                                        *const vertCol) = 0;

        virtual void calcTileCollection(ImageCollection *const vertCol,
                                        const Image *const image,
                                        int x, int y) = 0;

        virtual bool calcWindow(ImageCollection *const vertCol,
                                const int x, const int y,
                                const int w, const int h,
                                const ImageRect &imgRect) = 0;

        /**
         * Draws a rectangle using images. 4 corner images, 4 side images and 1
         * image for the inside.
         */
        inline void drawImageRect(const gcn::Rectangle &area,
                                  const ImageRect &imgRect)
        { drawImageRect(area.x, area.y, area.width, area.height, imgRect); }

        virtual void fillRectangle(const gcn::Rectangle& rectangle)
                                   override = 0;

        /**
         * Updates the screen. This is done by either copying the buffer to the
         * screen or swapping pages.
         */
        virtual void updateScreen() = 0;

        void setWindowSize(const int width, const int height);

        /**
         * Returns the width of the screen.
         */
        int getWidth() const A_WARN_UNUSED;

        /**
         * Returns the height of the screen.
         */
        int getHeight() const A_WARN_UNUSED;

        /**
         * Takes a screenshot and returns it as SDL surface.
         */
        virtual SDL_Surface *getScreenshot() A_WARN_UNUSED = 0;

        virtual void prepareScreenshot()
        { }

        int getMemoryUsage() const A_WARN_UNUSED;

        virtual bool drawNet(const int x1, const int y1,
                             const int x2, const int y2,
                             const int width, const int height);

        const gcn::Font *getFont() const A_WARN_UNUSED
        { return mFont; }

        gcn::ClipRectangle &getTopClip() A_WARN_UNUSED
        { return mClipStack.top(); }

        void setRedraw(const bool n)
        { mRedraw = n; }

        bool getRedraw() const A_WARN_UNUSED
        { return mRedraw; }

        void setSecure(const bool n)
        { mSecure = n; }

        bool getSecure() const A_WARN_UNUSED
        { return mSecure; }

        int getBpp() const A_WARN_UNUSED
        { return mBpp; }

        bool getFullScreen() const A_WARN_UNUSED
        { return mFullscreen; }

        bool getHWAccel() const A_WARN_UNUSED
        { return mHWAccel; }

        bool getDoubleBuffer() const A_WARN_UNUSED
        { return mDoubleBuffer; }

        RenderType getOpenGL() const A_WARN_UNUSED
        { return mOpenGL; }

        void setNoFrame(const bool n)
        { mNoFrame = n; }

        const std::string &getName() const A_WARN_UNUSED
        { return mName; }

        virtual void initArrays()
        { }

        void setColor(const gcn::Color &color) override
        {
            mColor = color;
            mColor2 = color;
            mAlpha = (color.a != 255);
        }

        void setColor2(const gcn::Color &color)
        { mColor2 = color; }

        void setColorAll(const gcn::Color &color, const gcn::Color &color2)
        {
            mColor = color;
            mColor2 = color2;
            mAlpha = (color.a != 255);
        }

        const gcn::Color &getColor() const override
        { return mColor; }

        const gcn::Color &getColor2() const
        { return mColor2; }

#ifdef DEBUG_DRAW_CALLS
        virtual unsigned int getDrawCalls() const
        { return 0; }
#endif
#ifdef DEBUG_BIND_TEXTURE
        virtual unsigned int getBinds() const
        { return 0; }
#endif
#ifdef USE_SDL2
        void dumpRendererInfo(const char *const str,
                              const SDL_RendererInfo &info);

        virtual void setRendererFlags(const uint32_t flags A_UNUSED)
        { }
#endif

        /**
         * Blits an image onto the screen.
         *
         * @return <code>true</code> if the image was blitted properly
         *         <code>false</code> otherwise.
         */
        virtual bool drawImage2(const Image *const image,
                                int srcX, int srcY,
                                int dstX, int dstY,
                                const int width, const int height,
                                const bool useColor) = 0;

        virtual void drawImageCached(const Image *const image,
                                     int srcX, int srcY) = 0;

        virtual void drawPatternCached(const Image *const image,
                                       const int x, const int y,
                                       const int w, const int h) = 0;

        virtual void completeCache() = 0;

        int mWidth;
        int mHeight;

    protected:
        /**
         * Constructor.
         */
        Graphics();

        void setMainFlags(const int w, const int h, const int bpp,
                          const bool fs, const bool hwaccel,
                          const bool resize, const bool noFrame);

        int getOpenGLFlags() const A_WARN_UNUSED;

        int getSoftwareFlags() const A_WARN_UNUSED;

        bool setOpenGLMode();

        void updateMemoryInfo();

        bool videoInfo();

        SDL_Window *mWindow;

#ifdef USE_SDL2
        SDL_Renderer *mRenderer;
#ifdef USE_OPENGL
        SDL_GLContext mGLContext;
#endif
#endif
        int mBpp;
        bool mAlpha;
        bool mFullscreen;
        bool mHWAccel;
        bool mRedraw;
        bool mDoubleBuffer;
        SDL_Rect mRect;
        bool mSecure;
        RenderType mOpenGL;
        bool mEnableResize;
        bool mNoFrame;
        std::string mName;
        int mStartFreeMem;
        bool mSync;
        gcn::Color mColor;
        gcn::Color mColor2;
};

extern Graphics *mainGraphics;

#endif  // RENDER_GRAPHICS_H