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-12-08 01:01:30 -05:00
|
|
|
|
using System.Collections.Generic;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
using System.Linq;
|
2018-12-08 01:01:30 -05:00
|
|
|
|
using OpenDiablo2.Common;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
using OpenDiablo2.Common.Enums;
|
2018-12-08 12:08:01 -05:00
|
|
|
|
using OpenDiablo2.Common.Exceptions;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
|
|
|
|
using OpenDiablo2.Common.Interfaces.Drawing;
|
|
|
|
|
using OpenDiablo2.Common.Models;
|
|
|
|
|
using SDL2;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.SDL2_
|
|
|
|
|
{
|
|
|
|
|
public sealed class SDL2CharacterRenderer : ICharacterRenderer
|
|
|
|
|
{
|
2018-12-08 01:01:30 -05:00
|
|
|
|
sealed class DirectionCacheItem
|
|
|
|
|
{
|
|
|
|
|
public int Direction { get; set; }
|
|
|
|
|
public eMobMode MobMode { get; set; }
|
|
|
|
|
|
|
|
|
|
public SDL.SDL_Rect[] SpriteRect { get; set; }
|
|
|
|
|
public IntPtr[] SpriteTexture { get; set; }
|
|
|
|
|
public int FramesToAnimate { get; set; }
|
|
|
|
|
public int AnimationSpeed { get; set; }
|
|
|
|
|
public int RenderFrameIndex { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-07 18:45:54 -05:00
|
|
|
|
static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
public Guid UID { get; set; }
|
|
|
|
|
public PlayerLocationDetails LocationDetails { get; set; }
|
|
|
|
|
public eHero Hero { get; set; }
|
|
|
|
|
public eWeaponClass WeaponClass { get; set; }
|
|
|
|
|
public eArmorType ArmorType { get; set; }
|
|
|
|
|
public eMobMode MobMode { get; set; }
|
|
|
|
|
|
2018-12-08 09:32:09 -05:00
|
|
|
|
private readonly IntPtr renderer;
|
2018-12-08 01:01:30 -05:00
|
|
|
|
|
2018-12-08 09:32:09 -05:00
|
|
|
|
private readonly List<DirectionCacheItem> directionCache = new List<DirectionCacheItem>();
|
2018-12-08 01:01:30 -05:00
|
|
|
|
DirectionCacheItem currentDirectionCache;
|
|
|
|
|
private float seconds;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
|
|
|
|
private readonly IResourceManager resourceManager;
|
|
|
|
|
private readonly IPaletteProvider paletteProvider;
|
|
|
|
|
|
|
|
|
|
private MPQCOF animationData;
|
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
static readonly byte[] directionConversion = new byte[] { 3, 15, 4, 8, 0, 9, 5, 10, 1, 11, 6, 12, 2, 13, 7, 14 };
|
|
|
|
|
|
2018-12-07 18:45:54 -05:00
|
|
|
|
public SDL2CharacterRenderer(IntPtr renderer, IResourceManager resourceManager, IPaletteProvider paletteProvider)
|
|
|
|
|
{
|
|
|
|
|
this.resourceManager = resourceManager;
|
|
|
|
|
this.paletteProvider = paletteProvider;
|
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Render(int pixelOffsetX, int pixelOffsetY)
|
|
|
|
|
{
|
2018-12-08 01:01:30 -05:00
|
|
|
|
if (currentDirectionCache == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var destRect = new SDL.SDL_Rect
|
|
|
|
|
{
|
|
|
|
|
x = pixelOffsetX + currentDirectionCache.SpriteRect[currentDirectionCache.RenderFrameIndex].x,
|
|
|
|
|
y = pixelOffsetY + currentDirectionCache.SpriteRect[currentDirectionCache.RenderFrameIndex].y,
|
|
|
|
|
w = currentDirectionCache.SpriteRect[currentDirectionCache.RenderFrameIndex].w,
|
|
|
|
|
h = currentDirectionCache.SpriteRect[currentDirectionCache.RenderFrameIndex].h
|
|
|
|
|
};
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
SDL.SDL_RenderCopy(renderer, currentDirectionCache.SpriteTexture[currentDirectionCache.RenderFrameIndex], IntPtr.Zero, ref destRect);
|
2018-12-07 18:45:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(long ms)
|
|
|
|
|
{
|
2018-12-08 01:01:30 -05:00
|
|
|
|
if (currentDirectionCache == null)
|
|
|
|
|
return;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 13:31:50 -05:00
|
|
|
|
seconds += ms / 1000f;
|
|
|
|
|
var animationSeg = 15f / currentDirectionCache.AnimationSpeed;
|
2018-12-08 01:01:30 -05:00
|
|
|
|
while (seconds >= animationSeg)
|
|
|
|
|
{
|
|
|
|
|
seconds -= animationSeg;
|
|
|
|
|
currentDirectionCache.RenderFrameIndex++;
|
|
|
|
|
if (currentDirectionCache.RenderFrameIndex >= currentDirectionCache.FramesToAnimate)
|
|
|
|
|
currentDirectionCache.RenderFrameIndex = 0;
|
|
|
|
|
}
|
2018-12-07 18:45:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetAnimationData()
|
|
|
|
|
{
|
2018-12-08 01:01:30 -05:00
|
|
|
|
switch (LocationDetails.MovementType)
|
|
|
|
|
{
|
|
|
|
|
case eMovementType.Stopped:
|
|
|
|
|
MobMode = eMobMode.PlayerTownNeutral;
|
|
|
|
|
break;
|
|
|
|
|
case eMovementType.Walking:
|
|
|
|
|
MobMode = eMobMode.PlayerTownWalk;
|
|
|
|
|
break;
|
|
|
|
|
case eMovementType.Running:
|
|
|
|
|
MobMode = eMobMode.PlayerRun;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
MobMode = eMobMode.PlayerNeutral;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentDirectionCache = directionCache.FirstOrDefault(x => x.MobMode == MobMode && x.Direction == directionConversion[LocationDetails.MovementDirection]);
|
|
|
|
|
if (currentDirectionCache != null)
|
|
|
|
|
{
|
|
|
|
|
currentDirectionCache.RenderFrameIndex = 0;
|
|
|
|
|
seconds = 0f;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
|
|
|
|
animationData = resourceManager.GetPlayerAnimation(Hero, WeaponClass, MobMode);
|
|
|
|
|
if (animationData == null)
|
2018-12-08 12:08:01 -05:00
|
|
|
|
throw new OpenDiablo2Exception("Could not locate animation for the character!");
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
|
|
|
|
var palette = paletteProvider.PaletteTable["Units"];
|
2018-12-08 11:51:11 -05:00
|
|
|
|
CacheFrames(animationData.Layers.Select(layer => resourceManager.GetPlayerDCC(layer, ArmorType, palette)));
|
2018-12-07 18:45:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
private unsafe void CacheFrames(IEnumerable<MPQDCC> layerData)
|
2018-12-07 18:45:54 -05:00
|
|
|
|
{
|
2018-12-08 01:01:30 -05:00
|
|
|
|
var cache = new DirectionCacheItem
|
|
|
|
|
{
|
|
|
|
|
MobMode = MobMode,
|
|
|
|
|
Direction = directionConversion[LocationDetails.MovementDirection]
|
|
|
|
|
};
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
var palette = paletteProvider.PaletteTable[Palettes.Units];
|
|
|
|
|
|
|
|
|
|
var dirAnimation = animationData.Animations[0];
|
|
|
|
|
cache.FramesToAnimate = dirAnimation.FramesPerDirection;
|
|
|
|
|
cache.AnimationSpeed = dirAnimation.AnimationSpeed;
|
|
|
|
|
cache.RenderFrameIndex = 0;
|
|
|
|
|
|
|
|
|
|
var minX = Int32.MaxValue;
|
|
|
|
|
var minY = Int32.MaxValue;
|
|
|
|
|
var maxX = Int32.MinValue;
|
|
|
|
|
var maxY = Int32.MinValue;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
var layersIgnored = 0;
|
|
|
|
|
var layersToRender = new List<MPQDCC>();
|
2018-12-07 18:45:54 -05:00
|
|
|
|
foreach (var layer in layerData)
|
|
|
|
|
{
|
2018-12-08 11:51:11 -05:00
|
|
|
|
if (layer == null)
|
|
|
|
|
{
|
|
|
|
|
layersIgnored++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layersToRender.Add(layer);
|
2018-12-08 01:01:30 -05:00
|
|
|
|
minX = Math.Min(minX, layer.Directions[directionConversion[LocationDetails.MovementDirection]].Box.Left);
|
|
|
|
|
minY = Math.Min(minY, layer.Directions[directionConversion[LocationDetails.MovementDirection]].Box.Top);
|
|
|
|
|
maxX = Math.Max(maxX, layer.Directions[directionConversion[LocationDetails.MovementDirection]].Box.Right);
|
|
|
|
|
maxY = Math.Max(maxY, layer.Directions[directionConversion[LocationDetails.MovementDirection]].Box.Bottom);
|
|
|
|
|
}
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
if (layersIgnored > 0)
|
|
|
|
|
log.Warn($"{layersIgnored} animation layer(s) were not found!");
|
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
var frameW = (maxX - minX) * 2; // Hack
|
|
|
|
|
var frameH = (maxY - minY) * 2;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
cache.SpriteTexture = new IntPtr[cache.FramesToAnimate];
|
|
|
|
|
cache.SpriteRect = new SDL.SDL_Rect[cache.FramesToAnimate];
|
|
|
|
|
|
|
|
|
|
for (var frameIndex = 0; frameIndex < cache.FramesToAnimate; frameIndex++)
|
|
|
|
|
{
|
|
|
|
|
var texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, frameW, frameH);
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 09:32:09 -05:00
|
|
|
|
SDL.SDL_LockTexture(texture, IntPtr.Zero, out IntPtr pixels, out int pitch);
|
2018-12-08 01:01:30 -05:00
|
|
|
|
UInt32* data = (UInt32*)pixels;
|
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
foreach (var layer in layersToRender)
|
2018-12-08 01:01:30 -05:00
|
|
|
|
{
|
2018-12-08 11:51:11 -05:00
|
|
|
|
if (layer == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
var direction = layer.Directions[directionConversion[LocationDetails.MovementDirection]];
|
|
|
|
|
var frame = direction.Frames[frameIndex];
|
|
|
|
|
|
|
|
|
|
foreach (var cell in frame.Cells)
|
|
|
|
|
{
|
|
|
|
|
if (cell.PixelData == null)
|
|
|
|
|
continue; // TODO: This isn't good
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < cell.Height; y++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < cell.Width; x++)
|
|
|
|
|
{
|
|
|
|
|
// Index 0 is always transparent
|
|
|
|
|
var paletteIndex = cell.PixelData[x + (y * cell.Width)];
|
|
|
|
|
|
|
|
|
|
if (paletteIndex == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var color = palette.Colors[paletteIndex];
|
|
|
|
|
|
|
|
|
|
var offsetX = x + cell.XOffset + (frame.Box.X - minX);
|
|
|
|
|
var offsetY = y + cell.YOffset + (frame.Box.Y - minY);
|
|
|
|
|
if (offsetX < 0 || offsetX > frameW || offsetY < 0 || offsetY > frameH)
|
2018-12-08 12:08:01 -05:00
|
|
|
|
throw new OpenDiablo2Exception("There is nothing we can do now.");
|
2018-12-08 01:01:30 -05:00
|
|
|
|
|
|
|
|
|
data[offsetX + (offsetY * (pitch / 4))] = color;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
SDL.SDL_UnlockTexture(texture);
|
|
|
|
|
SDL.SDL_SetTextureBlendMode(texture, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND);
|
2018-12-08 11:51:11 -05:00
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
// TODO: Temporary code!
|
|
|
|
|
cache.SpriteTexture[frameIndex] = texture;
|
|
|
|
|
cache.SpriteRect[frameIndex] = new SDL.SDL_Rect { x = minX, y = minY, w = frameW, h = frameH };
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-08 01:01:30 -05:00
|
|
|
|
directionCache.Add(cache);
|
|
|
|
|
currentDirectionCache = cache;
|
2018-12-07 18:45:54 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-08 01:01:30 -05:00
|
|
|
|
|
2018-12-07 18:45:54 -05:00
|
|
|
|
}
|
|
|
|
|
}
|