Screenshot using convoluted SDL2 approach
[ipdf/code.git] / src / screen.cpp
index 6909be2..6293fab 100644 (file)
@@ -18,6 +18,8 @@ Screen::Screen()
        }
 
        m_gl_context = SDL_GL_CreateContext(m_window);
+       
+       ResizeViewport(800, 600);
 
 }
 
@@ -55,6 +57,39 @@ bool Screen::PumpEvents()
                                break;
                        }
                        break;
+               case SDL_MOUSEMOTION:
+                       m_last_mouse_x = evt.motion.x;
+                       m_last_mouse_y = evt.motion.y;
+                       if (m_mouse_handler)
+                       {
+                               m_mouse_handler(evt.motion.x, evt.motion.y,evt.motion.state, 0);
+                       }
+                       break;
+               case SDL_MOUSEBUTTONDOWN:
+               case SDL_MOUSEBUTTONUP:
+                       m_last_mouse_x = evt.button.x;
+                       m_last_mouse_y = evt.button.y;
+                       if (m_mouse_handler)
+                       {
+                               m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0);
+                       }
+                       break;
+               case SDL_MOUSEWHEEL:
+                       if (m_mouse_handler)
+                       {
+                               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;
                }
@@ -62,8 +97,72 @@ bool Screen::PumpEvents()
        return no_quit_requested;
 }
 
+void Screen::SetMouseCursor(Screen::MouseCursors cursor)
+{
+       SDL_SystemCursor system_cursor_id = SDL_SYSTEM_CURSOR_ARROW;
+       switch (cursor)
+       {
+       case CursorArrow: system_cursor_id = SDL_SYSTEM_CURSOR_ARROW; break;
+       case CursorWait: system_cursor_id = SDL_SYSTEM_CURSOR_WAIT; break;
+       case CursorWaitArrow: system_cursor_id = SDL_SYSTEM_CURSOR_WAITARROW; break;
+       case CursorMove: system_cursor_id = SDL_SYSTEM_CURSOR_SIZEALL; break;
+       case CursorHand: system_cursor_id = SDL_SYSTEM_CURSOR_HAND; break;
+       default: break;
+       }
+       SDL_Cursor *system_cursor = SDL_CreateSystemCursor(system_cursor_id);
+       SDL_SetCursor(system_cursor);
+       //TODO: Check if we need to free the system cursors.
+}
+
 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!");
+
+
+}

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