Skip to content

Map Graphics

Maximilian Dorn edited this page Aug 30, 2022 · 2 revisions

Map Graphics

The heart and soul of maps is the MapGraphics class. It's basically the 2D graphics engine that powers this project. Everything in regards to drawing stuff on maps happens here.

Implementations

The base class already has most things implemented, the only thing that implementations need to implement are setPixel(), getPixel(), renderOnto(), copy(), getWidth() and getHeight(). Implementations can also optionally implement getDirectAccessData() to allow for faster write operations.

ClientsideMapGraphics
This implementation targets the ClientsideMap class. It's used to draw stuff on single ClientsideMaps.

MapScreenGraphics
This implementation targets MapScreens. It's used to draw stuff on MapScreens, aka an array of ClientsideMaps.

FastMapScreenGraphics
This implementation also targets MapScreens. This implementation provides a direct access buffer with a custom render logic for optimal performance.

StandaloneMapGraphics
This implementation is basically just a blank canvas with no real render target. You can use this to store basically any sort of image or graphics data. This implementation also provides direct access to the buffer for optimal performance.

Example usage
// These two methods do exactly the same, the only difference is the name
final MapGraphics<?, ?> graphics1 = StandaloneMapGraphics.standalone(256, 256);
final MapGraphics<?, ?> graphics2 = StandaloneMapGraphics.newGraphicsObject(300, 500);
// Copy the contents of a MapGraphics object into a new standalone object
MapGraphics<?, ?> graphics3 = StandaloneMapGraphics.copyOf(grpahics1);

FunctionalMapGraphics
This isn't really an implementation, it's more of a utility. You can use this to perform operations on a MapGraphics object in a functional manner.

Example usage
MapGraphics<?, ?> graphics = ...;
FunctionalMapGraphics.backedBy(graphics)
    .with(g -> g.fillComplete(ColorCache.rgbToMap(0, 0, 0)))
    .with(g -> g.fillRect(10, 10, 100, 100, ColorCache.rgbToMap(255, 255, 255), 1f))

Performance

MapGraphics implementations with direct buffer access are pretty fast. Depending on the workload the engine can perform multiple write operations in under one millisecond. Please take a look at the following two videos to see how the engine performs. In both videos the time it takes for the engine to render is shown in the action bar.

Minecraft Map UIs Devlog #2 - 2D engine rework
Minecraft Map UIs Devlog #3 - Proper per-element animations