GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/openglscreenshothelper.cpp Lines: 0 34 0.0 %
Date: 2017-11-29 Branches: 0 24 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
#ifdef USE_OPENGL
24
25
#include "resources/openglscreenshothelper.h"
26
27
#include "configuration.h"
28
#include "graphicsmanager.h"
29
30
#include "render/graphics.h"
31
#include "render/opengl/mgl.h"
32
#ifdef __native_client__
33
#include "render/opengl/naclglfunctions.h"
34
#endif  // __native_client__
35
36
#include "debug.h"
37
38
OpenGLScreenshotHelper::OpenGLScreenshotHelper() :
39
    ScreenshotHelper(),
40
    mFbo()
41
{
42
}
43
44
OpenGLScreenshotHelper::~OpenGLScreenshotHelper()
45
{
46
}
47
48
void OpenGLScreenshotHelper::prepare()
49
{
50
    if (config.getBoolValue("usefbo"))
51
        graphicsManager.createFBO(mainGraphics->mWidth,
52
            mainGraphics->mHeight,
53
            &mFbo);
54
}
55
56
SDL_Surface *OpenGLScreenshotHelper::getScreenshot()
57
{
58
    const int h = mainGraphics->mHeight;
59
    const int w = mainGraphics->mWidth - (mainGraphics->mWidth % 4);
60
    GLint pack = 1;
61
62
    SDL_Surface *const screenshot = MSDL_CreateRGBSurface(
63
        SDL_SWSURFACE, w, h, 24,
64
        0xff0000, 0x00ff00, 0x0000ff, 0x000000);
65
66
    if (screenshot == nullptr)
67
        return nullptr;
68
69
    if (SDL_MUSTLOCK(screenshot))
70
        SDL_LockSurface(screenshot);
71
72
    const size_t lineSize = 3 * w;
73
    GLubyte *const buf = new GLubyte[lineSize];
74
75
    // Grap the pixel buffer and write it to the SDL surface
76
    mglGetIntegerv(GL_PACK_ALIGNMENT, &pack);
77
    mglPixelStorei(GL_PACK_ALIGNMENT, 1);
78
    mglReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, screenshot->pixels);
79
80
    // Flip the screenshot, as OpenGL has 0,0 in bottom left
81
    const int h2 = h / 2;
82
    for (int i = 0; i < h2; i++)
83
    {
84
        GLubyte *const top = static_cast<GLubyte*>(
85
            screenshot->pixels) + lineSize * i;
86
        GLubyte *const bot = static_cast<GLubyte*>(
87
            screenshot->pixels) + lineSize * (h - 1 - i);
88
89
        memcpy(buf, top, lineSize);
90
        memcpy(top, bot, lineSize);
91
        memcpy(bot, buf, lineSize);
92
    }
93
94
    delete [] buf;
95
96
    if (config.getBoolValue("usefbo"))
97
        graphicsManager.deleteFBO(&mFbo);
98
99
    mglPixelStorei(GL_PACK_ALIGNMENT, pack);
100
101
    if (SDL_MUSTLOCK(screenshot))
102
        SDL_UnlockSurface(screenshot);
103
104
    return screenshot;
105
}
106
107
#endif  // USE_OPENGL