File: main_loop.py

package info (click to toggle)
balazarbrothers 1.0~rc1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 23,528 kB
  • ctags: 451
  • sloc: python: 3,196; xml: 165; makefile: 20
file content (142 lines) | stat: -rw-r--r-- 5,699 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
# -*- coding: latin-1 -*-
# Balazar Brothers
# Copyright (C) 2003-2007 Jean-Baptiste LAMY
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import os, os.path, time, random
import cerealizer, soya.tofu as tofu, soya, soya.sdlconst as sdlconst, soya._soya, soya.widget as widget, soya.opengl as opengl
import balazar_brothers, balazar_brothers.globdef as globdef, balazar_brothers.controller as controller
from balazar_brothers.camera     import Traveling
from balazar_brothers.weather    import WeatherAtmosphere
from balazar_brothers.discussion import Bubble


def init():
  if not soya.inited:
    soya.init(title = "Balazar Brothers", width = globdef.SCREEN_WIDTH, height = globdef.SCREEN_HEIGHT, fullscreen = globdef.FULLSCREEN, sound = 1, sound_reference_distance = 3.0, sound_doppler_factor = 0)
    soya.set_quality(globdef.QUALITY)
    soya.set_sound_volume(globdef.SOUND_VOLUME)
    
    SHADER_DEFAULT_MATERIAL = soya.Material.get("shader")
    SHADER_DEFAULT_MATERIAL.filename = "__SHADER_DEFAULT_MATERIAL__"
    soya._soya._set_shader_default_material(SHADER_DEFAULT_MATERIAL)
    
    widget.big_font     = widget.default_font
    widget.default_font = soya.Font(os.path.join(soya.DATADIR, "FreeSans.ttf"), 18, 15)
    
  soya.render()
  soya.root_widget = None
  

class MainLoop(tofu.MainLoop):
  def __init__(self, scene = None):
    self.music              = None
    self.min_frame_duration = globdef.MIN_FRAME_DURATION
    
    tofu.MainLoop.__init__(self, scene)
    
  def init_interface(self):
    self.camera = soya.MAIN_LOOP.scenes[0].camera = soya.TravelingCamera(soya.MAIN_LOOP.scenes[0])
    self.camera.back = 100.0
    self.camera.speed = 0.03
    self.traveling = Traveling()
    self.camera.add_traveling(self.traveling)
    
    self.root_widget = widget.Group()
    self.root_widget.add(self.camera)
    soya.set_root_widget(self.root_widget)
    if globdef.FPS_LABEL: soya.widget.FPSLabel(self.root_widget)
    
    self.level_label = widget.Label(self.root_widget, _("(loading...)"), font = FUN_FONT)
    self.level_label.width = 10000
    self.level_label.top = -10
    self.score_label = ImageLabel  (self.root_widget, None, "score", soya.Material.get("coffre2D"), 0)
    self.keys_label  = ImageLabel  (self.root_widget, None, "keys" , soya.Material.get("clef2D"  ), 1)
    
    self.left_bubble   = Bubble(self.root_widget, -1, "test -1", (1.0, 0.0, 0.0, 1.0))
    self.middle_bubble = Bubble(self.root_widget,  0, "test  0", (0.0, 0.0, 0.0, 1.0))
    self.right_bubble  = Bubble(self.root_widget,  1, "", (0.0, 0.0, 1.0, 1.0))
    self.entry         = None
    
    scene = self.scenes[0]
    scene.atmosphere = WeatherAtmosphere(scene)
    self.listener = controller.EventListener(scene)
    
  def advance_time(self, proportion):
    tofu.MainLoop.advance_time(self, proportion)
    
    if self.scenes[0].atmosphere: self.scenes[0].atmosphere.advance_time(proportion)

BIG_FONT          = soya.Font(os.path.join(soya.DATADIR, "FreeSans.ttf"), 50, 60)
#INDIGO_JOKER_FONT = soya.Font.get("indigo.ttf@30x30")
FUN_FONT = soya.Font.get("PenguinAttack.ttf@40x40")
import balazar_brothers.platform
balazar_brothers.platform.Label3D._font = FUN_FONT # Hack !
  

class ImageLabel(widget.Label):
  def __init__(self, master = None, character = None, attr = "", material = None, pos = 0):
    self.character = character
    self.attr      = attr
    self.material  = material
    self.pos       = pos
    
    widget.Label.__init__(self, master, font = FUN_FONT, align = 0)
    
    self.old      = getattr(character, attr, 0)
    self.text     = unicode(self.old)
    
  def set_text(self, text):
    widget.Label.set_text(self, text)
    
    self.resize(self.master.left, self.master.top, self.master.width, self.master.height)
  text = property(widget.Label.get_text, set_text)
  
  def build_display_list(self):
    widget.Label.build_display_list(self)
    
    self.material.activate()
    opengl.glEnable(opengl.GL_BLEND)
    
    opengl.glBegin(opengl.GL_QUADS)
    opengl.glTexCoord2f(0.0, 0.0); opengl.glVertex2f(self.left + self.width - 50, self.top     )
    opengl.glTexCoord2f(0.0, 1.0); opengl.glVertex2f(self.left + self.width - 50, self.top + 50)
    opengl.glTexCoord2f(1.0, 1.0); opengl.glVertex2f(self.left + self.width     , self.top + 50)
    opengl.glTexCoord2f(1.0, 0.0); opengl.glVertex2f(self.left + self.width     , self.top     )
    opengl.glEnd()
    
    soya.DEFAULT_MATERIAL.activate()
    opengl.glDisable(opengl.GL_BLEND)
    
  def widget_begin_round(self):
    dif = getattr(self.character, self.attr, 0) - self.old
    if   dif > 0:
      self.old += 1
      self.text = unicode(self.old)
      
    elif dif < 0:
      self.old -= 1
      self.text = unicode(self.old)
      
  def resize(self, parent_left, parent_top, parent_width, parent_height):
    w, h = self.font.get_print_size(self._text)
    
    self.left   = parent_left + parent_width  - 10 - 50 - w
    self.top    = 5 + 52 * self.pos
    self.width  = 50 + 5 + w
    self.height = 50
    self._changed = -2