Skip to content

Commit

Permalink
Fixed Issue #4: differentiating void/ocean and water/blank regions
Browse files Browse the repository at this point in the history
This allows for distinct overlay images to be added for ocean and region water.
  • Loading branch information
kf6kjg committed Jul 11, 2017
1 parent c8866c8 commit dc021a0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 12 deletions.
6 changes: 6 additions & 0 deletions Source/Anaximander/Anaximander.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
;OceanColorGreen = 0
;OceanColorBlue = 255

; Image used to overlay the water in void space. Default is nothing.
;OceanOverlay = "/path/to/image"

; Image used to overlay the water in regions. Default is nothing.
;WaterOverlay = "/path/to/image"

; The color of the highest waterline, default is 0, 255, 255.
;BeachColorRed = 0
;BeachColorGreen = 255
Expand Down
5 changes: 5 additions & 0 deletions Source/Anaximander/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,17 @@ public static class Constants {

public static readonly Color BeachColor = Color.FromArgb(0, 255, 255);
public static readonly Color OceanColor = Color.FromArgb(0, 0, 255);

public const string OceanOverlay = "";

public const string OceanTileName = "ocean";

public const int PixelScale = 256;

public const string TileNameFormat = "map-{Z}-{X}-{Y}-objects";

public const string WaterOverlay = "";

// TileZooming
public const int HighestZoomLevel = 8;

Expand Down
22 changes: 15 additions & 7 deletions Source/Anaximander/Renderers/FlatRenderer/FlatTileRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Drawing;
using DataReader;
using System.Drawing.Drawing2D;
using Nini.Config;

namespace Anaximander {
Expand All @@ -35,18 +34,27 @@ public FlatTileRenderer(IConfigSource config) {
_tileInfo = config.Configs["MapTileInfo"];
}

public DirectBitmap RenderToBitmap(DirectBitmap mapbmp) {
return RenderToBitmap(Color.FromArgb(
public DirectBitmap RenderToBitmap(DirectBitmap mapbmp, Bitmap overlay = null) {
var dbitmap = RenderToBitmap(Color.FromArgb(
_tileInfo?.GetInt("OceanColorRed", Constants.OceanColor.R) ?? Constants.OceanColor.R,
_tileInfo?.GetInt("OceanColorGreen", Constants.OceanColor.G) ?? Constants.OceanColor.G,
_tileInfo?.GetInt("OceanColorBlue", Constants.OceanColor.B) ?? Constants.OceanColor.B
), mapbmp);

if (overlay != null) {
using (var gfx = Graphics.FromImage(dbitmap.Bitmap)) {
gfx.DrawImage(overlay, 0, 0, dbitmap.Width, dbitmap.Height);
}
}

return dbitmap;
}

public DirectBitmap RenderToBitmap(Color color, DirectBitmap mapbmp) {
using (var gfx = Graphics.FromImage(mapbmp.Bitmap))
using (var brush = new SolidBrush(color)) {
gfx.FillRectangle(brush, 0, 0, mapbmp.Width, mapbmp.Height);
using (var gfx = Graphics.FromImage(mapbmp.Bitmap)) {
using (var brush = new SolidBrush(color)) {
gfx.FillRectangle(brush, 0, 0, mapbmp.Width, mapbmp.Height);
}
}
return mapbmp;
}
Expand Down
59 changes: 56 additions & 3 deletions Source/Anaximander/Renderers/OBBRenderer/OBBRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using DataReader;
using log4net;
Expand All @@ -37,6 +39,7 @@ public class OBBRenderer : RegionRendererInterface {

private static Color _waterColor;
private static Color _beachColor;
private static DirectBitmap _waterOverlay;

public struct DrawStruct {
public float sort_order;
Expand All @@ -57,6 +60,39 @@ public OBBRenderer(IConfigSource config) {
tileInfo?.GetInt("BeachColorGreen", Constants.BeachColor.G) ?? Constants.BeachColor.G,
tileInfo?.GetInt("BeachColorBlue", Constants.BeachColor.B) ?? Constants.BeachColor.B
);

var waterOverlayPath = tileInfo?.GetString("WaterOverlay", Constants.WaterOverlay) ?? Constants.WaterOverlay;

var pixelScale = tileInfo?.GetInt("PixelScale", Constants.PixelScale) ?? Constants.PixelScale;

if (!string.IsNullOrWhiteSpace(waterOverlayPath)) {
try {
var overlay = new Bitmap(Image.FromFile(waterOverlayPath));
//overlay.MakeTransparent();

_waterOverlay = new DirectBitmap(pixelScale, pixelScale);
using (var gfx = Graphics.FromImage(_waterOverlay.Bitmap)) {
gfx.CompositingMode = CompositingMode.SourceCopy;
gfx.DrawImage(overlay, 0, 0, pixelScale, pixelScale);
}
}
catch (Exception e) {
LOG.Warn($"Error loading water overlay file '{waterOverlayPath}', skipping.", e);
}

// Convert to premultiplied alpha
for (int y = 0; y < _waterOverlay.Bitmap.Height; y++) {
for (int x = 0; x < _waterOverlay.Bitmap.Width; x++) {
var c = _waterOverlay.Bitmap.GetPixel(x, y);

var r = c.R * c.A / 255;
var g = c.G * c.A / 255;
var b = c.B * c.A / 255;

_waterOverlay.Bitmap.SetPixel(x, y, Color.FromArgb(c.A, r, g, b));
}
}
}
}

public DirectBitmap RenderTileFrom(DataReader.Region region, DirectBitmap bitmap) {
Expand Down Expand Up @@ -186,8 +222,9 @@ private void TerrainToBitmap(DataReader.Region region, DirectBitmap mapbmp) {
low = tmp;
}

HSV hsv;
Color result;
if (heightvalue > waterHeight) {
HSV hsv;
// Above water
if (hmod <= low)
hsv = hsv1; // too low
Expand All @@ -205,6 +242,8 @@ private void TerrainToBitmap(DataReader.Region region, DirectBitmap mapbmp) {
else
hsv = hsv3.InterpolateHSV(ref hsv4, (float)((hmod * 3d) - 2d));
}

result = hsv.ToColor();
}
else {
// Under water.
Expand All @@ -213,7 +252,21 @@ private void TerrainToBitmap(DataReader.Region region, DirectBitmap mapbmp) {

var water = deepwater.InterpolateHSV(ref beachwater, (float)MathUtilities.SCurve(heightvalue / waterHeight));

hsv = water;
if (_waterOverlay == null) {
result = water.ToColor();
}
else {
// Overlay the water image
var baseColor = water.ToColor();

var overlayColor = _waterOverlay.Bitmap.GetPixel(x, y);

var resultR = overlayColor.R + (baseColor.R * (255 - overlayColor.A) / 255);
var resultG = overlayColor.G + (baseColor.G * (255 - overlayColor.A) / 255);
var resultB = overlayColor.B + (baseColor.B * (255 - overlayColor.A) / 255);

result = Color.FromArgb(resultR, resultG, resultB);
}
}

// Shade the terrain for shadows
Expand All @@ -240,7 +293,7 @@ private void TerrainToBitmap(DataReader.Region region, DirectBitmap mapbmp) {
// }
//}

mapbmp.Bitmap.SetPixel(x, yr, hsv.ToColor());
mapbmp.Bitmap.SetPixel(x, yr, result);
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion Source/Anaximander/Renderers/TileGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using log4net;
using Nini.Config;
Expand All @@ -39,6 +41,8 @@ public class TileGenerator {

private readonly FlatTileRenderer _flatRenderer;

private static Bitmap _oceanOverlay;

public TileGenerator(IConfigSource config) {
var tileInfo = config.Configs["MapTileInfo"];
_pixelSize = tileInfo?.GetInt("PixelScale", Constants.PixelScale) ?? Constants.PixelScale;
Expand All @@ -55,11 +59,31 @@ public TileGenerator(IConfigSource config) {
}

_flatRenderer = new FlatTileRenderer(config);

var oceanOverlayPath = tileInfo?.GetString("OceanOverlay", Constants.OceanOverlay) ?? Constants.OceanOverlay;

var pixelScale = tileInfo?.GetInt("PixelScale", Constants.PixelScale) ?? Constants.PixelScale;

if (!string.IsNullOrWhiteSpace(oceanOverlayPath)) {
try {
var overlay = new Bitmap(Image.FromFile(oceanOverlayPath));
//overlay.MakeTransparent();

_oceanOverlay = new Bitmap(pixelScale, pixelScale);
using (var gfx = Graphics.FromImage(_oceanOverlay)) {
gfx.CompositingMode = CompositingMode.SourceCopy;
gfx.DrawImage(overlay, 0, 0, pixelScale, pixelScale);
}
}
catch (Exception e) {
LOG.Warn($"Error loading ocean overlay file '{oceanOverlayPath}', skipping.", e);
}
}
}

public DirectBitmap GenerateOceanTile() {
var bitmap = new DirectBitmap(_pixelSize, _pixelSize);
_flatRenderer.RenderToBitmap(bitmap);
_flatRenderer.RenderToBitmap(bitmap, _oceanOverlay);
return bitmap;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Anaximander/Texture/DirectBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class DirectBitmap : IDisposable {
public DirectBitmap(int width, int height) {
Width = width;
Height = height;
Bits = new Int32[width * height];
Bits = new int[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
Expand Down

0 comments on commit dc021a0

Please sign in to comment.