X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fscreen.cpp;h=e97995c5af67c79a0da4b2b4b00cb72e811eb7c5;hp=d924c064dab55236814daa60ada129374d45a9bb;hb=4e0ddbcec6e182430a31e0f1c661f2c93c9e0308;hpb=e6c66a8e58f1dda071e6fc4abed39afe49d348f8 diff --git a/src/screen.cpp b/src/screen.cpp index d924c06..e97995c 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -58,6 +58,8 @@ bool Screen::PumpEvents() } 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); @@ -65,11 +67,29 @@ bool Screen::PumpEvents() 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; } @@ -99,3 +119,26 @@ void Screen::Present() SDL_GL_SwapWindow(m_window); } +void Screen::ScreenShot(const char * filename) const +{ + Debug("Attempting to save BMP to file %s", filename); + + int w = ViewportWidth(); + int h = ViewportHeight(); + unsigned char * pixels = new unsigned char[w*h*4]; + if (pixels == NULL) + Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4); + + glReadPixels(0,0,w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + + SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0,0,0,0); + if (surf == NULL) + Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError()); + + if (SDL_SaveBMP(surf, filename) != 0) + Fatal("SDL_SaveBMP failed - %s", SDL_GetError()); + + SDL_FreeSurface(surf); + delete [] pixels; + Debug("Succeeded!"); +}