File: gameinit.py

package info (click to toggle)
solarwolf 1.5%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,656 kB
  • sloc: python: 5,353; ansic: 159; makefile: 102; pascal: 50; sh: 27
file content (181 lines) | stat: -rw-r--r-- 5,684 bytes parent folder | download | duplicates (3)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"gameinit handler. splash and load resources"

import pygame, pygame.draw
from pygame.locals import *
import sys, threading
import game, gfx, snd, txt, input

load_total = 0
load_current = 0

load_finished_status = 0
load_finished_message = ''
load_finished_module = ''
load_finished_type = ''

def loadresources():
    global load_total, load_current
    hunt = 'load_game_resources'
    load_total = 0
    load_current = 0
    allmods = sys.modules.values()
    funcs = [(m.__name__, getattr(m, hunt)) for m in allmods if hasattr(m, hunt)]
    load_total = len(funcs)
    m = ''
    try:
        for m, f in funcs:
            if game.threadstop: break
            load_current += 1
            f()
    except:
        #raise
        global load_finished_status, load_finished_message, load_finished_module, load_finished_type
        load_finished_message = str(sys.exc_value)
        load_finished_type = str(sys.exc_type) + ' in module ' + m
        load_finished_status = -1
    game.thread = None


class GameInit:
    def __init__(self, prevhandler):
        self.prevhandler = prevhandler
        font = txt.Font(None, 20)
        self.font = txt.Font(None, 22)
        self.rect = Rect(50, 450, 700, 22)
        self.text = font.render('Loading Resources...', 1, (250, 230, 180))
        self.img_powered = gfx.load('pygame_powered.gif')
        self.img_logo = gfx.load('logo.png')
        self.textrect = self.text.get_rect()
        self.textrect.center = self.rect.center
        self.lastcurrent = -1
        snd.play('startup')
        self.top = 120
        self.left = 100
        self.blocks = []
        self.starttime = pygame.time.get_ticks()
        #self.gatherinfo()
        self.handlederror = 0
        self.thread = threading.Thread(None, loadresources)
        game.threadstop = 0
        game.thread = self.thread
        self.thread.start()


    def gatherinfo(self):
        lines = []
        info = pygame.display.Info()
        lines.append('Current Video Driver: %s' % pygame.display.get_driver())
        lines.append('Video Mode is Accelerated: %s' % ('No', 'Yes')[info.hw])
        lines.append('Display Depth (Bits Per Pixel): %d' % info.bitsize)
        self.buildblock(lines)

        lines = []
        info = pygame.mixer.get_init()
        if info:
            lines.append('Sound Frequency: %d' % info[0])
            lines.append('Sound Quality: %d bits' % abs(info[1]))
            lines.append('Sound Channels: %s' % ('Mono', 'Stereo')[info[2]])
        else:
            lines.append('Sound: None')
        self.buildblock(lines)


        lines = []
        if not input.joystick:
            lines.append('Input: Keyboard')
        else:
            lines.append('Input: Keyboard, %s' % input.joystick.get_name())
        self.buildblock(lines)


    def input(self, i):
        if load_finished_status < 0:
            self.gotfinishinput = 1


    def event(self, e):
        if load_finished_status < 0:
            if e.type in (KEYDOWN, MOUSEBUTTONDOWN, JOYBUTTONDOWN):
                self.gotfinishinput = 1


    def buildblock(self, text):
        imgs = []
        width = 0
        height = 0
        for line in text:
            img = self.font.render(line, 1, (250, 230, 180), (5, 50, 5))
            height += img.get_height()
            w = img.get_width()
            width = max(w, width)
            imgs.append(img)
        size = width+20, height+20
        if gfx.surface.get_bitsize() > 8:
            block = pygame.Surface(size)
        else:
            block = pygame.Surface(size, 0, 32)
        block.fill((5, 50, 5))
        block.fill((20, 80, 30), Rect(0, size[1]-2, size[0], 2))
        top = 10
        for i in imgs:
            pos = 10, top
            top += i.get_height()
            block.blit(i, pos)
        self.blocks.append((block, (self.left, self.top)))
        self.top += block.get_height() + 40
        self.gotfinishinput = 0


    def quit(self):
        import gamemenu
        gfx.dirty(self.background(gfx.rect))
        for b in self.blocks:
            r = b[0].get_rect().move(b[1])
            gfx.dirty(self.background(r))

        if load_finished_status >= 0:
            game.handler = gamemenu.GameMenu(self.prevhandler)
        else:
            game.handler = self.prevhandler


    def run(self):
        if self.rect:
            self.background(self.rect)
        gfx.updatestars(self.background, gfx)

        gfx.dirty(gfx.surface.blit(self.img_logo, (30, 25)))
        gfx.dirty(gfx.surface.blit(self.img_powered, (510, 490)))

        for b in self.blocks:
            gfx.dirty(gfx.surface.blit(*b))

        bar = Rect(self.rect)
        if load_total:
            bar.width = (float(load_current)/float(load_total)) * bar.width
            gfx.surface.fill((5, 50, 5), bar)
        r = Rect(self.rect.left, self.rect.bottom-2, self.rect.width, 2)
        gfx.surface.fill((20, 80, 30), r)
        gfx.surface.blit(self.text, self.textrect)
        gfx.dirty(self.rect)

        now = pygame.time.get_ticks()
        #we let the screen stay up for at about 1 second
        if not self.thread.is_alive():
            if load_finished_status >= 0:
                if now-self.starttime > 1200:
                    self.quit()
            else:
                if not self.handlederror:
                    msg = ('Fatal Error Loading Resources', load_finished_type, load_finished_message, 'Press Any Key To Quit')
                    self.buildblock(msg)
                    self.handlederror = 1
                if self.gotfinishinput:
                    self.quit()


    def background(self, area):
        return gfx.surface.fill((0, 0, 0), area)