GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/render/sdl2graphics.cpp Lines: 114 316 36.1 %
Date: 2021-03-17 Branches: 68 426 16.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
#ifdef USE_SDL2
68
69
#include "render/sdl2graphics.h"
70
71
#include "graphicsmanager.h"
72
73
#include "render/vertexes/imagecollection.h"
74
75
#include "resources/imagerect.h"
76
#include "resources/sdl2imagehelper.h"
77
78
#include "resources/image/image.h"
79
80
#include "utils/sdlcheckutils.h"
81
82
#include "debug.h"
83
84
#ifdef DEBUG_SDL_SURFACES
85
86
#define MSDL_RenderCopy(render, texture, src, dst) \
87
    FakeSDL_RenderCopy(render, texture, src, dst)
88
89
static int FakeSDL_RenderCopy(SDL_Renderer *restrict const renderer,
90
                              SDL_Texture *restrict const texture,
91
                              const SDL_Rect *restrict const srcrect,
92
                              const SDL_Rect *restrict const dstrect)
93
{
94
    int ret = SDL_RenderCopy(renderer, texture, srcrect, dstrect);
95
    if (ret)
96
    {
97
        logger->log("rendering error in texture %p: %s",
98
            static_cast<void*>(texture), SDL_GetError());
99
    }
100
    return ret;
101
}
102
103
#else  // DEBUG_SDL_SURFACES
104
105
#define MSDL_RenderCopy(render, texture, src, dst) \
106
    SDL_RenderCopy(render, texture, src, dst)
107
108
#endif  // DEBUG_SDL_SURFACES
109
110
#define setRenderDrawColor(mColor) \
111
    SDL_SetRenderDrawColor(mRenderer, \
112
        CAST_U8(mColor.r), \
113
        CAST_U8(mColor.g), \
114
        CAST_U8(mColor.b), \
115
        CAST_U8(mColor.a))
116
117
#define defRectFromArea(rect, area) \
118
    const SDL_Rect rect = \
119
    { \
120
        CAST_S32(area.x), \
121
        CAST_S32(area.y), \
122
        CAST_S32(area.width), \
123
        CAST_S32(area.height) \
124
    }
125
126
150
SDLGraphics::SDLGraphics() :
127
    Graphics(),
128
    mRendererFlags(SDL_RENDERER_SOFTWARE),
129
    mOldPixel(0),
130
150
    mOldAlpha(0)
131
{
132
150
    mOpenGL = RENDER_SDL2_DEFAULT;
133
300
    mName = "SDL2 default";
134
150
}
135
136
150
SDLGraphics::~SDLGraphics()
137
{
138
150
}
139
140
void SDLGraphics::drawRescaledImage(const Image *restrict const image,
141
                                    int dstX, int dstY,
142
                                    const int desiredWidth,
143
                                    const int desiredHeight) restrict2
144
{
145
    FUNC_BLOCK("Graphics::drawRescaledImage", 1)
146
    // Check that preconditions for blitting are met.
147
    if (!mWindow || !image || !image->mTexture)
148
        return;
149
150
    const ClipRect &top = mClipStack.top();
151
    const SDL_Rect &bounds = image->mBounds;
152
    const SDL_Rect srcRect =
153
    {
154
        CAST_S32(bounds.x),
155
        CAST_S32(bounds.y),
156
        CAST_S32(bounds.w),
157
        CAST_S32(bounds.h)
158
    };
159
    const SDL_Rect dstRect =
160
    {
161
        CAST_S32(dstX + top.xOffset),
162
        CAST_S32(dstY + top.yOffset),
163
        CAST_S32(desiredWidth),
164
        CAST_S32(desiredHeight)
165
    };
166
167
    MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect);
168
}
169
170
331
void SDLGraphics::drawImage(const Image *restrict const image,
171
                            int dstX, int dstY) restrict2
172
{
173
331
    drawImageInline(image, dstX, dstY);
174
331
}
175
176
void SDLGraphics::drawImageInline(const Image *restrict const image,
177
                                  int dstX, int dstY) restrict2
