mini-arcade-core 1.0.0__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/PKG-INFO +1 -1
  2. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/pyproject.toml +1 -1
  3. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/__init__.py +8 -3
  4. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/backend/__init__.py +2 -1
  5. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/backend/backend.py +142 -11
  6. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/engine/commands.py +26 -0
  7. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/engine/game.py +34 -14
  8. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/engine/render/pipeline.py +27 -3
  9. mini_arcade_core-1.0.2/src/mini_arcade_core/engine/render/viewport.py +203 -0
  10. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/managers/inputs.py +0 -2
  11. mini_arcade_core-1.0.2/src/mini_arcade_core/runtime/audio/audio_adapter.py +20 -0
  12. mini_arcade_core-1.0.2/src/mini_arcade_core/runtime/audio/audio_port.py +36 -0
  13. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/capture/capture_port.py +19 -0
  14. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/scene/scene_adapter.py +29 -1
  15. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/scene/scene_port.py +21 -0
  16. mini_arcade_core-1.0.2/src/mini_arcade_core/runtime/window/window_adapter.py +90 -0
  17. mini_arcade_core-1.0.2/src/mini_arcade_core/runtime/window/window_port.py +109 -0
  18. mini_arcade_core-1.0.2/src/mini_arcade_core/scenes/debug_overlay.py +67 -0
  19. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/registry.py +9 -8
  20. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/d2/boundaries2d.py +10 -1
  21. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/ui/menu.py +9 -4
  22. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/utils/logging.py +2 -8
  23. mini_arcade_core-1.0.0/src/mini_arcade_core/runtime/audio/audio_adapter.py +0 -13
  24. mini_arcade_core-1.0.0/src/mini_arcade_core/runtime/audio/audio_port.py +0 -17
  25. mini_arcade_core-1.0.0/src/mini_arcade_core/runtime/window/window_adapter.py +0 -26
  26. mini_arcade_core-1.0.0/src/mini_arcade_core/runtime/window/window_port.py +0 -47
  27. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/LICENSE +0 -0
  28. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/README.md +0 -0
  29. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/backend/events.py +0 -0
  30. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/backend/keys.py +0 -0
  31. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/backend/sdl_map.py +0 -0
  32. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/backend/types.py +0 -0
  33. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/bus.py +0 -0
  34. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/engine/__init__.py +0 -0
  35. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/engine/render/__init__.py +0 -0
  36. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/engine/render/packet.py +0 -0
  37. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/managers/__init__.py +0 -0
  38. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/managers/cheats.py +0 -0
  39. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/__init__.py +0 -0
  40. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/audio/__init__.py +0 -0
  41. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/capture/__init__.py +0 -0
  42. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/capture/capture_adapter.py +0 -0
  43. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/context.py +0 -0
  44. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/file/__init__.py +0 -0
  45. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/file/file_adapter.py +0 -0
  46. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/file/file_port.py +0 -0
  47. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/input/__init__.py +0 -0
  48. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/input/input_adapter.py +0 -0
  49. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/input/input_port.py +0 -0
  50. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/input_frame.py +0 -0
  51. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/scene/__init__.py +0 -0
  52. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/services.py +0 -0
  53. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/runtime/window/__init__.py +0 -0
  54. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/__init__.py +0 -0
  55. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/autoreg.py +0 -0
  56. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/sim_scene.py +0 -0
  57. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/systems/__init__.py +0 -0
  58. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/systems/base_system.py +0 -0
  59. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/scenes/systems/system_pipeline.py +0 -0
  60. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/sim/__init__.py +0 -0
  61. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/sim/protocols.py +0 -0
  62. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/sim/runner.py +0 -0
  63. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/__init__.py +0 -0
  64. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/d2/__init__.py +0 -0
  65. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/d2/collision2d.py +0 -0
  66. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/d2/geometry2d.py +0 -0
  67. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/d2/kinematics2d.py +0 -0
  68. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/spaces/d2/physics2d.py +0 -0
  69. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/ui/__init__.py +0 -0
  70. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/utils/__init__.py +0 -0
  71. {mini_arcade_core-1.0.0 → mini_arcade_core-1.0.2}/src/mini_arcade_core/utils/deprecated_decorator.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mini-arcade-core
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Tiny scene-based game loop core for small arcade games.
5
5
  License: Copyright (c) 2025 Santiago Rincón
