Screenshot using convoluted SDL2 approach
authorSam Moore <[email protected]>
Tue, 15 Apr 2014 17:19:33 +0000 (01:19 +0800)
committerSam Moore <[email protected]>
Tue, 15 Apr 2014 17:19:33 +0000 (01:19 +0800)
David has just told me I should have used glReadPixels
Stay tuned for another commit.

.gitignore
bin/ipdf [new file with mode: 0755]
src/main.h
src/screen.cpp
src/screen.h
src/view.cpp

index da6ac03..5041ffa 100644 (file)
@@ -6,3 +6,4 @@
 *.out
 *.err
 *.vcd
+*.bmp
diff --git a/bin/ipdf b/bin/ipdf
new file mode 100755 (executable)
index 0000000..f242971
Binary files /dev/null and b/bin/ipdf differ
index 2346119..63c68b3 100644 (file)
@@ -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();
index 9ee15b4..6293fab 100644 (file)
@@ -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!");
+
+
+}
index 4d92cb3..c8acbcf 100644 (file)
@@ -43,6 +43,8 @@ namespace IPDF
                        CursorHand
                };
                void SetMouseCursor(MouseCursors cursor);
+
+               void ScreenShot(const char * filename) const;
        private:
                void ResizeViewport(int width, int height);
                
index 80f4986..ce7489d 100644 (file)
@@ -79,5 +79,4 @@ void View::Render()
                glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
                glEnd();
        }
-
 }

UCC git Repository :: git.ucc.asn.au