178
{
179
    FUNC_BLOCK("Graphics::drawImage", 1)
180
    // Check that preconditions for blitting are met.
181









515
    if (!mWindow || !image || !image->mTexture)
182
515
        return;
183
184
245
    const ClipRect &top = mClipStack.top();
185






245
    if (!top.width || !top.height)
186
        return;
187
188
    const SDL_Rect &bounds = image->mBounds;
189
    const SDL_Rect srcRect =
190
    {
191
        CAST_S32(bounds.x),
192
        CAST_S32(bounds.y),
193
        CAST_S32(bounds.w),
194
        CAST_S32(bounds.h)
195
    };
196
197
    const SDL_Rect dstRect =
198
    {
199
        CAST_S32(dstX + top.xOffset),
200
        CAST_S32(dstY + top.yOffset),
201
        CAST_S32(bounds.w),
202
        CAST_S32(bounds.h)
203
    };
204
205
    MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect);
206
}
207
208
void SDLGraphics::copyImage(const Image *restrict const image,
209
                            int dstX, int dstY) restrict2
210
{
211
    drawImageInline(image, dstX, dstY);
212
}
213
214
void SDLGraphics::drawImageCached(const Image *restrict const image,
215
                                  int x, int y) restrict2
216
{
217
    FUNC_BLOCK("Graphics::drawImageCached", 1)
218
    // Check that preconditions for blitting are met.
219
    if (!mWindow || !image || !image->mTexture)
220
        return;
221
222
    const ClipRect &top = mClipStack.top();
223
    if (!top.width || !top.height)
224
        return;
225
226
    const SDL_Rect &bounds = image->mBounds;
227
    const SDL_Rect srcRect =
228
    {
229
        CAST_S32(bounds.x),
230
        CAST_S32(bounds.y),
231
        CAST_S32(bounds.w),
232
        CAST_S32(bounds.h)
233
    };
234
235
    const SDL_Rect dstRect =
236
    {
237
        CAST_S32(x + top.xOffset),
238
        CAST_S32(y + top.yOffset),
239
        CAST_S32(bounds.w),
240
        CAST_S32(bounds.h)
241
    };
242
243
    MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect);
244
}
245
246
void SDLGraphics::drawPatternCached(const Image *restrict const image,
247
                                    const int x, const int y,
248
                                    const int w, const int h) restrict2
249
{
250
    FUNC_BLOCK("Graphics::drawPatternCached", 1)
251
    // Check that preconditions for blitting are met.
252
    if (!mWindow || !image)
253
        return;
254
    if (!image->mTexture)
255
        return;
256
257
    const ClipRect &top = mClipStack.top();
258
    if (!top.width || !top.height)
259
        return;
260
261
    const SDL_Rect &bounds = image->mBounds;
262
    const int iw = bounds.w;
263
    const int ih = bounds.h;
264
    if (iw == 0 || ih == 0)
265
        return;
266
267
    const int xOffset = top.xOffset + x;
268
    const int yOffset = top.yOffset + y;
269
270
    SDL_Rect dstRect;
271
    SDL_Rect srcRect;
272
    srcRect.x = CAST_S32(bounds.x);
273
    srcRect.y = CAST_S32(bounds.y);
274
    for (int py = 0; py < h; py += ih)
275
    {
276
        const int dh = (py + ih >= h) ? h - py : ih;
277
        dstRect.y = CAST_S32(py + yOffset);
278
        srcRect.h = CAST_S32(dh);
279
        dstRect.h = CAST_S32(dh);
280
281
        for (int px = 0; px < w; px += iw)
282
        {
283
            const int dw = (px + iw >= w) ? w - px : iw;
284
            dstRect.x = CAST_S32(px + xOffset);
285
            srcRect.w = CAST_S32(dw);
286
            dstRect.w = CAST_S32(dw);
287
288
            MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect);
289
        }
290
    }
291
}
292
293
void SDLGraphics::completeCache() restrict2
294
{
295
}
296
297
void SDLGraphics::drawPattern(const Image *restrict const image,
298
                              const int x, const int y,
299
                              const int w, const int h) restrict2
300
{
301
    drawPatternInline(image, x, y, w, h);
302
}
303
304
void SDLGraphics::drawPatternInline(const Image *restrict const image,
305
                                    const int x, const int y,
306
                                    const int w, const int h) restrict2
