GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/mobileopenglscreenshothelper.cpp Lines: 0 41 0.0 %
Date: 2021-03-17 Branches: 0 22 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
#if defined(USE_OPENGL) && !defined(ANDROID)
25
26
#include "resources/mobileopenglscreenshothelper.h"
27
28
#include "configuration.h"
29
#include "graphicsmanager.h"
30
31
#include "render/graphics.h"
32
33
#include "render/opengl/mgl.h"
34
#ifdef __native_client__
35
#include "render/opengl/naclglfunctions.h"
36
#endif  // __native_client__
37
38
#include "debug.h"
39
40
MobileOpenGLScreenshotHelper::MobileOpenGLScreenshotHelper() :
41
    ScreenshotHelper(),
42
    mFbo()
43
{
44
}
45
46
MobileOpenGLScreenshotHelper::~MobileOpenGLScreenshotHelper()
47
{
48
}
49
50
void MobileOpenGLScreenshotHelper::prepare()
51
{
52
    if (config.getBoolValue("usefbo"))
53
        graphicsManager.createFBO(mainGraphics->mWidth,
54
            mainGraphics->mHeight,
55
            &mFbo);
56
}
57
58
SDL_Surface *MobileOpenGLScreenshotHelper::getScreenshot()
59
{
60
    const int h = mainGraphics->mHeight;
61
    const int w = mainGraphics->mWidth - (mainGraphics->mWidth % 4);
62
    GLint pack = 1;
63
64
    SDL_Surface *const tmpImage = MSDL_CreateRGBSurface(
65
        SDL_SWSURFACE, w, h, 32,
66
        0x00ff0000U, 0x0000ff00U, 0x000000ffU, 0xff000000U);
67
68
    if (tmpImage == nullptr)
69
        return nullptr;
70
71
    // Grap the pixel buffer and write it to the SDL surface
72
    mglGetIntegerv(GL_PACK_ALIGNMENT, &pack);
73
    mglPixelStorei(GL_PACK_ALIGNMENT, 1);
74
    mglReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tmpImage->pixels);
75
76
    const size_t lineSize = 3 * w;
77
    GLubyte *const buf = new GLubyte[lineSize];
78
79
    SDL_Surface *const screenshot = MSDL_CreateRGBSurface(
80
        SDL_SWSURFACE, w, h, 24,
81
        0xff0000, 0x00ff00, 0x0000ff, 0x000000);
82
83
    if (screenshot == nullptr)
84
    {
85
        MSDL_FreeSurface(tmpImage);
86
        delete [] buf;
87
        return nullptr;
88
    }
89
90
#ifdef USE_SDL2
91
    SDL_SetSurfaceAlphaMod(tmpImage, SDL_ALPHA_OPAQUE);
92
    SDL_SetSurfaceBlendMode(tmpImage, SDL_BLENDMODE_NONE);
93
#else  // USE_SDL2
94
95
    // Make sure the alpha channel is not used, but copied to destination
96
    SDL_SetAlpha(tmpImage, 0, SDL_ALPHA_OPAQUE);
97
#endif  // USE_SDL2
98
99
    if (SDL_MUSTLOCK(screenshot))
100
        SDL_LockSurface(screenshot);
101
102
    SDL_BlitSurface(tmpImage, nullptr, screenshot, nullptr);
103
    MSDL_FreeSurface(tmpImage);
104
105
    // Flip the screenshot, as OpenGL has 0,0 in bottom left
106
    const int h2 = h / 2;
107
    for (int i = 0; i < h2; i++)
108
    {
109
        GLubyte *const top = static_cast<GLubyte*>(
110
            screenshot->pixels) + lineSize * i;
111
        GLubyte *const bot = static_cast<GLubyte*>(
112
            screenshot->pixels) + lineSize * (h - 1 - i);
113
114
        memcpy(buf, top, lineSize);
115
        memcpy(top, bot, lineSize);
116
        memcpy(bot, buf, lineSize);
117
    }
118
119
    delete [] buf;
120
121
    if (config.getBoolValue("usefbo"))
122
        graphicsManager.deleteFBO(&mFbo);
123
124
    mglPixelStorei(GL_PACK_ALIGNMENT, pack);
125
126
    if (SDL_MUSTLOCK(screenshot))
127
        SDL_UnlockSurface(screenshot);
128
129
    return screenshot;
130
}
131
132
#endif  // defined(USE_OPENGL) && !defined(ANDROID)