Public feed
#379401
/*************************************************************************** * 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 <config.h>#endif#include <iostream>#include <cstdlib>#include <SDL.h>#include <SDL_image.h>using namespace std;SDL_Surface *screen;SDL_Surface *image;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 main(int argc, char *argv[]){ screen = NULL; image = NULL; cout <<"Initializing SDL." << endl; if(SDL_Init(SDL_INIT_VIDEO)< 0) { CSDLError( "Initializing SDL" ); } screen = SDL_SetVideoMode(1280,1024,16,SDL_SWSURFACE|SDL_DOUBLEBUF); if( screen == NULL ) { CSDLError( "Initializing Video" ); } image = IMG_Load( "/usr/local/share/cyle/tux.png" ); if( image == NULL ) { CSDLError("Loading Image"); } bool dirUp = true; int alpha = 0; while( true ) { if( dirUp ) { cerr << "inc: " << alpha << endl; if( ++alpha == 255 ) { dirUp = false; } } else { cerr << "dec: " << alpha << endl; if( --alpha == 0 ) { dirUp = true; } } DrawImage( image, 0, 0, image->w, image->h, screen, 100, 200, alpha ); SDL_Flip( screen ); SDL_Delay( (1000/30) ); } SDL_FreeSurface( image ); SDL_Quit();}