2018-11-22 00:18:42 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using SDL2;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using OpenDiablo2.Common.Models;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.SDL2_
|
|
|
|
|
{
|
2018-11-22 16:22:39 -05:00
|
|
|
|
internal sealed class SDL2Sprite : ISprite
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
2018-11-22 20:23:00 -05:00
|
|
|
|
static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
public Point Location { get; set; } = new Point();
|
|
|
|
|
public Size FrameSize { get; set; } = new Size();
|
|
|
|
|
public int Frame { get; set; }
|
|
|
|
|
public int TotalFrames { get; internal set; }
|
|
|
|
|
|
2018-11-22 18:06:15 -05:00
|
|
|
|
private bool blend = false;
|
|
|
|
|
public bool Blend
|
|
|
|
|
{
|
|
|
|
|
get => blend;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
blend = value;
|
2018-11-22 20:23:00 -05:00
|
|
|
|
|
2018-11-22 18:06:15 -05:00
|
|
|
|
foreach (var texture in textures)
|
|
|
|
|
SDL.SDL_SetTextureBlendMode(texture, blend ? SDL.SDL_BlendMode.SDL_BLENDMODE_ADD : SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
private Palette palette;
|
|
|
|
|
public Palette CurrentPalette
|
|
|
|
|
{
|
|
|
|
|
get => palette;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
palette = value;
|
|
|
|
|
UpdateTextureData();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 12:35:21 -05:00
|
|
|
|
internal readonly ImageSet source;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
private readonly IntPtr renderer;
|
|
|
|
|
internal IntPtr[] textures = new IntPtr[0];
|
|
|
|
|
|
|
|
|
|
public SDL2Sprite(ImageSet source, IntPtr renderer)
|
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
|
|
|
|
|
|
2018-11-23 00:41:39 -05:00
|
|
|
|
this.TotalFrames = source.Frames.Count();
|
|
|
|
|
this.FrameSize = new Size(Pow2((int)source.Frames.Max(x => x.Width)), Pow2((int)source.Frames.Max(x => x.Height)));
|
|
|
|
|
this.textures = new IntPtr[TotalFrames];
|
|
|
|
|
for(var i = 0; i < this.textures.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
this.textures[i] = IntPtr.Zero;
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal Point GetRenderPoint()
|
|
|
|
|
=> new Point(
|
|
|
|
|
Location.X + source.Frames[Frame].OffsetX,
|
|
|
|
|
(Location.Y - FrameSize.Height) + source.Frames[Frame].OffsetY
|
|
|
|
|
);
|
|
|
|
|
|
2018-11-22 12:19:38 -05:00
|
|
|
|
public Size LocalFrameSize => new Size((int)source.Frames[Frame].Width, (int)source.Frames[Frame].Height);
|
|
|
|
|
|
2018-11-22 23:44:01 -05:00
|
|
|
|
// TODO: This is slow. Make fix.
|
2018-11-22 00:18:42 -05:00
|
|
|
|
private void UpdateTextureData()
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < source.Frames.Count(); i++)
|
2018-11-22 23:44:01 -05:00
|
|
|
|
LoadFrame(i, renderer);
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Less dumb color correction
|
|
|
|
|
private Color AdjustColor(Color source)
|
|
|
|
|
=> Color.FromArgb(
|
|
|
|
|
source.A,
|
2018-11-22 16:22:39 -05:00
|
|
|
|
(byte)Math.Min((float)source.R * 1.2, 255),
|
|
|
|
|
(byte)Math.Min((float)source.G * 1.2, 255),
|
|
|
|
|
(byte)Math.Min((float)source.B * 1.2, 255)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
);
|
|
|
|
|
|
2018-11-22 23:44:01 -05:00
|
|
|
|
object bob = new object();
|
|
|
|
|
private void LoadFrame(int index, IntPtr renderer)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
2018-11-22 23:44:01 -05:00
|
|
|
|
var frame = source.Frames[index];
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-11-22 23:44:01 -05:00
|
|
|
|
if (textures[index] == IntPtr.Zero)
|
|
|
|
|
textures[index] = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, Pow2(FrameSize.Width), Pow2(FrameSize.Height));
|
|
|
|
|
|
|
|
|
|
if (textures[index] == IntPtr.Zero)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
throw new ApplicationException("Unaple to initialize texture.");
|
|
|
|
|
|
2018-11-22 23:44:01 -05:00
|
|
|
|
SDL.SDL_SetTextureBlendMode(textures[index], SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
|
|
|
|
SDL.SDL_SetRenderTarget(renderer, textures[index]);
|
2018-11-22 00:18:42 -05:00
|
|
|
|
SDL.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
|
|
|
|
SDL.SDL_RenderFillRect(renderer, IntPtr.Zero);
|
|
|
|
|
SDL.SDL_SetRenderTarget(renderer, IntPtr.Zero);
|
|
|
|
|
|
|
|
|
|
var binaryData = new UInt32[frame.Width * frame.Height];
|
2018-11-22 23:44:01 -05:00
|
|
|
|
for (var y = 0; y < frame.Height; y++)
|
|
|
|
|
{
|
2018-11-22 00:18:42 -05:00
|
|
|
|
for (int x = 0; x < frame.Width; x++)
|
|
|
|
|
{
|
2018-11-22 23:44:01 -05:00
|
|
|
|
var palColor = frame.GetColor(x, (int)y, CurrentPalette);
|
|
|
|
|
//var col = AdjustColor(palColor);
|
|
|
|
|
binaryData[x + y * frame.Width] = (uint)palColor.ToArgb();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
2018-11-22 23:44:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
var rect = new SDL.SDL_Rect { x = 0, y = FrameSize.Height - (int)frame.Height, w = (int)frame.Width, h = (int)frame.Height };
|
|
|
|
|
GCHandle pinnedArray = GCHandle.Alloc(binaryData, GCHandleType.Pinned);
|
2018-11-22 23:44:01 -05:00
|
|
|
|
SDL.SDL_UpdateTexture(textures[index], ref rect, pinnedArray.AddrOfPinnedObject(), (int)frame.Width * 4);
|
2018-11-22 00:18:42 -05:00
|
|
|
|
pinnedArray.Free();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int Pow2(int val)
|
|
|
|
|
{
|
|
|
|
|
int result = 1;
|
|
|
|
|
while (result < val)
|
|
|
|
|
result *= 2;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (var texture in textures)
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_DestroyTexture(texture);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|