/*************************************************************************** * Copyright (C) 2007 by Cyle Riggs * * cyleriggs@gmail.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. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 1024 using namespace std; SDL_Surface *screen; SDL_Surface *image; SDL_Thread *eventThread; bool needToQuit; class CSDLError { public: CSDLError( string doingWhat ) { cerr << "SDL error while "+doingWhat+": " << SDL_GetError() << endl; SDL_Quit(); } }; void DrawImage(SDL_Surface *srcimg, int sx, int sy, int sw, int sh, SDL_Surface *dstimg, int dx, int dy, int alpha = 255) { if ((srcimg == NULL) || (alpha == 0)) return; //If theres no image, or its 100% transparent. SDL_Rect src, dst; src.x = sx; src.y = sy; src.w = sw; src.h = sh; dst.x = dx; dst.y = dy; dst.w = src.w; dst.h = src.h; if (alpha != 255) SDL_SetAlpha(srcimg, SDL_SRCALPHA, alpha); SDL_BlitSurface(srcimg, &src, dstimg, &dst); } int eventThreadFn(void*) { SDL_Event *event; while( SDL_WaitEvent(event) >= 0 ) { switch( event->type ) { case SDL_QUIT: needToQuit = true; return 0; break; } } } int main(int argc, char *argv[]) { needToQuit = false; screen = NULL; image = NULL; cout <<"Initializing SDL." << endl; if(SDL_Init(SDL_INIT_VIDEO)< 0) { CSDLError( "Initializing SDL" ); } screen = SDL_SetVideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,16,SDL_HWSURFACE|SDL_DOUBLEBUF); if( screen == NULL ) { CSDLError( "Initializing Video" ); } image = IMG_Load( "/usr/local/share/cyle/tux.png" ); if( image == NULL ) { CSDLError("Loading Image"); } eventThread = SDL_CreateThread( &eventThreadFn, NULL ); bool dirUp = true; int alpha = 0; int frameStart = 0; int frameEnd = 0; int waitTime = 0; const int incAmount = 20; const int decAmount = 20; const int frameLength = 100; int drawx = 0; int drawy = 0; while( true ) { frameStart = SDL_GetTicks(); DrawImage( image, 0, 0, image->w, image->h, screen, drawx, drawy, alpha ); SDL_Flip( screen ); frameEnd = SDL_GetTicks(); waitTime = frameLength - (frameEnd-frameStart); if( dirUp ) { cerr << "inc: " << alpha << endl; alpha += incAmount; if( alpha >= 255 ) { alpha = 255; dirUp = false; } } else { cerr << "dec: " << alpha << endl; alpha -= decAmount; if( alpha <= 0 ) { alpha = 0; dirUp = true; } } if( ++drawx + image->w >= WINDOW_WIDTH ) { drawx = 0; } if( ++drawy + image->h >= WINDOW_HEIGHT ) { drawy = 0; } cerr << "Frame took " << frameEnd - frameStart << " ms to draw ( " << frameStart << " , " << frameEnd << " )" << endl; cerr << "will wait " << waitTime << endl; if( needToQuit ) { cerr << "we've been asked to quit" << endl; break; } if( waitTime > 0 ) { SDL_Delay( waitTime ); } } SDL_KillThread( eventThread ); SDL_FreeSurface( image ); SDL_Quit(); }