C client should't reject inf tex terrain.png

This commit is contained in:
UnknownShadow200 2018-07-31 21:57:40 +10:00
parent 915781c7ca
commit 91e707f03b
7 changed files with 129 additions and 132 deletions

View File

@ -43,9 +43,9 @@ namespace ClassicalSharp {
public bool ChangeTerrainAtlas(Bitmap atlas) { public bool ChangeTerrainAtlas(Bitmap atlas) {
if (!ValidateBitmap("terrain.png", atlas)) return false; if (!ValidateBitmap("terrain.png", atlas)) return false;
if (atlas.Width != atlas.Height && (atlas.Width * 2 != atlas.Height)) { if (atlas.Height < atlas.Width) {
Chat.Add("&cUnable to use terrain.png from the texture pack."); Chat.Add("&cUnable to use terrain.png from the texture pack.");
Chat.Add("&c Its width is not the same as its height."); Chat.Add("&c Its height is less than its width.");
return false; return false;
} }
if (Graphics.LostContext) return false; if (Graphics.LostContext) return false;
@ -190,7 +190,7 @@ namespace ClassicalSharp {
public bool UpdateTexture(ref int texId, string file, byte[] data, ref SkinType type) { public bool UpdateTexture(ref int texId, string file, byte[] data, ref SkinType type) {
bool calc = type != SkinType.Invalid; bool calc = type != SkinType.Invalid;
using (Bitmap bmp = Platform.ReadBmp(Drawer2D, data)) { using (Bitmap bmp = Platform.ReadBmp(Drawer2D, data)) {
if (!ValidateBitmap(file, bmp)) return false; if (!ValidateBitmap(file, bmp)) return false;
if (calc) type = Utils.GetSkinType(bmp); if (calc) type = Utils.GetSkinType(bmp);
Graphics.DeleteTexture(ref texId); Graphics.DeleteTexture(ref texId);

View File

@ -421,9 +421,7 @@ namespace ClassicalSharp.Network.Protocols {
BlockID order = reader.ReadBlock(); BlockID order = reader.ReadBlock();
game.Inventory.Remove(block); game.Inventory.Remove(block);
// TODO: kinda hacky, get rid of 255 completely when servers updated if (order != 0) { game.Inventory.Map[order - 1] = block; }
bool hidden = order == 0 || (order == 255 && !net.cpeData.extTexs);
if (!hidden) { game.Inventory.Map[order - 1] = block; }
} }
#endregion #endregion

View File

@ -40,7 +40,7 @@ namespace ClassicalSharp.Particles {
void TextureChanged(object sender, TextureEventArgs e) { void TextureChanged(object sender, TextureEventArgs e) {
if (e.Name == "particles.png") if (e.Name == "particles.png")
game.Loadtexture(ref ParticlesTexId, e.Name, e.Data); game.LoadTexture(ref ParticlesTexId, e.Name, e.Data);
} }

View File

@ -1,118 +1,119 @@
// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
using System; using System;
using System.Drawing; using System.Drawing;
using ClassicalSharp.GraphicsAPI; using ClassicalSharp.GraphicsAPI;
#if ANDROID #if ANDROID
using Android.Graphics; using Android.Graphics;
#endif #endif
namespace ClassicalSharp.Textures { namespace ClassicalSharp.Textures {
/// <summary> Represents a 2D packed texture atlas, specifically for terrain.png. </summary> /// <summary> Represents a 2D packed texture atlas, specifically for terrain.png. </summary>
public static class Atlas2D { public static class Atlas2D {
public const int TilesPerRow = 16, MaxRowsCount = 32; public const int TilesPerRow = 16, MaxRowsCount = 32;
public static Bitmap Atlas; public static Bitmap Atlas;
public static int TileSize, RowsCount; public static int TileSize, RowsCount;
internal static Game game; internal static Game game;
public static void UpdateState(Bitmap bmp) { public static void UpdateState(Bitmap bmp) {
Atlas = bmp; Atlas = bmp;
TileSize = bmp.Width / TilesPerRow; TileSize = bmp.Width / TilesPerRow;
RowsCount = bmp.Height / TileSize; RowsCount = bmp.Height / TileSize;
BlockInfo.RecalculateSpriteBB(); RowsCount = Math.Min(RowsCount, MaxRowsCount);
} BlockInfo.RecalculateSpriteBB();
}
public static int LoadTile(int texLoc) {
int size = TileSize, x = texLoc % TilesPerRow, y = texLoc / TilesPerRow; public static int LoadTile(int texLoc) {
if (y >= RowsCount) return 0; int size = TileSize, x = texLoc % TilesPerRow, y = texLoc / TilesPerRow;
if (y >= RowsCount) return 0;
using (FastBitmap atlas = new FastBitmap(Atlas, true, true))
using (Bitmap bmp = Platform.CreateBmp(size, size)) using (FastBitmap atlas = new FastBitmap(Atlas, true, true))
using (FastBitmap dst = new FastBitmap(bmp, true, false)) using (Bitmap bmp = Platform.CreateBmp(size, size))
{ using (FastBitmap dst = new FastBitmap(bmp, true, false))
FastBitmap.MovePortion(x * size, y * size, 0, 0, atlas, dst, size); {
return game.Graphics.CreateTexture(dst, false, game.Graphics.Mipmaps); FastBitmap.MovePortion(x * size, y * size, 0, 0, atlas, dst, size);
} return game.Graphics.CreateTexture(dst, false, game.Graphics.Mipmaps);
} }
}
public static void Dispose() {
if (Atlas != null) Atlas.Dispose(); public static void Dispose() {
} if (Atlas != null) Atlas.Dispose();
} }
}
/// <summary> Represents a 2D packed texture atlas that has been converted into an array of 1D atlases. </summary>
public static class Atlas1D { /// <summary> Represents a 2D packed texture atlas that has been converted into an array of 1D atlases. </summary>
public static int TilesPerAtlas, AtlasesCount; public static class Atlas1D {
public static float invTileSize; public static int TilesPerAtlas, AtlasesCount;
internal static Game game; public static float invTileSize;
public const int MaxAtlases = Atlas2D.MaxRowsCount * Atlas2D.TilesPerRow; internal static Game game;
public static int[] TexIds = new int[MaxAtlases]; public const int MaxAtlases = Atlas2D.MaxRowsCount * Atlas2D.TilesPerRow;
public static int[] TexIds = new int[MaxAtlases];
public static TextureRec GetTexRec(int texLoc, int uCount, out int index) {
index = texLoc / TilesPerAtlas; public static TextureRec GetTexRec(int texLoc, int uCount, out int index) {
int y = texLoc % TilesPerAtlas; index = texLoc / TilesPerAtlas;
// Adjust coords to be slightly inside - fixes issues with AMD/ATI cards. int y = texLoc % TilesPerAtlas;
return new TextureRec(0, y * invTileSize, (uCount - 1) + 15.99f/16f, (15.99f/16f) * invTileSize); // Adjust coords to be slightly inside - fixes issues with AMD/ATI cards.
} return new TextureRec(0, y * invTileSize, (uCount - 1) + 15.99f/16f, (15.99f/16f) * invTileSize);
}
/// <summary> Returns the index of the 1D texture within the array of 1D textures
/// containing the given texture id. </summary> /// <summary> Returns the index of the 1D texture within the array of 1D textures
public static int Get1DIndex(int texLoc) { return texLoc / TilesPerAtlas; } /// containing the given texture id. </summary>
public static int Get1DIndex(int texLoc) { return texLoc / TilesPerAtlas; }
/// <summary> Returns the index of the given texture id within a 1D texture. </summary>
public static int Get1DRowId(int texLoc) { return texLoc % TilesPerAtlas; } /// <summary> Returns the index of the given texture id within a 1D texture. </summary>
public static int Get1DRowId(int texLoc) { return texLoc % TilesPerAtlas; }
public static void UpdateState() {
int tileSize = Atlas2D.TileSize; public static void UpdateState() {
int maxTiles = Atlas2D.RowsCount * Atlas2D.TilesPerRow; int tileSize = Atlas2D.TileSize;
int maxAtlasHeight = Math.Min(4096, game.Graphics.MaxTextureDimensions); int maxTiles = Atlas2D.RowsCount * Atlas2D.TilesPerRow;
int maxTilesPerAtlas = maxAtlasHeight / tileSize; int maxAtlasHeight = Math.Min(4096, game.Graphics.MaxTextureDimensions);
int maxTilesPerAtlas = maxAtlasHeight / tileSize;
TilesPerAtlas = Math.Min(maxTilesPerAtlas, maxTiles);
AtlasesCount = Utils.CeilDiv(maxTiles, TilesPerAtlas); TilesPerAtlas = Math.Min(maxTilesPerAtlas, maxTiles);
invTileSize = 1f / TilesPerAtlas; AtlasesCount = Utils.CeilDiv(maxTiles, TilesPerAtlas);
invTileSize = 1f / TilesPerAtlas;
Utils.LogDebug("Loaded new atlas: {0} bmps, {1} per bmp", AtlasesCount, TilesPerAtlas);
int index = 0, atlasHeight = TilesPerAtlas * tileSize; Utils.LogDebug("Loaded new atlas: {0} bmps, {1} per bmp", AtlasesCount, TilesPerAtlas);
int index = 0, atlasHeight = TilesPerAtlas * tileSize;
using (FastBitmap atlas = new FastBitmap(Atlas2D.Atlas, true, true)) {
for (int i = 0; i < AtlasesCount; i++) { using (FastBitmap atlas = new FastBitmap(Atlas2D.Atlas, true, true)) {
Make1DTexture(i, atlas, atlasHeight, ref index); for (int i = 0; i < AtlasesCount; i++) {
} Make1DTexture(i, atlas, atlasHeight, ref index);
} }
} }
}
static void Make1DTexture(int i, FastBitmap atlas, int atlas1DHeight, ref int index) {
int tileSize = Atlas2D.TileSize; static void Make1DTexture(int i, FastBitmap atlas, int atlas1DHeight, ref int index) {
using (Bitmap atlas1d = Platform.CreateBmp(tileSize, atlas1DHeight)) int tileSize = Atlas2D.TileSize;
using (FastBitmap dst = new FastBitmap(atlas1d, true, false)) using (Bitmap atlas1d = Platform.CreateBmp(tileSize, atlas1DHeight))
{ using (FastBitmap dst = new FastBitmap(atlas1d, true, false))
for (int index1D = 0; index1D < TilesPerAtlas; index1D++) { {
int atlasX = (index % Atlas2D.TilesPerRow) * tileSize; for (int index1D = 0; index1D < TilesPerAtlas; index1D++) {
int atlasY = (index / Atlas2D.TilesPerRow) * tileSize; int atlasX = (index % Atlas2D.TilesPerRow) * tileSize;
int atlasY = (index / Atlas2D.TilesPerRow) * tileSize;
FastBitmap.MovePortion(atlasX, atlasY,
0, index1D * tileSize, atlas, dst, tileSize); FastBitmap.MovePortion(atlasX, atlasY,
index++; 0, index1D * tileSize, atlas, dst, tileSize);
} index++;
TexIds[i] = game.Graphics.CreateTexture(dst, true, game.Graphics.Mipmaps); }
} TexIds[i] = game.Graphics.CreateTexture(dst, true, game.Graphics.Mipmaps);
} }
}
public static int UsedAtlasesCount() {
int maxTexLoc = 0; public static int UsedAtlasesCount() {
for (int i = 0; i < BlockInfo.textures.Length; i++) { int maxTexLoc = 0;
maxTexLoc = Math.Max(maxTexLoc, BlockInfo.textures[i]); for (int i = 0; i < BlockInfo.textures.Length; i++) {
} maxTexLoc = Math.Max(maxTexLoc, BlockInfo.textures[i]);
return Get1DIndex(maxTexLoc) + 1; }
} return Get1DIndex(maxTexLoc) + 1;
}
public static void Dispose() {
if (TexIds == null) return; public static void Dispose() {
if (TexIds == null) return;
for (int i = 0; i < AtlasesCount; i++) {
game.Graphics.DeleteTexture(ref TexIds[i]); for (int i = 0; i < AtlasesCount; i++) {
} game.Graphics.DeleteTexture(ref TexIds[i]);
} }
} }
}
} }

View File

@ -139,9 +139,9 @@ bool Game_ChangeTerrainAtlas(struct Bitmap* atlas) {
String terrain = String_FromConst("terrain.png"); String terrain = String_FromConst("terrain.png");
if (!Game_ValidateBitmap(&terrain, atlas)) return false; if (!Game_ValidateBitmap(&terrain, atlas)) return false;
if (atlas->Width != atlas->Height) { if (atlas->Height < atlas->Width) {
String m1 = String_FromConst("&cUnable to use terrain.png from the texture pack."); Chat_Add(&m1); String m1 = String_FromConst("&cUnable to use terrain.png from the texture pack."); Chat_Add(&m1);
String m2 = String_FromConst("&c Its width is not the same as its height."); Chat_Add(&m2); String m2 = String_FromConst("&c Its height is less than its width."); Chat_Add(&m2);
return false; return false;
} }
if (Gfx_LostContext) return false; if (Gfx_LostContext) return false;

View File

@ -2941,12 +2941,12 @@ static void TexIdsOverlay_ContextRecreated(void* obj) {
String prefix = String_FromConst("f"); String prefix = String_FromConst("f");
TextAtlas_Make(&screen->IdAtlas, &chars, &screen->TextFont, &prefix); TextAtlas_Make(&screen->IdAtlas, &chars, &screen->TextFont, &prefix);
Int32 size = Game_Height / ATLAS2D_ROWS_COUNT; Int32 size = Game_Height / ATLAS2D_TILES_PER_ROW;
size = (size / 8) * 8; size = (size / 8) * 8;
Math_Clamp(size, 8, 40); Math_Clamp(size, 8, 40);
screen->XOffset = Gui_CalcPos(ANCHOR_CENTRE, 0, size * ATLAS2D_TILES_PER_ROW, Game_Width); screen->XOffset = Gui_CalcPos(ANCHOR_CENTRE, 0, size * ATLAS2D_ROWS_COUNT, Game_Width);
screen->YOffset = Gui_CalcPos(ANCHOR_CENTRE, 0, size * ATLAS2D_ROWS_COUNT, Game_Height); screen->YOffset = Gui_CalcPos(ANCHOR_CENTRE, 0, size * ATLAS2D_TILES_PER_ROW, Game_Height);
screen->TileSize = size; screen->TileSize = size;
String title = String_FromConst("Texture ID reference sheet"); String title = String_FromConst("Texture ID reference sheet");

View File

@ -1145,9 +1145,7 @@ static void CPE_SetInventoryOrder(struct Stream* stream) {
BlockID order = Handlers_ReadBlock(stream); BlockID order = Handlers_ReadBlock(stream);
Inventory_Remove(block); Inventory_Remove(block);
if (order != 255 && order != 0) { if (order) { Inventory_Map[order - 1] = block; }
Inventory_Map[order - 1] = block;
}
} }
static void CPE_Reset(void) { static void CPE_Reset(void) {