Go back to widescreen. Try a new upscaling method

This commit is contained in:
Pyrex 2023-09-09 17:36:37 -07:00
parent 79e5e94cc2
commit 50a5fc6985
5 changed files with 23 additions and 73 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -5,10 +5,8 @@ import 'package:dartterm/terminal_painter.dart';
import 'package:flutter/material.dart';
class Terminal {
static const int width = 80;
static const int width = 88;
static const int height = 50;
static const int cellW = 64;
static const int cellH = 64;
static const int nTiles = width * height;
late List<Tile> tiles;
@ -90,8 +88,6 @@ class Content {
Terminal terminal = Terminal();
const int width = Terminal.width;
const int height = Terminal.height;
const int cellW = Terminal.cellW;
const int cellH = Terminal.cellH;
const int nTiles = Terminal.nTiles;
Widget toWidget(BuildContext context) {

View File

@ -1,4 +1,5 @@
import 'dart:ui' as ui;
import 'dart:ui';
import 'package:dartterm/assets.dart';
import 'package:dartterm/fonts.dart';
@ -9,6 +10,9 @@ class TerminalCustomPainter extends CustomPainter {
Terminal t;
double scalingFactor;
static const int cellW = 8;
static const int cellH = 8;
TerminalCustomPainter(this.t, this.scalingFactor);
@override
@ -18,76 +22,32 @@ class TerminalCustomPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..filterQuality = FilterQuality.high;
var pr = PictureRecorder();
// == Fill backdrop ==
final paint2 = Paint()
..color = Colors.black
..style = PaintingStyle.fill;
canvas.drawRect(const Offset(0, 0) & size, paint2);
var smallSize = Size((cellW * Terminal.width).toDouble(),
(cellH * Terminal.height).toDouble());
// == Estimate dimensions for scaling ==
var sx = size.width / Terminal.width;
var sy = size.height / Terminal.height;
var dpx = 0.0;
var dpy = 0.0;
var invScalingFactor = 1 / scalingFactor;
var virtualCanvas = Canvas(pr, const Offset(0, 0) & smallSize);
virtualPaint(virtualCanvas, smallSize);
var image = pr
.endRecording()
// Renderer does something bizarre if I don't multiply by something greater than 1
.toImageSync(cellW * Terminal.width * 2, cellH * Terminal.height * 2);
canvas.drawImageRect(image, const Offset(0, 0) & smallSize,
const Offset(0, 0) & size, Paint()..filterQuality = FilterQuality.none);
}
for (double preferredSx in [64, 56, 48, 40, 32, 24, 16, 8]) {
preferredSx *= invScalingFactor;
var preferredSy = preferredSx;
if (preferredSx < sx && preferredSy < sy) {
sx = preferredSx.toDouble();
sy = preferredSy.toDouble();
dpx = ((size.width - (sx * Terminal.width)) / 2);
dpy = ((size.height - (sy * Terminal.height)) / 2);
dpx /= invScalingFactor;
dpx = dpx.floorToDouble();
dpx *= invScalingFactor;
dpy /= invScalingFactor;
dpy = dpy.floorToDouble();
dpy *= invScalingFactor;
break;
}
}
for (var i = 0; i < nTiles; i++) {
final tile = t.tiles[i];
final (pcxDst, pcyDst) = terminal.toXY(i) ?? (0, 0);
final (pxDst, pyDst) = (dpx + pcxDst * sx, dpy + pcyDst * sy);
final rectDst = Rect.fromLTWH(pxDst, pyDst, sx, sy);
var content = tile.content;
if (content != null) {
var source = content.sourceImage;
var image = assets.getImageIfAvailable(source);
if (image != null) {
var fgRect = Rect.fromLTWH(
content.sourceCx.toDouble() * cellW,
content.sourceCy.toDouble() * cellH,
cellW.toDouble(),
cellH.toDouble());
canvas.drawImageRect(
image,
fgRect,
rectDst,
paint,
);
}
}
}
void virtualPaint(Canvas canvas, Size size) {
final paint = Paint();
var todos = Todos();
// == Draw the background and foreground of every tile ==
for (var i = 0; i < nTiles; i++) {
final (pcxDst, pcyDst) = terminal.toXY(i) ?? (0, 0);
final (pxDst, pyDst) = (dpx + pcxDst * sx, dpy + pcyDst * sy);
final rectDst = Rect.fromLTWH(pxDst, pyDst, sx, sy);
final (pxDst, pyDst) = (pcxDst * cellW, pcyDst * cellH);
final rectDst = Rect.fromLTWH(pxDst.toDouble(), pyDst.toDouble(),
cellW.toDouble(), cellH.toDouble());
final tile = t.tiles[i];
@ -137,15 +97,9 @@ class ImageTodos {
ImageTodos(this.atlas);
void add(Rect src, Rect dst, Color color) {
// NOTE: Because main.dart uses aspectRatio, scaleX and scaleY should be close to the same
var scaleX = dst.width / src.width;
// dst.width / src.width;
// log("scaleX: $scaleX ($dstWidth, $srcWidth)");
// var scaleY = dst.height / src.height;
transforms.add(RSTransform.fromComponents(
rotation: 0.0,
scale: scaleX,
scale: 1.0,
anchorX: 0.0,
anchorY: 0.0,
translateX: dst.left,