summaryrefslogtreecommitdiff
path: root/noncore/games/sfcave-sdl/util.cpp
blob: 743f16e2447c8b5bc56d16fae1bebe36bc749b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <SDL/SDL.h>

#include <dirent.h>

#include <vector>
using namespace std;

#include "util.h"
#include "random.h"

Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
    int bpp = surface->format->BytesPerPixel;
    /* Here p is the address to the pixel we want to retrieve */
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

    switch(bpp) {
    case 1:
        return *p;

    case 2:
        return *(Uint16 *)p;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
            return p[0] << 16 | p[1] << 8 | p[2];
        else
            return p[0] | p[1] << 8 | p[2] << 16;

    case 4:
        return *(Uint32 *)p;

    default:
        return 0;       /* shouldn't happen, but avoids warnings */
    }
}

string chooseRandomFile( string path, string fileType )
{
    vector<string> files;
    DIR *d = opendir( path.c_str() );
    if ( !d )
        return "";

    struct dirent *item = readdir( d );
    while ( item )
    {
        string file = string( path ) + item->d_name;

        // Rip extension from file
        int pos = file.find( ".", 1 ) + 1;
        string tmp = file.substr( pos );
        if ( tmp.size() > 0 && fileType.find( tmp ) != -1 )
        {
            files.push_back( file );
        }
        item = readdir( d );
    }

    closedir( d );
    return files[nextInt( files.size() )];
}


string getHomeDir()
{
	string home;
#ifdef QWS
	home = getenv( "HOME" );
#else
	home = ".";
#endif

	return home;
}