Example #1
0
def run():
    clock = pygame.time.Clock()
    running = True

    ignore_resize_events_next_tick = False

    while running:
        # processing user input events
        all_resize_events = []
        toggled_fullscreen = False

        input_state = inputs.get_instance()
        for py_event in pygame.event.get():
            if py_event.type == pygame.QUIT:
                running = False
                continue
            elif py_event.type == pygame.KEYDOWN:
                input_state.set_key(py_event.key, True)
            elif py_event.type == pygame.KEYUP:
                input_state.set_key(py_event.key, False)

            elif py_event.type in (pygame.MOUSEMOTION, pygame.MOUSEBUTTONDOWN,
                                   pygame.MOUSEBUTTONUP):
                scr_pos = window.get_instance().window_to_screen_pos(
                    py_event.pos)
                game_pos = Utils.round(
                    Utils.mult(
                        scr_pos,
                        1 / renderengine.get_instance().get_pixel_scale()))
                input_state.set_mouse_pos(game_pos)

                if py_event.type == pygame.MOUSEBUTTONDOWN:
                    input_state.set_mouse_down(True, button=py_event.button)
                elif py_event.type == pygame.MOUSEBUTTONUP:
                    input_state.set_mouse_down(False, button=py_event.button)

            elif py_event.type == pygame.VIDEORESIZE:
                all_resize_events.append(py_event)

            if py_event.type == pygame.KEYDOWN and py_event.key == pygame.K_F4:
                toggled_fullscreen = True

            if not pygame.mouse.get_focused():
                input_state.set_mouse_pos(None)

        ignore_resize_events_this_tick = ignore_resize_events_next_tick
        ignore_resize_events_next_tick = False

        if toggled_fullscreen:
            # print("INFO {}: toggled fullscreen".format(gs.get_instance().tick_counter))
            win = window.get_instance()
            win.set_fullscreen(not win.is_fullscreen())

            new_size = win.get_display_size()
            new_pixel_scale = _calc_pixel_scale(new_size)
            if new_pixel_scale != renderengine.get_instance().get_pixel_scale(
            ):
                renderengine.get_instance().set_pixel_scale(new_pixel_scale)
            renderengine.get_instance().resize(new_size[0],
                                               new_size[1],
                                               px_scale=new_pixel_scale)

            # when it goes from fullscreen to windowed mode, pygame sends a VIDEORESIZE event
            # on the next frame that claims the window has been resized to the maximum resolution.
            # this is annoying so we ignore it. we want the window to remain the same size it was
            # before the fullscreen happened.
            ignore_resize_events_next_tick = True

        if not ignore_resize_events_this_tick and len(all_resize_events) > 0:
            last_resize_event = all_resize_events[-1]

            print("INFO: resizing to {}, {}".format(last_resize_event.w,
                                                    last_resize_event.h))

            window.get_instance().set_window_size(last_resize_event.w,
                                                  last_resize_event.h)

            display_w, display_h = window.get_instance().get_display_size()
            new_pixel_scale = _calc_pixel_scale(
                (last_resize_event.w, last_resize_event.h))

            renderengine.get_instance().resize(display_w,
                                               display_h,
                                               px_scale=new_pixel_scale)

        input_state.update(gs.get_instance().tick_count)
        sounds.update()

        if gs.get_instance().is_dev() and input_state.was_pressed(pygame.K_F1):
            # used to help find performance bottlenecks
            import src.utils.profiling as profiling
            profiling.get_instance().toggle()

        if input_state.was_pressed(pygame.K_F5):
            current_scale = gs.get_instance().px_scale
            options = gs.get_instance().px_scale_options
            if current_scale in options:
                new_scale = (options.index(current_scale) + 1) % len(options)
            else:
                print("WARN: illegal pixel scale={}, reverting to default".
                      format(current_scale))
                new_scale = options[0]
            gs.get_instance().px_scale = new_scale

            display_size = window.get_instance().get_display_size()
            new_pixel_scale = _calc_pixel_scale(display_size,
                                                px_scale_opt=new_scale)
            renderengine.get_instance().set_pixel_scale(new_pixel_scale)

        if input_state.was_pressed(pygame.K_r):
            print("INFO: restarting game")
            renderengine.get_instance().clear_all_sprites()  # full nuke
            gs.get_instance().set_game_state(gamestate.GameState())

        renderengine.get_instance().set_clear_color((0, 0, 0))

        gs.get_instance().update_all()

        renderengine.get_instance().render_layers()
        pygame.display.flip()

        slo_mo_mode = gs.get_instance().is_dev() and input_state.is_held(
            pygame.K_TAB)
        if slo_mo_mode:
            clock.tick(15)
        else:
            clock.tick(60)

        gs.get_instance().tick_count += 1

        if gs.get_instance().tick_count % 60 == 0:
            if clock.get_fps() < 55 and gs.get_instance().is_dev(
            ) and not slo_mo_mode:
                print("WARN: fps drop: {} ({} sprites)".format(
                    round(clock.get_fps() * 10) / 10.0,
                    renderengine.get_instance().count_sprites()))

    print("INFO: quitting game")
    pygame.quit()
