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