Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/usmpgames/application.py
blob: 7871ee8eda58eaf182321e65a2d34d3e13c6d78f (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
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
145
146
147
148
149
150
151
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import pygame
import olpcgames
import constants

class Application():

    _instance = None
    _resources = {}

    @staticmethod
    def instance():
        return Application._instance

    @staticmethod
    def get_resource_background(name):
        if name is None:
            return None
        if not Application._resources.has_key(name) :
            size = constants.screen_size
            if olpcgames.ACTIVITY:
                size = olpcgames.ACTIVITY.game_size
            Application._resources[name] = pygame.transform.scale( pygame.image.load(name), size)
        return Application._resources[name]

    def __init__(self):
        self._state_stack = []
        self._cookie_stack = []
        self._current_state = None
        self._current_cookie = None
        self._running = True
        self._fps = 25
        if Application._instance is None:
            Application._instance = self
            
    def set_screen(self,  screen):
        self._screen = screen
        
    def screen(self):
        return self._screen
            
    def push_state(self,  new_state, new_cookie = None):
        # exit from current state
        if self._current_state is not None:
                self._current_state.exiting_state( True )
                if new_state is not None:
                    self._state_stack.append(self._current_state)
                    self._cookie_stack.append(self._current_state.cookie())
        
        # process new state
        fromStack = False
        if new_state is None:
            if len(self._state_stack) == 0 :
                self.set_running( False )
                return
            else :
                self._current_state = self.pop_state()
                self._current_cookie = self._current_state.cookie()
                fromStack = True
        else:
            self._current_state = new_state
            self._current_cookie = new_cookie

        # entering new state    
        if self._current_state is None:
            self.set_running(False)
        else:
            self._current_state.set_screen(self.screen())
            self._current_state.entering_state(fromStack, self._current_cookie)
    
    def change_state(self,  new_state, new_cookie):
        # exit from current state
        if self._current_state is not None:        
            self._current_state.exiting_state(False)
            
        # process current state
        if new_state is None:
            if len(self._state_stack) == 0 :
                self.set_running( False )
                return
            else :
                self.pop_state()
		return
        else:
            self._current_state = new_state
            self._current_cookie = new_cookie
        
        # entering new state
        if self._current_state is not None:        
            self._current_state.set_screen(self.screen())
            self._current_state.entering_state( False, self._current_cookie )
        else:
            self.set_running(False)
            
        # entering new state    
        
    def pop_state(self):
        if self._current_state is not None:        
            self._current_state.exiting_state( False )
        if len(self._state_stack) >= 1:
            self._current_state = self._state_stack.pop()
            self._current_cookie = self._current_state.cookie()
        else:
            self._current_state = None
            self._current_cookie = None
        if self._current_state is not None:
            self._current_state.set_screen(self.screen())
            self._current_state.entering_state( True, self._current_cookie )

    def current_state(self):
        return self._current_state
        
    def running(self):
        if not self._running:
           return False
        if self.current_state() is None:
            return False
        state_running = self.current_state().running()
        if not state_running:
            self.pop_state()
            return self.running()
        return True
        
    def set_running(self,  new_value):
        self._running = new_value
        
    def set_fps(self,  new_value):
        self._fps = new_value
        
    def fps(self):
        return self._fps

    def initialize(self):
        pygame.init()
        
    def shutdown(self):
        pygame.quit()
        
    def runLoop(self):
        self.initialize()
        clock = pygame.time.Clock()
        while self.running() :
            ms = clock.tick( self.fps() )
            #try:
            self.current_state().loop(ms)
            #except:
            #    print "Unexpected error:", sys.exc_info()[0]
        self.shutdown()