307
{
308
    FUNC_BLOCK("Graphics::drawPattern", 1)
309
    // Check that preconditions for blitting are met.
310






230
    if (!mWindow || !image)
311
230
        return;
312



230
    if (!image->mTexture)
313
        return;
314
315
    const ClipRect &top = mClipStack.top();
316
    if (!top.width || !top.height)
317
        return;
318
319
    const SDL_Rect &bounds = image->mBounds;
320
    const int iw = bounds.w;
321
    const int ih = bounds.h;
322
    if (iw == 0 || ih == 0)
323
        return;
324
325
    const int xOffset = top.xOffset + x;
326
    const int yOffset = top.yOffset + y;
327
328
    SDL_Rect dstRect;
329
    SDL_Rect srcRect;
330
    srcRect.x = CAST_S32(bounds.x);
331
    srcRect.y = CAST_S32(bounds.y);
332
    for (int py = 0; py < h; py += ih)
333
    {
334
        const int dh = (py + ih >= h) ? h - py : ih;
335
        dstRect.y = CAST_S32(py + yOffset);
336
        srcRect.h = CAST_S32(dh);
337
        dstRect.h = CAST_S32(dh);
338
339
        for (int px = 0; px < w; px += iw)
340
        {
341
            const int dw = (px + iw >= w) ? w - px : iw;
342
            dstRect.x = CAST_S32(px + xOffset);
343
            srcRect.w = CAST_S32(dw);
344
            dstRect.w = CAST_S32(dw);
345
346
            MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect);
347
        }
348
    }
349
}
350
351
void SDLGraphics::drawRescaledPattern(const Image *restrict const image,
352
                                      const int x, const int y,
353
                                      const int w, const int h,
354
                                      const int scaledWidth,
355
                                      const int scaledHeight) restrict2
356
{
357
    // Check that preconditions for blitting are met.
358
    if (!mWindow || !image)
359
        return;
360
    if (!image->mTexture)
361
        return;
362
363
    if (scaledHeight == 0 || scaledWidth == 0)
364
        return;
365
366
    const ClipRect &top = mClipStack.top();
367
    if (!top.width || !top.height)
368
        return;
369
370
    Image *const tmpImage = image->SDLgetScaledImage(
371
        scaledWidth, scaledHeight);
372
    if (!tmpImage)
373
        return;
374
375
    const SDL_Rect &bounds = tmpImage->mBounds;
376
    const int iw = bounds.w;
377
    const int ih = bounds.h;
378
    if (iw == 0 || ih == 0)
379
        return;
380
381
    const int xOffset = top.xOffset + x;
382
    const int yOffset = top.yOffset + y;
383
384
    SDL_Rect dstRect;
385
    SDL_Rect srcRect;
386
    srcRect.x = CAST_S32(bounds.x);
387
    srcRect.y = CAST_S32(bounds.y);
388
    for (int py = 0; py < h; py += ih)
389
    {
390
        const int dh = (py + ih >= h) ? h - py : ih;
391
        dstRect.y = CAST_S32(py + yOffset);
392
        srcRect.h = CAST_S32(dh);
393
        dstRect.h = CAST_S32(dh);
394
395
        for (int px = 0; px < w; px += iw)
396
        {
397
            const int dw = (px + iw >= w) ? w - px : iw;
398
            dstRect.x = CAST_S32(px + xOffset);
399
            srcRect.w = CAST_S32(dw);
400
            dstRect.w = CAST_S32(dw);
401
402
            MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect);
403
        }
404
    }
405
406
    delete tmpImage;
407
}
408
409
void SDLGraphics::calcPattern(ImageVertexes *restrict const vert,
410
                              const Image *restrict const image,
411
                              const int x, const int y,
412
                              const int w, const int h) const restrict2
413
{
414
    calcPatternInline(vert, image, x, y, w, h);
415
}
416
417
void SDLGraphics::calcPatternInline(ImageVertexes *restrict const vert,
418
                                    const Image *restrict const image,
419
                                    const int x, const int y,
420
                                    const int w, const int h) const restrict2
