Skip to content

Commit

Permalink
feat: add custom render size instead of fullscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Aug 31, 2024
1 parent cad9fc8 commit a806e9e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/doggo/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def run() -> None:
size=(config.WORLD_WIDTH, config.WORLD_HEIGHT),
icon=ASSETS_PATH.joinpath("icon.png"),
fps=config.WORLD_FPS,
fullscreen=config.WORLD_FULLSCREEN,
render_size=config.WORLD_RENDER_SIZE,
)

if COMPILED_ENV and WIN:
Expand Down
7 changes: 6 additions & 1 deletion src/doggo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from typing import TYPE_CHECKING

from loguru import logger

from doggo.dog import StateID
from doggo.dog.brain import Direction

Expand All @@ -16,6 +18,9 @@
COMPILED_ENV = getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")
WIN = sys.platform.startswith("win")

DOGGO_7_9_SCREEN = os.getenv("DOGGO_7_9_SCREEN", "False").lower() in ("1", "true")
logger.debug(f"7.9 screen: {DOGGO_7_9_SCREEN}")

# --- World configuration ---

WORLD_TITLE = "Doggo"
Expand All @@ -27,7 +32,7 @@
WORLD_GROUND = (
WORLD_HEIGHT - WORLD_GROUND_HEIGHT
) # The ground level in the world, where the dog can walk on.
WORLD_FULLSCREEN = os.getenv("DOGGO_FULLSCREEN", "False").lower() in ("1", "true")
WORLD_RENDER_SIZE = (1280, 400) if DOGGO_7_9_SCREEN else None

# --- Dog sprite sheet configuration ---

Expand Down
14 changes: 6 additions & 8 deletions src/doggo/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,21 @@ def __init__(
size: tuple[int, int],
icon: Path,
fps: int = 60,
fullscreen: bool = False,
render_size: tuple[int, int] | None = None,
) -> None:
pg.init()
self.window: pg.window.Window = pg.window.Window(
title=title,
size=size,
size=render_size if render_size else size,
borderless=True,
always_on_top=True,
fullscreen=fullscreen,
)
self._running: bool = False
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 = fullscreen
self.render_size: tuple[int, int] | None = render_size
self.fps: int = fps
self.clock: pg.time.Clock = pg.time.Clock()
self.dt: float = 0.0
Expand All @@ -64,8 +63,8 @@ def get_screen(self) -> pg.Surface:
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())
if self.render_size:
return pg.transform.scale(self.screen, self.render_size)

return self.screen

Expand All @@ -77,8 +76,7 @@ def process_inputs(self) -> None:
):
self.stop()

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

def get_dt(self) -> None:
"""Calculate the delta time."""
Expand Down
33 changes: 7 additions & 26 deletions tests/test_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
def create_world():
world = None

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

return world
Expand All @@ -40,18 +40,18 @@ def test_world_initializes_correctly(create_world):
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.render_size is None
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(create_world, fullscreen):
world = create_world(fullscreen)
@pytest.mark.parametrize("render_size", [(1280, 400), None])
def test_world_screen_is_scaled_regarding_render_size(create_world, render_size):
world = create_world(render_size)
screen = world.get_screen()

if fullscreen:
if render_size:
assert screen.get_size() == world.window_surf.get_size()
else:
assert screen.get_size() == (340, 106)
Expand Down Expand Up @@ -83,25 +83,6 @@ def test_world_stop_at_some_events(mocker, create_world, event):
assert world._running is False


@pytest.mark.parametrize(
"fullscreen",
[True, False],
)
def test_world_draggable_window_is_processed_correctly(
mocker, create_world, fullscreen
):
mocker.patch("pygame.event.get", return_value=[pg.event.Event(pg.MOUSEBUTTONDOWN)])
world = create_world(fullscreen)
mocker.patch.object(world.draggable, "process_event", spec=True)

world.process_inputs()

if fullscreen:
world.draggable.process_event.assert_not_called()
else:
world.draggable.process_event.assert_called_once_with(event=pg.event.get()[0])


def test_world_update(mocker, create_world):
world = create_world()
world.dt = 0.1
Expand Down

0 comments on commit a806e9e

Please sign in to comment.