www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 1969ffee39e206fcd2c10d89ded15ae526efca13
parent da82332d0503042249384e06e05cdd9c6bb39a91
Author: Yoann <yoann.b87@voila.fr>
Date:   Fri, 20 Jan 2012 10:05:12 +0100

Ajout d'une fonction de screenshot (touche t).

Diffstat:
MMakefile | 2+-
Mall_includes.hh | 1+
Mview.cpp | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mview.hh | 3+++
4 files changed, 76 insertions(+), 1 deletion(-)

diff --git 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 @@ -16,6 +16,7 @@ class Chose; #include <queue> #include <SDL/SDL.h> +#include <SDL/SDL_image.h> #include <GL/glew.h> #include <GL/glu.h> #include <GL/gl.h> diff --git 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 @@ -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); };