6
6
 
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [project]
6
6
  name = "mini-arcade-core"
7
- version = "1.0.0"
7
+ version = "1.0.2"
8
8
  description = "Tiny scene-based game loop core for small arcade games."
9
9
  authors = [
10
10
  { name = "Santiago Rincon", email = "rincores@gmail.com" },
@@ -18,6 +18,8 @@ SceneFactoryLike = Union[Type[SimScene], Callable[[Game], SimScene]]
18
18
 
19
19
 
20
20
  # TODO: Improve exception handling and logging in run_game
21
+ # TODO: Consider reducing parameters by using a single config object
22
+ # TODO: Delegate responsibilities to Game class where appropriate
21
23
  def run_game(
22
24
  scene: SceneFactoryLike | None = None,
23
25
  config: GameConfig | None = None,
@@ -28,9 +30,12 @@ def run_game(
28
30
  Convenience helper to bootstrap and run a game with a single scene.
29
31
 
30
32
  Supports both:
31
- - run_game(SceneClass, cfg) # legacy
32
- - run_game(config=cfg, initial_scene="main", registry=...) # registry-based
33
- - run_game(cfg) # config-only
33
+ - run_game(SceneClass, cfg) # legacy
34
+ - run_game(config=cfg, initial_scene="main", registry=...) # registry-based
35
+ - run_game(cfg) # config-only
36
+
37
+ :param scene: Optional SimScene factory/class to register
38
+ :type scene: SceneFactoryLike | None
34
39
 
35
40
  :param initial_scene: The SimScene ID to start the game with.
36
41
  :type initial_scene: str
@@ -6,8 +6,9 @@ This is the only part of the code that talks to SDL/pygame directly.
6
6
 
7
7
  from __future__ import annotations
8
8
 
9
- from .backend import Backend
9
+ from .backend import Backend, WindowSettings
10
10
 
11
11
  __all__ = [
12
12
  "Backend",
13
+ "WindowSettings",
13
14
  ]
@@ -5,12 +5,29 @@ This is the only part of the code that talks to SDL/pygame directly.
5
5
 
6
6
  from __future__ import annotations
7
7
 
8
+ from dataclasses import dataclass
8
9
  from typing import Iterable, Protocol
9
10
 
10
11
  from .events import Event
11
12
  from .types import Color
12
13
 
13
14
 
15
+ @dataclass
16
+ class WindowSettings:
17
+ """
18
+ Settings for the backend window.
19
+
20
+ :ivar width (int): Width of the window in pixels.
21
+ :ivar height (int): Height of the window in pixels.
22
+ """
23
+
24
+ width: int
25
+ height: int
26
+
27
+
28
+ # TODO: Refactor backend interface into smaller protocols?
29
+ # Justification: Many public methods needed for backend interface
30
+ # pylint: disable=too-many-public-methods
14
31
  class Backend(Protocol):
15
32
  """
16
33
  Interface that any rendering/input backend must implement.
@@ -18,19 +35,13 @@ class Backend(Protocol):
18
35
  mini-arcade-core only talks to this protocol, never to SDL/pygame directly.
19
36
  """
20
37
 
21
- def init(self, width: int, height: int, title: str):
38
+ def init(self, window_settings: WindowSettings):
22
39
  """
23
40
  Initialize the backend and open a window.
24
41
  Should be called once before the main loop.
25
42
 
26
- :param width: Width of the window in pixels.
27
- :type width: int
28
-
29
- :param height: Height of the window in pixels.
30
- :type height: int
31
-
32
- :param title: Title of the window.
33
- :type title: str
43
+ :param window_settings: Settings for the backend window.
44
+ :type window_settings: WindowSettings
34
45
  """
35
46
 
36
47
  def set_window_title(self, title: str):
@@ -105,14 +116,13 @@ class Backend(Protocol):
105
116
  :type color: Color
106
117
  """
107
118
 
108
- # pylint: enable=too-many-arguments,too-many-positional-arguments
109
-
110
119
  def draw_text(
111
120
  self,
112
121
  x: int,
113
122
  y: int,
114
123
  text: str,
115
124
  color: Color = (255, 255, 255),
125
+ font_size: int | None = None,
116
126
  ):
117
127
  """
118
128
  Draw text at the given position in a default font and color.
@@ -133,6 +143,8 @@ class Backend(Protocol):
133
143
  :type color: Color
134
144
  """
135
145
 
146
+ # pylint: enable=too-many-arguments,too-many-positional-arguments
147
+
136
148
  def measure_text(self, text: str) -> tuple[int, int]:
137
149
  """
138
150
  Measure the width and height of the given text string in pixels.
@@ -158,3 +170,122 @@ class Backend(Protocol):
158
170
  :rtype: bytes | None
159
171
  """
160
172
  raise NotImplementedError
173
+
174
+ def init_audio(
175
+ self, frequency: int = 44100, channels: int = 2, chunk_size: int = 2048
176
+ ):
177
+ """
178
+ Initialize SDL_mixer audio.
179
+
180
+ :param frequency: Audio frequency in Hz.
181
+ :type frequency: int
182
+
183
+ :param channels: Number of audio channels (1=mono, 2=stereo).
184
+ :type channels: int
185
+
186
+ :param chunk_size: Size of audio chunks.
187
+ :type chunk_size: int
188
+ """
189
+
190
+ def shutdown_audio(self):
191
+ """Shutdown SDL_mixer audio and free loaded sounds."""
192
+
193
+ def load_sound(self, sound_id: str, path: str):
194
+ """
195
+ Load a WAV sound and store it by ID.
196
+ Example: backend.load_sound("hit", "assets/sfx/hit.wav")
197
+
198
+ :param sound_id: Unique identifier for the sound.
199
+ :type sound_id: str
200
+
201
+ :param path: File path to the WAV sound.
202
+ :type path: str
203
+ """
204
+
205
+ def play_sound(self, sound_id: str, loops: int = 0):
206
+ """
207
+ Play a loaded sound.
208
+ loops=0 => play once
209
+ loops=-1 => infinite loop
210
+ loops=1 => play twice (SDL convention)
211
+
212
+ :param sound_id: Unique identifier for the sound.
213
+ :type sound_id: str
214
+
215
+ :param loops: Number of times to loop the sound.
216
+ :type loops: int
217
+ """
218
+
219
+ def set_master_volume(self, volume: int):
220
+ """
221
+ Master volume: 0..128
222
+ """
223
+
224
+ def set_sound_volume(self, sound_id: str, volume: int):
225
+ """
226
+ Per-sound volume: 0..128
227
+
228
+ :param sound_id: Unique identifier for the sound.
229
+ :type sound_id: str
230
+
231
+ :param volume: Volume level (0-128).
232
+ :type volume: int
233
+ """
234
+
235
+ def stop_all_sounds(self):
236
+ """Stop all channels."""
237
+
238
+ def set_viewport_transform(
239
+ self, offset_x: int, offset_y: int, scale: float
240
+ ):
241
+ """
242
+ Apply a transform so draw_* receives VIRTUAL coords and backend maps to screen.
243
+
244
+ :param offset_x: X offset in pixels.
245
+ :type offset_x: int
246
+
247
+ :param offset_y: Y offset in pixels.
248
+ :type offset_y: int
249
+
250
+ :param scale: Scale factor.
251
+ :type scale: float
252
+ """
253
+ raise NotImplementedError
254
+
255
+ def clear_viewport_transform(self):
256
+ """Reset any viewport transform back to identity."""
257
+ raise NotImplementedError
258
+
259
+ def resize_window(self, width: int, height: int):
260
+ """
261
+ Resize the actual OS window (SDL_SetWindowSize in native backend).
262
+
263
+ :param width: New width in pixels.
264
+ :type width: int
265
+
266
+ :param height: New height in pixels.
267
+ :type height: int
268
+ """
269
+ raise NotImplementedError
270
+
271
+ def set_clip_rect(self, x: int, y: int, w: int, h: int):
272
+ """
273
+ Set a clipping rectangle for rendering.
274
+
275
+ :param x: X position of the rectangle's top-left corner.
276
+ :type x: int
277
+
278
+ :param y: Y position of the rectangle's top-left corner.
279
+ :type y: int
280
+
281
+ :param w: Width of the rectangle.
282
+ :type w: int
283
+
284
+ :param h: Height of the rectangle.
285
+ :type h: int
286
+ """
287
+ raise NotImplementedError
288
+
289
+ def clear_clip_rect(self):
290
+ """Clear any clipping rectangle."""
291
+ raise NotImplementedError
@@ -7,6 +7,8 @@ from __future__ import annotations
7
7
  from dataclasses import dataclass, field
8
8
  from typing import TYPE_CHECKING, List, Optional, Protocol, TypeVar
9
9
 
10
+ from mini_arcade_core.runtime.scene.scene_port import ScenePolicy
11
+
10
12
  if TYPE_CHECKING:
11
13
  from mini_arcade_core.runtime.services import RuntimeServices
12
14
 
@@ -167,3 +169,27 @@ class ChangeSceneCommand(Command):
167
169
  context: CommandContext,
168
170
  ):
169
171
  context.services.scenes.change(self.scene_id)
172
+
173
+
174
+ @dataclass(frozen=True)
175
+ class ToggleDebugOverlayCommand(Command):
176
+ """Toggle the debug overlay scene."""
177
+
178
+ DEBUG_OVERLAY_ID = "debug_overlay"
179
+
180
+ def execute(self, context: CommandContext) -> None:
181
+ scenes = context.services.scenes
182
+ if scenes.has_scene(self.DEBUG_OVERLAY_ID):
183
+ scenes.remove_scene(self.DEBUG_OVERLAY_ID)
184
+ return
185
+
186
+ scenes.push(
187
+ self.DEBUG_OVERLAY_ID,
188
+ as_overlay=True,
189
+ policy=ScenePolicy(
190
+ blocks_update=False,
191
+ blocks_input=False,
192
+ is_opaque=False,
193
+ receives_input=False,
194
+ ),
195
+ )
@@ -8,18 +8,19 @@ from dataclasses import dataclass, field
8
8
  from time import perf_counter, sleep
9
9
  from typing import Dict, Literal
10
10
 
11
- from mini_arcade_core.backend import Backend
11
+ from mini_arcade_core.backend import Backend, WindowSettings
12
+ from mini_arcade_core.backend.events import EventType
13
+ from mini_arcade_core.backend.keys import Key
12
14
  from mini_arcade_core.engine.commands import (
13
15
  CommandContext,
14
16
  CommandQueue,
15
17
  QuitCommand,
18
+ ToggleDebugOverlayCommand,
16
19
  )
17
-
18
- # from mini_arcade_core.sim.runner import SimRunner, SimRunnerConfig
19
20
  from mini_arcade_core.engine.render.packet import RenderPacket
20
21
  from mini_arcade_core.engine.render.pipeline import RenderPipeline
21
22
  from mini_arcade_core.managers.cheats import CheatManager
22
- from mini_arcade_core.runtime.audio.audio_adapter import NullAudioAdapter
23
+ from mini_arcade_core.runtime.audio.audio_adapter import SDLAudioAdapter
23
24
  from mini_arcade_core.runtime.capture.capture_adapter import CaptureAdapter
24
25
  from mini_arcade_core.runtime.file.file_adapter import LocalFilesAdapter
25
26
  from mini_arcade_core.runtime.input.input_adapter import InputAdapter
@@ -91,7 +92,7 @@ class FrameTimer:
91
92
  :ivar marks (Dict[str, float]): Recorded time marks.
92
93
  """
93
94
 
94
- enabled: bool = True
95
+ enabled: bool = False
95
96
  marks: Dict[str, float] = field(default_factory=dict)
96
97
 
97
98
  def mark(self, name: str):
@@ -177,9 +178,13 @@ class Game:
177
178
  self.services = RuntimeServices(
178
179
  window=WindowAdapter(
179
180
  self.backend,
181
+ WindowSettings(
182
+ width=self.config.window.width,
183
+ height=self.config.window.height,
184
+ ),
180
185
  ),
181
186
  scenes=SceneAdapter(self.registry, self),
182
- audio=NullAudioAdapter(),
187
+ audio=SDLAudioAdapter(self.backend),
183
188
  files=LocalFilesAdapter(),
184
189
  capture=CaptureAdapter(self.backend),
185
190
  input=InputAdapter(),
@@ -222,7 +227,7 @@ class Game:
222
227
  packet_cache: dict[int, RenderPacket] = {}
223
228
 
224
229
  timer = FrameTimer(enabled=True)
225
- report_every = 60 # print once per second at 60fps
230
+ # report_every = 60 # print once per second at 60fps
226
231
 
227
232
  # TODO: Integrate SimRunner for simulation stepping
228
233
  # TODO: Fix assignment-from-no-return warning in self.services.input.build
@@ -239,6 +244,15 @@ class Game:
239
244
  last_time = now
240
245
 
241
246
  events = list(backend.poll_events())
247
+
248
+ for e in events:
249
+ if e.type == EventType.WINDOWRESIZED and e.size:
250
+ w, h = e.size
251
+ logger.debug(f"Window resized event: {w}x{h}")
252
+ self.services.window.on_window_resized(w, h)
253
+ # if F1 pressed, toggle debug overlay
254
+ if e.type == EventType.KEYDOWN and e.key == Key.F1:
255
+ self.command_queue.push(ToggleDebugOverlayCommand())
242
256
  timer.mark("events_polled")
243
257
 
244
258
  input_frame = self.services.input.build(events, frame_index, dt)
@@ -294,6 +308,7 @@ class Game:
294
308
  backend.begin_frame()
295
309
  timer.mark("begin_frame_done")
296
310
 
311
+ vp = self.services.window.get_viewport()
297
312
  for entry in self.services.scenes.visible_entries():
298
313
  scene = entry.scene
299
314
  packet = packet_cache.get(id(scene))
@@ -302,7 +317,7 @@ class Game:
302
317
  packet = scene.tick(_neutral_input(frame_index, 0.0), 0.0)
303
318
  packet_cache[id(scene)] = packet
304
319
 
305
- pipeline.draw_packet(backend, packet)
320
+ pipeline.draw_packet(backend, packet, vp)
306
321
 
307
322
  timer.mark("draw_done")
308
323
  backend.end_frame()
@@ -314,12 +329,14 @@ class Game:
314
329
  timer.mark("sleep_end")
315
330
 
316
331
  # --- report ---
317
- if frame_index % report_every == 0 and frame_index > 0:
318
- ms = timer.report_ms()
319
- total = (perf_counter() - timer.marks["frame_start"]) * 1000.0
320
- logger.debug(
321
- f"[Frame {frame_index}] total={total:.2f}ms | {ms}"
322
- )
332
+ # if timer.enabled and (
333
+ # frame_index % report_every == 0 and frame_index > 0
334
+ # ):
335
+ # ms = timer.report_ms()
336
+ # total = (perf_counter() - timer.marks["frame_start"]) * 1000.0
337
+ # logger.debug(
338
+ # f"[Frame {frame_index}] total={total:.2f}ms | {ms}"
339
+ # )
323
340
 
324
341
  frame_index += 1
325
342
 
@@ -340,6 +357,9 @@ class Game:
340
357
  br, bg, bb = self.config.window.background_color
341
358
  self.services.window.set_clear_color(br, bg, bb)
342
359
 
360
+ # the “authoring resolution”
361
+ self.services.window.set_virtual_resolution(800, 600)
362
+
343
363
  def _resolve_world(self) -> object | None:
344
364
  # Prefer gameplay world underneath overlays:
345
365
  # scan from top to bottom and pick the first scene that has .world
@@ -9,6 +9,7 @@ from dataclasses import dataclass
9
9
 
10
10
  from mini_arcade_core.backend import Backend
11
11
  from mini_arcade_core.engine.render.packet import RenderPacket
12
+ from mini_arcade_core.engine.render.viewport import ViewportState
12
13
 
13
14
 
14
15
  @dataclass
@@ -23,7 +24,12 @@ class RenderPipeline:
23
24
  - backend draw pass
24
25
  """
25
26
 
26
- def draw_packet(self, backend: Backend, packet: RenderPacket):
27
+ def draw_packet(
28
+ self,
29
+ backend: Backend,
30
+ packet: RenderPacket,
31
+ viewport_state: ViewportState,
32
+ ):
27
33
  """
28
34
  Draw the given RenderPacket using the provided Backend.
29
35
 
@@ -35,5 +41,23 @@ class RenderPipeline:
35
41
  """
36
42
  if not packet:
37
43
  return
38
- for op in packet.ops:
39
- op(backend)
44
+
45
+ backend.set_viewport_transform(
46
+ viewport_state.offset_x,
47
+ viewport_state.offset_y,
48
+ viewport_state.scale,
49
+ )
50
+
51
+ # backend.set_clip_rect(
52
+ # viewport_state.offset_x,
53
+ # viewport_state.offset_y,
54
+ # viewport_state.viewport_w,
55
+ # viewport_state.viewport_h,
56
+ # )
57
+
58
+ try:
59
+ for op in packet.ops:
60
+ op(backend)
61
+ finally:
62
+ backend.clear_clip_rect()
63
+ backend.clear_viewport_transform()
@@ -0,0 +1,203 @@
1
+ """
2
+ Viewport management for virtual to screen coordinate transformations.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from dataclasses import dataclass
8
+ from enum import Enum
9
+
10
+ from mini_arcade_core.utils import logger
11
+
12
+
13
+ class ViewportMode(str, Enum):
14
+ """
15
+ Viewport scaling modes.
16
+
17
+ :cvar FIT: Scale to fit within window, preserving aspect ratio (letterbox).
18
+ :cvar FILL: Scale to fill entire window, preserving aspect ratio (crop).
19
+ """
20
+
21
+ FIT = "fit" # letterbox
22
+ FILL = "fill" # crop
23
+
24
+
25
+ # Justification: Many attributes needed to describe viewport state
26
+ # pylint: disable=too-many-instance-attributes
27
+ @dataclass(frozen=True)
28
+ class ViewportState:
29
+ """
30
+ Current state of the viewport.
31
+
32
+ :ivar virtual_w (int): Virtual canvas width.
33
+ :ivar virtual_h (int): Virtual canvas height.
34
+ :ivar window_w (int): Current window width.
35
+ :ivar window_h (int): Current window height.
36
+ :ivar mode (ViewportMode): Current viewport mode.
37
+ :ivar scale (float): Current scale factor.
38
+ :ivar viewport_w (int): Width of the viewport rectangle on screen.
39
+ :ivar viewport_h (int): Height of the viewport rectangle on screen.
40
+ :ivar offset_x (int): X offset of the viewport rectangle on screen.
41
+ :ivar offset_y (int): Y offset of the viewport rectangle on screen.
42
+ """
43
+
44
+ virtual_w: int
45
+ virtual_h: int
46
+
47
+ window_w: int
48
+ window_h: int
49
+
50
+ mode: ViewportMode
51
+ scale: float
52
+
53
+ # viewport rect in screen pixels where the virtual canvas lands
54
+ # (can be larger than window in FILL mode -> offsets can be negative)
55
+ viewport_w: int
56
+ viewport_h: int
57
+ offset_x: int
58
+ offset_y: int
59
+
60
+
61
+ # pylint: enable=too-many-instance-attributes
62
+
63
+
64
+ class Viewport:
65
+ """
66
+ Manages viewport transformations between virtual and screen coordinates.
67
+ """
68
+
69
+ def __init__(
70
+ self,
71
+ virtual_w: int,
72
+ virtual_h: int,
73
+ mode: ViewportMode = ViewportMode.FIT,
74
+ ):
75
+ """
76
+ :param virtual_w: Virtual canvas width.
77
+ :type virtual_w: int
78
+
79
+ :param virtual_h: Virtual canvas height.
80
+ :type virtual_h: int
81
+
82
+ :param mode: Viewport scaling mode.
83
+ :type mode: ViewportMode
84
+ """
85
+ self._virtual_w = int(virtual_w)
86
+ self._virtual_h = int(virtual_h)
87
+ self._mode = mode
88
+ self._state: ViewportState | None = None
89
+
90
+ def set_virtual_resolution(self, w: int, h: int):
91
+ """
92
+ Set a new virtual resolution.
93
+
94
+ :param w: New virtual width.
95
+ :type w: int
96
+
97
+ :param h: New virtual height.
98
+ :type h: int
99
+ """
100
+ self._virtual_w = int(w)
101
+ self._virtual_h = int(h)
102
+ if self._state:
103
+ self.resize(self._state.window_w, self._state.window_h)
104
+
105
+ def set_mode(self, mode: ViewportMode):
106
+ """
107
+ Set a new viewport mode.
108
+
109
+ :param mode: New viewport mode.
110
+ :type mode: ViewportMode
111
+ """
112
+ self._mode = mode
113
+ if self._state:
114
+ self.resize(self._state.window_w, self._state.window_h)
115
+
116
+ def resize(self, window_w: int, window_h: int):
117
+ """
118
+ Resize the viewport based on the current window size.
119
+
120
+ :param window_w: Current window width.
121
+ :type window_w: int
122
+
123
+ :param window_h: Current window height.
124
+ :type window_h: int
125
+ """
126
+ window_w = int(window_w)
127
+ window_h = int(window_h)
128
+
129
+ sx = window_w / self._virtual_w
130
+ sy = window_h / self._virtual_h
131
+ scale = min(sx, sy) if self._mode == ViewportMode.FIT else max(sx, sy)
132
+
133
+ vw = int(round(self._virtual_w * scale))
134
+ vh = int(round(self._virtual_h * scale))
135
+ ox = int(round((window_w - vw) / 2))
136
+ oy = int(round((window_h - vh) / 2))
137
+
138
+ self._state = ViewportState(
139
+ virtual_w=self._virtual_w,
140
+ virtual_h=self._virtual_h,
141
+ window_w=window_w,
142
+ window_h=window_h,
143
+ mode=self._mode,
144
+ scale=float(scale),
145
+ viewport_w=vw,
146
+ viewport_h=vh,
147
+ offset_x=ox,
148
+ offset_y=oy,
149
+ )
150
+ logger.debug(
151
+ f"Viewport resized: window=({window_w}x{window_h}), "
152
+ f"virtual=({self._virtual_w}x{self._virtual_h}), "
153
+ f"mode={self._mode}, scale={scale:.3f}, "
154
+ f"viewport=({vw}x{vh})@({ox},{oy})"
155
+ )
156
+
157
+ @property
158
+ def state(self) -> ViewportState:
159
+ """
160
+ Get the current viewport state.
161
+
162
+ :return: Current ViewportState.
163
+ :rtype: ViewportState
164
+
165
+ :raises RuntimeError: If the viewport has not been initialized.
166
+ """
167
+ if self._state is None:
168
+ raise RuntimeError(
169
+ "Viewport not initialized. Call resize(window_w, window_h)."
170
+ )
171
+ return self._state
172
+
173
+ def screen_to_virtual(self, x: float, y: float) -> tuple[float, float]:
174
+ """
175
+ Convert screen coordinates to virtual coordinates.
176
+
177
+ :param x: X coordinate on the screen.
178
+ :type x: float
179
+
180
+ :param y: Y coordinate on the screen.
181
+ :type y: float
182
+
183
+ :return: Corresponding virtual coordinates (x, y).
184
+ :rtype: tuple[float, float]
185
+ """
186
+ s = self.state
187
+ return ((x - s.offset_x) / s.scale, (y - s.offset_y) / s.scale)
188
+
189
+ def virtual_to_screen(self, x: float, y: float) -> tuple[float, float]:
190
+ """
191
+ Convert virtual coordinates to screen coordinates.
192
+
193
+ :param x: X coordinate in virtual space.
194
+ :type x: float
195
+
196
+ :param y: Y coordinate in virtual space.
197
+ :type y: float
198
+
199
+ :return: Corresponding screen coordinates (x, y).
200
+ :rtype: tuple[float, float]
201
+ """
202
+ s = self.state
203
+ return (s.offset_x + x * s.scale, s.offset_y + y * s.scale)
@@ -143,8 +143,6 @@ class InputManager:
143
143
  )
144
144
  binding.command.execute(to_inject)
145
145
 
146
- # --- Convenience API ------------------------------------------------------
147
-
148
146
  def on_quit(self, command: BaseCommand, action: str = "quit"):
149
147
  """
150
148
  Bind a command to the QUIT event.
@@ -0,0 +1,20 @@
1
+ """
2
+ Module providing runtime adapters for window and scene management.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ from mini_arcade_core.runtime.audio.audio_port import AudioPort
8
+
9
+
10
+ class SDLAudioAdapter(AudioPort):
11
+ """A no-op audio adapter."""
12
+
13
+ def __init__(self, backend):
14
+ self.backend = backend
15
+
16
+ def load_sound(self, sound_id: str, file_path: str):
17
+ self.backend.load_sound(sound_id, file_path)
18
+
19
+ def play(self, sound_id: str, loops: int = 0):
20
+ self.backend.play_sound(sound_id, loops)