421
{
422
    // Check that preconditions for blitting are met.
423











820
    if (!vert || !mWindow || !image || !image->mTexture)
424
        return;
425
426
    const ClipRect &top = mClipStack.top();
427
    if (!top.width || !top.height)
428
        return;
429
430
    const SDL_Rect &bounds = image->mBounds;
431
    const int iw = bounds.w;
432
    const int ih = bounds.h;
433
    if (iw == 0 || ih == 0)
434
        return;
435
436
    const int xOffset = top.xOffset + x;
437
    const int yOffset = top.yOffset + y;
438
    const int srcX = bounds.x;
439
    const int srcY = bounds.y;
440
    for (int py = 0; py < h; py += ih)
441
    {
442
        const int dh = (py + ih >= h) ? h - py : ih;
443
        const int dstY = py + yOffset;
444
445
        for (int px = 0; px < w; px += iw)
446
        {
447
            const int dw = (px + iw >= w) ? w - px : iw;
448
            const int dstX = px + xOffset;
449
450
            DoubleRect *const r = new DoubleRect;
451
            SDL_Rect &dstRect = r->dst;
452
            SDL_Rect &srcRect = r->src;
453
            srcRect.x = CAST_S32(srcX);
454
            srcRect.y = CAST_S32(srcY);
455
            srcRect.w = CAST_S32(dw);
456
            srcRect.h = CAST_S32(dh);
457
            dstRect.x = CAST_S32(dstX);
458
            dstRect.y = CAST_S32(dstY);
459
            dstRect.w = CAST_S32(dw);
460
            dstRect.h = CAST_S32(dh);
461
462
            vert->sdl.push_back(r);
463
        }
464
    }
465
}
466
467
40
void SDLGraphics::calcPattern(ImageCollection *restrict const vertCol,
468
                              const Image *restrict const image,
469
                              const int x, const int y,
470
                              const int w, const int h) const restrict2
471
{
472
40
    ImageVertexes *vert = nullptr;
473
40
    if (vertCol->currentImage != image)
474
    {
475
40
        vert = new ImageVertexes;
476
40
        vertCol->currentImage = image;
477
40
        vertCol->currentVert = vert;
478
40
        vert->image = image;
479
40
        vertCol->draws.push_back(vert);
480
    }
481
    else
482
    {
483
        vert = vertCol->currentVert;
484
    }
485
486
80
    calcPatternInline(vert, image, x, y, w, h);
487
40
}
488
489
void SDLGraphics::calcTileVertexes(ImageVertexes *restrict const vert,
490
                                   const Image *restrict const image,
491
                                   int x, int y) const restrict2
492
{
493
    vert->image = image;
494
    calcTileSDL(vert, x, y);
495
}
496
497
void SDLGraphics::calcTileVertexesInline(ImageVertexes *restrict const vert,
498
                                         const Image *restrict const image,
499
                                         int x, int y) const restrict2
500
{
501
635
    vert->image = image;
502
635
    calcTileSDL(vert, x, y);
503
}
504
505
697
void SDLGraphics::calcTileSDL(ImageVertexes *restrict const vert,
506
                              int x, int y) const restrict2
507
{
508
    // Check that preconditions for blitting are met.
509

697
    if (!vert || !vert->image || !vert->image->mTexture)
510
697
        return;
511
512
    const ClipRect &top = mClipStack.top();
513
    if (!top.width || !top.height)
514
        return;
515
516
    const Image *const image = vert->image;
517
    const SDL_Rect &bounds = image->mBounds;
518
519
    x += top.xOffset;
520
    y += top.yOffset;
521
522
    DoubleRect *rect = new DoubleRect;
523
    SDL_Rect &dstRect = rect->dst;
524
    SDL_Rect &srcRect = rect->src;
525
526
    srcRect.x = CAST_S32(bounds.x);
527
    srcRect.y = CAST_S32(bounds.y);
528
    srcRect.w = CAST_S32(bounds.w);
529
    srcRect.h = CAST_S32(bounds.h);
530
    dstRect.x = CAST_S32(x);
531
    dstRect.y = CAST_S32(y);
532
    dstRect.w = CAST_S32(bounds.w);
533
    dstRect.h = CAST_S32(bounds.h);
534
535
    vert->sdl.push_back(rect);
536
}
537
538
62
void SDLGraphics::calcTileCollection(ImageCollection *restrict const vertCol,
539
                                     const Image *restrict const image,
540
                                     int x, int y) restrict2
