summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/util.cpp
Unidiff
Diffstat (limited to 'noncore/games/sfcave-sdl/util.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/sfcave-sdl/util.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/util.cpp b/noncore/games/sfcave-sdl/util.cpp
new file mode 100644
index 0000000..86738ad
--- a/dev/null
+++ b/noncore/games/sfcave-sdl/util.cpp
@@ -0,0 +1,64 @@
1#include "SDL.h"
2
3#include <dirent.h>
4
5#include <vector>
6using namespace std;
7
8#include "util.h"
9#include "random.h"
10
11Uint32 getpixel(SDL_Surface *surface, int x, int y)
12{
13 int bpp = surface->format->BytesPerPixel;
14 /* Here p is the address to the pixel we want to retrieve */
15 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
16
17 switch(bpp) {
18 case 1:
19 return *p;
20
21 case 2:
22 return *(Uint16 *)p;
23
24 case 3:
25 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
26 return p[0] << 16 | p[1] << 8 | p[2];
27 else
28 return p[0] | p[1] << 8 | p[2] << 16;
29
30 case 4:
31 return *(Uint32 *)p;
32
33 default:
34 return 0; /* shouldn't happen, but avoids warnings */
35 }
36}
37
38const char *chooseRandomFile( string path, string fileType )
39{
40 vector<string> files;
41 DIR *d = opendir( path.c_str() );
42 if ( !d )
43 return "";
44
45 struct dirent *item = readdir( d );
46 while ( item )
47 {
48 string file = string( path ) + item->d_name;
49
50 // Rip extension from file
51 int pos = file.find( ".", 1 ) + 1;
52 string tmp = file.substr( pos );
53 printf( "pos = %d, tmp =%s\n", pos, tmp.c_str() );
54 if ( tmp.size() > 0 && fileType.find( tmp ) != -1 )
55 {
56 printf( "Matching <%s> - %s with <%s>\n", file.substr( pos ).c_str(), file.c_str(), fileType.c_str() );
57 files.push_back( file );
58 }
59 item = readdir( d );
60 }
61
62 closedir( d );
63 return files[nextInt( files.size() )].c_str();
64}