Skip to content

Commit

Permalink
-added 'tick' function to timer.Clock (with fps cap)
Browse files Browse the repository at this point in the history
-gui18.py uses lightbox for popup dialogs
-full integration with new Clock
  • Loading branch information
peter.rogers committed Aug 23, 2009
1 parent 2ee3b19 commit babbe7a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
50 changes: 36 additions & 14 deletions examples/gui18.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,25 @@ def setup_menu(this):

dlg = TestDialog()

def click_cb():
this.engine.pause()
def dialog_cb():
dlg.open()
while (dlg.is_open()):
for ev in pygame.event.get():
this.event(ev)
rects = this.update()
if (rects):
pygame.display.update(rects)
this.engine.resume()

btn = gui.Button("Pause clock", height=50)
btn.connect(gui.CLICK, click_cb)
btn = gui.Button("Modal dialog", height=50)
btn.connect(gui.CLICK, dialog_cb)
tbl.td(btn)

# Add a button for pausing / resuming the game clock
def pause_cb():
if (this.engine.clock.paused):
this.engine.resume()
else:
this.engine.pause()

btn = gui.Button("Pause/resume clock", height=50)
btn.connect(gui.CLICK, pause_cb)
tbl.td(btn)

# Add a slider for adjusting the game clock speed
tbl2 = gui.Table()

timeLabel = gui.Label("Clock speed")
Expand All @@ -101,11 +105,28 @@ def update_speed():

this.menuArea.add(tbl, 0, 0)

def open(this, w, pos=None):
def open(this, dlg, pos=None):
# Gray out the game area before showing the popup
rect = this.gameArea.get_abs_rect()
dark = pygame.Surface(rect.size).convert_alpha()
dark.fill((0,0,0,150))
pygame.display.get_surface().blit(dark, rect)
# Save whatever has been rendered to the 'game area' so we can
# render it as a static image while the dialog is open.
this.gameArea.save_background()
gui.Desktop.open(this, w, pos)
# Pause the gameplay while the dialog is visible
running = not(this.engine.clock.paused)
this.engine.pause()
gui.Desktop.open(this, dlg, pos)
while (dlg.is_open()):
for ev in pygame.event.get():
this.event(ev)
rects = this.update()
if (rects):
pygame.display.update(rects)
if (running):
# Resume gameplay
this.engine.resume()

def get_render_area(this):
return this.gameArea.get_abs_rect()
Expand Down Expand Up @@ -180,7 +201,8 @@ def run(this):
updates += lst
this.disp.set_clip()

#this.clock.tick()
# Cap it at 30fps
this.clock.tick(30)

# Give pgu a chance to update the display
lst = this.app.update()
Expand Down
21 changes: 13 additions & 8 deletions pgu/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Clock(object):
lastGameTime = None
# The real time corresponding to the last game time
lastRealTime = None
# The game time when 'tick' was last called
lastTickTime = None

# Whether the timer is paused or not
paused = False
Expand All @@ -23,6 +25,7 @@ class Clock(object):
def __init__(self):
#self.startTime = time.time()
self.lastGameTime = 0
self.lastTickTime = 0
self.lastRealTime = time.time()
self.startTime = time.time()

Expand All @@ -46,14 +49,16 @@ def resume(self):
self.paused = False
self.lastRealTime = time.time()

# def tick(self, fps=0):
# if (self.paused):
# # Time never passes when the clock is paused
# return 0
# tm = time.time()
# dt = tm-self.lastTime
# self.lastTime = tm
# return dt
def tick(self, fps=0):
tm = self.get_time()
dt = tm - self.lastTickTime
if (fps > 0):
minTime = 1.0/fps
if (dt < minTime):
pygame.time.wait(int((minTime-dt)*1000))
dt = minTime
self.lastTickTime = tm
return dt

# Returns the amount of 'game time' that has passed since creating
# the clock (paused time does not count).
Expand Down

0 comments on commit babbe7a

Please sign in to comment.