diff --git a/Makefile b/Makefile index 277d210..6aa4fc2 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ CFLAGS=-O0 -g -rdynamic -I. $(CCWARN) SOURCES = $(shell echo *.cpp geometry/*.cpp rules/*.cpp) HEADERS = $(shell echo *.hh geometry/*.hh rules/*.hh) -LIBS = -lm -lGL -lGLU -lSDL -lGLEW +LIBS = -lm -lGL -lGLU -lSDL -lSDL_image -lGLEW EXECUTABLE = city .PHONY: all diff --git a/all_includes.hh b/all_includes.hh index 10c144b..32aa356 100644 --- a/all_includes.hh +++ b/all_includes.hh @@ -16,6 +16,7 @@ class Chose; #include #include +#include #include #include #include diff --git a/view.cpp b/view.cpp index 2816123..67c16a1 100644 --- a/view.cpp +++ b/view.cpp @@ -274,6 +274,9 @@ void Camera::keyboard(const SDL_KeyboardEvent &eventKey) { if (eventKey.type != SDL_KEYDOWN) break; std::cout << *this << std::endl; break; + case 't': + takeScreenshot("123.bmp"); + break; default: break; } @@ -297,3 +300,71 @@ void Camera::animation(int elapsedTime) { if(pageDown) cameraCenter = cameraCenter + Vertex::fromSpherical(diff, yAngle + 90, xAngle); } + + + + +SDL_Surface * flipSurface(SDL_Surface * surface) { + int current_line,pitch; + SDL_Surface * fliped_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, + surface->w,surface->h, + surface->format->BitsPerPixel, + surface->format->Rmask, + surface->format->Gmask, + surface->format->Bmask, + surface->format->Amask); + + SDL_LockSurface(surface); + SDL_LockSurface(fliped_surface); + + pitch = surface->pitch; + for (current_line = 0; current_line < surface->h; current_line ++) { + memcpy(&((unsigned char* )fliped_surface->pixels)[current_line*pitch], + &((unsigned char* )surface->pixels)[(surface->h - 1 - + current_line)*pitch], + pitch); + } + + SDL_UnlockSurface(fliped_surface); + SDL_UnlockSurface(surface); + return fliped_surface; +} + +int Camera::takeScreenshot(const char * filename) { + GLint viewport[4]; + Uint32 rmask, gmask, bmask, amask; + SDL_Surface * picture, * finalpicture; + + glGetIntegerv(GL_VIEWPORT, viewport); + + #if SDL_BYTEORDER == SDL_BIG_ENDIAN + + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; + #else + + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; + #endif + + picture = SDL_CreateRGBSurface(SDL_SWSURFACE,viewport[2],viewport[3], 32, + rmask, gmask, bmask, amask); + SDL_LockSurface(picture); + glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3],GL_RGBA, + GL_UNSIGNED_BYTE,picture->pixels); + SDL_UnlockSurface(picture); + + finalpicture = flipSurface(picture); + + if (SDL_SaveBMP(finalpicture, filename)) { + exit(1); + } + SDL_FreeSurface(finalpicture); + SDL_FreeSurface(picture); + + return 0; +} diff --git a/view.hh b/view.hh index 8b382b6..ead2a5b 100644 --- a/view.hh +++ b/view.hh @@ -28,6 +28,9 @@ public : void animation(int elapsedTime); std::ostream& print(std::ostream& os) const; friend std::ostream& operator<<(std::ostream& os, const Camera& c) { return c.print(os); } + + private : + int takeScreenshot(const char * filename); };