541
{
542
62
    if (!vertCol)
543
        return;
544
62
    if (vertCol->currentImage != image)
545
    {
546
62
        ImageVertexes *const vert = new ImageVertexes;
547
62
        vertCol->currentImage = image;
548
62
        vertCol->currentVert = vert;
549
62
        vert->image = image;
550
62
        vertCol->draws.push_back(vert);
551
62
        calcTileSDL(vert, x, y);
552
    }
553
    else
554
    {
555
        calcTileSDL(vertCol->currentVert, x, y);
556
    }
557
}
558
559
241
void SDLGraphics::drawTileCollection(const ImageCollection
560
                                     *restrict const vertCol) restrict2
561
{
562
241
    const ImageVertexesVector &draws = vertCol->draws;
563
482
    const ImageCollectionCIter it_end = draws.end();
564
992
    for (ImageCollectionCIter it = draws.begin(); it != it_end; ++ it)
565
    {
566
269
        const ImageVertexes *const vert = *it;
567
269
        const Image *const img = vert->image;
568
269
        const DoubleRects *const rects = &vert->sdl;
569
538
        DoubleRects::const_iterator it2 = rects->begin();
570
538
        const DoubleRects::const_iterator it2_end = rects->end();
571
269
        while (it2 != it2_end)
572
        {
573
            MSDL_RenderCopy(mRenderer, img->mTexture,
574
                &(*it2)->src, &(*it2)->dst);
575
            ++ it2;
576
        }
577
    }
578
241
}
579
580
void SDLGraphics::drawTileVertexes(const ImageVertexes *restrict const vert)
581
                                   restrict2
582
{
583
    if (!vert)
584
        return;
585
    // vert and img must be != 0
586
    const Image *const img = vert->image;
587
    const DoubleRects *const rects = &vert->sdl;
588
    DoubleRects::const_iterator it = rects->begin();
589
    const DoubleRects::const_iterator it_end = rects->end();
590
    while (it != it_end)
591
    {
592
        MSDL_RenderCopy(mRenderer, img->mTexture, &(*it)->src, &(*it)->dst);
593
        ++ it;
594
    }
595
}
596
597
67
void SDLGraphics::updateScreen() restrict2
598
{
599
    BLOCK_START("Graphics::updateScreen")
600
67
    SDL_RenderPresent(mRenderer);
601
//    SDL_RenderClear(mRenderer);
602
    BLOCK_END("Graphics::updateScreen")
603
67
}
604
605
167
void SDLGraphics::calcWindow(ImageCollection *restrict const vertCol,
606
                             const int x, const int y,
607
                             const int w, const int h,
608
                             const ImageRect &restrict imgRect) restrict2
