File: gameinit.py

package info (click to toggle)
solarwolf 1.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,348 kB
  • ctags: 371
  • sloc: python: 2,640; makefile: 56; sh: 31
file content (159 lines) | stat: -rw-r--r-- 5,001 bytes parent folder | download
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
"gameinit handler. splash and load resources"

import pygame, pygame.draw
from pygame.locals import *
import sys, threading
import game, gfx, snd, 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:
            load_current += 1
            f()
    except:
        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


class GameInit:
    def __init__(self, prevhandler):
        self.prevhandler = prevhandler
        font = pygame.font.Font(None, 20)
        self.font = pygame.font.Font(None, 22)
        self.rect = Rect(50, 50, 700, 22)
        self.text = font.render('Loading Resources...', 1, (250, 230, 180))
        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)
        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 = []        
        if not input.joystick:
            lines.append('Joystick: NONE')
        else:
            lines.append('Joystick: %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(self.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)

        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 3 seconds
        if not self.thread.isAlive():
            if load_finished_status >= 0:
                if now-self.starttime > 3200:
                    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)