2018-12-08 09:32:09 -05:00
|
|
|
|
/* OpenDiablo 2 - An open source re-implementation of Diablo 2 in C#
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
2018-12-08 12:08:01 -05:00
|
|
|
|
using OpenDiablo2.Common.Exceptions;
|
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-23 15:22:35 -05:00
|
|
|
|
internal readonly ImageSet source;
|
|
|
|
|
private readonly IntPtr renderer;
|
2018-12-09 14:55:01 -05:00
|
|
|
|
private readonly bool cacheFrames;
|
|
|
|
|
|
|
|
|
|
private bool disposed = false;
|
|
|
|
|
|
|
|
|
|
private IntPtr[] texture;
|
|
|
|
|
private bool[] frameLoaded;
|
|
|
|
|
|
|
|
|
|
public IntPtr Texture
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!frameLoaded[TextureIndex])
|
|
|
|
|
LoadFrame();
|
|
|
|
|
|
|
|
|
|
return texture[TextureIndex];
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-23 15:22:35 -05:00
|
|
|
|
|
2018-12-08 13:31:50 -05:00
|
|
|
|
public Point Location { get; set; }
|
|
|
|
|
public Size FrameSize { get; set; }
|
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;
|
2018-12-09 14:55:01 -05:00
|
|
|
|
ClearAllFrames();
|
2018-11-23 18:03:29 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
private int frame;
|
2018-11-23 14:19:48 -05:00
|
|
|
|
public int Frame
|
|
|
|
|
{
|
|
|
|
|
get => frame;
|
|
|
|
|
set
|
|
|
|
|
{
|
2018-12-09 14:55:01 -05:00
|
|
|
|
if (frame == value)
|
2018-11-23 15:22:35 -05:00
|
|
|
|
return;
|
|
|
|
|
|
2018-11-23 14:19:48 -05:00
|
|
|
|
frame = Math.Max(0, Math.Min(value, TotalFrames));
|
2018-12-09 14:55:01 -05:00
|
|
|
|
|
|
|
|
|
if (cacheFrames)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
frameLoaded[TextureIndex] = false;
|
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-12-09 14:55:01 -05:00
|
|
|
|
if (cacheFrames)
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < TotalFrames; i++)
|
|
|
|
|
if (texture[i] != IntPtr.Zero)
|
|
|
|
|
SDL.SDL_SetTextureBlendMode(texture[i], blend ? SDL.SDL_BlendMode.SDL_BLENDMODE_ADD : SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
if (texture[TextureIndex] != IntPtr.Zero)
|
|
|
|
|
SDL.SDL_SetTextureBlendMode(texture[TextureIndex], 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;
|
2018-12-09 14:55:01 -05:00
|
|
|
|
ClearAllFrames();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-23 15:22:35 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
private int TextureIndex => cacheFrames ? frame : 0;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
public SDL2Sprite(ImageSet source, IntPtr renderer, bool cacheFrames = false)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
this.renderer = renderer;
|
2018-12-09 14:55:01 -05:00
|
|
|
|
this.cacheFrames = cacheFrames;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
texture = new IntPtr[cacheFrames ? source.Frames.Count() : 1];
|
|
|
|
|
frameLoaded = new bool[cacheFrames ? source.Frames.Count() : 1];
|
2018-11-23 15:22:35 -05:00
|
|
|
|
TotalFrames = source.Frames.Count();
|
2018-12-09 14:55:01 -05:00
|
|
|
|
|
|
|
|
|
ClearAllFrames();
|
|
|
|
|
|
2018-12-08 13:31:50 -05:00
|
|
|
|
Location = Point.Empty;
|
2018-11-23 15:22:35 -05:00
|
|
|
|
FrameSize = new Size(Pow2((int)source.Frames.Max(x => x.Width)), Pow2((int)source.Frames.Max(x => x.Height)));
|
2018-12-09 14:55:01 -05:00
|
|
|
|
|
|
|
|
|
Frame = 0;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
internal Point GetRenderPoint()
|
2018-11-24 10:54:02 -05:00
|
|
|
|
{
|
|
|
|
|
return source == null
|
2018-11-24 20:34:21 -05:00
|
|
|
|
? Location
|
2018-12-08 13:11:52 -05:00
|
|
|
|
: new Point(Location.X + source.Frames[Frame].OffsetX, Location.Y - FrameSize.Height + source.Frames[Frame].OffsetY);
|
2018-11-24 10:54:02 -05:00
|
|
|
|
}
|
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-12-09 14:55:01 -05:00
|
|
|
|
Frame = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private unsafe void LoadFrame()
|
|
|
|
|
{
|
|
|
|
|
if (texture[TextureIndex] == IntPtr.Zero)
|
2018-11-23 15:22:35 -05:00
|
|
|
|
{
|
2018-12-09 14:55:01 -05:00
|
|
|
|
texture[TextureIndex] = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, Pow2(FrameSize.Width), Pow2(FrameSize.Height));
|
2018-11-23 15:22:35 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
if (texture[TextureIndex] == IntPtr.Zero)
|
2018-12-08 12:08:01 -05:00
|
|
|
|
throw new OpenDiablo2Exception("Unaple to initialize texture.");
|
2018-12-09 14:55:01 -05:00
|
|
|
|
SDL.SDL_SetTextureBlendMode(texture[TextureIndex], blend ? SDL.SDL_BlendMode.SDL_BLENDMODE_ADD : SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
2018-11-23 15:22:35 -05:00
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
var sourceFrame = source.Frames[frame];
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
SDL.SDL_LockTexture(texture[TextureIndex], IntPtr.Zero, out IntPtr pixels, out int pitch);
|
2018-11-23 15:22:35 -05:00
|
|
|
|
try
|
2018-11-22 23:44:01 -05:00
|
|
|
|
{
|
2018-11-23 15:22:35 -05:00
|
|
|
|
UInt32* data = (UInt32*)pixels;
|
2018-12-08 13:31:50 -05:00
|
|
|
|
var frameOffset = FrameSize.Height - sourceFrame.Height;
|
2018-11-28 19:28:41 -05:00
|
|
|
|
var frameWidth = FrameSize.Width;
|
|
|
|
|
var frameHeight = FrameSize.Height;
|
2018-12-09 14:55:01 -05:00
|
|
|
|
for (int y = 0; y < frameHeight; y++)
|
2018-11-22 00:18:42 -05:00
|
|
|
|
{
|
2018-11-28 19:28:41 -05:00
|
|
|
|
for (int x = 0; x < frameWidth; x++)
|
2018-11-23 15:22:35 -05:00
|
|
|
|
{
|
2018-12-08 13:31:50 -05:00
|
|
|
|
if ((x >= sourceFrame.Width) || (y < frameOffset))
|
2018-11-23 15:22:35 -05:00
|
|
|
|
{
|
|
|
|
|
data[x + (y * (pitch / 4))] = 0;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 13:31:50 -05:00
|
|
|
|
var color = sourceFrame.GetColor(x, (int)(y - frameOffset), CurrentPalette);
|
2018-11-23 18:03:29 -05:00
|
|
|
|
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
|
|
|
|
|
{
|
2018-12-09 14:55:01 -05:00
|
|
|
|
SDL.SDL_UnlockTexture(texture[TextureIndex]);
|
2018-11-23 15:22:35 -05:00
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
frameLoaded[TextureIndex] = true;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int Pow2(int val)
|
|
|
|
|
{
|
|
|
|
|
int result = 1;
|
|
|
|
|
while (result < val)
|
|
|
|
|
result *= 2;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-09 14:55:01 -05:00
|
|
|
|
private void ClearAllFrames()
|
|
|
|
|
{
|
|
|
|
|
var framestoClear = cacheFrames ? TotalFrames : 1;
|
|
|
|
|
for (int i = 0; i < framestoClear; i++)
|
|
|
|
|
frameLoaded[i] = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-12-09 14:55:01 -05:00
|
|
|
|
if (disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var framestoClear = cacheFrames ? TotalFrames : 1;
|
|
|
|
|
for (var i = 0; i < framestoClear; i++)
|
|
|
|
|
{
|
|
|
|
|
SDL.SDL_DestroyTexture(texture[i]);
|
|
|
|
|
texture[i] = IntPtr.Zero;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
texture = Array.Empty<IntPtr>();
|
|
|
|
|
disposed = true;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
2018-11-24 10:54:02 -05:00
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
}
|