Mercurial > skaapsteker
annotate skaapsteker/data.py @ 601:73bdeb3d3f7e pyweek12
Bump version to 0.2
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 10 Apr 2011 20:07:19 +0200 |
parents | 2258c2a6dbae |
children |
rev | line source |
---|---|
0 | 1 '''Simple data loader module. |
2 | |
3 Loads data files from the "data" directory shipped with a game. | |
4 | |
5 Enhancing this to handle caching etc. is left as an exercise for the reader. | |
6 | |
7 Note that pyglet users should probably just add the data directory to the | |
8 pyglet.resource search path. | |
9 ''' | |
10 | |
11 import os | |
12 | |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
13 import pygame.image |
58
04e83cf43329
Add convert_alpha to load_image
Neil Muller <drnlmuller@gmail.com>
parents:
8
diff
changeset
|
14 import pygame.display |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
15 |
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
16 |
0 | 17 data_py = os.path.abspath(os.path.dirname(__file__)) |
18 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data')) | |
19 | |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
20 |
0 | 21 def filepath(filename): |
22 '''Determine the path to a file in the data directory. | |
23 ''' | |
205
466147799786
Add support for Windows in data loading.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
24 filename = os.path.join(*filename.split('/')) |
0 | 25 return os.path.join(data_dir, filename) |
26 | |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
27 |
0 | 28 def load(filename, mode='rb'): |
29 '''Open a file in the data directory. | |
30 | |
31 "mode" is passed as the second arg to open(). | |
32 ''' | |
205
466147799786
Add support for Windows in data loading.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
33 return open(filepath(filename), mode) |
0 | 34 |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
35 |
343
2258c2a6dbae
Cache get_files() stuff to avoid unnecessary filesystem operations.
Jeremy Thurgood <firxen@gmail.com>
parents:
205
diff
changeset
|
36 DIR_LISTS = {} |
2258c2a6dbae
Cache get_files() stuff to avoid unnecessary filesystem operations.
Jeremy Thurgood <firxen@gmail.com>
parents:
205
diff
changeset
|
37 |
205
466147799786
Add support for Windows in data loading.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
38 def get_files(folder): |
88
bf4adb40ca3c
Rough outline of a player object, completely with statuesque flying fox
Neil Muller <drnlmuller@gmail.com>
parents:
58
diff
changeset
|
39 """Return a sorted list of files in the given directory, with the directory stripped""" |
343
2258c2a6dbae
Cache get_files() stuff to avoid unnecessary filesystem operations.
Jeremy Thurgood <firxen@gmail.com>
parents:
205
diff
changeset
|
40 if folder not in DIR_LISTS: |
2258c2a6dbae
Cache get_files() stuff to avoid unnecessary filesystem operations.
Jeremy Thurgood <firxen@gmail.com>
parents:
205
diff
changeset
|
41 DIR_LISTS[folder] = sorted(os.listdir(filepath(folder))) |
2258c2a6dbae
Cache get_files() stuff to avoid unnecessary filesystem operations.
Jeremy Thurgood <firxen@gmail.com>
parents:
205
diff
changeset
|
42 return DIR_LISTS[folder] |
88
bf4adb40ca3c
Rough outline of a player object, completely with statuesque flying fox
Neil Muller <drnlmuller@gmail.com>
parents:
58
diff
changeset
|
43 |
bf4adb40ca3c
Rough outline of a player object, completely with statuesque flying fox
Neil Muller <drnlmuller@gmail.com>
parents:
58
diff
changeset
|
44 |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
45 IMAGES = {} |
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
46 |
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
47 def load_image(filename): |
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
48 if filename not in IMAGES: |
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
49 IMAGES[filename] = pygame.image.load(load(filename)) |
58
04e83cf43329
Add convert_alpha to load_image
Neil Muller <drnlmuller@gmail.com>
parents:
8
diff
changeset
|
50 if pygame.display.get_init(): |
04e83cf43329
Add convert_alpha to load_image
Neil Muller <drnlmuller@gmail.com>
parents:
8
diff
changeset
|
51 IMAGES[filename] = IMAGES[filename].convert_alpha(pygame.display.get_surface()) |
8
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
52 return IMAGES[filename] |
addaa5b857d6
Some level display things.
Jeremy Thurgood <firxen@gmail.com>
parents:
0
diff
changeset
|
53 |