From: Sam Moore Date: Tue, 15 Apr 2014 17:19:33 +0000 (+0800) Subject: Screenshot using convoluted SDL2 approach X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=d1f4d742e6634fda7c8a1e018b4f9c8ec0f4e51c Screenshot using convoluted SDL2 approach David has just told me I should have used glReadPixels Stay tuned for another commit. --- diff --git a/.gitignore b/.gitignore index da6ac03..5041ffa 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ *.out *.err *.vcd +*.bmp diff --git a/bin/ipdf b/bin/ipdf new file mode 100755 index 0000000..f242971 Binary files /dev/null and b/bin/ipdf differ diff --git a/src/main.h b/src/main.h index 2346119..63c68b3 100644 --- a/src/main.h +++ b/src/main.h @@ -12,7 +12,7 @@ inline void MainLoop(Document & doc) { View view(doc); Screen scr; - scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) + scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf { static bool oldButtonDown = false; static int oldx, oldy; @@ -42,6 +42,7 @@ inline void MainLoop(Document & doc) } } ); + while (scr.PumpEvents()) { view.Render(); diff --git a/src/screen.cpp b/src/screen.cpp index 9ee15b4..6293fab 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -80,6 +80,16 @@ bool Screen::PumpEvents() m_mouse_handler(m_last_mouse_x, m_last_mouse_y, 0, evt.wheel.y); } break; + case SDL_KEYDOWN: + { + Debug("Key %c down", (char)evt.key.keysym.sym); + if (isalnum((char)evt.key.keysym.sym)) + { + char filename[] = "0.bmp"; + filename[0] = (char)evt.key.keysym.sym; + ScreenShot(filename); + } + } default: break; } @@ -109,3 +119,50 @@ void Screen::Present() SDL_GL_SwapWindow(m_window); } +void Screen::ScreenShot(const char * filename) const +{ + Debug("Attempting to save BMP to file %s", filename); + SDL_Surface * info = SDL_GetWindowSurface(m_window); + if (info == NULL) + { + Fatal("Failed to create info surface from m_window - %s", SDL_GetError()); + } + + unsigned num_pix = info->w * info->h * info->format->BytesPerPixel; + unsigned char * pixels = new unsigned char[num_pix]; + if (pixels == NULL) + { + Fatal("Failed to allocate %u pixel array - %s", num_pix, strerror(errno)); + } + + SDL_Renderer * renderer = SDL_GetRenderer(m_window); + if (renderer == NULL) + { + Fatal("Couldn't get renderer from m_window - %s", SDL_GetError()); + } + if (SDL_RenderReadPixels(renderer, &(info->clip_rect), info->format->format, pixels, info->w * info->format->BytesPerPixel) != 0) + { + Fatal("SDL_RenderReadPixels failed - %s", SDL_GetError()); + } + + // This line is disgusting + SDL_Surface * save = SDL_CreateRGBSurfaceFrom(pixels, info->w, info->h, info->format->BitsPerPixel, info->w * info->format->BytesPerPixel, + info->format->Rmask, info->format->Gmask, info->format->Bmask, info->format->Amask); + if (save == NULL) + { + Fatal("Couldn't create SDL_Surface from renderer pixel data - %s", SDL_GetError()); + } + if (SDL_SaveBMP(save, filename) != 0) + { + Fatal("SDL_SaveBMP to %s failed - %s", filename, SDL_GetError()); + } + + //SDL_DestroyRenderer(renderer); + SDL_FreeSurface(save); + SDL_FreeSurface(info); + delete [] pixels; + + Debug("Succeeded!"); + + +} diff --git a/src/screen.h b/src/screen.h index 4d92cb3..c8acbcf 100644 --- a/src/screen.h +++ b/src/screen.h @@ -43,6 +43,8 @@ namespace IPDF CursorHand }; void SetMouseCursor(MouseCursors cursor); + + void ScreenShot(const char * filename) const; private: void ResizeViewport(int width, int height); diff --git a/src/view.cpp b/src/view.cpp index 80f4986..ce7489d 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -79,5 +79,4 @@ void View::Render() glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h)); glEnd(); } - }