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

This commit is contained in:
Yoann 2012-01-20 10:05:12 +01:00
parent da82332d05
commit 1969ffee39
4 changed files with 76 additions and 1 deletions

View File

@ -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

View File

@ -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>

View File

@ -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;
}

View File

@ -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);
};