2018-11-23 15:22:35 -05:00
|
|
|
|
using System;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2018-11-24 10:54:02 -05:00
|
|
|
|
using OpenDiablo2.Common.Enums;
|
2018-11-23 15:22:35 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
using OpenDiablo2.Common.Models;
|
2018-11-23 15:22:35 -05:00
|
|
|
|
using SDL2;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
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-23 15:22:35 -05:00
|
|
|
|
internal readonly ImageSet source;
|
|
|
|
|
private readonly IntPtr renderer;
|
|
|
|
|
internal IntPtr texture = IntPtr.Zero;
|
2018-11-24 10:54:02 -05:00
|
|
|
|
private Point globalFrameOffset = new Point(0, 0);
|
2018-11-23 15:22:35 -05:00
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
public Point Location { get; set; } = new Point();
|
|
|
|
|
public Size FrameSize { get; set; } = new Size();
|
2018-11-23 14:19:48 -05:00
|
|
|
|
|
2018-11-24 10:54:02 -05:00
|
|
|
|
|
2018-11-23 18:03:29 -05:00
|
|
|
|
private bool darken;
|
|
|
|
|
public bool Darken
|
|
|
|
|
{
|
|
|
|
|
get => darken;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (darken == value)
|
|
|
|
|
return;
|
|
|
|
|
darken = value;
|
|
|
|
|
LoadFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 15:22:35 -05:00
|
|
|
|
private int frame = -1;
|
2018-11-23 14:19:48 -05:00
|
|
|
|
public int Frame
|
|
|
|
|
{
|
|
|
|
|
get => frame;
|
|
|
|
|
set
|
|
|
|
|
{
|
2018-11-23 15:22:35 -05:00
|
|
|
|
if (frame == value && texture != IntPtr.Zero)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-11-23 14:19:48 -05:00
|
|
|
|
frame = Math.Max(0, Math.Min(value, TotalFrames));
|
2018-11-23 15:22:35 -05:00
|
|
|
|
LoadFrame(frame);
|
2018-11-23 14:19:48 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
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-23 15:22:35 -05:00
|
|
|
|
SDL.SDL_SetTextureBlendMode(texture, blend ? SDL.SDL_BlendMode.SDL_BLENDMODE_ADD : SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
2018-11-22 18:06:15 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
private Palette palette;
|
|
|
|
|
public Palette CurrentPalette
|
|
|
|
|
{
|
|
|
|
|
get => palette;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
palette = value;
|
|
|
|
|
UpdateTextureData();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-23 15:22:35 -05:00
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
public SDL2Sprite(ImageSet source, IntPtr renderer)
|
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
|
|
|
|
|
|
2018-11-23 15:22:35 -05:00
|
|
|
|
TotalFrames = source.Frames.Count();
|
|
|
|
|
FrameSize = new Size(Pow2((int)source.Frames.Max(x => x.Width)), Pow2((int)source.Frames.Max(x => x.Height)));
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:54:02 -05:00
|
|
|
|
public unsafe SDL2Sprite(IntPtr renderer, Palette palette, MPQDS1 mapData, int x, int y, eRenderCellType cellType)
|
|
|
|
|
{
|
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
// TODO: Cell types
|
|
|
|
|
|
|
|
|
|
// Floor cell types
|
|
|
|
|
// Todo: multiple floor layers
|
|
|
|
|
var floorLayer = mapData.FloorLayers.First();
|
|
|
|
|
var floor = floorLayer.Props[x + (y * mapData.Width)];
|
|
|
|
|
|
|
|
|
|
if (floor.Prop1 == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var sub_index = floor.Prop2;
|
|
|
|
|
var main_index = (floor.Prop3 / 16) + ((floor.Prop4 & 0x0F) * 16);
|
|
|
|
|
var tile = mapData.DT1s[main_index].Tiles[sub_index];
|
|
|
|
|
|
|
|
|
|
FrameSize = new Size(tile.Width, Math.Abs(tile.Height));
|
|
|
|
|
TotalFrames = 1;
|
|
|
|
|
frame = 0;
|
|
|
|
|
IntPtr pixels;
|
|
|
|
|
int pitch;
|
|
|
|
|
|
|
|
|
|
texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, FrameSize.Width, FrameSize.Height);
|
|
|
|
|
|
|
|
|
|
if (texture == IntPtr.Zero)
|
|
|
|
|
throw new ApplicationException($"Unaple to initialize texture: {SDL.SDL_GetError()}");
|
|
|
|
|
|
|
|
|
|
SDL.SDL_LockTexture(texture, IntPtr.Zero, out pixels, out pitch);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
UInt32* data = (UInt32*)pixels;
|
|
|
|
|
for (var i = 0; i < FrameSize.Width * FrameSize.Height; i++)
|
|
|
|
|
data[i] = 0;
|
|
|
|
|
|
|
|
|
|
for (var subtileX = 0; subtileX < 5; subtileX++)
|
|
|
|
|
{
|
|
|
|
|
for (var subtileY = 0; subtileY < 5; subtileY++)
|
|
|
|
|
{
|
|
|
|
|
var subtileFlags = tile.SubTileFlags[subtileX + (subtileY * 5)];
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_UnlockTexture(texture);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
internal Point GetRenderPoint()
|
2018-11-24 10:54:02 -05:00
|
|
|
|
{
|
|
|
|
|
return source == null
|
|
|
|
|
? globalFrameOffset
|
|
|
|
|
: new Point(Location.X + source.Frames[Frame].OffsetX, (Location.Y - FrameSize.Height) + source.Frames[Frame].OffsetY);
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
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 00:18:42 -05:00
|
|
|
|
private void UpdateTextureData()
|
|
|
|
|
{
|
2018-11-23 15:22:35 -05:00
|
|
|
|
if (texture == IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, Pow2(FrameSize.Width), Pow2(FrameSize.Height));
|
|
|
|
|
|
|
|
|
|
if (texture == IntPtr.Zero)
|
|
|
|
|
throw new ApplicationException("Unaple to initialize texture.");
|
|
|
|
|
|
|
|
|
|
Frame = 0;
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 15:22:35 -05:00
|
|
|
|
private unsafe void LoadFrame(int index)
|
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-23 15:22:35 -05:00
|
|
|
|
IntPtr pixels;
|
|
|
|
|
int pitch;
|
|
|
|
|
var fullRect = new SDL.SDL_Rect { x = 0, y = 0, w = FrameSize.Width, h = FrameSize.Height };
|
|
|
|
|
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
|
|
|
|
|
2018-11-23 15:22:35 -05:00
|
|
|
|
SDL.SDL_LockTexture(texture, IntPtr.Zero, out pixels, out pitch);
|
|
|
|
|
try
|
2018-11-22 23:44:01 -05:00
|
|
|
|
{
|
2018-11-23 15:22:35 -05:00
|
|
|
|
UInt32* data = (UInt32*)pixels;
|
|
|
|
|
var frameOffset = FrameSize.Height - frame.Height;
|
|
|
|
|
for (var y = 0; y < FrameSize.Height; y++)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
2018-11-23 15:22:35 -05:00
|
|
|
|
for (int x = 0; x < FrameSize.Width; x++)
|
|
|
|
|
{
|
|
|
|
|
if ((x >= frame.Width) || (y < frameOffset))
|
|
|
|
|
{
|
|
|
|
|
data[x + (y * (pitch / 4))] = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 18:03:29 -05:00
|
|
|
|
var color = frame.GetColor(x, (int)(y - frameOffset), CurrentPalette);
|
|
|
|
|
if (darken)
|
|
|
|
|
color = ((color & 0xFF000000) > 0) ? (color >> 1) & 0xFF7F7F7F | 0xFF000000 : 0;
|
|
|
|
|
data[x + (y * (pitch / 4))] = color;
|
2018-11-23 15:22:35 -05:00
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
2018-11-22 23:44:01 -05:00
|
|
|
|
}
|
2018-11-23 15:22:35 -05:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_UnlockTexture(texture);
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int Pow2(int val)
|
|
|
|
|
{
|
|
|
|
|
int result = 1;
|
|
|
|
|
while (result < val)
|
|
|
|
|
result *= 2;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-11-23 15:22:35 -05:00
|
|
|
|
SDL.SDL_DestroyTexture(texture);
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
2018-11-24 10:54:02 -05:00
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|