From ca5d0899fd4654c51d25f94ae4b7e2e0099a1cf1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 1 Jun 2018 19:33:44 +1000 Subject: [PATCH] inf tex --- ClassicalSharp/2D/IsometricBlockDrawer.cs | 11 +-- ClassicalSharp/2D/Screens/HudScreen.cs | 2 +- ClassicalSharp/2D/Screens/LoadingMapScreen.cs | 4 +- .../2D/Screens/Overlays/TexIdsOverlay.cs | 26 +++--- .../2D/Widgets/Chat/TextGroupWidget.cs | 2 +- ClassicalSharp/2D/Widgets/InputWidget.cs | 2 +- ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs | 3 +- ClassicalSharp/Blocks/BlockInfo.cs | 20 ++--- ClassicalSharp/ClassicalSharp.csproj | 6 +- .../Entities/Components/ShadowComponent.cs | 4 +- ClassicalSharp/Entities/Entity.cs | 4 +- ClassicalSharp/Entities/Model/BlockModel.cs | 11 +-- ClassicalSharp/Entities/Model/CustomModel.cs | 2 +- ClassicalSharp/Entities/Model/IModel.cs | 4 +- ClassicalSharp/Entities/Player.cs | 7 +- ClassicalSharp/Game/Game.Init.cs | 4 +- ClassicalSharp/Game/Game.cs | 16 ++-- ClassicalSharp/GraphicsAPI/Direct3D9Api.cs | 4 +- ClassicalSharp/GraphicsAPI/OpenGLApi.cs | 12 +-- ClassicalSharp/GraphicsAPI/OpenGLESApi.cs | 12 +-- .../MeshBuilder/AdvLightingBuilder.cs | 49 ++++++------ ClassicalSharp/MeshBuilder/Builder.cs | 6 +- ClassicalSharp/MeshBuilder/CuboidDrawer.cs | 54 ++++++------- ClassicalSharp/MeshBuilder/NormalBuilder.cs | 21 ++--- ClassicalSharp/MeshBuilder/TileDrawer.cs | 37 ++------- ClassicalSharp/Network/CPESupport.cs | 8 +- ClassicalSharp/Network/Protocols/BlockDefs.cs | 29 ++++--- ClassicalSharp/Particles/Particle.cs | 3 +- ClassicalSharp/Particles/ParticleManager.cs | 29 +++---- ClassicalSharp/Rendering/ChunkUpdater.cs | 51 +++++------- ClassicalSharp/Rendering/Env/EnvRenderer.cs | 4 +- .../Rendering/Env/MapBordersRenderer.cs | 9 ++- .../Rendering/Env/SkyboxRenderer.cs | 4 +- ClassicalSharp/Rendering/MapRenderer.cs | 14 ++-- ClassicalSharp/TexturePack/Animations.cs | 14 ++-- .../{TerrainAtlas1D.cs => TerrainAtlas.cs} | 79 +++++++++++++------ ClassicalSharp/TexturePack/TerrainAtlas2D.cs | 44 ----------- ClassicalSharp/Utils/StringBuffer.cs | 2 +- OpenTK/NativeWindow.cs | 4 +- 39 files changed, 289 insertions(+), 328 deletions(-) rename ClassicalSharp/TexturePack/{TerrainAtlas1D.cs => TerrainAtlas.cs} (54%) delete mode 100644 ClassicalSharp/TexturePack/TerrainAtlas2D.cs diff --git a/ClassicalSharp/2D/IsometricBlockDrawer.cs b/ClassicalSharp/2D/IsometricBlockDrawer.cs index df5a676f1..a83cff5eb 100644 --- a/ClassicalSharp/2D/IsometricBlockDrawer.cs +++ b/ClassicalSharp/2D/IsometricBlockDrawer.cs @@ -45,9 +45,6 @@ namespace ClassicalSharp { } public void DrawBatch(BlockID block, float size, float x, float y) { - drawer.tilesPerAtlas1D = TerrainAtlas1D.TilesPerAtlas; - drawer.invVerTileSize = TerrainAtlas1D.invTileSize; - bool bright = BlockInfo.FullBright[block]; if (BlockInfo.Draw[block] == DrawType.Gas) return; @@ -94,7 +91,7 @@ namespace ClassicalSharp { int GetTex(BlockID block, int side) { int texLoc = BlockInfo.GetTextureLoc(block, side); - texIndex = texLoc / TerrainAtlas1D.TilesPerAtlas; + texIndex = texLoc / Atlas1D.TilesPerAtlas; if (lastTexIndex != texIndex) Flush(); return texLoc; @@ -103,7 +100,7 @@ namespace ClassicalSharp { static Vector3 pos = Vector3.Zero; void SpriteZQuad(BlockID block, bool firstPart) { int texLoc = BlockInfo.GetTextureLoc(block, Side.Right); - TextureRec rec = TerrainAtlas1D.GetTexRec(texLoc, 1, out texIndex); + TextureRec rec = Atlas1D.GetTexRec(texLoc, 1, out texIndex); if (lastTexIndex != texIndex) Flush(); VertexP3fT2fC4b v = default(VertexP3fT2fC4b); @@ -127,7 +124,7 @@ namespace ClassicalSharp { void SpriteXQuad(BlockID block, bool firstPart) { int texLoc = BlockInfo.GetTextureLoc(block, Side.Right); - TextureRec rec = TerrainAtlas1D.GetTexRec(texLoc, 1, out texIndex); + TextureRec rec = Atlas1D.GetTexRec(texLoc, 1, out texIndex); if (lastTexIndex != texIndex) Flush(); VertexP3fT2fC4b v = default(VertexP3fT2fC4b); @@ -152,7 +149,7 @@ namespace ClassicalSharp { int lastTexIndex, texIndex; void Flush() { if (lastTexIndex != -1) { - game.Graphics.BindTexture(TerrainAtlas1D.TexIds[lastTexIndex]); + game.Graphics.BindTexture(Atlas1D.TexIds[lastTexIndex]); game.Graphics.UpdateDynamicVb_IndexedTris(vb, vertices, index); } diff --git a/ClassicalSharp/2D/Screens/HudScreen.cs b/ClassicalSharp/2D/Screens/HudScreen.cs index 702b024bd..251fe72d8 100644 --- a/ClassicalSharp/2D/Screens/HudScreen.cs +++ b/ClassicalSharp/2D/Screens/HudScreen.cs @@ -76,7 +76,7 @@ namespace ClassicalSharp.Gui.Screens { const int chExtent = 16, chWeight = 2; static TextureRec chRec = new TextureRec(0, 0, 15/256f, 15/256f); void DrawCrosshairs() { - if (game.Gui.IconsTex <= 0) return; + if (game.Gui.IconsTex == 0) return; int cenX = game.Width / 2, cenY = game.Height / 2; int extent = (int)(chExtent * game.Scale(game.Height / 480f)); diff --git a/ClassicalSharp/2D/Screens/LoadingMapScreen.cs b/ClassicalSharp/2D/Screens/LoadingMapScreen.cs index b392f5e82..3f75e8e08 100644 --- a/ClassicalSharp/2D/Screens/LoadingMapScreen.cs +++ b/ClassicalSharp/2D/Screens/LoadingMapScreen.cs @@ -132,7 +132,7 @@ namespace ClassicalSharp.Gui.Screens { int texLoc = BlockInfo.GetTextureLoc(Block.Dirt, Side.Top); Texture tex = new Texture(0, 0, 0, game.Width, 64, - TerrainAtlas1D.GetTexRec(texLoc, 1, out atlasIndex)); + Atlas1D.GetTexRec(texLoc, 1, out atlasIndex)); tex.U2 = (float)game.Width / 64; bool bound = false; @@ -150,7 +150,7 @@ namespace ClassicalSharp.Gui.Screens { if (index == 0) return; if (!bound) { bound = true; - game.Graphics.BindTexture(TerrainAtlas1D.TexIds[atlasIndex]); + game.Graphics.BindTexture(Atlas1D.TexIds[atlasIndex]); } ModelCache cache = game.ModelCache; diff --git a/ClassicalSharp/2D/Screens/Overlays/TexIdsOverlay.cs b/ClassicalSharp/2D/Screens/Overlays/TexIdsOverlay.cs index 841b1d740..960a73e9a 100644 --- a/ClassicalSharp/2D/Screens/Overlays/TexIdsOverlay.cs +++ b/ClassicalSharp/2D/Screens/Overlays/TexIdsOverlay.cs @@ -12,7 +12,7 @@ namespace ClassicalSharp.Gui.Screens { TextAtlas idAtlas; public TexIdsOverlay(Game game) : base(game) { widgets = new Widget[1]; } - const int verticesCount = TerrainAtlas2D.TilesPerRow * TerrainAtlas2D.RowsCount * 4; + const int verticesCount = Atlas2D.TilesPerRow * Atlas2D.MaxRowsCount * 4; static VertexP3fT2fC4b[] vertices; int dynamicVb; int xOffset, yOffset, tileSize; @@ -46,26 +46,26 @@ namespace ClassicalSharp.Gui.Screens { idAtlas = new TextAtlas(game, 16); idAtlas.Pack("0123456789", textFont, "f"); - tileSize = game.Height / TerrainAtlas2D.RowsCount; + tileSize = game.Height / Atlas2D.RowsCount; tileSize = (tileSize / 8) * 8; Utils.Clamp(ref tileSize, 8, 40); - xOffset = CalcPos(Anchor.Centre, 0, tileSize * TerrainAtlas2D.TilesPerRow, game.Width); - yOffset = CalcPos(Anchor.Centre, 0, tileSize * TerrainAtlas2D.RowsCount, game.Height); + xOffset = CalcPos(Anchor.Centre, 0, tileSize * Atlas2D.TilesPerRow, game.Width); + yOffset = CalcPos(Anchor.Centre, 0, tileSize * Atlas2D.RowsCount, game.Height); widgets[0] = TextWidget.Create(game, "Texture ID reference sheet", titleFont) .SetLocation(Anchor.Centre, Anchor.Min, 0, yOffset - 30); } void RenderTerrain() { - int elementsPerAtlas = TerrainAtlas1D.TilesPerAtlas; - for (int i = 0; i < TerrainAtlas2D.TilesPerRow * TerrainAtlas2D.RowsCount;) { + int elementsPerAtlas = Atlas1D.TilesPerAtlas; + for (int i = 0; i < Atlas2D.TilesPerRow * Atlas2D.RowsCount;) { int index = 0, texIdx = i / elementsPerAtlas, ignored; for (int j = 0; j < elementsPerAtlas; j++) { - TextureRec rec = TerrainAtlas1D.GetTexRec(i + j, 1, out ignored); - int x = (i + j) % TerrainAtlas2D.TilesPerRow; - int y = (i + j) / TerrainAtlas2D.TilesPerRow; + TextureRec rec = Atlas1D.GetTexRec(i + j, 1, out ignored); + int x = (i + j) % Atlas2D.TilesPerRow; + int y = (i + j) / Atlas2D.TilesPerRow; Texture tex = new Texture(0, xOffset + x * tileSize, yOffset + y * tileSize, tileSize, tileSize, rec); @@ -73,7 +73,7 @@ namespace ClassicalSharp.Gui.Screens { } i += elementsPerAtlas; - game.Graphics.BindTexture(TerrainAtlas1D.TexIds[texIdx]); + game.Graphics.BindTexture(Atlas1D.TexIds[texIdx]); game.Graphics.UpdateDynamicVb_IndexedTris(dynamicVb, vertices, index); } } @@ -83,10 +83,10 @@ namespace ClassicalSharp.Gui.Screens { int index = 0; idAtlas.tex.Y = (short)(yOffset + (tileSize - idAtlas.tex.Height)); - for (int y = 0; y < TerrainAtlas2D.RowsCount; y++) { - for (int x = 0; x < TerrainAtlas2D.TilesPerRow; x++) { + for (int y = 0; y < Atlas2D.RowsCount; y++) { + for (int x = 0; x < Atlas2D.TilesPerRow; x++) { idAtlas.curX = xOffset + tileSize * x + textOffset; - int id = x + y * TerrainAtlas2D.TilesPerRow; + int id = x + y * Atlas2D.TilesPerRow; idAtlas.AddInt(id, vertices, ref index); } idAtlas.tex.Y += (short)tileSize; diff --git a/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs b/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs index dfb6607f1..0763f9d71 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextGroupWidget.cs @@ -184,7 +184,7 @@ namespace ClassicalSharp.Gui.Widgets { tex = MakeTexture(index, text); lines[index] = text; } else { - tex = new Texture(-1, 0, 0, 0, 0, 0, 0); + tex = default(Texture); tex.Height = (ushort)(PlaceholderHeight[index] ? defaultHeight : 0); lines[index] = null; } diff --git a/ClassicalSharp/2D/Widgets/InputWidget.cs b/ClassicalSharp/2D/Widgets/InputWidget.cs index 6f0e472fe..b2281a6c5 100644 --- a/ClassicalSharp/2D/Widgets/InputWidget.cs +++ b/ClassicalSharp/2D/Widgets/InputWidget.cs @@ -70,7 +70,7 @@ namespace ClassicalSharp.Gui.Widgets { public override void Init() { int numLines = UsedLines; if (numLines > 1) { - Text.WordWrap(game.Drawer2D, lines, numLines, MaxCharsPerLine); + Text.WordWrap(lines, numLines, MaxCharsPerLine); } else { lines[0] = Text.ToString(); } diff --git a/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs b/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs index b725239fc..61e8343ac 100644 --- a/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs +++ b/ClassicalSharp/Blocks/BlockInfo.BoundsBox.cs @@ -2,6 +2,7 @@ using System; using OpenTK; using ClassicalSharp; +using ClassicalSharp.Textures; using BlockID = System.UInt16; namespace ClassicalSharp { @@ -42,7 +43,7 @@ namespace ClassicalSharp { } internal static void RecalculateSpriteBB() { - using (FastBitmap fastBmp = new FastBitmap(TerrainAtlas2D.Atlas, true, true)) { + using (FastBitmap fastBmp = new FastBitmap(Atlas2D.Atlas, true, true)) { for (int i = 0; i < Count; i++) { if (Draw[i] != DrawType.Sprite) continue; RecalculateBB((BlockID)i, fastBmp); diff --git a/ClassicalSharp/Blocks/BlockInfo.cs b/ClassicalSharp/Blocks/BlockInfo.cs index 9c2502cc8..95f3b3ae3 100644 --- a/ClassicalSharp/Blocks/BlockInfo.cs +++ b/ClassicalSharp/Blocks/BlockInfo.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using ClassicalSharp.Blocks; using OpenTK; using BlockID = System.UInt16; +using TexLoc = System.UInt16; namespace ClassicalSharp { @@ -63,11 +64,12 @@ namespace ClassicalSharp { public static bool[] IsLiquid, BlocksLight, FullBright; public static bool[] CanPlace, CanDelete, Tinted, FullOpaque; - public static byte[] Collide, ExtendedCollide, textures, hidden; + public static byte[] Collide, ExtendedCollide, hidden; public static byte[] LightOffset, Draw, SpriteOffset, CanStretch; public static byte[] DigSounds, StepSounds; public static string[] Name; public static float[] FogDensity, SpeedMultiplier; + public static TexLoc[] textures; public static FastColour[] FogColour; public static Vector3[] MinBB, MaxBB, RenderMinBB, RenderMaxBB; static uint[] DefinedCustomBlocks; @@ -83,7 +85,7 @@ namespace ClassicalSharp { FullOpaque = new bool[count]; Collide = new byte[count]; ExtendedCollide = new byte[count]; - textures = new byte[count * Side.Sides]; + textures = new TexLoc[count * Side.Sides]; hidden = new byte[count * count]; LightOffset = new byte[count]; Draw = new byte[count]; @@ -267,15 +269,15 @@ namespace ClassicalSharp { } - internal static void SetSide(byte textureId, BlockID blockId) { - textures[blockId * Side.Sides + Side.Left] = textureId; - textures[blockId * Side.Sides + Side.Right] = textureId; - textures[blockId * Side.Sides + Side.Front] = textureId; - textures[blockId * Side.Sides + Side.Back] = textureId; + internal static void SetSide(TexLoc texLoc, BlockID blockId) { + textures[blockId * Side.Sides + Side.Left] = texLoc; + textures[blockId * Side.Sides + Side.Right] = texLoc; + textures[blockId * Side.Sides + Side.Front] = texLoc; + textures[blockId * Side.Sides + Side.Back] = texLoc; } - internal static void SetTex(byte textureId, int face, BlockID blockId) { - textures[blockId * Side.Sides + face] = textureId; + internal static void SetTex(TexLoc texLoc, int face, BlockID blockId) { + textures[blockId * Side.Sides + face] = texLoc; } public static int GetTextureLoc(BlockID block, int face) { diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 6cb10e834..c5aceee85 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -47,7 +47,7 @@ Project - AnyCPU + x86 ..\output\debug\ true Full @@ -56,6 +56,7 @@ DEBUG;TRACE;USE_DX; Project obj\ + unk aa 127.0.0.1 25566 ..\output\release\ @@ -282,8 +283,7 @@ - - + diff --git a/ClassicalSharp/Entities/Components/ShadowComponent.cs b/ClassicalSharp/Entities/Components/ShadowComponent.cs index 2b9bbe0d3..8f70bb0c6 100644 --- a/ClassicalSharp/Entities/Components/ShadowComponent.cs +++ b/ClassicalSharp/Entities/Components/ShadowComponent.cs @@ -187,9 +187,9 @@ namespace ClassicalSharp.Entities { } internal static bool boundShadowTex = false; - internal static int shadowTex = -1; + internal static int shadowTex = 0; static void CheckShadowTexture(IGraphicsApi gfx) { - if (shadowTex != -1) return; + if (shadowTex != 0) return; const int size = 128, half = size / 2; using (Bitmap bmp = Platform.CreateBmp(size, size)) using (FastBitmap fastBmp = new FastBitmap(bmp, true, false)) diff --git a/ClassicalSharp/Entities/Entity.cs b/ClassicalSharp/Entities/Entity.cs index 553617d5f..a0f70ff6a 100644 --- a/ClassicalSharp/Entities/Entity.cs +++ b/ClassicalSharp/Entities/Entity.cs @@ -33,7 +33,7 @@ namespace ClassicalSharp.Entities { /// Returns the size of the model that is used for collision detection. public Vector3 Size; - public int TextureId = -1, MobTextureId = -1; + public int TextureId, MobTextureId; public short Health = 10; public Vector3 Position; @@ -130,7 +130,7 @@ namespace ClassicalSharp.Entities { Model = game.ModelCache.Get(ModelName); ParseScale(scale); - MobTextureId = -1; + MobTextureId = 0; Model.RecalcProperties(this); UpdateModelBounds(); diff --git a/ClassicalSharp/Entities/Model/BlockModel.cs b/ClassicalSharp/Entities/Model/BlockModel.cs index 2c8553de0..26e9027f4 100644 --- a/ClassicalSharp/Entities/Model/BlockModel.cs +++ b/ClassicalSharp/Entities/Model/BlockModel.cs @@ -82,7 +82,7 @@ namespace ClassicalSharp.Model { void Flush() { if (lastTexIndex != -1) { - game.Graphics.BindTexture(TerrainAtlas1D.TexIds[lastTexIndex]); + game.Graphics.BindTexture(Atlas1D.TexIds[lastTexIndex]); UpdateVB(); } @@ -103,9 +103,6 @@ namespace ClassicalSharp.Model { SpriteXQuad(true, false); SpriteXQuad(true, true); } else { - drawer.tilesPerAtlas1D = TerrainAtlas1D.TilesPerAtlas; - drawer.invVerTileSize = TerrainAtlas1D.invTileSize; - drawer.minBB = BlockInfo.MinBB[block]; drawer.minBB.Y = 1 - drawer.minBB.Y; drawer.maxBB = BlockInfo.MaxBB[block]; drawer.maxBB.Y = 1 - drawer.maxBB.Y; @@ -128,7 +125,7 @@ namespace ClassicalSharp.Model { int GetTex(int side) { int texLoc = BlockInfo.GetTextureLoc(block, side); - texIndex = texLoc / TerrainAtlas1D.TilesPerAtlas; + texIndex = texLoc / Atlas1D.TilesPerAtlas; if (lastTexIndex != texIndex) Flush(); return texLoc; @@ -136,7 +133,7 @@ namespace ClassicalSharp.Model { void SpriteZQuad(bool firstPart, bool mirror) { int texLoc = BlockInfo.GetTextureLoc(block, Side.Back); - TextureRec rec = TerrainAtlas1D.GetTexRec(texLoc, 1, out texIndex); + TextureRec rec = Atlas1D.GetTexRec(texLoc, 1, out texIndex); if (lastTexIndex != texIndex) Flush(); int col = cols[0]; @@ -161,7 +158,7 @@ namespace ClassicalSharp.Model { void SpriteXQuad(bool firstPart, bool mirror) { int texLoc = BlockInfo.GetTextureLoc(block, Side.Right); - TextureRec rec = TerrainAtlas1D.GetTexRec(texLoc, 1, out texIndex); + TextureRec rec = Atlas1D.GetTexRec(texLoc, 1, out texIndex); if (lastTexIndex != texIndex) Flush(); int col = cols[0]; diff --git a/ClassicalSharp/Entities/Model/CustomModel.cs b/ClassicalSharp/Entities/Model/CustomModel.cs index 7d8bf173e..163e10630 100644 --- a/ClassicalSharp/Entities/Model/CustomModel.cs +++ b/ClassicalSharp/Entities/Model/CustomModel.cs @@ -30,7 +30,7 @@ namespace ClassicalSharp.Model { public override AABB PickingBounds { get { return pickingBounds; } } public override void DrawModel(Entity p) { - int texId = p.TextureId <= 0 ? cache.HumanoidTexId : p.TextureId; + int texId = p.TextureId == 0 ? cache.HumanoidTexId : p.TextureId; } internal void ReadSetupPacket() { diff --git a/ClassicalSharp/Entities/Model/IModel.cs b/ClassicalSharp/Entities/Model/IModel.cs index cc7dfb83a..beabcab78 100644 --- a/ClassicalSharp/Entities/Model/IModel.cs +++ b/ClassicalSharp/Entities/Model/IModel.cs @@ -139,7 +139,7 @@ namespace ClassicalSharp.Model { bool _64x64 = p.SkinType != SkinType.Type64x32; // only apply when using humanoid skins - _64x64 &= UsesHumanSkin || p.MobTextureId > 0; + _64x64 &= UsesHumanSkin || p.MobTextureId != 0; uScale = p.uScale * 0.015625f; vScale = p.vScale * (_64x64 ? 0.015625f : 0.03125f); @@ -184,7 +184,7 @@ namespace ClassicalSharp.Model { protected int GetTexture(Entity entity) { int pTex = UsesHumanSkin ? entity.TextureId : entity.MobTextureId; - return pTex > 0 ? pTex : game.ModelCache.Textures[texIndex].TexID; + return pTex != 0 ? pTex : game.ModelCache.Textures[texIndex].TexID; } diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index b7eda49b4..03704601d 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -33,6 +33,7 @@ namespace ClassicalSharp.Entities { } public override void ContextLost() { + if (nameTex.ID < 0) return; game.Graphics.DeleteTexture(ref nameTex.ID); } @@ -178,7 +179,7 @@ namespace ClassicalSharp.Entities { void ApplySkin(Player src) { TextureId = src.TextureId; - MobTextureId = -1; + MobTextureId = 0; SkinType = src.SkinType; uScale = src.uScale; vScale = src.vScale; @@ -188,8 +189,8 @@ namespace ClassicalSharp.Entities { internal void ResetSkin() { uScale = 1; vScale = 1; - MobTextureId = -1; - TextureId = -1; + MobTextureId = 0; + TextureId = 0; SkinType = game.DefaultPlayerSkinType; } diff --git a/ClassicalSharp/Game/Game.Init.cs b/ClassicalSharp/Game/Game.Init.cs index d08e05889..6fe1fbc9d 100644 --- a/ClassicalSharp/Game/Game.Init.cs +++ b/ClassicalSharp/Game/Game.Init.cs @@ -83,8 +83,8 @@ namespace ClassicalSharp { Drawer2D.BlackTextShadows = Options.GetBool(OptionsKey.BlackText, false); Graphics.Mipmaps = Options.GetBool(OptionsKey.Mipmaps, false); - TerrainAtlas1D.game = this; - TerrainAtlas2D.game = this; + Atlas1D.game = this; + Atlas2D.game = this; Animations = new Animations(); Components.Add(Animations); Inventory = new Inventory(); Components.Add(Inventory); Inventory.Map = new BlockID[BlockInfo.Count]; diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 9e5fd7bb1..67a3bbf4f 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -40,17 +40,17 @@ namespace ClassicalSharp { public bool ChangeTerrainAtlas(Bitmap atlas) { if (!ValidateBitmap("terrain.png", atlas)) return false; - if (atlas.Width != atlas.Height) { + if (atlas.Width != atlas.Height && (atlas.Width * 2 != atlas.Height)) { Chat.Add("&cUnable to use terrain.png from the texture pack."); Chat.Add("&c Its width is not the same as its height."); return false; } if (Graphics.LostContext) return false; - TerrainAtlas1D.Dispose(); - TerrainAtlas2D.Dispose(); - TerrainAtlas2D.UpdateState(atlas); - TerrainAtlas1D.UpdateState(); + Atlas1D.Dispose(); + Atlas2D.Dispose(); + Atlas2D.UpdateState(atlas); + Atlas1D.UpdateState(); Events.RaiseTerrainAtlasChanged(); return true; @@ -170,7 +170,7 @@ namespace ClassicalSharp { Entity[] entities = Entities.List; for (int i = 0; i < EntityList.MaxCount; i++) { - if (entities[i] == null || entities[i].TextureId != -1) continue; + if (entities[i] == null || entities[i].TextureId != 0) continue; entities[i].SkinType = DefaultPlayerSkinType; } } @@ -393,8 +393,8 @@ namespace ClassicalSharp { public void Dispose() { ChunkUpdater.Dispose(); - TerrainAtlas2D.Dispose(); - TerrainAtlas1D.Dispose(); + Atlas2D.Dispose(); + Atlas1D.Dispose(); ModelCache.Dispose(); Entities.Dispose(); WorldEvents.OnNewMap -= OnNewMapCore; diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index 34d7b85ac..6356e35c0 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -524,14 +524,14 @@ namespace ClassicalSharp.GraphicsAPI { } static void Delete(T[] array, ref int id) where T : class, IDisposable { - if (id <= 0 || id >= array.Length) return; + if (id == 0 || id >= array.Length) return; T value = array[id]; if (value != null) { value.Dispose(); } array[id] = null; - id = -1; + id = 0; } public override void Dispose() { diff --git a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs index 9f9924e44..96e06eef1 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs @@ -232,9 +232,9 @@ namespace ClassicalSharp.GraphicsAPI { } public override void DeleteTexture(ref int texId) { - if (texId <= 0) return; + if (texId == 0) return; int id = texId; GL.DeleteTextures(1, &id); - texId = -1; + texId = 0; } public override void EnableMipmaps() { } @@ -334,8 +334,8 @@ namespace ClassicalSharp.GraphicsAPI { #endif public override void DeleteVb(ref int vb) { - if (vb <= 0) return; - int id = vb; vb = -1; + if (vb == 0) return; + int id = vb; vb = 0; #if !GL11 GL.DeleteBuffers(1, &id); #else @@ -345,8 +345,8 @@ namespace ClassicalSharp.GraphicsAPI { public override void DeleteIb(ref int ib) { #if !GL11 - if (ib <= 0) return; - int id = ib; ib = -1; + if (ib == 0) return; + int id = ib; ib = 0; GL.DeleteBuffers(1, &id); #else return; diff --git a/ClassicalSharp/GraphicsAPI/OpenGLESApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLESApi.cs index 8337c9429..ebaf75afa 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLESApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLESApi.cs @@ -146,10 +146,10 @@ namespace ClassicalSharp.GraphicsAPI { } public override void DeleteTexture(ref int texId) { - if (texId <= 0) return; + if (texId == 0) return; int id = texId; GL.DeleteTextures(1, &id); - texId = -1; + texId = 0; } #endregion @@ -192,15 +192,15 @@ namespace ClassicalSharp.GraphicsAPI { } public override void DeleteVb(ref int vb) { - if (vb <= 0) return; + if (vb == 0) return; int id = vb; GL.DeleteBuffers(1, &id); - vb = -1; + vb = 0; } public override void DeleteIb(ref int ib) { - if (ib <= 0) return; + if (ib == 0) return; int id = ib; GL.DeleteBuffers(1, &id); - ib = -1; + ib = 0; } VertexFormat batchFormat = (VertexFormat)999; diff --git a/ClassicalSharp/MeshBuilder/AdvLightingBuilder.cs b/ClassicalSharp/MeshBuilder/AdvLightingBuilder.cs index 719fe01d7..39e71197f 100644 --- a/ClassicalSharp/MeshBuilder/AdvLightingBuilder.cs +++ b/ClassicalSharp/MeshBuilder/AdvLightingBuilder.cs @@ -2,6 +2,7 @@ using System; using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Map; +using ClassicalSharp.Textures; using OpenTK; using BlockID = System.UInt16; @@ -124,13 +125,13 @@ namespace ClassicalSharp { void DrawLeftFace(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Left]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; int offset = (lightFlags >> Side.Left) & 1; float u1 = minBB.Z, u2 = (count - 1) + maxBB.Z * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * 15.99f/16f; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; int F = bitFlags[cIndex]; @@ -166,13 +167,13 @@ namespace ClassicalSharp { void DrawRightFace(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Right]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; int offset = (lightFlags >> Side.Right) & 1; float u1 = (count - minBB.Z), u2 = (1 - maxBB.Z) * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * 15.99f/16f; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; int F = bitFlags[cIndex]; @@ -208,13 +209,13 @@ namespace ClassicalSharp { void DrawFrontFace(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Front]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; int offset = (lightFlags >> Side.Front) & 1; float u1 = (count - minBB.X), u2 = (1 - maxBB.X) * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * 15.99f/16f; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; int F = bitFlags[cIndex]; @@ -250,13 +251,13 @@ namespace ClassicalSharp { void DrawBackFace(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Back]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; int offset = (lightFlags >> Side.Back) & 1; float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * 15.99f/16f; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; int F = bitFlags[cIndex]; @@ -292,13 +293,13 @@ namespace ClassicalSharp { void DrawBottomFace(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Bottom]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; int offset = (lightFlags >> Side.Bottom) & 1; float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f; - float v1 = vOrigin + minBB.Z * invVerTileSize; - float v2 = vOrigin + maxBB.Z * invVerTileSize * 15.99f/16f; + float v1 = vOrigin + minBB.Z * Atlas1D.invTileSize; + float v2 = vOrigin + maxBB.Z * Atlas1D.invTileSize * 15.99f/16f; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; int F = bitFlags[cIndex]; @@ -334,13 +335,13 @@ namespace ClassicalSharp { void DrawTopFace(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Top]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; int offset = (lightFlags >> Side.Top) & 1; float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f; - float v1 = vOrigin + minBB.Z * invVerTileSize; - float v2 = vOrigin + maxBB.Z * invVerTileSize * 15.99f/16f; + float v1 = vOrigin + minBB.Z * Atlas1D.invTileSize; + float v2 = vOrigin + maxBB.Z * Atlas1D.invTileSize * 15.99f/16f; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; int F = bitFlags[cIndex]; diff --git a/ClassicalSharp/MeshBuilder/Builder.cs b/ClassicalSharp/MeshBuilder/Builder.cs index b3671bc7f..2c7221d7e 100644 --- a/ClassicalSharp/MeshBuilder/Builder.cs +++ b/ClassicalSharp/MeshBuilder/Builder.cs @@ -24,10 +24,8 @@ namespace ClassicalSharp { protected const int chunkSize2 = 16 * 16, extChunkSize2 = 18 * 18; protected const int chunkSize3 = 16 * 16 * 16, extChunkSize3 = 18 * 18 * 18; - public void Init(Game game) { - this.game = game; - game.Events.TerrainAtlasChanged += TerrainAtlasChanged; - } + public void Init(Game game) { this.game = game; } + public void Dispose() { } protected internal int width, length, height, sidesLevel, edgeLevel; protected int maxX, maxY, maxZ, chunkEndX, chunkEndZ; diff --git a/ClassicalSharp/MeshBuilder/CuboidDrawer.cs b/ClassicalSharp/MeshBuilder/CuboidDrawer.cs index 9a2a79d66..80f7ca908 100644 --- a/ClassicalSharp/MeshBuilder/CuboidDrawer.cs +++ b/ClassicalSharp/MeshBuilder/CuboidDrawer.cs @@ -1,16 +1,13 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 using System; using ClassicalSharp.GraphicsAPI; +using ClassicalSharp.Textures; using OpenTK; namespace ClassicalSharp { /// Draws the vertices for a cuboid region. public sealed class CuboidDrawer { - - public int tilesPerAtlas1D; - public float invVerTileSize; - /// Whether a colour tinting effect should be applied to all faces. public bool Tinted; @@ -19,14 +16,15 @@ namespace ClassicalSharp { public Vector3 minBB, maxBB; public float x1, y1, z1, x2, y2, z2; + const float uv2Scale = 15.99f/16f; /// Draws the left face of the given cuboid region. public void Left(int count, int col, int texLoc, VertexP3fT2fC4b[] vertices, ref int index) { - float vOrigin = (texLoc % tilesPerAtlas1D) * invVerTileSize; - float u1 = minBB.Z, u2 = (count - 1) + maxBB.Z * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float vOrigin = (texLoc % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; + float u1 = minBB.Z, u2 = (count - 1) + maxBB.Z * uv2Scale; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * uv2Scale; if (Tinted) col = TintBlock(col); VertexP3fT2fC4b v; v.X = x1; v.Colour = col; @@ -38,10 +36,10 @@ namespace ClassicalSharp { /// Draws the right face of the given cuboid region. public void Right(int count, int col, int texLoc, VertexP3fT2fC4b[] vertices, ref int index) { - float vOrigin = (texLoc % tilesPerAtlas1D) * invVerTileSize; - float u1 = (count - minBB.Z), u2 = (1 - maxBB.Z) * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float vOrigin = (texLoc % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; + float u1 = (count - minBB.Z), u2 = (1 - maxBB.Z) * uv2Scale; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * uv2Scale; if (Tinted) col = TintBlock(col); VertexP3fT2fC4b v; v.X = x2; v.Colour = col; @@ -53,10 +51,10 @@ namespace ClassicalSharp { /// Draws the front face of the given cuboid region. public void Front(int count, int col, int texLoc, VertexP3fT2fC4b[] vertices, ref int index) { - float vOrigin = (texLoc % tilesPerAtlas1D) * invVerTileSize; - float u1 = (count - minBB.X), u2 = (1 - maxBB.X) * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float vOrigin = (texLoc % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; + float u1 = (count - minBB.X), u2 = (1 - maxBB.X) * uv2Scale; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * uv2Scale; if (Tinted) col = TintBlock(col); VertexP3fT2fC4b v; v.Z = z1; v.Colour = col; @@ -68,10 +66,10 @@ namespace ClassicalSharp { /// Draws the back face of the given cuboid region. public void Back(int count, int col, int texLoc, VertexP3fT2fC4b[] vertices, ref int index) { - float vOrigin = (texLoc % tilesPerAtlas1D) * invVerTileSize; - float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f; - float v1 = vOrigin + maxBB.Y * invVerTileSize; - float v2 = vOrigin + minBB.Y * invVerTileSize * 15.99f/16f; + float vOrigin = (texLoc % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; + float u1 = minBB.X, u2 = (count - 1) + maxBB.X * uv2Scale; + float v1 = vOrigin + maxBB.Y * Atlas1D.invTileSize; + float v2 = vOrigin + minBB.Y * Atlas1D.invTileSize * uv2Scale; if (Tinted) col = TintBlock(col); VertexP3fT2fC4b v; v.Z = z2; v.Colour = col; @@ -83,10 +81,10 @@ namespace ClassicalSharp { /// Draws the bottom face of the given cuboid region. public void Bottom(int count, int col, int texLoc, VertexP3fT2fC4b[] vertices, ref int index) { - float vOrigin = (texLoc % tilesPerAtlas1D) * invVerTileSize; - float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f; - float v1 = vOrigin + minBB.Z * invVerTileSize; - float v2 = vOrigin + maxBB.Z * invVerTileSize * 15.99f/16f; + float vOrigin = (texLoc % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; + float u1 = minBB.X, u2 = (count - 1) + maxBB.X * uv2Scale; + float v1 = vOrigin + minBB.Z * Atlas1D.invTileSize; + float v2 = vOrigin + maxBB.Z * Atlas1D.invTileSize * uv2Scale; if (Tinted) col = TintBlock(col); VertexP3fT2fC4b v; v.Y = y1; v.Colour = col; @@ -98,10 +96,10 @@ namespace ClassicalSharp { /// Draws the top face of the given cuboid region. public void Top(int count, int col, int texLoc, VertexP3fT2fC4b[] vertices, ref int index) { - float vOrigin = (texLoc % tilesPerAtlas1D) * invVerTileSize; - float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f; - float v1 = vOrigin + minBB.Z * invVerTileSize; - float v2 = vOrigin + maxBB.Z * invVerTileSize * 15.99f/16f; + float vOrigin = (texLoc % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; + float u1 = minBB.X, u2 = (count - 1) + maxBB.X * uv2Scale; + float v1 = vOrigin + minBB.Z * Atlas1D.invTileSize; + float v2 = vOrigin + maxBB.Z * Atlas1D.invTileSize * uv2Scale; if (Tinted) col = TintBlock(col); VertexP3fT2fC4b v; v.Y = y2; v.Colour = col; diff --git a/ClassicalSharp/MeshBuilder/NormalBuilder.cs b/ClassicalSharp/MeshBuilder/NormalBuilder.cs index 0bb266c00..b431ab4ac 100644 --- a/ClassicalSharp/MeshBuilder/NormalBuilder.cs +++ b/ClassicalSharp/MeshBuilder/NormalBuilder.cs @@ -2,6 +2,7 @@ using System; using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Map; +using ClassicalSharp.Textures; using OpenTK; using BlockID = System.UInt16; @@ -88,13 +89,7 @@ namespace ClassicalSharp { } return 0; } - - protected override void PreStretchTiles(int x1, int y1, int z1) { - base.PreStretchTiles(x1, y1, z1); - drawer.invVerTileSize = invVerTileSize; - drawer.tilesPerAtlas1D = tilesPerAtlas1D; - } - + protected override void RenderTile(int index) { if (BlockInfo.Draw[curBlock] == DrawType.Sprite) { this.fullBright = BlockInfo.FullBright[curBlock]; @@ -126,7 +121,7 @@ namespace ClassicalSharp { if (leftCount != 0) { int texLoc = BlockInfo.textures[curBlock * Side.Sides + Side.Left]; - int i = texLoc / tilesPerAtlas1D; + int i = texLoc / Atlas1D.TilesPerAtlas; int offset = (lightFlags >> Side.Left) & 1; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; @@ -137,7 +132,7 @@ namespace ClassicalSharp { if (rightCount != 0) { int texLoc = BlockInfo.textures[curBlock * Side.Sides + Side.Right]; - int i = texLoc / tilesPerAtlas1D; + int i = texLoc / Atlas1D.TilesPerAtlas; int offset = (lightFlags >> Side.Right) & 1; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; @@ -148,7 +143,7 @@ namespace ClassicalSharp { if (frontCount != 0) { int texLoc = BlockInfo.textures[curBlock * Side.Sides + Side.Front]; - int i = texLoc / tilesPerAtlas1D; + int i = texLoc / Atlas1D.TilesPerAtlas; int offset = (lightFlags >> Side.Front) & 1; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; @@ -159,7 +154,7 @@ namespace ClassicalSharp { if (backCount != 0) { int texLoc = BlockInfo.textures[curBlock * Side.Sides + Side.Back]; - int i = texLoc / tilesPerAtlas1D; + int i = texLoc / Atlas1D.TilesPerAtlas; int offset = (lightFlags >> Side.Back) & 1; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; @@ -170,7 +165,7 @@ namespace ClassicalSharp { if (bottomCount != 0) { int texLoc = BlockInfo.textures[curBlock * Side.Sides + Side.Bottom]; - int i = texLoc / tilesPerAtlas1D; + int i = texLoc / Atlas1D.TilesPerAtlas; int offset = (lightFlags >> Side.Bottom) & 1; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; @@ -180,7 +175,7 @@ namespace ClassicalSharp { if (topCount != 0) { int texLoc = BlockInfo.textures[curBlock * Side.Sides + Side.Top]; - int i = texLoc / tilesPerAtlas1D; + int i = texLoc / Atlas1D.TilesPerAtlas; int offset = (lightFlags >> Side.Top) & 1; DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i]; diff --git a/ClassicalSharp/MeshBuilder/TileDrawer.cs b/ClassicalSharp/MeshBuilder/TileDrawer.cs index b48f74a41..2a77cb25d 100644 --- a/ClassicalSharp/MeshBuilder/TileDrawer.cs +++ b/ClassicalSharp/MeshBuilder/TileDrawer.cs @@ -13,26 +13,6 @@ namespace ClassicalSharp { protected DrawInfo[] normalParts, translucentParts; protected int arraysCount = 0; protected bool fullBright, tinted; - protected float invVerTileSize; - protected int tilesPerAtlas1D; - - void TerrainAtlasChanged(object sender, EventArgs e) { - int newArraysCount = TerrainAtlas1D.TexIds.Length; - if (arraysCount == newArraysCount) return; - arraysCount = newArraysCount; - Array.Resize(ref normalParts, arraysCount); - Array.Resize(ref translucentParts, arraysCount); - - for (int i = 0; i < normalParts.Length; i++) { - if (normalParts[i] != null) continue; - normalParts[i] = new DrawInfo(); - translucentParts[i] = new DrawInfo(); - } - } - - public void Dispose() { - game.Events.TerrainAtlasChanged -= TerrainAtlasChanged; - } protected class DrawInfo { public int[] vIndex = new int[6], vCount = new int[6]; @@ -69,13 +49,12 @@ namespace ClassicalSharp { protected abstract void RenderTile(int index); protected virtual void PreStretchTiles(int x1, int y1, int z1) { - invVerTileSize = TerrainAtlas1D.invTileSize; - tilesPerAtlas1D = TerrainAtlas1D.TilesPerAtlas; - arraysCount = TerrainAtlas1D.TexIds.Length; + arraysCount = Math.Max(Atlas1D.AtlasesCount, game.MapRenderer._1DUsed); // TODO: hacky workaround fix - if (normalParts == null) { + if (normalParts == null || normalParts.Length < arraysCount) { normalParts = new DrawInfo[arraysCount]; translucentParts = new DrawInfo[arraysCount]; + for (int i = 0; i < normalParts.Length; i++) { normalParts[i] = new DrawInfo(); translucentParts[i] = new DrawInfo(); @@ -113,13 +92,13 @@ namespace ClassicalSharp { } void AddSpriteVertices(BlockID block) { - int i = TerrainAtlas1D.Get1DIndex(BlockInfo.GetTextureLoc(block, Side.Left)); + int i = Atlas1D.Get1DIndex(BlockInfo.GetTextureLoc(block, Side.Left)); DrawInfo part = normalParts[i]; part.spriteCount += 4 * 4; } void AddVertices(BlockID block, int face) { - int i = TerrainAtlas1D.Get1DIndex(BlockInfo.GetTextureLoc(block, face)); + int i = Atlas1D.Get1DIndex(BlockInfo.GetTextureLoc(block, face)); DrawInfo part = BlockInfo.Draw[block] == DrawType.Translucent ? translucentParts[i] : normalParts[i]; part.vCount[face] += 4; } @@ -127,13 +106,13 @@ namespace ClassicalSharp { static JavaRandom spriteRng = new JavaRandom(0); protected virtual void DrawSprite(int count) { int texId = BlockInfo.textures[curBlock * Side.Sides + Side.Right]; - int i = texId / tilesPerAtlas1D; - float vOrigin = (texId % tilesPerAtlas1D) * invVerTileSize; + int i = texId / Atlas1D.TilesPerAtlas; + float vOrigin = (texId % Atlas1D.TilesPerAtlas) * Atlas1D.invTileSize; float x1 = X + 2.50f/16, y1 = Y, z1 = Z + 2.50f/16; float x2 = X + 13.5f/16, y2 = Y + 1, z2 = Z + 13.5f/16; const float u1 = 0, u2 = 15.99f/16f; - float v1 = vOrigin, v2 = vOrigin + invVerTileSize * 15.99f/16f; + float v1 = vOrigin, v2 = vOrigin + Atlas1D.invTileSize * 15.99f/16f; byte offsetType = BlockInfo.SpriteOffset[curBlock]; if (offsetType >= 6 && offsetType <= 7) { diff --git a/ClassicalSharp/Network/CPESupport.cs b/ClassicalSharp/Network/CPESupport.cs index e7e6d8e78..d6c78df8c 100644 --- a/ClassicalSharp/Network/CPESupport.cs +++ b/ClassicalSharp/Network/CPESupport.cs @@ -9,7 +9,7 @@ namespace ClassicalSharp.Network { internal int ServerExtensionsCount; internal bool sendHeldBlock, useMessageTypes; internal int envMapVer = 2, blockDefsExtVer = 2; - internal bool needD3Fix, extEntityPos, twoWayPing, blockPerms, fastMap; + internal bool needD3Fix, extEntityPos, twoWayPing, blockPerms, fastMap, extTexs; public Game game; public void Reset() { @@ -17,6 +17,7 @@ namespace ClassicalSharp.Network { sendHeldBlock = false; useMessageTypes = false; envMapVer = 2; blockDefsExtVer = 2; needD3Fix = false; extEntityPos = false; twoWayPing = false; fastMap = false; + extTexs = false; game.SupportsCPEBlocks = false; } @@ -60,6 +61,10 @@ namespace ClassicalSharp.Network { } else if (ext == "FastMap") { net.packetSizes[Opcode.LevelInit] += 4; fastMap = true; + } else if (ext == "ExtendedTextures") { + net.packetSizes[Opcode.CpeDefineBlock] += 3; + net.packetSizes[Opcode.CpeDefineBlockExt] += 6; + extTexs = true; } #if !ONLY_8BIT else if (ext == "ExtendedBlocks") { @@ -91,6 +96,7 @@ namespace ClassicalSharp.Network { "EnvWeatherType", "MessageTypes", "HackControl", "PlayerClick", "FullCP437", "LongerMessages", "BlockDefinitions", "BlockDefinitionsExt", "BulkBlockUpdate", "TextColors", "EnvMapAspect", "EntityProperty", "ExtEntityPositions", "TwoWayPing", "InventoryOrder", "InstantMOTD", "FastMap", + "ExtendedTextures", #if !ONLY_8BIT "ExtendedBlocks", #endif diff --git a/ClassicalSharp/Network/Protocols/BlockDefs.cs b/ClassicalSharp/Network/Protocols/BlockDefs.cs index aedc0f607..f87f203da 100644 --- a/ClassicalSharp/Network/Protocols/BlockDefs.cs +++ b/ClassicalSharp/Network/Protocols/BlockDefs.cs @@ -1,7 +1,9 @@ // Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 using System; +using ClassicalSharp.Textures; using OpenTK; using BlockID = System.UInt16; +using TexLoc = System.UInt16; namespace ClassicalSharp.Network.Protocols { @@ -15,7 +17,7 @@ namespace ClassicalSharp.Network.Protocols { net.Set(Opcode.CpeDefineBlock, HandleDefineBlock, 80); net.Set(Opcode.CpeUndefineBlock, HandleRemoveBlockDefinition, 2); net.Set(Opcode.CpeDefineBlockExt, HandleDefineBlockExt, 85); - } + } public override void Tick() { } internal void HandleDefineBlock() { @@ -29,7 +31,7 @@ namespace ClassicalSharp.Network.Protocols { HandleDefineBlockCommonEnd(reader, shape, block); // Update sprite BoundingBox if necessary if (BlockInfo.Draw[block] == DrawType.Sprite) { - using (FastBitmap dst = new FastBitmap(TerrainAtlas2D.Atlas, true, true)) + using (FastBitmap dst = new FastBitmap(Atlas2D.Atlas, true, true)) BlockInfo.RecalculateBB(block, dst); } } @@ -76,6 +78,13 @@ namespace ClassicalSharp.Network.Protocols { HandleDefineBlockCommonEnd(reader, 1, block); } + TexLoc ReadTex(NetReader reader) { + if (!net.cpeData.extTexs) return reader.ReadUInt8(); + + const int maxTexCount = Atlas2D.TilesPerRow * Atlas2D.MaxRowsCount; + return (TexLoc)(reader.ReadUInt16() % maxTexCount); + } + BlockID HandleDefineBlockCommonStart(NetReader reader, bool uniqueSideTexs) { BlockID block = reader.ReadBlock(); bool didBlockLight = BlockInfo.BlocksLight[block]; @@ -85,23 +94,23 @@ namespace ClassicalSharp.Network.Protocols { BlockInfo.SetCollide(block, reader.ReadUInt8()); BlockInfo.SpeedMultiplier[block] = (float)Math.Pow(2, (reader.ReadUInt8() - 128) / 64f); - BlockInfo.SetTex(reader.ReadUInt8(), Side.Top, block); + BlockInfo.SetTex(ReadTex(reader), Side.Top, block); if (uniqueSideTexs) { - BlockInfo.SetTex(reader.ReadUInt8(), Side.Left, block); - BlockInfo.SetTex(reader.ReadUInt8(), Side.Right, block); - BlockInfo.SetTex(reader.ReadUInt8(), Side.Front, block); - BlockInfo.SetTex(reader.ReadUInt8(), Side.Back, block); + BlockInfo.SetTex(ReadTex(reader), Side.Left, block); + BlockInfo.SetTex(ReadTex(reader), Side.Right, block); + BlockInfo.SetTex(ReadTex(reader), Side.Front, block); + BlockInfo.SetTex(ReadTex(reader), Side.Back, block); } else { - BlockInfo.SetSide(reader.ReadUInt8(), block); + BlockInfo.SetSide(ReadTex(reader), block); } - BlockInfo.SetTex(reader.ReadUInt8(), Side.Bottom, block); + BlockInfo.SetTex(ReadTex(reader), Side.Bottom, block); BlockInfo.BlocksLight[block] = reader.ReadUInt8() == 0; OnBlockUpdated(block, didBlockLight); byte sound = reader.ReadUInt8(); BlockInfo.StepSounds[block] = sound; - BlockInfo.DigSounds[block] = sound; + BlockInfo.DigSounds[block] = sound; if (sound == SoundType.Glass) BlockInfo.StepSounds[block] = SoundType.Stone; BlockInfo.FullBright[block] = reader.ReadUInt8() != 0; diff --git a/ClassicalSharp/Particles/Particle.cs b/ClassicalSharp/Particles/Particle.cs index 62791bc52..7e1afcace 100644 --- a/ClassicalSharp/Particles/Particle.cs +++ b/ClassicalSharp/Particles/Particle.cs @@ -5,6 +5,7 @@ using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Map; using OpenTK; using BlockID = System.UInt16; +using TexLoc = System.UInt16; namespace ClassicalSharp.Particles { @@ -141,7 +142,7 @@ namespace ClassicalSharp.Particles { public sealed class TerrainParticle : Particle { internal TextureRec rec; - internal byte texLoc; + internal TexLoc texLoc; internal BlockID block; public bool Tick(Game game, double delta) { diff --git a/ClassicalSharp/Particles/ParticleManager.cs b/ClassicalSharp/Particles/ParticleManager.cs index 6fb6ed41d..572dbd9d6 100644 --- a/ClassicalSharp/Particles/ParticleManager.cs +++ b/ClassicalSharp/Particles/ParticleManager.cs @@ -5,6 +5,7 @@ using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Textures; using OpenTK; using BlockID = System.UInt16; +using TexLoc = System.UInt16; namespace ClassicalSharp.Particles { @@ -14,7 +15,8 @@ namespace ClassicalSharp.Particles { TerrainParticle[] terrainParticles = new TerrainParticle[maxParticles]; RainParticle[] rainParticles = new RainParticle[maxParticles]; int terrainCount, rainCount; - int[] terrain1DCount, terrain1DIndices; + int[] terrain1DCount = new int[Atlas1D.MaxAtlases]; + int[] terrain1DIndices = new int[Atlas1D.MaxAtlases]; Game game; Random rnd = new Random(); @@ -23,7 +25,6 @@ namespace ClassicalSharp.Particles { void IGameComponent.Init(Game game) { this.game = game; - game.Events.TerrainAtlasChanged += TerrainAtlasChanged; game.UserEvents.BlockChanged += BreakBlockEffect; game.Events.TextureChanged += TextureChanged; @@ -36,11 +37,6 @@ namespace ClassicalSharp.Particles { void IGameComponent.Reset(Game game) { rainCount = 0; terrainCount = 0; } void IGameComponent.OnNewMap(Game game) { rainCount = 0; terrainCount = 0; } void IGameComponent.OnNewMapLoaded(Game game) { } - - void TerrainAtlasChanged(object sender, EventArgs e) { - terrain1DCount = new int[TerrainAtlas1D.TexIds.Length]; - terrain1DIndices = new int[TerrainAtlas1D.TexIds.Length]; - } void TextureChanged(object sender, TextureEventArgs e) { if (e.Name == "particles.png") @@ -72,7 +68,7 @@ namespace ClassicalSharp.Particles { Update1DCounts(particles, elems); for (int i = 0; i < elems; i++) { - int index = TerrainAtlas1D.Get1DIndex(particles[i].texLoc); + int index = Atlas1D.Get1DIndex(particles[i].texLoc); particles[i].Render(game, t, vertices, ref terrain1DIndices[index]); } int drawCount = Math.Min(count, maxParticles * 4); @@ -81,11 +77,11 @@ namespace ClassicalSharp.Particles { fixed (VertexP3fT2fC4b* ptr = vertices) { gfx.SetDynamicVbData(vb, (IntPtr)ptr, drawCount); int offset = 0; - for (int i = 0; i < terrain1DCount.Length; i++) { + for (int i = 0; i < Atlas1D.AtlasesCount; i++) { int partCount = terrain1DCount[i]; if (partCount == 0) continue; - gfx.BindTexture(TerrainAtlas1D.TexIds[i]); + gfx.BindTexture(Atlas1D.TexIds[i]); gfx.DrawVb_IndexedTris(partCount, offset); offset += partCount; } @@ -93,15 +89,15 @@ namespace ClassicalSharp.Particles { } void Update1DCounts(TerrainParticle[] particles, int elems) { - for (int i = 0; i < terrain1DCount.Length; i++) { + for (int i = 0; i < Atlas1D.MaxAtlases; i++) { terrain1DCount[i] = 0; terrain1DIndices[i] = 0; } for (int i = 0; i < elems; i++) { - int index = TerrainAtlas1D.Get1DIndex(particles[i].texLoc); + int index = Atlas1D.Get1DIndex(particles[i].texLoc); terrain1DCount[index] += 4; } - for (int i = 1; i < terrain1DCount.Length; i++) { + for (int i = 1; i < Atlas1D.AtlasesCount; i++) { terrain1DIndices[i] = terrain1DIndices[i - 1] + terrain1DCount[i - 1]; } } @@ -169,8 +165,8 @@ namespace ClassicalSharp.Particles { Vector3 worldPos = new Vector3(position.X, position.Y, position.Z); int texLoc = BlockInfo.GetTextureLoc(block, Side.Left), texIndex = 0; - TextureRec baseRec = TerrainAtlas1D.GetTexRec(texLoc, 1, out texIndex); - float uScale = (1/16f), vScale = (1/16f) * TerrainAtlas1D.invTileSize; + TextureRec baseRec = Atlas1D.GetTexRec(texLoc, 1, out texIndex); + float uScale = (1/16f), vScale = (1/16f) * Atlas1D.invTileSize; Vector3 minBB = BlockInfo.MinBB[block]; Vector3 maxBB = BlockInfo.MaxBB[block]; @@ -211,7 +207,7 @@ namespace ClassicalSharp.Particles { p.ResetState(worldPos + cell, velocity, life); p.rec = rec; - p.texLoc = (byte)texLoc; + p.texLoc = (TexLoc)texLoc; p.block = block; int type = rnd.Next(0, 30); p.Size = (byte)(type >= 28 ? 12 : (type >= 25 ? 10 : 8)); @@ -267,7 +263,6 @@ namespace ClassicalSharp.Particles { void IDisposable.Dispose() { game.Graphics.DeleteTexture(ref ParticlesTexId); game.UserEvents.BlockChanged -= BreakBlockEffect; - game.Events.TerrainAtlasChanged -= TerrainAtlasChanged; game.Events.TextureChanged -= TextureChanged; ContextLost(); diff --git a/ClassicalSharp/Rendering/ChunkUpdater.cs b/ClassicalSharp/Rendering/ChunkUpdater.cs index b7488b852..1e138abcb 100644 --- a/ClassicalSharp/Rendering/ChunkUpdater.cs +++ b/ClassicalSharp/Rendering/ChunkUpdater.cs @@ -83,8 +83,7 @@ namespace ClassicalSharp.Renderers { ResetChunkCache(); } - renderer.normalPartsCount = new int[TerrainAtlas1D.TexIds.Length]; - renderer.translucentPartsCount = new int[TerrainAtlas1D.TexIds.Length]; + ResetPartsCounts(); } void RefreshBorders(int clipLevel) { @@ -115,21 +114,18 @@ namespace ClassicalSharp.Renderers { } void TerrainAtlasChanged(object sender, EventArgs e) { - if (renderer._1DUsed == -1) { - renderer.normalPartsCount = new int[TerrainAtlas1D.TexIds.Length]; - renderer.translucentPartsCount = new int[TerrainAtlas1D.TexIds.Length]; - } else { - bool refreshRequired = elementsPerBitmap != TerrainAtlas1D.TilesPerAtlas; + if (renderer._1DUsed != -1) { + bool refreshRequired = elementsPerBitmap != Atlas1D.TilesPerAtlas; if (refreshRequired) Refresh(); } - renderer._1DUsed = TerrainAtlas1D.UsedAtlasesCount(); - elementsPerBitmap = TerrainAtlas1D.TilesPerAtlas; + renderer._1DUsed = Atlas1D.UsedAtlasesCount(); + elementsPerBitmap = Atlas1D.TilesPerAtlas; ResetUsedFlags(); } void BlockDefinitionChanged(object sender, EventArgs e) { - renderer._1DUsed = TerrainAtlas1D.UsedAtlasesCount(); + renderer._1DUsed = Atlas1D.UsedAtlasesCount(); ResetUsedFlags(); Refresh(); } @@ -143,30 +139,25 @@ namespace ClassicalSharp.Renderers { } void ResetUsedFlags() { - int count = renderer._1DUsed; - bool[] used = renderer.usedTranslucent; - if (used == null || count > used.Length) { - renderer.usedTranslucent = new bool[count]; - renderer.usedNormal = new bool[count]; - renderer.pendingTranslucent = new bool[count]; - renderer.pendingNormal = new bool[count]; - } - - for (int i = 0; i < count; i++) { + for (int i = 0; i < Atlas1D.MaxAtlases; i++) { renderer.pendingTranslucent[i] = true; - renderer.usedTranslucent[i] = false; - renderer.pendingNormal[i] = true; - renderer.usedNormal[i] = false; + renderer.usedTranslucent[i] = false; + renderer.pendingNormal[i] = true; + renderer.usedNormal[i] = false; + } + } + + void ResetPartsCounts() { + for (int i = 0; i < Atlas1D.MaxAtlases; i++) { + renderer.normalPartsCount[i] = 0; + renderer.translucentPartsCount[i] = 0; } } void OnNewMap(object sender, EventArgs e) { game.ChunkUpdates = 0; ClearChunkCache(); - for (int i = 0; i < renderer.normalPartsCount.Length; i++) { - renderer.normalPartsCount[i] = 0; - renderer.translucentPartsCount[i] = 0; - } + ResetPartsCounts(); renderer.chunks = null; renderer.unsortedChunks = null; @@ -218,11 +209,11 @@ namespace ClassicalSharp.Renderers { void ClearChunkCache() { if (renderer.chunks == null) return; - for (int i = 0; i < renderer.chunks.Length; i++) + for (int i = 0; i < renderer.chunks.Length; i++) { DeleteChunk(renderer.chunks[i]); + } - renderer.normalPartsCount = new int[TerrainAtlas1D.TexIds.Length]; - renderer.translucentPartsCount = new int[TerrainAtlas1D.TexIds.Length]; + ResetPartsCounts(); } void DeleteChunk(ChunkInfo info) { diff --git a/ClassicalSharp/Rendering/Env/EnvRenderer.cs b/ClassicalSharp/Rendering/Env/EnvRenderer.cs index a8a73dfac..026d22e2e 100644 --- a/ClassicalSharp/Rendering/Env/EnvRenderer.cs +++ b/ClassicalSharp/Rendering/Env/EnvRenderer.cs @@ -11,7 +11,7 @@ namespace ClassicalSharp.Renderers { public unsafe sealed class EnvRenderer : IGameComponent { - int cloudsVb = -1, cloudVertices, skyVb = -1, skyVertices, cloudsTex; + int cloudsVb, cloudVertices, skyVb, skyVertices, cloudsTex; World map; Game game; internal bool legacy, minimal; @@ -59,7 +59,7 @@ namespace ClassicalSharp.Renderers { public void Render(double deltaTime) { if (minimal) { RenderMinimal(deltaTime); return; } - if (skyVb == -1 || cloudsVb == -1) return; + if (skyVb == 0 || cloudsVb == 0) return; if (!game.SkyboxRenderer.ShouldRender) { RenderSky(deltaTime); diff --git a/ClassicalSharp/Rendering/Env/MapBordersRenderer.cs b/ClassicalSharp/Rendering/Env/MapBordersRenderer.cs index b40dd773d..038c75845 100644 --- a/ClassicalSharp/Rendering/Env/MapBordersRenderer.cs +++ b/ClassicalSharp/Rendering/Env/MapBordersRenderer.cs @@ -5,6 +5,7 @@ using System.Drawing; using ClassicalSharp.Events; using ClassicalSharp.GraphicsAPI; using ClassicalSharp.Map; +using ClassicalSharp.Textures; using OpenTK; using BlockID = System.UInt16; @@ -15,7 +16,7 @@ namespace ClassicalSharp.Renderers { World map; Game game; - int sidesVb = -1, edgesVb = -1; + int sidesVb, edgesVb; int edgeTexId, sideTexId; int sidesVertices, edgesVertices; internal bool legacy; @@ -38,7 +39,7 @@ namespace ClassicalSharp.Renderers { } public void RenderSides(double delta) { - if (sidesVb == -1) return; + if (sidesVb == 0) return; BlockID block = game.World.Env.SidesBlock; IGraphicsApi gfx = game.Graphics; @@ -57,7 +58,7 @@ namespace ClassicalSharp.Renderers { } public void RenderEdges(double delta) { - if (edgesVb == -1) return; + if (edgesVb == 0) return; BlockID block = game.World.Env.EdgeBlock; IGraphicsApi gfx = game.Graphics; @@ -319,7 +320,7 @@ namespace ClassicalSharp.Renderers { lastTexLoc = texLoc; game.Graphics.DeleteTexture(ref id); - id = TerrainAtlas2D.LoadTile(texLoc); + id = Atlas2D.LoadTile(texLoc); } } } \ No newline at end of file diff --git a/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs b/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs index 8be484c4c..7bd0b282d 100644 --- a/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs +++ b/ClassicalSharp/Rendering/Env/SkyboxRenderer.cs @@ -9,7 +9,7 @@ namespace ClassicalSharp.Renderers { public sealed class SkyboxRenderer : IGameComponent { - int tex, vb = -1; + int tex, vb; Game game; const int count = 6 * 4; @@ -62,7 +62,7 @@ namespace ClassicalSharp.Renderers { } public void Render(double deltaTime) { - if (vb == -1) return; + if (vb == 0) return; game.Graphics.DepthWrite = false; game.Graphics.Texturing = true; game.Graphics.BindTexture(tex); diff --git a/ClassicalSharp/Rendering/MapRenderer.cs b/ClassicalSharp/Rendering/MapRenderer.cs index 6f5c2c248..8e178a5f7 100644 --- a/ClassicalSharp/Rendering/MapRenderer.cs +++ b/ClassicalSharp/Rendering/MapRenderer.cs @@ -54,11 +54,15 @@ namespace ClassicalSharp.Renderers { internal int _1DUsed = -1, chunksX, chunksY, chunksZ; internal int renderCount = 0; internal ChunkInfo[] chunks, renderChunks, unsortedChunks; - internal bool[] usedTranslucent, usedNormal; - internal bool[] pendingTranslucent, pendingNormal; - internal int[] normalPartsCount, translucentPartsCount; bool inTranslucent = false; + internal bool[] usedTranslucent = new bool[Atlas1D.MaxAtlases]; + internal bool[] usedNormal = new bool[Atlas1D.MaxAtlases]; + internal bool[] pendingTranslucent = new bool[Atlas1D.MaxAtlases]; + internal bool[] pendingNormal = new bool[Atlas1D.MaxAtlases]; + internal int[] normalPartsCount = new int[Atlas1D.MaxAtlases]; + internal int[] translucentPartsCount = new int[Atlas1D.MaxAtlases]; + public MapRenderer(Game game) { this.game = game; } @@ -86,7 +90,7 @@ namespace ClassicalSharp.Renderers { if (chunks == null) return; IGraphicsApi gfx = game.Graphics; - int[] texIds = TerrainAtlas1D.TexIds; + int[] texIds = Atlas1D.TexIds; gfx.SetBatchFormat(VertexFormat.P3fT2fC4b); gfx.Texturing = true; gfx.AlphaTest = true; @@ -137,7 +141,7 @@ namespace ClassicalSharp.Renderers { gfx.ColourWriteMask(true, true, true, true); gfx.DepthWrite = false; // we already calculated depth values in depth pass - int[] texIds = TerrainAtlas1D.TexIds; + int[] texIds = Atlas1D.TexIds; gfx.EnableMipmaps(); for (int batch = 0; batch < _1DUsed; batch++) { if (translucentPartsCount[batch] <= 0) continue; diff --git a/ClassicalSharp/TexturePack/Animations.cs b/ClassicalSharp/TexturePack/Animations.cs index b88b62d29..070bba58b 100644 --- a/ClassicalSharp/TexturePack/Animations.cs +++ b/ClassicalSharp/TexturePack/Animations.cs @@ -55,11 +55,11 @@ namespace ClassicalSharp.Textures { /// Runs through all animations and if necessary updates the terrain atlas. public void Tick(ScheduledTask task) { if (useLavaAnim) { - int size = Math.Min(TerrainAtlas2D.TileSize, 64); + int size = Math.Min(Atlas2D.TileSize, 64); DrawAnimation(null, 30, size); } if (useWaterAnim) { - int size = Math.Min(TerrainAtlas2D.TileSize, 64); + int size = Math.Min(Atlas2D.TileSize, 64); DrawAnimation(null, 14, size); } @@ -154,8 +154,8 @@ namespace ClassicalSharp.Textures { } unsafe void DrawAnimationCore(AnimationData data, int texId, int size, byte* temp) { - int index = TerrainAtlas1D.Get1DIndex(texId); - int rowNum = TerrainAtlas1D.Get1DRowId(texId); + int index = Atlas1D.Get1DIndex(texId); + int rowNum = Atlas1D.Get1DRowId(texId); animPart.SetData(size, size, size * 4, (IntPtr)temp, false); if (data == null) { @@ -169,8 +169,8 @@ namespace ClassicalSharp.Textures { data.FrameY, 0, 0, animsBuffer, animPart, size); } - int y = rowNum * TerrainAtlas2D.TileSize; - game.Graphics.UpdateTexturePart(TerrainAtlas1D.TexIds[index], 0, y, animPart, game.Graphics.Mipmaps); + int y = rowNum * Atlas2D.TileSize; + game.Graphics.UpdateTexturePart(Atlas1D.TexIds[index], 0, y, animPart, game.Graphics.Mipmaps); } bool IsDefaultZip() { @@ -209,7 +209,7 @@ namespace ClassicalSharp.Textures { const string terrainFormat = "&cAnimation frames for tile ({0}, {1}) are bigger than the size of a tile in terrain.png"; void ValidateAnimations() { validated = true; - int elemSize = TerrainAtlas2D.TileSize; + int elemSize = Atlas2D.TileSize; for (int i = animations.Count - 1; i >= 0; i--) { AnimationData a = animations[i]; if (a.FrameSize > elemSize) { diff --git a/ClassicalSharp/TexturePack/TerrainAtlas1D.cs b/ClassicalSharp/TexturePack/TerrainAtlas.cs similarity index 54% rename from ClassicalSharp/TexturePack/TerrainAtlas1D.cs rename to ClassicalSharp/TexturePack/TerrainAtlas.cs index df9e38bd9..9bf385f6c 100644 --- a/ClassicalSharp/TexturePack/TerrainAtlas1D.cs +++ b/ClassicalSharp/TexturePack/TerrainAtlas.cs @@ -8,12 +8,46 @@ using Android.Graphics; namespace ClassicalSharp.Textures { - /// Represents a 2D packed texture atlas that has been converted into an array of 1D atlases. - public static class TerrainAtlas1D { - public static int TilesPerAtlas; - public static float invTileSize; - public static int[] TexIds; + /// Represents a 2D packed texture atlas, specifically for terrain.png. + public static class Atlas2D { + public const int TilesPerRow = 16, MaxRowsCount = 32; + public static Bitmap Atlas; + public static int TileSize, RowsCount; internal static Game game; + + public static void UpdateState(Bitmap bmp) { + Atlas = bmp; + TileSize = bmp.Width / TilesPerRow; + RowsCount = bmp.Height / TilesPerRow; + BlockInfo.RecalculateSpriteBB(); + } + + public static int LoadTile(int texLoc) { + int size = TileSize; + using (FastBitmap atlas = new FastBitmap(Atlas, true, true)) + using (Bitmap bmp = Platform.CreateBmp(size, size)) + using (FastBitmap dst = new FastBitmap(bmp, true, false)) + { + int x = texLoc % TilesPerRow, y = texLoc / TilesPerRow; + y %= RowsCount; + + 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(); + } + } + + /// Represents a 2D packed texture atlas that has been converted into an array of 1D atlases. + public static class Atlas1D { + public static int TilesPerAtlas, AtlasesCount; + public static float invTileSize; + internal static Game game; + 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; @@ -30,38 +64,33 @@ namespace ClassicalSharp.Textures { public static int Get1DRowId(int texLoc) { return texLoc % TilesPerAtlas; } public static void UpdateState() { - int tileSize = TerrainAtlas2D.TileSize; + int tileSize = Atlas2D.TileSize; + int maxTiles = Atlas2D.RowsCount * Atlas2D.TilesPerRow; int maxAtlasHeight = Math.Min(4096, game.Graphics.MaxTextureDimensions); int maxTilesPerAtlas = maxAtlasHeight / tileSize; - const int maxTiles = TerrainAtlas2D.RowsCount * TerrainAtlas2D.TilesPerRow; TilesPerAtlas = Math.Min(maxTilesPerAtlas, maxTiles); - int atlasesCount = Utils.CeilDiv(maxTiles, TilesPerAtlas); - int atlasHeight = TilesPerAtlas * tileSize; - + AtlasesCount = Utils.CeilDiv(maxTiles, TilesPerAtlas); invTileSize = 1f / TilesPerAtlas; - Convert2DTo1D(atlasesCount, atlasHeight); - } - - static void Convert2DTo1D(int atlasesCount, int atlas1DHeight) { - TexIds = new int[atlasesCount]; - Utils.LogDebug("Loaded new atlas: {0} bmps, {1} per bmp", atlasesCount, TilesPerAtlas); - int index = 0; - using (FastBitmap atlas = new FastBitmap(TerrainAtlas2D.Atlas, true, true)) { - for (int i = 0; i < TexIds.Length; i++) - Make1DTexture(i, atlas, atlas1DHeight, ref index); + 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++) { + Make1DTexture(i, atlas, atlasHeight, ref index); + } } } static void Make1DTexture(int i, FastBitmap atlas, int atlas1DHeight, ref int index) { - int tileSize = TerrainAtlas2D.TileSize; + int tileSize = Atlas2D.TileSize; 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 % TerrainAtlas2D.TilesPerRow) * tileSize; - int atlasY = (index / TerrainAtlas2D.TilesPerRow) * tileSize; + int atlasX = (index % Atlas2D.TilesPerRow) * tileSize; + int atlasY = (index / Atlas2D.TilesPerRow) * tileSize; FastBitmap.MovePortion(atlasX, atlasY, 0, index1D * tileSize, atlas, dst, tileSize); @@ -75,14 +104,14 @@ namespace ClassicalSharp.Textures { int maxTexLoc = 0; for (int i = 0; i < BlockInfo.textures.Length; i++) { maxTexLoc = Math.Max(maxTexLoc, BlockInfo.textures[i]); - } + } return Get1DIndex(maxTexLoc) + 1; } public static void Dispose() { if (TexIds == null) return; - for (int i = 0; i < TexIds.Length; i++) { + for (int i = 0; i < AtlasesCount; i++) { game.Graphics.DeleteTexture(ref TexIds[i]); } } diff --git a/ClassicalSharp/TexturePack/TerrainAtlas2D.cs b/ClassicalSharp/TexturePack/TerrainAtlas2D.cs deleted file mode 100644 index 5b9a19991..000000000 --- a/ClassicalSharp/TexturePack/TerrainAtlas2D.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 -using System; -using System.Drawing; -using ClassicalSharp.GraphicsAPI; -#if ANDROID -using Android.Graphics; -#endif - -namespace ClassicalSharp { - - /// Represents a 2D packed texture atlas, specifically for terrain.png. - public static class TerrainAtlas2D { - - public const int TilesPerRow = 16, RowsCount = 16; - public static Bitmap Atlas; - public static int TileSize; - internal static Game game; - - /// Updates the underlying atlas bitmap, fields, and texture. - public static void UpdateState(Bitmap bmp) { - Atlas = bmp; - TileSize = bmp.Width / TilesPerRow; - BlockInfo.RecalculateSpriteBB(); - } - - /// Creates a new texture that contains the tile at the specified index. - public static int LoadTile(int index) { - int size = TileSize; - using (FastBitmap atlas = new FastBitmap(Atlas, true, true)) - using (Bitmap bmp = Platform.CreateBmp(size, size)) - using (FastBitmap dst = new FastBitmap(bmp, true, false)) - { - int x = index % TilesPerRow, y = index / TilesPerRow; - FastBitmap.MovePortion(x * size, y * size, 0, 0, atlas, dst, size); - return game.Graphics.CreateTexture(dst, false, game.Graphics.Mipmaps); - } - } - - /// Disposes of the underlying atlas bitmap and texture. - public static void Dispose() { - if (Atlas != null) Atlas.Dispose(); - } - } -} diff --git a/ClassicalSharp/Utils/StringBuffer.cs b/ClassicalSharp/Utils/StringBuffer.cs index 229954d23..f0d9d9d12 100644 --- a/ClassicalSharp/Utils/StringBuffer.cs +++ b/ClassicalSharp/Utils/StringBuffer.cs @@ -116,7 +116,7 @@ namespace ClassicalSharp { return new String(tmp, 0, len); } - public void WordWrap(IDrawer2D drawer, string[] lines, int numLines, int lineLen) { + public void WordWrap(string[] lines, int numLines, int lineLen) { for (int i = 0; i < numLines; i++) { lines[i] = null; } int lineStart = 0, lineEnd; diff --git a/OpenTK/NativeWindow.cs b/OpenTK/NativeWindow.cs index 510da7b9d..e8bd0e496 100644 --- a/OpenTK/NativeWindow.cs +++ b/OpenTK/NativeWindow.cs @@ -50,8 +50,8 @@ namespace OpenTK { /// If width or height is less than 1. /// If mode or device is null. public NativeWindow(int width, int height, string title, GraphicsMode mode, DisplayDevice device) - : this(device.Bounds.Left + (device.Bounds.Width - width) / 2, - device.Bounds.Top + (device.Bounds.Height - height) / 2, + : this(device.Bounds.Left + (device.Bounds.Width - width) / 2, + device.Bounds.Top + (device.Bounds.Height - height) / 2, width, height, title, mode, device) { } public NativeWindow(int width, int height, string title, GameWindowFlags flags, GraphicsMode mode, DisplayDevice device) : this(width, height, title, mode, device) {}