609
{
610
167
    ImageVertexes *vert = nullptr;
611
167
    Image *const image = imgRect.grid[4];
612
167
    if (!image)
613
        return;
614
167
    if (vertCol->currentImage != image)
615
    {
616
167
        vert = new ImageVertexes;
617
167
        vertCol->currentImage = image;
618
167
        vertCol->currentVert = vert;
619
167
        vert->image = image;
620
167
        vertCol->draws.push_back(vert);
621
    }
622
    else
623
    {
624
        vert = vertCol->currentVert;
625
    }
626
334
    calcImageRect(vert, x, y, w, h, imgRect);
627
}
628
629
2
void SDLGraphics::fillRectangle(const Rect &restrict rectangle) restrict2
630
{
631
2
    const ClipRect &top = mClipStack.top();
632
    const SDL_Rect rect =
633
    {
634
2
        CAST_S32(rectangle.x + top.xOffset),
635
2
        CAST_S32(rectangle.y + top.yOffset),
636
2
        CAST_S32(rectangle.width),
637
2
        CAST_S32(rectangle.height)
638
8
    };
639
640
2
    setRenderDrawColor(mColor);
641
2
    SDL_RenderFillRects(mRenderer, &rect, 1);
642
2
}
643
644
void SDLGraphics::beginDraw() restrict2
645
{
646
    pushClipArea(Rect(0, 0, mRect.w, mRect.h));
647
}
648
649
void SDLGraphics::endDraw() restrict2
650
{
651
    popClipArea();
652
}
653
654
571
void SDLGraphics::pushClipArea(const Rect &restrict area) restrict2
655
{
656
571
    Graphics::pushClipArea(area);
657
658
571
    const ClipRect &carea = mClipStack.top();
659
571
    defRectFromArea(rect, carea);
660
571
    SDL_RenderSetClipRect(mRenderer, &rect);
661
571
}
662
663
571
void SDLGraphics::popClipArea() restrict2
664
{
665
571
    Graphics::popClipArea();
666
667
571
    if (mClipStack.empty())
668
67
        return;
669
670
504
    const ClipRect &carea = mClipStack.top();
671
504
    defRectFromArea(rect, carea);
672
504
    SDL_RenderSetClipRect(mRenderer, &rect);
673
}
674
675
void SDLGraphics::drawPoint(int x, int y) restrict2
676
{
677
    if (mClipStack.empty())
678
        return;
679
680
    const ClipRect &top = mClipStack.top();
681
682
    x += top.xOffset;
683
    y += top.yOffset;
684
685
    if (!top.isPointInRect(x, y))
686
        return;
687
688
    setRenderDrawColor(mColor);
689
    const SDL_Point point =
690
    {
691
        x,
692
        y
693
    };
694
695
    SDL_RenderDrawPoints(mRenderer, &point, 1);
696
}
697
698
699
void SDLGraphics::drawRectangle(const Rect &restrict rectangle) restrict2
700
{
701
    const ClipRect &top = mClipStack.top();
702
    setRenderDrawColor(mColor);
703
704
    const int x1 = rectangle.x + top.xOffset;
705
    const int y1 = rectangle.y + top.yOffset;
706
    const int x2 = x1 + rectangle.width - 1;
707
    const int y2 = y1 + rectangle.height - 1;
708
    SDL_Point points[] =
709
    {
710
        {x1, y1},
711
        {x2, y1},
712
        {x2, y2},
713
        {x1, y2},
714
        {x1, y1}
715
    };
716
717
    SDL_RenderDrawLines(mRenderer, points, 5);
718
}
719
720
4
void SDLGraphics::drawLine(int x1, int y1,
721
                           int x2, int y2) restrict2
722
{
723
4
    const ClipRect &top = mClipStack.top();
724
4
    setRenderDrawColor(mColor);
725
726
4
    const int x0 = top.xOffset;
727
4
    const int y0 = top.yOffset;
728
729
    SDL_Point points[] =
730
    {
731
8
        {x1 + x0, y1 + y0},
732
8
        {x2 + x0, y2 + y0}
733
16
    };
734
735
4
    SDL_RenderDrawLines(mRenderer, points, 2);
736
4
}
737
738
77
bool SDLGraphics::setVideoMode(const int w, const int h,
739
                               const int scale,
740
                               const int bpp,
741
                               const bool fs,
742
                               const bool hwaccel,
743
                               const bool resize,
744
                               const bool noFrame,
745
                               const bool allowHighDPI) restrict2
746
{
747
77
    setMainFlags(w, h,
748
        scale,
749
        bpp,
750
        fs,
751
        hwaccel,
752
        resize,
753
        noFrame,
754
77
        allowHighDPI);
755
756
77
    if (!(mWindow = GraphicsManager::createWindow(w, h, bpp,
757
        getSoftwareFlags())))
758
    {
759
        mRect.w = 0;
760
        mRect.h = 0;
761
        return false;
762
    }
763
764
77
    int w1 = 0;
765
77
    int h1 = 0;
766
77
    SDL_GetWindowSize(mWindow, &w1, &h1);
767
77
    mRect.w = w1;
768
77
    mRect.h = h1;
769
770
77
    mRenderer = graphicsManager.createRenderer(mWindow, mRendererFlags);
771
154
    SDLImageHelper::setRenderer(mRenderer);
772
77
    return videoInfo();
773
}
774
775
46
void SDLGraphics::drawImageRect(const int x, const int y,
776
                                const int w, const int h,
777
                                const ImageRect &restrict imgRect) restrict2
778
{
779
    #include "render/graphics_drawImageRect.hpp"
780
46
}
781
782
void SDLGraphics::calcImageRect(ImageVertexes *restrict const vert,
783
                                const int x, const int y,
784
                                const int w, const int h,
785
                                const ImageRect &restrict imgRect) restrict2
786
{
787
    #include "render/graphics_calcImageRect.hpp"
788
}
789
790
#endif  // USE_SDL2