00001 #include <SDL/SDL.h>
00002
00003 #include <dirent.h>
00004
00005 #include <vector>
00006 using namespace std;
00007
00008 #include "util.h"
00009 #include "random.h"
00010
00011 Uint32 getpixel(SDL_Surface *surface, int x, int y)
00012 {
00013 int bpp = surface->format->BytesPerPixel;
00014
00015 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00016
00017 switch(bpp) {
00018 case 1:
00019 return *p;
00020
00021 case 2:
00022 return *(Uint16 *)p;
00023
00024 case 3:
00025 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00026 return p[0] << 16 | p[1] << 8 | p[2];
00027 else
00028 return p[0] | p[1] << 8 | p[2] << 16;
00029
00030 case 4:
00031 return *(Uint32 *)p;
00032
00033 default:
00034 return 0;
00035 }
00036 }
00037
00038 string chooseRandomFile( string path, string fileType )
00039 {
00040 vector<string> files;
00041 DIR *d = opendir( path.c_str() );
00042 if ( !d )
00043 return "";
00044
00045 struct dirent *item = readdir( d );
00046 while ( item )
00047 {
00048 string file = string( path ) + item->d_name;
00049
00050
00051 int pos = file.find( ".", 1 ) + 1;
00052 string tmp = file.substr( pos );
00053 if ( tmp.size() > 0 && fileType.find( tmp ) != -1 )
00054 {
00055 files.push_back( file );
00056 }
00057 item = readdir( d );
00058 }
00059
00060 closedir( d );
00061 return files[nextInt( files.size() )];
00062 }
00063
00064
00065 string getHomeDir()
00066 {
00067 string home;
00068 #ifdef QWS
00069 home = getenv( "HOME" );
00070 #else
00071 home = ".";
00072 #endif
00073
00074 return home;
00075 }