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