Example #2
0
    def run(self):
        running = True

        ignore_resize_events_next_tick = False

        while running:
            # processing user input events
            all_resize_events = []

            input_state = inputs.get_instance()
            for py_event in pygame.event.get():
                if py_event.type == pygame.QUIT:
                    running = False
                    continue
                elif py_event.type == pygame.KEYDOWN:
                    input_state.set_key(py_event.key, True)
                elif py_event.type == pygame.KEYUP:
                    input_state.set_key(py_event.key, False)

                elif py_event.type in (pygame.MOUSEMOTION,
                                       pygame.MOUSEBUTTONDOWN,
                                       pygame.MOUSEBUTTONUP):
                    scr_pos = window.get_instance().window_to_screen_pos(
                        py_event.pos)
                    game_pos = Utils.round(
                        Utils.mult(
                            scr_pos,
                            1 / renderengine.get_instance().get_pixel_scale()))
                    input_state.set_mouse_pos(game_pos)

                    if py_event.type == pygame.MOUSEBUTTONDOWN:
                        input_state.set_mouse_down(True,
                                                   button=py_event.button)
                    elif py_event.type == pygame.MOUSEBUTTONUP:
                        input_state.set_mouse_down(False,
                                                   button=py_event.button)

                elif py_event.type == pygame.VIDEORESIZE:
                    all_resize_events.append(py_event)

                if not pygame.mouse.get_focused():
                    input_state.set_mouse_pos(None)

            ignore_resize_events_this_tick = ignore_resize_events_next_tick
            ignore_resize_events_next_tick = False

            if input_state.was_pressed(
                    pygame.K_F4) and configs.allow_fullscreen:
                win = window.get_instance()
                win.set_fullscreen(not win.is_fullscreen())

                new_size = win.get_display_size()
                new_pixel_scale = self._calc_pixel_scale(new_size)
                if new_pixel_scale != renderengine.get_instance(
                ).get_pixel_scale():
                    renderengine.get_instance().set_pixel_scale(
                        new_pixel_scale)
                renderengine.get_instance().resize(new_size[0],
                                                   new_size[1],
                                                   px_scale=new_pixel_scale)

                # when it goes from fullscreen to windowed mode, pygame sends a VIDEORESIZE event
                # on the next frame that claims the window has been resized to the maximum resolution.
                # this is annoying so we ignore it. we want the window to remain the same size it was
                # before the fullscreen happened.
                ignore_resize_events_next_tick = True

            if not ignore_resize_events_this_tick and len(
                    all_resize_events) > 0:
                last_resize_event = all_resize_events[-1]

                window.get_instance().set_window_size(last_resize_event.w,
                                                      last_resize_event.h)

                display_w, display_h = window.get_instance().get_display_size()
                new_pixel_scale = self._calc_pixel_scale(
                    (last_resize_event.w, last_resize_event.h))

                renderengine.get_instance().resize(display_w,
                                                   display_h,
                                                   px_scale=new_pixel_scale)

            if configs.is_dev and input_state.was_pressed(pygame.K_F1):
                # used to help find performance bottlenecks
                import src.utils.profiling as profiling
                profiling.get_instance().toggle()

            input_state.update()
            sounds.update()

            # updates the actual game state
            self._game.update()

            # draws the actual game state
            for spr in self._game.all_sprites():
                if spr is not None:
                    renderengine.get_instance().update(spr)

            renderengine.get_instance().set_clear_color(configs.clear_color)
            renderengine.get_instance().render_layers()

            pygame.display.flip()

            slo_mo_mode = configs.is_dev and input_state.is_held(pygame.K_TAB)
            target_fps = configs.target_fps if not slo_mo_mode else configs.target_fps // 4

            self._wait_until_next_frame(target_fps)

            globaltimer.inc_tick_count()

            if globaltimer.tick_count() % configs.target_fps == 0:
                if globaltimer.get_fps(
                ) < 0.9 * configs.target_fps and configs.is_dev and not slo_mo_mode:
                    print("WARN: fps drop: {} ({} sprites)".format(
                        round(globaltimer.get_fps() * 10) / 10.0,
                        renderengine.get_instance().count_sprites()))

        print("INFO: quitting game")
        pygame.quit()