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-2018 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 
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__
34 #endif // __native_client__
35 
36 #include "debug.h"
37 
40  mFbo()
41 {
42 }
43 
45 {
46 }
47 
49 {
50  if (config.getBoolValue("usefbo"))
53  &mFbo);
54 }
55 
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"))
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
int mWidth
Definition: graphics.h:483
Configuration config
bool screenshot(InputEvent &event)
Definition: actions.cpp:50
const Image *restrict const top
bool getBoolValue(const std::string &key) const
static void deleteFBO(FBOInfo *const fbo)
Graphics * mainGraphics
Definition: graphics.cpp:108
int mHeight
Definition: graphics.h:484
#define mglPixelStorei(...)
Definition: mgl.hpp:106
#define mglGetIntegerv(...)
Definition: mgl.hpp:94
#define mglReadPixels(...)
Definition: mgl.hpp:108
GraphicsManager graphicsManager
static void createFBO(const int width, const int height, FBOInfo *const fbo)
#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a)
Definition: debug.h:54