Skip to content

Commit

Permalink
feat: add fullscreen env variable for raspberry pi usage
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Aug 30, 2024
1 parent 3bf2d45 commit 3498592
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/doggo/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import os

import pygame as pg

from doggo import ASSETS_PATH
Expand All @@ -20,13 +22,18 @@ def run() -> None:
Initialize the pygame and start the world.
"""
# Check if the app should run in fullscreen mode.
# Fu
fullscreen = os.getenv("DOGGO_FULLSCREEN", "False").lower() in ("1", "true")

pg.init()

world = World(
title=config.WORLD_TITLE,
size=(config.WORLD_WIDTH, config.WORLD_HEIGHT),
icon=ASSETS_PATH.joinpath("icon.png"),
fps=config.WORLD_FPS,
fullscreen=fullscreen,
)

if COMPILED_ENV and WIN:
Expand Down
27 changes: 23 additions & 4 deletions src/doggo/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,43 @@ class World:
"""

def __init__(
self, title: str, size: tuple[int, int], icon: Path, fps: int = 60
self,
title: str,
size: tuple[int, int],
icon: Path,
fps: int = 60,
fullscreen: bool = False,
) -> None:
self.window: pg.window.Window = pg.window.Window(
title=title,
size=size,
borderless=True,
always_on_top=True,
)
self.screen: pg.Surface = self.window.get_surface()
self.window_surf: pg.Surface = self.window.get_surface()
self.screen: pg.Surface = pg.Surface(size=size)
self.window.set_icon(pg.image.load(icon).convert_alpha())
self.draggable: DraggableWindow = DraggableWindow(window=self.window)
self.fullscreen: bool = False
self.fps: int = fps
self.clock: pg.time.Clock = pg.time.Clock()
self.running: bool = False
self.dt: float = 0.0
self.prev_time: float = time.time()

self.landscape = build_landscape()
self.dog: Dog = build_dog()

def get_screen(self) -> pg.Surface:
"""Adapt the screen to the window size if fullscreen.
Didn't respect the aspect ratio, to use only if the display ratio is the same
as the default window size one.
"""
if self.fullscreen:
return pg.transform.scale(self.screen, self.window_surf.get_size())

return self.screen

def process_inputs(self) -> None:
"""Process the inputs of the world."""
for event in pg.event.get():
Expand All @@ -54,7 +71,8 @@ def process_inputs(self) -> None:
):
self.running = False

self.draggable.process_event(event=event)
if not self.fullscreen:
self.draggable.process_event(event=event)

def get_dt(self) -> None:
"""Calculate the delta time."""
Expand All @@ -72,6 +90,7 @@ def render(self) -> None:
self.landscape.background.draw(screen=self.screen)
self.dog.draw(screen=self.screen)
self.landscape.foreground.draw(screen=self.screen)
self.window_surf.blit(self.get_screen(), (0, 0))
self.window.flip()

def start(self) -> None:
Expand Down
62 changes: 62 additions & 0 deletions tests/test_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from __future__ import annotations

import time

import pytest

from doggo import ASSETS_PATH
from doggo.world import World


def test_world_initializes_correctly():
world = World(
title="Doggo Test",
size=(340, 106),
icon=ASSETS_PATH.joinpath("icon.png"),
fps=30,
fullscreen=False,
)

assert world.window.title == "Doggo Test"
assert world.window.size == (340, 106)
assert world.window.borderless is True
assert world.window.always_on_top is True
assert world.window_surf.get_size() == (340, 106)
assert world.screen.get_size() == (340, 106)
assert world.fullscreen is False
assert world.fps == 30
assert world.running is False
assert world.dt == 0.0


@pytest.mark.parametrize("fullscreen", [True, False])
def test_world_screen_is_scaled_regarding_fullscreen(fullscreen):
world = World(
title="Doggo Test",
size=(340, 106),
icon=ASSETS_PATH.joinpath("icon.png"),
fps=30,
fullscreen=fullscreen,
)

screen = world.get_screen()

if fullscreen:
assert screen.get_size() == (340, 106)
else:
assert screen.get_size() == world.window_surf.get_size()


def test_world_get_dt():
world = World(
title="Doggo Test",
size=(340, 106),
icon=ASSETS_PATH.joinpath("icon.png"),
fps=30,
fullscreen=False,
)
time.sleep(0.1)

world.get_dt()

assert world.dt > 0.0

0 comments on commit 3498592

Please sign in to comment.