Skip to content

Commit

Permalink
Some more Functions for Imaging
Browse files Browse the repository at this point in the history
Lay some groundwork for an 2D Rendering Engine.
  • Loading branch information
LoneWandererProductions committed Dec 27, 2023
1 parent 240d117 commit 3ab8ee1
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Aurorae/Aurora.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<UserControl x:Class="Aurorae.Aurora"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Aurorae"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Image Name="LayerOne"/>
<Image Name="LayerTwo"/>
<Image Name="LayerThree"/>
<Label Name="Touch" MouseDown="Touch_MouseDown"/>
</Grid>
</UserControl>
208 changes: 208 additions & 0 deletions Aurorae/Aurora.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using Imaging;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ExtendedSystemObjects;

namespace Aurorae
{
/// <inheritdoc cref="UserControl" />
/// <summary>
/// Generate a playing field
/// </summary>
public partial class Aurora
{
private readonly ImageRender _render = new();

public static readonly DependencyProperty MapHeight = DependencyProperty.Register(nameof(MapHeight),
typeof(int),
typeof(Aurora), null);

public static readonly DependencyProperty MapWidth = DependencyProperty.Register(nameof(MapWidth),
typeof(int),
typeof(Aurora), null);

public static readonly DependencyProperty TextureSize = DependencyProperty.Register(nameof(TextureSize),
typeof(int),
typeof(Aurora), null);

public static readonly DependencyProperty Map = DependencyProperty.Register(nameof(Map),
typeof(List<int>),
typeof(Aurora), null);

public static readonly DependencyProperty Textures = DependencyProperty.Register(nameof(Textures),
typeof(List<Texture>),
typeof(Aurora), null);

public static readonly DependencyProperty Grid = DependencyProperty.Register(nameof(Grid),
typeof(bool),
typeof(Aurora), null);

public static readonly DependencyProperty Number = DependencyProperty.Register(nameof(Number),
typeof(bool),
typeof(Aurora), null);

private Bitmap _LayerOne;
private Bitmap _LayerTwo;
private Bitmap _LayerThree;
private Cursor _cursor;

public int DependencyHeight
{
get => (int)GetValue(MapHeight);
set => SetValue(MapHeight, value);
}


public int DependencyWidth
{
get => (int)GetValue(MapWidth);
set => SetValue(MapWidth, value);
}

public int DependencyTextureSize
{
get => (int)GetValue(TextureSize);
set => SetValue(TextureSize, value);
}

public List<int> DependencyMap
{
get => (List<int>)GetValue(Map);
set => SetValue(Map, value);
}

public List<Texture> DependencyTextures
{
get => (List<Texture>)GetValue(Textures);
set => SetValue(Map, value);
}

public bool DependencyGrid
{
get => (bool)GetValue(Grid);
set => SetValue(Grid, value);
}

public bool DependencyNumber
{
get => (bool)GetValue(Number);
set => SetValue(Number, value);
}

public Aurora()
{
InitializeComponent();
Initiate();
}

private void Initiate()
{
if (DependencyWidth == 0 || DependencyHeight == 0 || DependencyTextureSize == 0) return;

_LayerOne = new Bitmap(DependencyWidth * DependencyTextureSize, DependencyHeight * DependencyTextureSize);
Generate();

if (DependencyGrid) _LayerTwo = new Bitmap(DependencyWidth * DependencyTextureSize, DependencyHeight * DependencyTextureSize);

_LayerThree = new Bitmap(DependencyWidth * DependencyTextureSize, DependencyHeight * DependencyTextureSize);

Touch.Height = _LayerOne.Height;
Touch.Width = _LayerOne.Width;
}

private void Generate()
{
DrawMap();
LayerOne.Source = _LayerOne.ToBitmapImage();

if (DependencyGrid)
{
DrawGrid();
LayerTwo.Source = _LayerTwo.ToBitmapImage();
}

if (DependencyNumber)
{
DrawNumbers();
LayerThree.Source = _LayerThree.ToBitmapImage();
}
}

private void DrawMap()
{
var boxes = new List<Box>();

var layers = DependencyMap.ChunkBy(DependencyHeight);


for (var y = 0; y < layers.Count; y++)
{
var slice = layers[y];

for (var x = 0; x < slice.Count; x++)
{
var id = slice[x];
if (id <= 0) continue;

var texture = DependencyTextures[id];
var image = _render.GetBitmapFile(texture.Path);

var box = new Box
{
X = x * DependencyTextureSize,
Y = y * DependencyTextureSize,
Image = image,
Layer = texture.Layer
};

boxes.Add(box);
}
}

foreach (var slice in boxes.OrderBy(layer => layer.Layer).ToList())
{
_LayerOne = _render.CombineBitmap(_LayerOne, slice.Image, slice.X, slice.Y);
}
}

private void DrawGrid()
{
for (int y = 0; y < DependencyHeight; y++)
{
for (int x = 0; x < DependencyWidth; x++)
{
using Graphics graphics = Graphics.FromImage(_LayerTwo);
graphics.DrawRectangle(Pens.Black, x * DependencyTextureSize, y * DependencyTextureSize, DependencyTextureSize, DependencyTextureSize);
}
}
}

private void DrawNumbers()
{
throw new System.NotImplementedException();
}

private void Touch_MouseDown(object sender, MouseButtonEventArgs e)
{
_cursor = new Cursor();
var position = e.GetPosition(Touch);


if (position.X < DependencyTextureSize) _cursor.X = 0;
else
{
_cursor.X = (int)position.X / DependencyTextureSize;
}

if (position.Y < DependencyTextureSize) _cursor.Y = 0;
else
{
_cursor.Y = (int)position.X / DependencyTextureSize;
}
}
}
}
13 changes: 13 additions & 0 deletions Aurorae/Aurorae.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ExtendedSystemObjects\ExtendedSystemObjects.csproj" />
<ProjectReference Include="..\Imaging\Imaging.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Aurorae/Box.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Drawing;

