📄 sdlhandler.cpp
字号:
/*************************************************************************** sdlhandler.cpp - description ------------------- begin : Sat May 3 2003 copyright : (C) 2003 by Gabor Torok email : cctorok@yahoo.com ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include "sdlhandler.h"SDLHandler::SDLHandler(){ /* These are to calculate our fps */ T0 = 0; Frames = 0; fps = 0; screen = NULL; mouseX = mouseY = mouseButton = mouseEvent = 0; mouseDragging = false; text = NULL; handlerCount = 0; invertMouse = false; cursorMode = CURSOR_NORMAL; our_font_initialized = false;}SDLHandler::~SDLHandler(){}void SDLHandler::pushHandlers(SDLEventHandler *eventHandler, SDLScreenView *screenView) { if(handlerCount == 10) { fprintf(stderr, "Error: can't push any more handlers."); exit(1); } eventHandlers[handlerCount] = this->eventHandler; screenViews[handlerCount] = this->screenView; handlerCount++; setHandlers(eventHandler, screenView);}bool SDLHandler::popHandlers() { if(handlerCount == 0) return true; handlerCount--; setHandlers(eventHandlers[handlerCount], screenViews[handlerCount]); return false;}void SDLHandler::setHandlers(SDLEventHandler *eventHandler, SDLScreenView *screenView) { this->eventHandler = eventHandler; this->screenView = screenView; if(screen) resizeWindow( screen->w, screen->h );}/* function to release/destroy our resources and restoring the old desktop */void SDLHandler::quit( int returnCode ) { /* clean up the window */ SDL_Quit( ); /* and exit appropriately */ exit( returnCode );}/* function to reset our viewport after a window resize */int SDLHandler::resizeWindow( int width, int height ) { // Height / width ratio// GLfloat ratio; // Protect against a divide by zero if ( height == 0 ) height = 1; this->lastWidth = width; this->lastHeight = height; // ratio = ( GLfloat )width / ( GLfloat )height; // Setup our viewport. glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height ); // change to the projection matrix and set our viewing volume. glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); // Set our perspective //gluPerspective( 45.0f, ratio, 0.1f, 100.0f ); setOrthoView(); // Make sure we're chaning the model view and not the projection glMatrixMode( GL_MODELVIEW ); // Reset The View glLoadIdentity( ); return( TRUE );}void SDLHandler::setOrthoView() { glOrtho(0.0f, lastWidth, lastHeight, 0.0f, -1000.0f, 1000.0f);}/* general OpenGL initialization function */int SDLHandler::initGL( GLvoid ) { shapePal = new ShapePalette( ); /* Enable Texture Mapping */ glEnable( GL_TEXTURE_2D ); /* Enable smooth shading */ glShadeModel( GL_SMOOTH ); // which one to use? // default is good //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /* Set the background black */ glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); /* Depth buffer setup */ glClearDepth( 1.0f ); glClearStencil(0); // Clear The Stencil Buffer To 0 /* Enables Depth Testing */ glEnable( GL_DEPTH_TEST ); /* The Type Of Depth Test To Do */ glDepthFunc( GL_LEQUAL ); /* Really Nice Perspective Calculations */// glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); glColorMaterial ( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE ) ; glEnable( GL_COLOR_MATERIAL ); /* initialize opengl extensions */ if(Constants::multitexture) { fprintf(stderr, "BEFORE: glSDLActiveTextureARB=%u\n", glSDLActiveTextureARB); glSDLActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress ("glActiveTextureARB"); fprintf(stderr, "AFTER: glSDLActiveTextureARB=%u\n", glSDLActiveTextureARB); glSDLMultiTexCoord2fARB = (PFNGLMULTITEXCOORD2FARBPROC)SDL_GL_GetProcAddress ("glMultiTexCoord2fARB"); glSDLMultiTexCoord2iARB = (PFNGLMULTITEXCOORD2IARBPROC)SDL_GL_GetProcAddress ("glMultiTexCoord2iARB"); } return( TRUE );}bool testModesInFormat(SDL_PixelFormat *format, Uint32 flags) { SDL_Rect **modes; int i; printf("Available modes with given flags in %d bpp:\n", format->BitsPerPixel); /* Get available fullscreen/hardware modes */ modes=SDL_ListModes(format, flags); /* Check is there are any modes available */ if(modes == (SDL_Rect **)0){ printf("\tNo modes available!\n"); return false; } /* Check if our resolution is restricted */ if(modes == (SDL_Rect **)-1){ printf("\tAll resolutions available.\n"); return true; } /* Print valid modes */ for(i=0;modes[i];++i) printf("\t%d x %d\n", modes[i]->w, modes[i]->h); //free(modes); // crashes; ok to not free since we only do this a few times return true;}int testModes(Uint32 flags, bool findMaxBpp=false) { int bpp[] = { 32, 24, 16, 15, 8, 0 }; SDL_PixelFormat format; for(int i = 0; bpp[i]; i++) { format.BitsPerPixel=bpp[i]; if(testModesInFormat(&format, flags) && findMaxBpp) return bpp[i]; } return -1;}char ** SDLHandler::getVideoModes(int &nbModes){ SDL_Rect **modes; char ** modesDescription; char temp [50]; Uint32 flags; int i; if(!screen){ fprintf(stderr, "SDLHandler :: you must allocate screen before calling getVideoModes!!\n"); exit(-1); } // Get current video flags (hwsurface/swsurface, fullscreen/not fullscreen..) flags = screen->flags; // Get available modes for the current flags modes = SDL_ListModes(NULL, flags); // Copy them to a char array if(modes != (SDL_Rect **)0){ nbModes = 0; if(modes == (SDL_Rect **)-1) { // All modes are available, so let's go.. nbModes = 16; modesDescription = (char **) malloc (nbModes * sizeof(char *)); modesDescription[0] = strdup(" 320 x 200"); modesDescription[1] = strdup(" 320 x 240"); modesDescription[2] = strdup(" 400 x 300"); modesDescription[3] = strdup(" 512 x 384"); modesDescription[4] = strdup(" 640 x 480"); modesDescription[5] = strdup(" 800 x 600"); modesDescription[6] = strdup(" 848 x 480"); modesDescription[7] = strdup(" 1024 x 768"); modesDescription[8] = strdup(" 1152 x 864"); modesDescription[9] = strdup(" 1280 x 720"); modesDescription[10] = strdup(" 1280 x 768"); modesDescription[11] = strdup(" 1280 x 960"); modesDescription[12] = strdup("1280 x 1024"); modesDescription[13] = strdup(" 1360 x 768"); modesDescription[14] = strdup("1600 x 1024"); modesDescription[15] = strdup("1600 x 1200"); } else{ // Only a few modes available, which ones ? for(i=0;modes[i];++i){} nbModes = i - 1; modesDescription = (char **) malloc (nbModes * sizeof(char *)); for(i=0;modes[i];++i){ sprintf(temp, "%d x %d", modes[i]->w, modes[i]->h); modesDescription[i] = strdup(temp); } } } else{ nbModes = 1; modesDescription = (char **) malloc (sizeof(char *)); modesDescription[0] = strdup("No modes available!\n"); } return modesDescription;}void SDLHandler::setVideoMode( UserConfiguration * uc ) { /* this holds some info about our display */ const SDL_VideoInfo *videoInfo; /* initialize SDL */ if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { fprintf( stderr, "Video initialization failed: %s\n", SDL_GetError( ) ); quit( 1 ); } /* Fetch the video info */ videoInfo = SDL_GetVideoInfo( ); if ( !videoInfo ) { fprintf( stderr, "Video query failed: %s\n", SDL_GetError( ) ); quit( 1 ); } /* the flags to pass to SDL_SetVideoMode */ videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */ if(uc->getDoublebuf()) videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */ if(uc->getHwpal()) videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */ if(uc->getResizeable()) videoFlags |= SDL_RESIZABLE; /* Enable window resizing */ /* This checks to see if surfaces can be stored in memory */ if(uc->getForce_hwsurf()) videoFlags |= SDL_HWSURFACE; else if(uc->getForce_swsurf()) videoFlags |= SDL_SWSURFACE; else { if ( videoInfo->hw_available ) videoFlags |= SDL_HWSURFACE; else videoFlags |= SDL_SWSURFACE; } /* This checks if hardware blits can be done */ if ( uc->getHwaccel() && videoInfo->blit_hw ) videoFlags |= SDL_HWACCEL; if(uc->getFullscreen()) videoFlags |= SDL_FULLSCREEN; if(uc->getTest()) { testModes(videoFlags); quit(0); } // try to find the highest bpp for this mode int bpp; if(uc->getBpp() == -1) { bpp = testModes(videoFlags, true); if(bpp == -1) { fprintf(stderr, "Could not detect suitable opengl video mode.\n"); fprintf(stderr, "You can manually select one with the -bpp option\n"); quit(0); } else{ uc->setBpp(bpp); } } /* Sets up OpenGL double buffering */ if(uc->getDoublebuf()) SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); if(uc->getStencilbuf()) { uc->setStencilBufInitialized(true); SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -