1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - screenshot.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2008 Richard42 *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28 #include <SDL_opengles2.h>
30 #include <SDL_opengl.h>
38 #define M64P_CORE_PROTOTYPES 1
39 #include "api/m64p_types.h"
40 #include "api/callbacks.h"
41 #include "api/m64p_config.h"
42 #include "api/config.h"
43 #include "main/main.h"
44 #include "main/util.h"
46 #include "osal/files.h"
47 #include "osal/preproc.h"
48 #include "plugin/plugin.h"
51 /*********************************************************************************************************
52 * PNG support functions for writing screenshot files
55 static void mupen_png_error(png_structp png_write, const char *message)
57 DebugMessage(M64MSG_ERROR, "PNG Error: %s", message);
60 static void mupen_png_warn(png_structp png_write, const char *message)
62 DebugMessage(M64MSG_WARNING, "PNG Warning: %s", message);
65 static void user_write_data(png_structp png_write, png_bytep data, png_size_t length)
67 FILE *fPtr = (FILE *) png_get_io_ptr(png_write);
68 if (fwrite(data, 1, length, fPtr) != length)
69 DebugMessage(M64MSG_ERROR, "Failed to write %zi bytes to screenshot file.", length);
72 static void user_flush_data(png_structp png_write)
74 FILE *fPtr = (FILE *) png_get_io_ptr(png_write);
78 /*********************************************************************************************************
79 * Other Local (static) functions
82 static int SaveRGBBufferToFile(const char *filename, const unsigned char *buf, int width, int height, int pitch)
86 // allocate PNG structures
87 png_structp png_write = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, mupen_png_error, mupen_png_warn);
90 DebugMessage(M64MSG_ERROR, "Error creating PNG write struct.");
93 png_infop png_info = png_create_info_struct(png_write);
96 png_destroy_write_struct(&png_write, (png_infopp)NULL);
97 DebugMessage(M64MSG_ERROR, "Error creating PNG info struct.");
101 if (setjmp(png_jmpbuf(png_write)))
103 png_destroy_write_struct(&png_write, &png_info);
104 DebugMessage(M64MSG_ERROR, "Error calling setjmp()");
107 // open the file to write
108 FILE *savefile = fopen(filename, "wb");
109 if (savefile == NULL)
111 DebugMessage(M64MSG_ERROR, "Error opening '%s' to save screenshot.", filename);
114 // set function pointers in the PNG library, for write callbacks
115 png_set_write_fn(png_write, (png_voidp) savefile, user_write_data, user_flush_data);
117 png_set_IHDR(png_write, png_info, width, height, 8, PNG_COLOR_TYPE_RGB,
118 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
119 // allocate row pointers and scale each row to 24-bit color
120 png_byte **row_pointers;
121 row_pointers = (png_byte **) malloc(height * sizeof(png_bytep));
122 for (i = 0; i < height; i++)
124 row_pointers[i] = (png_byte *) (buf + (height - 1 - i) * pitch);
126 // set the row pointers
127 png_set_rows(png_write, png_info, row_pointers);
128 // write the picture to disk
129 png_write_png(png_write, png_info, 0, NULL);
132 png_destroy_write_struct(&png_write, &png_info);
139 static int CurrentShotIndex;
141 static char *GetNextScreenshotPath(void)
143 char *ScreenshotPath;
144 char ScreenshotFileName[20 + 8 + 1];
146 // generate the base name of the screenshot
147 // add the ROM name, convert to lowercase, convert spaces to underscores
148 strcpy(ScreenshotFileName, ROM_PARAMS.headername);
149 for (char *pch = ScreenshotFileName; *pch != '\0'; pch++)
150 *pch = (*pch == ' ') ? '_' : tolower(*pch);
151 strcat(ScreenshotFileName, "-###.png");
153 // add the base path to the screenshot file name
154 const char *SshotDir = ConfigGetParamString(g_CoreConfig, "ScreenshotPath");
155 if (SshotDir == NULL || *SshotDir == '\0')
157 // note the trick to avoid an allocation. we add a NUL character
158 // instead of the separator, call mkdir, then add the separator
159 ScreenshotPath = formatstr("%sscreenshot%c%s", ConfigGetUserDataPath(), '\0', ScreenshotFileName);
160 if (ScreenshotPath == NULL)
162 osal_mkdirp(ScreenshotPath, 0700);
163 ScreenshotPath[strlen(ScreenshotPath)] = OSAL_DIR_SEPARATORS[0];
167 ScreenshotPath = combinepath(SshotDir, ScreenshotFileName);
168 if (ScreenshotPath == NULL)
172 // patch the number part of the name (the '###' part) until we find a free spot
173 char *NumberPtr = ScreenshotPath + strlen(ScreenshotPath) - 7;
174 for (; CurrentShotIndex < 1000; CurrentShotIndex++)
176 sprintf(NumberPtr, "%03i.png", CurrentShotIndex);
177 FILE *pFile = fopen(ScreenshotPath, "r");
183 if (CurrentShotIndex >= 1000)
185 DebugMessage(M64MSG_ERROR, "Can't save screenshot; folder already contains 1000 screenshots for this ROM");
190 return ScreenshotPath;
193 /*********************************************************************************************************
194 * Global screenshot functions
197 extern "C" void ScreenshotRomOpen(void)
199 CurrentShotIndex = 0;
202 extern "C" void TakeScreenshot(int iFrameNumber)
206 // look for an unused screenshot filename
207 filename = GetNextScreenshotPath();
208 if (filename == NULL)
211 // get the width and height
214 gfx.readScreen(NULL, &width, &height, 0);
216 // allocate memory for the image
217 unsigned char *pucFrame = (unsigned char *) malloc(width * height * 3);
218 if (pucFrame == NULL)
224 // grab the back image from OpenGL by calling the video plugin
225 gfx.readScreen(pucFrame, &width, &height, 0);
227 // write the image to a PNG
228 SaveRGBBufferToFile(filename, pucFrame, width, height, width * 3);
232 // print message -- this allows developers to capture frames and use them in the regression test
233 main_message(M64MSG_INFO, OSD_BOTTOM_LEFT, "Captured screenshot for frame %i.", iFrameNumber);