/
app.py
144 lines (111 loc) · 4.1 KB
/
app.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import pygame
import os
from resman import ResourceManager
from audman import AudioManager
on_android = False
try:
import android
on_android = True
except ImportError:
pass
class App():
def __init__(self, title, fullscreen=False,
resolutions=[(1024, 768), (960, 640), (854, 480), (800, 480), (640, 480), (480, 320)],
appstates=[], fps=30, version="0.1"):
global on_android
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
vidinfo = pygame.display.Info()
for r in resolutions:
if r[0] <= vidinfo.current_w and r[1] <= vidinfo.current_h:
break
self.screen_w, self.screen_h = r
if fullscreen:
pygame.display.set_mode((vidinfo.current_w, vidinfo.current_h))
self.fullscreen = pygame.display.get_surface()
self.screen = pygame.Surface((self.screen_w, self.screen_h))
self.x = 0
self.y = 0
self.b_fullscreen = True
else:
pygame.display.set_mode((self.screen_w, self.screen_h))
self.screen = pygame.display.get_surface()
self.b_fullscreen = False
pygame.display.set_caption(title)
self.is_running = True # used to trigger exit by ESC
# Map the back button to the escape key.
if on_android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
self.clock = pygame.time.Clock()
self._fps = fps
self.resman = ResourceManager(self)
self.audioman = AudioManager(self)
self._dirty_rects = []
self.init()
self.version = version
self._appstates = []
for asc in appstates:
s = asc(self)
self._appstates.append(s)
self.appstate = self._appstates[0]
def init(self):
pass
def run(self):
## Main Loop
while self.is_running:
self.process()
pygame.quit() # cleanup finally
def _get_appstate(self, s):
for astate in self._appstates:
if astate.__class__.__name__ == s:
return astate
def dirty(self, r):
"""AppStates call this when having drawed on app.screen somewhere"""
if r not in self._dirty_rects:
self._dirty_rects.append(r)
def process(self):
global on_android
self.clock.tick(self._fps)
# Android-specific:
if on_android:
if android.check_pause():
android.wait_for_resume()
self._dirty_rects = []
if not self.appstate:
raise Exception("AppState is None")
p = self.appstate.process()
if p:
next_state, state_arg = p
if next_state:
if next_state == "GoodBye":
self.is_running = False
else:
# appstate wants to change!
self.appstate = self._get_appstate(next_state)
self.appstate.resume(state_arg)
events = pygame.event.get()
for event in events:
try: # hack to enable fullscreen coordinates
pos_x, pos_y = event.pos
event.pos = tuple([pos_x - self.x, pos_y - self.y])
except:
#print "silly event:", event
pass
p = self.appstate.process_input(event)
# ESC quits app
if event.type == pygame.QUIT:
self.is_running = False
## DRAW
self.appstate.draw()
# write fps
if "font" in self.__dict__:
fps_surf = self.font.render("FPS: %2.2f" % self.clock.get_fps(),
False, (255, 255, 255), (0, 0, 0))
self.dirty(self.screen.blit(fps_surf, (0, 0)))
if self.b_fullscreen:
self.x = self.fullscreen.get_width() / 2 - self.screen.get_width() / 2
self.y = self.fullscreen.get_height() / 2 - self.screen.get_height() / 2
self.fullscreen.blit(self.screen, (self.x, self.y))
pygame.display.update(self._dirty_rects)
pygame.event.pump()