namespace Aurorae
{
internal sealed class Box
{
internal Bitmap Image { get; set; }

internal int X { get; set; }

internal int Y { get; set; }

internal int Layer { get; set; }
}
}
9 changes: 9 additions & 0 deletions Aurorae/Coordinate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Aurorae
{
public class Cursor
{
public int X { get; set; }

public int Y { get; set; }
}
}
9 changes: 9 additions & 0 deletions Aurorae/Texture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Aurorae
{
public sealed class Texture
{
public string Path { get; set; }

public int Layer { get; set; }
}
}
6 changes: 6 additions & 0 deletions CoreLibrary.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mathematics", "Mathematics\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Imaging", "Imaging\Imaging.csproj", "{810F6A32-3C86-4DB8-B3C3-F9CF438A1D47}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aurorae", "Aurorae\Aurorae.csproj", "{A8CCA803-65EC-449A-ACDB-E2FA89F3D240}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -149,6 +151,10 @@ Global
{810F6A32-3C86-4DB8-B3C3-F9CF438A1D47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{810F6A32-3C86-4DB8-B3C3-F9CF438A1D47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{810F6A32-3C86-4DB8-B3C3-F9CF438A1D47}.Release|Any CPU.Build.0 = Release|Any CPU
{A8CCA803-65EC-449A-ACDB-E2FA89F3D240}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8CCA803-65EC-449A-ACDB-E2FA89F3D240}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8CCA803-65EC-449A-ACDB-E2FA89F3D240}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8CCA803-65EC-449A-ACDB-E2FA89F3D240}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
11 changes: 11 additions & 0 deletions Imaging/IImageRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ internal interface IImageRender
/// <exception cref="ArgumentNullException">if Image is null</exception>
Bitmap CombineBitmap(List<string> files);

/// <summary>
/// Combines the bitmaps.
/// </summary>
/// <param name="original">The original image.</param>
/// <param name="overlay">The overlay image.</param>
/// <param name="x">The x position.</param>
/// <param name="y">The y position.</param>
/// <returns>Combined Image</returns>
/// <exception cref="ArgumentNullException"></exception>
Bitmap CombineBitmap(Bitmap original, Bitmap overlay, int x, int y);

/// <summary>
/// Cuts a piece out of a bitmap.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions Imaging/ImageRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ public Bitmap CombineBitmap(List<string> files)
return ImageStream.CombineBitmap(files);
}

/// <inheritdoc />
/// <summary>
/// Combines the bitmaps.
/// </summary>
/// <param name="original">The original image.</param>
/// <param name="overlay">The overlay image.</param>
/// <param name="x">The x position.</param>
/// <param name="y">The y position.</param>
/// <returns>Combined Image</returns>
/// <exception cref="ArgumentNullException"></exception>
[return: MaybeNull]
public Bitmap CombineBitmap(Bitmap original, Bitmap overlay, int x, int y)
{
return ImageStream.CombineBitmap(original, overlay, x, y);
}

/// <inheritdoc />
/// <summary>
/// Cuts a piece out of a bitmap.
Expand Down
33 changes: 33 additions & 0 deletions Imaging/ImageStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,39 @@ internal static Bitmap CombineBitmap(List<string> files)
return btm;
}

/// <summary>
/// Combines the bitmaps.
/// </summary>
/// <param name="original">The original image.</param>
/// <param name="overlay">The overlay image.</param>
/// <param name="x">The x position.</param>
/// <param name="y">The y position.</param>
/// <returns>Combined Image</returns>
/// <exception cref="ArgumentNullException"></exception>
internal static Bitmap CombineBitmap(Bitmap original, Bitmap overlay, int x, int y)
{
if (original == null)
{
var innerException = new ArgumentNullException(nameof(original));
throw new ArgumentNullException(ImagingResources.ErrorWrongParameters, innerException);
}

if (overlay == null)
{
var innerException = new ArgumentNullException(nameof(overlay));
throw new ArgumentNullException(ImagingResources.ErrorWrongParameters, innerException);
}


//get a graphics object from the image so we can draw on it
using var graph = Graphics.FromImage(original);

graph.DrawImage(overlay,
new Rectangle(x, y, overlay.Width, overlay.Height));

return original;
}

/// <summary>
/// Cuts a piece out of a bitmap.
/// </summary>
Expand Down

0 comments on commit 3ab8ee1

Please sign in to comment.