ManaPlus
openglscreenshothelper.cpp
Go to the documentation of this file.
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 
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__
35 #endif // __native_client__
36 
37 #include "debug.h"
38 
41  mFbo()
42 {
43 }
44 
46 {
47 }
48 
50 {
51  if (config.getBoolValue("usefbo"))
54  &mFbo);
55 }
56 
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"))
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
bool getBoolValue(const std::string &key) const
static void deleteFBO(FBOInfo *const fbo)
static void createFBO(const int width, const int height, FBOInfo *const fbo)
int mWidth
Definition: graphics.h:484
int mHeight
Definition: graphics.h:485
Configuration config
#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a)
Definition: debug.h:55
Graphics * mainGraphics
Definition: graphics.cpp:109
const Image *restrict const top
GraphicsManager graphicsManager
#define mglReadPixels(...)
Definition: mgl.hpp:109
#define mglGetIntegerv(...)
Definition: mgl.hpp:95
#define mglPixelStorei(...)
Definition: mgl.hpp:107
bool screenshot(InputEvent &event)
Definition: actions.cpp:51