fixed screenshot crash when windows is resized
This commit is contained in:
parent
84cb01d733
commit
67b0c4da5e
3 changed files with 15 additions and 11 deletions
19
src/global.c
19
src/global.c
|
@ -168,12 +168,15 @@ void take_screenshot(void)
|
|||
char outfile[128], *outpath;
|
||||
time_t rawtime;
|
||||
struct tm * timeinfo;
|
||||
int w, h;
|
||||
int w, h, rw, rh;
|
||||
|
||||
w = video.current.width;
|
||||
h = video.current.height;
|
||||
|
||||
data = malloc(3 * w * h);
|
||||
rw = video.real.width;
|
||||
rh = video.real.height;
|
||||
|
||||
data = malloc(3 * rw * rh);
|
||||
|
||||
time(&rawtime);
|
||||
timeinfo = localtime(&rawtime);
|
||||
|
@ -187,20 +190,18 @@ void take_screenshot(void)
|
|||
out = fopen(outpath, "wb");
|
||||
free(outpath);
|
||||
|
||||
if(!out)
|
||||
{
|
||||
if(!out) {
|
||||
perror("fopen");
|
||||
free(data);
|
||||
return;
|
||||
}
|
||||
|
||||
glReadBuffer(GL_FRONT);
|
||||
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
glReadPixels(0, 0, rw, rh, GL_RGB, GL_UNSIGNED_BYTE, data);
|
||||
|
||||
png_structp png_ptr;
|
||||
png_infop info_ptr;
|
||||
png_byte **row_pointers;
|
||||
int y;
|
||||
|
||||
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
info_ptr = png_create_info_struct (png_ptr);
|
||||
|
@ -210,17 +211,17 @@ void take_screenshot(void)
|
|||
|
||||
row_pointers = png_malloc(png_ptr, h*sizeof(png_byte *));
|
||||
|
||||
for(y = 0; y < h; y++) {
|
||||
for(int y = 0; y < h; y++) {
|
||||
row_pointers[y] = png_malloc(png_ptr, 8*3*w);
|
||||
|
||||
memcpy(row_pointers[y], data + w*3*(h-1-y), w*3);
|
||||
memcpy(row_pointers[y], data + rw*3*(h-1-y), w*3);
|
||||
}
|
||||
|
||||
png_init_io(png_ptr, out);
|
||||
png_set_rows(png_ptr, info_ptr, row_pointers);
|
||||
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
|
||||
|
||||
for(y = 0; y < h; y++)
|
||||
for(int y = 0; y < h; y++)
|
||||
png_free(png_ptr, row_pointers[y]);
|
||||
|
||||
png_free(png_ptr, row_pointers);
|
||||
|
|
|
@ -107,6 +107,8 @@ static void _video_setmode(int w, int h, int fs, int fallback) {
|
|||
|
||||
video_update_vsync();
|
||||
SDL_GL_GetDrawableSize(video.window, &video.current.width, &video.current.height);
|
||||
video.real.width = video.current.width;
|
||||
video.real.height = video.current.height;
|
||||
video_set_viewport();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -22,8 +22,9 @@ typedef struct {
|
|||
int mcount;
|
||||
VideoMode intended;
|
||||
VideoMode current;
|
||||
SDL_Window *window;
|
||||
SDL_GLContext *glcontext;
|
||||
VideoMode real;
|
||||
SDL_Window *window;
|
||||
SDL_GLContext *glcontext;
|
||||
} Video;
|
||||
|
||||
Video video;
|
||||
|
|
Loading…
Reference in a new issue