Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render map to PNG format #1632

Merged
merged 25 commits into from
Nov 20, 2023
Merged

render map to PNG format #1632

merged 25 commits into from
Nov 20, 2023

Conversation

kostmo
Copy link
Member

@kostmo kostmo commented Nov 16, 2023

Towards #1415.

Uses

This capability could be used to quickly iterate on DSL world descriptions, e.g. when tuning noise parameters.

Implementation notes

  • For the hard-coded ANSI terminal color names, I chose RGB triples that matched my own terminal settings. This means that a rendered PNG might not exactly match one's own terminal colors.
  • Blank terrain corresponds to a transparent pixel.
  • Implemented parse-time validation of attr references. Previously, referencing a nonexistent attr by an entity would fail silently at runtime.
  • Normalization: strings like "rock" now only exist once; the string is shared via toplevel variable definitions
  • Entities and terrain have TUI-independent color definitions from which VTY Attrs are derived, but all TUI user-interface colors are defined only as VTY Attrs.

Demos

Each pixel in the output image correponds to one world cell. To enlarge, can use imagemagick:

stack run -- map data/scenarios/classic.yaml --seed 0 --png -w 300 -h 200 && convert output.png -scale 800% out2.png

out2

stack run -- map data/scenarios/Challenges/bridge-building.yaml --png && convert output.png -scale 800% out2.png

image

@kostmo kostmo force-pushed the feature/render-png branch 2 times, most recently from aa10513 to 0453ed1 Compare November 18, 2023 06:49
@kostmo kostmo changed the title [WIP] render map to PNG format render map to PNG format Nov 18, 2023
@kostmo kostmo marked this pull request as ready for review November 18, 2023 07:29
@kostmo kostmo requested a review from byorgey November 18, 2023 07:29
@kostmo kostmo added the CHANGELOG Once merged, this PR should be highlighted in the changelog for the next release. label Nov 18, 2023
@kostmo kostmo force-pushed the feature/render-png branch 2 times, most recently from 4014896 to de6e1fd Compare November 18, 2023 08:02
@byorgey
Copy link
Member

byorgey commented Nov 19, 2023

FYI, I'm still in the middle of reviewing this PR, but even before I'm finished it's worth noting that this PR somehow changes all the colors in my terminal and it looks really bad:

bad-colors

For comparison, this is what it looks like on the main branch:

good-colors

@kostmo
Copy link
Member Author

kostmo commented Nov 19, 2023

PR somehow changes all the colors in my terminal

I see... I have a theory that the ANSI color constants cannot be reproduced with RGB equivalents. I will try creating a parallel "vty-independent" enum that will map back to the vty constants for TUI rendering.

@kostmo
Copy link
Member Author

kostmo commented Nov 19, 2023

Ah, fixed the terrain color, at least, with 42db654.

@byorgey
Copy link
Member

byorgey commented Nov 19, 2023

Hmm, that's better, but the water and tree colors still look strange to me:

better-colors

@kostmo
Copy link
Member Author

kostmo commented Nov 19, 2023

Ok, have now fixed trees/water (via introducing NamedColor) and have fixed PNG colors by round-tripping through the terminal colorspace (i.e. color240CodeToRGB . rgbColorToColor240).

@kostmo kostmo force-pushed the feature/render-png branch 2 times, most recently from 94bcf54 to e85517e Compare November 19, 2023 06:59
Copy link
Member

@byorgey byorgey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool! Definitely a nice feature to be able to render the world to a PNG, and I think I will make heavy use of it when I get back around to stuff like adding cities and so on to the "classic" world.

src/Swarm/Game/Entity/Cosmetic/Specimen.hs Outdated Show resolved Hide resolved
app/Main.hs Outdated Show resolved Hide resolved
src/Swarm/Game/Entity/Cosmetic.hs Outdated Show resolved Hide resolved
swarm.cabal Show resolved Hide resolved
app/Main.hs Outdated Show resolved Hide resolved
app/Main.hs Outdated Show resolved Hide resolved
@kostmo kostmo added the merge me Trigger the merge process of the Pull request. label Nov 20, 2023
@mergify mergify bot merged commit 01a5b07 into main Nov 20, 2023
10 checks passed
@mergify mergify bot deleted the feature/render-png branch November 20, 2023 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CHANGELOG Once merged, this PR should be highlighted in the changelog for the next release. merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants