#include #include #include #include #define window_width 640 #define window_height 480 // Keydown booleans bool key[321]; // main screen SDL_Surface *screen; // Process pending events bool SDL_OGL_SaveScreenshot(const char *FileName); bool events() { SDL_Event event; if ( SDL_PollEvent(&event) ) { switch ( event.type ) { case SDL_KEYDOWN : key[ event.key.keysym.sym ]=true ; break; case SDL_KEYUP : key[ event.key.keysym.sym ]=false; break; case SDL_QUIT : return false; break; } } return true; } int main_loop_function() { float angle_x=0; float angle_y=0; float angle_z=0; bool TextureOn=true; GLuint texture; // Texture object handle SDL_Surface *surface; // Gives us the information to make the texture if ( (surface = SDL_LoadBMP("image.bmp")) ) { // Check that the image's width is a power of 2 if ( (surface->w & (surface->w - 1)) != 0 ) { printf("warning: image.bmp's width is not a power of 2\n"); } // Also check if the height is a power of 2 if ( (surface->h & (surface->h - 1)) != 0 ) { printf("warning: image.bmp's height is not a power of 2\n"); } // Have OpenGL generate a texture object handle for us glGenTextures( 1, &texture ); // Bind the texture object glBindTexture( GL_TEXTURE_2D, texture ); // Set the texture's stretching properties glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // Edit the texture object's image data using the information SDL_Surface gives us glTexImage2D( GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, surface->pixels ); } else { printf("SDL could not load image.bmp: %s\n", SDL_GetError()); SDL_Quit(); return 1; } SDL_SaveBMP(surface,"surface.bmp"); // Free the SDL_Surface only if it was successfully created if ( surface ) { SDL_FreeSurface( surface ); } while ( events() ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,0, -10); glRotatef(angle_x, 1, 0, 0); glRotatef(angle_y, 0, 1, 0); glRotatef(angle_z, 0, 0, 1); glBegin(GL_QUADS); glColor4f ( 1,0,0,0.5/*alpha[currentSheet]*/ );// Set the sprite's alpha // glColor3ub(255, 000, 000); glVertex3f(-1, 1, -1); // glColor3ub(000, 255, 000); glVertex3f( 1, 1, -1); // glColor3ub(000, 000, 255); glVertex3f( 1, -1, -1); // glColor3ub(255, 255, 000); glVertex3f(-1, -1, -1); glColor3ub(255, 000, 000); glVertex3f( 1, -1, 1); glColor3ub(000, 255, 000); glVertex3f( 1, 1, 1); glColor3ub(000, 000, 255); glVertex3f( 1, 1, -1); glColor3ub(255, 255, 000); glVertex3f( 1, -1, -1); glColor3ub(255, 000, 000); glVertex3f(-1, -1, 1); glColor3ub(000, 255, 000); glVertex3f(-1, 1, 1); glColor3ub(000, 000, 255); glVertex3f(-1, 1, -1); glColor3ub(255, 255, 000); glVertex3f(-1, -1, -1); glColor3ub(255, 000, 000); glVertex3f(-1, 1, 1); glColor3ub(000, 255, 000); glVertex3f( 1, 1, 1); glColor3ub(000, 000, 255); glVertex3f( 1, 1, -1); glColor3ub(255, 255, 000); glVertex3f(-1, 1, -1); glColor3ub(255, 000, 000); glVertex3f(-1, -1, 1); glColor3ub(000, 255, 000); glVertex3f( 1, -1, 1); glColor3ub(000, 000, 255); glVertex3f( 1, -1, -1); glColor3ub(255, 255, 000); glVertex3f(-1, -1, -1); glEnd(); glColor3ub(255, 255, 255); if(TextureOn) glEnable( GL_TEXTURE_2D ); if(TextureOn) glBindTexture( GL_TEXTURE_2D, texture ); glBegin(GL_QUADS); if(TextureOn) glTexCoord2i( 0, 0 ); else glColor3ub(255, 000, 000); glVertex3f(-1, 1, 1); if(TextureOn) glTexCoord2i( 1, 0 ); else glColor3ub(000, 255, 000); glVertex3f( 1, 1, 1); if(TextureOn) glTexCoord2i( 1, 1 ); else glColor3ub(000, 000, 255); glVertex3f( 1, -1, 1); if(TextureOn) glTexCoord2i( 0, 1 ); else glColor3ub(255, 255, 000); glVertex3f(-1, -1, 1); glEnd(); if(TextureOn) glDisable( GL_TEXTURE_2D ); glBegin(GL_QUADS); glColor3ub(255, 000, 000); glVertex3f(-1, 1, 1.1); glColor3ub(000, 255, 000); glVertex3f( 1, 1, 1.1); glColor3ub(000, 000, 255); glVertex3f( 1, -1, 1.1); glColor3ub(255, 255, 000); glVertex3f(-1, -1, 1.1); glEnd(); if ( key[SDLK_SPACE] ) { SDL_OGL_SaveScreenshot("toto.bmp"); printf("Screenshot taken\n"); } SDL_GL_SwapBuffers(); // Check keypresses if ( key[SDLK_d] ){ angle_x-=0.5; } if ( key[SDLK_q] ){ angle_x+=0.5; } if ( key[SDLK_z] ){ angle_y-=0.5; } if ( key[SDLK_s] ){ angle_y+=0.5; } if ( key[SDLK_a] ){ angle_z-=0.5; } if ( key[SDLK_e] ){ angle_z+=0.5; } if ( key[SDLK_TAB] ){ TextureOn=!TextureOn; } // if(TextureOn) printf("Texture:On\n"); // else printf("Texture:Off\n"); } return(0); } // Initialze OpenGL perspective matrix void GL_Setup(int width, int height) { glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glEnable( GL_DEPTH_TEST ); gluPerspective( 45, (float)width/height, 0.1, 100 ); glMatrixMode( GL_MODELVIEW ); } int main() { // Initialize SDL with best video mode SDL_Init(SDL_INIT_VIDEO); const SDL_VideoInfo* info = SDL_GetVideoInfo(); int vidFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER; if (info->hw_available) {vidFlags |= SDL_HWSURFACE;} else {vidFlags |= SDL_SWSURFACE;} int bpp = info->vfmt->BitsPerPixel; screen=SDL_SetVideoMode(window_width, window_height, bpp, vidFlags); GL_Setup(window_width, window_height); main_loop_function(); } bool SDL_OGL_SaveScreenshot(const char *FileName) { // Do you actually need this? // SDL_Surface *screen = SDL_CreateRGBSurface(SDL_SWSURFACE, // window_width, window_height, window.bpp, // 0x0000ff, 0x00ff00, 0xff0000, 0x000000); int N=4; // Create two arrays of unsigned bytes (chars). 4 bytes per pixel (RGBA) unsigned char *pixels = new unsigned char[window_width * window_height * N]; unsigned char *pixelsbuf = new unsigned char[window_width * window_height * N]; // Read the front buffer glReadPixels(0, 0, window_width, window_height, GL_BGRA /*GL_RGBA*/, GL_UNSIGNED_BYTE, pixelsbuf); // Copy lines of pixels from pixelsbuf to pixels, flipping the image at the same time. for(int i=0; i < window_height; ++i) memcpy(pixels+(window_height-i-1)*window_width*N, pixelsbuf+i*window_width*N, window_width*N); screen->pixels = pixels; SDL_SaveBMP(screen, FileName); // SDL_FreeSurface(screen); // screen = 0; delete pixels; delete pixelsbuf; pixels = 0; pixelsbuf = 0; return true; }