source: nagslang/engine.py@ 142:cd77974b2a87

Last change on this file since 142:cd77974b2a87 was 142:cd77974b2a87, checked in by Stefano Rivera <stefano@…>, 8 years ago

Send a fake resize event, to discover the real window size under a tiling window manager

File size: 2.0 KB
RevLine 
[15]1"""Top-level event dispatching and area transition."""
2
[18]3import pygame
[85]4import pygame.locals as pgl
[15]5
6from nagslang import constants
[18]7from nagslang.screens.menu import MenuScreen
[20]8from nagslang.screens.area import AreaScreen
9from nagslang.events import ScreenChange
[15]10
11
12class Engine(object):
[18]13 def __init__(self, surface):
14 self._surface = surface
15 self._clock = pygame.time.Clock()
[15]16 self._fps = constants.FPS
[41]17 self._dt = 1. / self._fps
[20]18 self._world = None # TODO: create the world
19 self._current_screen = None
20 self._screens = {
21 'menu': MenuScreen,
22 'level1': AreaScreen,
23 }
24 self.change_screen('menu')
[142]25 # Dummy resize event, to force us to realise our real size
26 # http://stackoverflow.com/q/16442573/8629
27 pygame.event.post(pygame.event.Event(pgl.VIDEORESIZE,
28 size=(0, 0), w=0, h=0))
[20]29
30 def change_screen(self, new_screen):
31 if self._current_screen is not None:
32 self._current_screen.teardown()
[37]33 screen_cls = self._screens[new_screen]
34 self._current_screen = screen_cls(new_screen, self._world)
35 self._current_screen.setup()
[15]36
37 def run(self):
38 running = True
39 while running:
[18]40 for ev in pygame.event.get():
[85]41 if ev.type == pgl.QUIT:
[15]42 running = False
[85]43 elif ev.type == pgl.VIDEORESIZE:
44 pygame.display.set_mode(ev.size,
[128]45 pgl.SWSURFACE | pgl.RESIZABLE)
[85]46 self._surface = pygame.display.get_surface()
[20]47 elif ScreenChange.matches(ev):
[128]48 self._surface.fill(pygame.color.Color(0, 0, 0))
[20]49 self.change_screen(ev.screen)
[18]50 else:
[20]51 self._current_screen.handle_event(ev)
[41]52 self._current_screen.tick(self._dt)
[20]53 self._current_screen.render(self._surface)
[18]54 pygame.display.flip()
[41]55 self._clock.tick(self._fps)
Note: See TracBrowser for help on using the repository browser.