Skip to content

Commit

Permalink
Fixing image scaling and BGRA format.
Browse files Browse the repository at this point in the history
  • Loading branch information
NPBruce committed May 26, 2017
1 parent 2a6a804 commit 7507b57
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
53 changes: 46 additions & 7 deletions unity/Assets/Scripts/Content/ContentData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,23 +731,62 @@ public static Texture2D FileToTexture(string file, Vector2 pos, Vector2 size)
int height = ddsBytes[13] * 256 + ddsBytes[12];
int width = ddsBytes[17] * 256 + ddsBytes[16];

/*20-23 pit
* 24-27 dep
* 28-31 mm
* 32-35 al
* 36-39 res
* 40-43 sur
* 44-51 over
* 52-59 des
* 60-67 sov
* 68-75 sblt
* 76-79 size
* 80-83 flags
* 84-87 fourCC
* 88-91 bpp
* 92-95 red
*/

char[] type = new char[4];
type[0] = (char)ddsBytes[84];
type[1] = (char)ddsBytes[85];
type[2] = (char)ddsBytes[86];
type[3] = (char)ddsBytes[87];

// Copy image data (skip header)
int DDS_HEADER_SIZE = 128;
byte[] dxtBytes = new byte[ddsBytes.Length - DDS_HEADER_SIZE];
System.Buffer.BlockCopy(ddsBytes, DDS_HEADER_SIZE, dxtBytes, 0, ddsBytes.Length - DDS_HEADER_SIZE);

// Create empty texture
texture = new Texture2D(width, height, TextureFormat.DXT5, false);
// Load data into texture
try
if (ddsBytes[87] == '5')
{
texture.LoadRawTextureData(dxtBytes);
texture = new Texture2D(width, height, TextureFormat.DXT5, false);
}
catch (System.Exception)
else if (ddsBytes[87] == '1')
{
texture = new Texture2D(width, height, TextureFormat.DXT1, false);
texture.LoadRawTextureData(dxtBytes);
}
else if (ddsBytes[88] == 32)
{
if (ddsBytes[92] == 0)
{
texture = new Texture2D(width, height, TextureFormat.BGRA32, false);
}
else
{
texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
}
}
else if (ddsBytes[88] == 24)
{
texture = new Texture2D(width, height, TextureFormat.RGB24, false);
}
else
{
ValkyrieDebug.Log("Warning: Image invalid: " + file);
}
texture.LoadRawTextureData(dxtBytes);
texture.Apply();
}
else
Expand Down
15 changes: 8 additions & 7 deletions unity/Assets/Scripts/UI/UIWindowSelectionListImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected override void DrawItemList()

float offset = 0;
float xOffset = 0;
float yOffset = 4;
foreach (SelectionItemTraits item in traitItems)
{
bool display = true;
Expand All @@ -105,33 +106,33 @@ protected override void DrawItemList()
{
if (20 - xOffset < spriteCache[item.GetKey()].width)
{
offset += 4;
offset += yOffset;
xOffset = 0;
}

xOffset = DrawItem(item.GetKey(), itemScrollArea.GetScrollTransform(), offset, xOffset);
xOffset = DrawItem(item.GetKey(), itemScrollArea.GetScrollTransform(), offset, xOffset, out yOffset);
}
else
{
if (xOffset > 0) offset += 4;
if (xOffset > 0) offset += yOffset;
xOffset = 0;
offset = DrawItem(item, itemScrollArea.GetScrollTransform(), offset);
}
}
if (xOffset != 0)
{
offset += 4;
offset += yOffset;
}
itemScrollArea.SetScrollSize(offset);
}

protected float DrawItem(string key, Transform transform, float offset, float xOffset)
protected float DrawItem(string key, Transform transform, float offset, float xOffset, out float yOffset)
{
UIElement ui = new UIElement(transform);
ui.SetButton(delegate { SelectItem(key); });
ui.SetImage(spriteCache[key].sprite);
ui.SetBGColor(spriteCache[key].color);
ui.SetLocation(xOffset, offset, spriteCache[key].width, 3.95f);
ui.SetLocation(xOffset, offset, spriteCache[key].width, spriteCache[key].height);
yOffset = spriteCache[key].height + 0.05f;
return xOffset + spriteCache[key].width + 0.05f;
}

Expand Down

0 comments on commit 7507b57

Please sign in to comment.