winzik/engine/tests/Renderer/HdTest.php
cdy-respawnsive ad389c5efc HD 2D (game.yaml hd), sharp TTF text with bold / outline, WebGL layer composed by the 2D's alpha
All three are opt-in: a game that does not ask for them draws exactly as before.

- Renderer::setHd() (game.yaml `hd: auto` or 2..4; default 1 = off): the view texture is made hd times bigger and
  SDL_RenderSetLogicalSize scales every drawing call to it, so games keep their view coordinates (translate()
  included) while text, circles and positions at fractions of a pixel are drawn at the window's resolution.
  viewPixels() gives the texture's real size (OpenGl::begin / end and WebGl's 'view' stage use it; = the view's
  size without HD).
- Renderer::clear($color, $keepAlpha = false): opaque as before; with true the colour's alpha is kept.
- TtfFont: in HD the glyphs are rendered at their real size and drawn back at their view size; new optional
  arguments style (TtfFont::BOLD / ITALIC), thicken (a thin font made bolder) and outline (draw()'s shadow colour
  becomes an outline around the letters). Without HD and without them: the same font, the same sizes.
- WebGl::setLayer('below', 'view', null): no key colour; the 2D's own alpha says where the 3D shows (clear it with
  clear([0, 0, 0, 0], true)), so smooth text and translucent panels of any colour lie over the 3D. The key colour
  stays the default.

Tests: engine/tests/Renderer/HdTest.php (off by default, a frame in HD, text sizes with and without HD).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 18:40:24 +01:00

91 lines
3.2 KiB
PHP

<?php
namespace JumPHP\Tests\Renderer;
use JumPHP\Kernel\GameConfig;
use JumPHP\Renderer\Renderer;
use JumPHP\Renderer\Sdl;
use JumPHP\Renderer\TtfFont;
use PHPUnit\Framework\TestCase;
/**
* HD 2D (game.yaml `hd`, Renderer::setHd()) and TtfFont's options: off by default, nothing changes for a game that
* does not ask for them; on, the view texture is bigger while every size stays in view pixels.
*/
final class HdTest extends TestCase
{
private const FONT = __DIR__ . '/../../../skeleton-gui/assets/fonts/ui.ttf';
private static ?Sdl $sdl = null;
/** One headless SDL (software renderer into a surface) for the class, back to no HD before each test. */
private function renderer(): Renderer
{
if (self::$sdl === null) {
putenv('JUMPHP_TEST=1');
self::$sdl = new Sdl(new GameConfig([], sys_get_temp_dir()));
self::$sdl->initSDL(false, 'hd', 320, 180, false, 1);
}
$r = self::$sdl->getRenderer();
$r->setHd(1);
return $r;
}
public function testHdIsOffByDefault(): void
{
$r = $this->renderer();
self::assertSame(1, $r->hd());
self::assertSame([320, 180], $r->viewPixels());
self::assertSame([$r->getWidth(), $r->getHeight()], $r->viewPixels());
}
public function testAFixedHdMakesTheViewTextureBiggerAndKeepsTheViewSize(): void
{
$r = $this->renderer();
$r->setHd(3);
self::assertSame(3, $r->hd());
self::assertSame([960, 540], $r->viewPixels());
self::assertSame(320, $r->getWidth());
self::assertSame(180, $r->getHeight());
// a whole frame draws without an error, translation included
$r->beginFrame([10, 20, 30]);
$r->translate(5, 5);
$r->fillRect(0.5, 0.5, 10.25, 3, [255, 0, 0, 128]);
$r->fillCircle(40, 40, 6.5, '#00ff00');
$r->drawLine(0, 0, 50.5, 20.5, [255, 255, 255]);
$r->translate(-5, -5);
$r->clear([0, 0, 0, 0], true);
$r->endFrame();
$r->setHd(1);
self::assertSame([320, 180], $r->viewPixels());
}
public function testTextKeepsItsSizeInViewPixelsWithAndWithoutHd(): void
{
if (!is_file(self::FONT)) {
self::markTestSkipped('no font file');
}
$this->renderer();
$plain = new TtfFont(self::$sdl, self::FONT, 8);
$width = $plain->width('Hello world', 2);
$height = $plain->height(2);
// the new options, unused: the same font
$default = new TtfFont(self::$sdl, self::FONT, 8, false, 0, 0.0, 0.0);
self::assertSame($width, $default->width('Hello world', 2));
self::assertSame($height, $default->height(2));
// in HD the glyphs are rendered at 4x their size, measured back in view pixels: about the same
self::$sdl->getRenderer()->setHd(4);
$hd = new TtfFont(self::$sdl, self::FONT, 8, true, TtfFont::BOLD, 0.3, 0.5);
self::assertEqualsWithDelta($width, $hd->width('Hello world', 2), $width * 0.15);
self::assertEqualsWithDelta($height, $hd->height(2), 3);
$r = self::$sdl->getRenderer();
$r->beginFrame();
$hd->draw($r, "Hello\nworld", 10, 10, 2, [255, 255, 255], 'center', [0, 0, 0]);
$r->endFrame();
$r->setHd(1);
}
}