From 7a77a34b142dd3ab763206808f21c06ff9b826d6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 19 Sep 2017 23:11:59 +1000 Subject: [PATCH] Little bit less use of generics --- .../Widgets/PlayerList/ExtPlayerListWidget.cs | 20 +++++++++---------- .../PlayerList/NormalPlayerListWidget.cs | 3 ++- .../2D/Widgets/PlayerList/PlayerListWidget.cs | 5 ++--- .../Rendering/Env/WeatherRenderer.cs | 8 ++++---- src/Client/Bitmap.c | 15 +++++++------- src/Client/Bitmap.h | 20 +++++++------------ src/Client/D3D9Api.c | 2 +- src/Client/PackedCol.c | 4 ++++ src/Client/PackedCol.h | 11 ++++------ src/Client/Stream.h | 6 +++--- src/Client/TerrainAtlas.c | 2 +- 11 files changed, 44 insertions(+), 52 deletions(-) diff --git a/ClassicalSharp/2D/Widgets/PlayerList/ExtPlayerListWidget.cs b/ClassicalSharp/2D/Widgets/PlayerList/ExtPlayerListWidget.cs index 3abb3fa72..da96a0bca 100644 --- a/ClassicalSharp/2D/Widgets/PlayerList/ExtPlayerListWidget.cs +++ b/ClassicalSharp/2D/Widgets/PlayerList/ExtPlayerListWidget.cs @@ -90,7 +90,8 @@ namespace ClassicalSharp.Gui.Widgets { for (int i = 0; i < namesCount; i++) { PlayerInfo pInfo = info[i]; if (!pInfo.IsGroup && pInfo.NameId == e.Id) { - RemoveInfoAt(info, i); + RemoveItemAt(info, i); + RemoveTextureAt(i); return; } } @@ -168,21 +169,18 @@ namespace ClassicalSharp.Gui.Widgets { DrawTextArgs args = new DrawTextArgs(group, titleFont, true); Texture tex = game.Drawer2D.MakeTextTexture(ref args, 0, 0); game.Drawer2D.ReducePadding(ref tex, Utils.Floor(titleFont.Size), 3); - PlayerInfo pInfo = new PlayerInfo(group); - PushDown(info, index, pInfo); - PushDown(textures, index, tex); + for (int i = info.Length - 1; i > index; i--) { + info[i] = info[i - 1]; + textures[i] = textures[i - 1]; + } + info[index] = new PlayerInfo(group); + textures[index] = tex; + index++; namesCount++; } - void PushDown(T[] array, int index, T value) { - for (int i = array.Length - 1; i > index; i--) { - array[i] = array[i - 1]; - } - array[index] = value; - } - int GetGroupCount(int startIndex) { string group = info[startIndex].GroupName; int count = 0; diff --git a/ClassicalSharp/2D/Widgets/PlayerList/NormalPlayerListWidget.cs b/ClassicalSharp/2D/Widgets/PlayerList/NormalPlayerListWidget.cs index 648150ec3..a5628e4df 100644 --- a/ClassicalSharp/2D/Widgets/PlayerList/NormalPlayerListWidget.cs +++ b/ClassicalSharp/2D/Widgets/PlayerList/NormalPlayerListWidget.cs @@ -98,7 +98,8 @@ namespace ClassicalSharp.Gui.Widgets { for (int i = 0; i < namesCount; i++) { PlayerInfo pInfo = info[i]; if (pInfo.Id == e.Id) { - RemoveInfoAt(info, i); + RemoveItemAt(info, i); + RemoveTextureAt(i); return; } } diff --git a/ClassicalSharp/2D/Widgets/PlayerList/PlayerListWidget.cs b/ClassicalSharp/2D/Widgets/PlayerList/PlayerListWidget.cs index 0859f64a6..6ccb3ddfc 100644 --- a/ClassicalSharp/2D/Widgets/PlayerList/PlayerListWidget.cs +++ b/ClassicalSharp/2D/Widgets/PlayerList/PlayerListWidget.cs @@ -119,10 +119,9 @@ namespace ClassicalSharp.Gui.Widgets { protected abstract void SortInfoList(); - protected void RemoveInfoAt(T[] info, int i) { + protected void RemoveTextureAt(int i) { Texture tex = textures[i]; - gfx.DeleteTexture(ref tex); - RemoveItemAt(info, i); + gfx.DeleteTexture(ref tex); RemoveItemAt(textures, i); namesCount--; SortPlayerInfo(); diff --git a/ClassicalSharp/Rendering/Env/WeatherRenderer.cs b/ClassicalSharp/Rendering/Env/WeatherRenderer.cs index 3168eb7db..c4066f6c8 100644 --- a/ClassicalSharp/Rendering/Env/WeatherRenderer.cs +++ b/ClassicalSharp/Rendering/Env/WeatherRenderer.cs @@ -86,10 +86,10 @@ namespace ClassicalSharp.Renderers { v.X = x2; v.Z = z2; v.U = 1; vertices[vCount++] = v; v.Y = y1; v.V = v1; vertices[vCount++] = v; - v.Z = z1; vertices[vCount++] = v; - v.Y = y2; v.V = v2; vertices[vCount++] = v; - v.X = x1; v.Z = z2; v.U = 0; vertices[vCount++] = v; - v.Y = y1; v.V = v1; vertices[vCount++] = v; + v.Z = z1; vertices[vCount++] = v; + v.Y = y2; v.V = v2; vertices[vCount++] = v; + v.X = x1; v.Z = z2; v.U = 0; vertices[vCount++] = v; + v.Y = y1; v.V = v1; vertices[vCount++] = v; } if (particles && (rainAcc >= 0.25 || moved)) { rainAcc = 0; diff --git a/src/Client/Bitmap.c b/src/Client/Bitmap.c index 64e254a03..84aa6cc79 100644 --- a/src/Client/Bitmap.c +++ b/src/Client/Bitmap.c @@ -1,14 +1,13 @@ #include "Bitmap.h" #include "Platform.h" +#include "PackedCol.h" +#include "ExtMath.h" -void Bitmap_Create(Bitmap* bmp, Int32 width, Int32 height, Int32 stride, UInt8* scan0) { - bmp->Width = width; bmp->Height = height; bmp->Stride = stride; bmp->Scan0 = scan0; +void Bitmap_Create(Bitmap* bmp, Int32 width, Int32 height, UInt8* scan0) { + bmp->Width = width; bmp->Height = height; + bmp->Stride = width * Bitmap_PixelBytesSize; + bmp->Scan0 = scan0; } - -UInt32* Bitmap_GetRow(Bitmap* bmp, Int32 y) { - return (UInt32*)(bmp->Scan0 + (y * bmp->Stride)); -} - void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, Bitmap* src, Bitmap* dst, Int32 size) { Int32 x, y; for (y = 0; y < size; y++) { @@ -31,6 +30,6 @@ void Bitmap_CopyRow(Int32 srcY, Int32 dstY, Bitmap* src, Bitmap* dst, Int32 widt void Bitmap_Allocate(Bitmap* bmp, Int32 width, Int32 height) { bmp->Width = width; bmp->Height = height; - bmp->Stride = width * sizeof(UInt32); + bmp->Stride = width * Bitmap_PixelBytesSize; bmp->Scan0 = Platform_MemAlloc(Bitmap_DataSize(width, height)); } \ No newline at end of file diff --git a/src/Client/Bitmap.h b/src/Client/Bitmap.h index 9caae8248..c057fba7e 100644 --- a/src/Client/Bitmap.h +++ b/src/Client/Bitmap.h @@ -1,6 +1,7 @@ #ifndef CS_BITMAP_H #define CS_BITMAP_H #include "Typedefs.h" +#include "Stream.h" /* Represents a 2D array of pixels. Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3 */ @@ -8,13 +9,10 @@ typedef struct Bitmap_ { /* Pointer to first scaneline. */ UInt8* Scan0; - /* Number of bytes in each scanline. */ - Int32 Stride; - + Int32 Stride; /* TODO: Obsolete this completely and just use Width << 2 ? */ /* Number of pixels horizontally. */ Int32 Width; - /* Number of pixels vertically. */ Int32 Height; } Bitmap; @@ -22,23 +20,19 @@ typedef struct Bitmap_ { /* Size of each ARGB pixel in bytes. */ #define Bitmap_PixelBytesSize 4 - /* Calculates size of data of a 2D bitmap in bytes. */ #define Bitmap_DataSize(width, height) ((UInt32)(width) * (UInt32)(height) * (UInt32)Bitmap_PixelBytesSize) - +/* Returns a pointer to the start of the y'th scanline. */ +#define Bitmap_GetRow(bmp, y) ((UInt32*)((bmp)->Scan0 + ((y) * (bmp)->Stride))) /* Constructs or updates a Bitmap instance. */ -void Bitmap_Create(Bitmap* bmp, Int32 width, Int32 height, Int32 stride, UInt8* scan0); - -/* Returns a pointer to the start of the y'th scanline. */ -UInt32* Bitmap_GetRow(Bitmap* bmp, Int32 y); - +void Bitmap_Create(Bitmap* bmp, Int32 width, Int32 height, UInt8* scan0); /* Copies a block of pixels from one bitmap to another. */ void Bitmap_CopyBlock(Int32 srcX, Int32 srcY, Int32 dstX, Int32 dstY, Bitmap* src, Bitmap* dst, Int32 size); - /* Copies a row of pixels from one bitmap to another. */ void Bitmap_CopyRow(Int32 srcY, Int32 dstY, Bitmap* src, Bitmap* dst, Int32 width); - /* Allocates a new bitmap of the given dimensions. You are responsible for freeing its memory! */ void Bitmap_Allocate(Bitmap* bmp, Int32 width, Int32 height); +/* Decodes a PNG bitmap from the given stream. */ +void Bitmap_DecodePng(Bitmap* bmp, Stream* stream); #endif \ No newline at end of file diff --git a/src/Client/D3D9Api.c b/src/Client/D3D9Api.c index d4b7f5f9b..086568d48 100644 --- a/src/Client/D3D9Api.c +++ b/src/Client/D3D9Api.c @@ -335,7 +335,7 @@ void D3D9_DoMipmaps(IDirect3DTexture9* texture, Int32 x, Int32 y, Bitmap* bmp, b GfxCommon_GenMipmaps(width, height, cur, prev); Bitmap mipmap; - Bitmap_Create(&mipmap, width, height, width * Bitmap_PixelBytesSize, cur); + Bitmap_Create(&mipmap, width, height, cur); if (partial) { D3D9_SetTexturePartData(texture, x, y, &mipmap, lvl); } else { diff --git a/src/Client/PackedCol.c b/src/Client/PackedCol.c index 1bbbd11b2..8f9656c02 100644 --- a/src/Client/PackedCol.c +++ b/src/Client/PackedCol.c @@ -13,6 +13,10 @@ PackedCol PackedCol_Create3(UInt8 r, UInt8 g, UInt8 b) { return col; } +UInt32 PackedCol_ToARGB(PackedCol col) { + return PackedCol_ARGB(col.R, col.G, col.B, col.A); +} + PackedCol PackedCol_Scale(PackedCol value, Real32 t) { value.R = (UInt8)(value.R * t); value.G = (UInt8)(value.G * t); diff --git a/src/Client/PackedCol.h b/src/Client/PackedCol.h index 7446059dd..0a0916b7e 100644 --- a/src/Client/PackedCol.h +++ b/src/Client/PackedCol.h @@ -20,28 +20,25 @@ typedef struct PackedCol_ { /* Constructs a new ARGB colour. */ PackedCol PackedCol_Create4(UInt8 r, UInt8 g, UInt8 b, UInt8 a); - /* Constructs a new ARGB colour. */ PackedCol PackedCol_Create3(UInt8 r, UInt8 g, UInt8 b); - /* Returns whether two packed colours are equal. */ #define PackedCol_Equals(a, b) (a.Packed == b.Packed) - +/* Converts a colour to ARGB form. */ +#define PackedCol_ARGB(r, g, b, a) (((UInt32)(r) << 16) | ((UInt32)(g) << 8) | ((UInt32)(b)) | ((UInt32)(a) << 24)) +/* Converts a colour to ARGB form. */ +UInt32 PackedCol_ToARGB(PackedCol col); /* Multiplies the RGB components by t, where t is in [0, 1] */ PackedCol PackedCol_Scale(PackedCol value, Real32 t); - /* Linearly interpolates the RGB components of both colours by t, where t is in [0, 1] */ PackedCol PackedCol_Lerp(PackedCol a, PackedCol b, Real32 t); - #define PackedCol_ShadeX 0.6f #define PackedCol_ShadeZ 0.8f #define PackedCol_ShadeYBottom 0.5f - /* Retrieves shaded colours for ambient block face lighting. */ void PackedCol_GetShaded(PackedCol normal, PackedCol* xSide, PackedCol* zSide, PackedCol* yBottom); - /* TODO: actual constant values? may need to rethink PackedCol */ #define PackedCol_White PackedCol_Create3(255, 255, 255) #define PackedCol_Black PackedCol_Create3( 0, 0, 0) diff --git a/src/Client/Stream.h b/src/Client/Stream.h index 3012c0d31..3242caa94 100644 --- a/src/Client/Stream.h +++ b/src/Client/Stream.h @@ -4,9 +4,9 @@ #include "String.h" #include "ErrorHandler.h" -#define STREAM_SEEK_BEGIN 0 -#define STREAM_SEEK_CURRENT 1 -#define STREAM_SEEK_END 2 +#define STREAM_SEEKFROM_BEGIN 0 +#define STREAM_SEEKFROM_CURRENT 1 +#define STREAM_SEEKFROM_END 2 typedef ReturnCode(*Stream_Operation)(UInt8* data, UInt32 count, UInt32* modified); typedef ReturnCode(*Stream_Seek)(Int32 offset, Int32 seekType); diff --git a/src/Client/TerrainAtlas.c b/src/Client/TerrainAtlas.c index 4e7efcd70..bffab595f 100644 --- a/src/Client/TerrainAtlas.c +++ b/src/Client/TerrainAtlas.c @@ -38,7 +38,7 @@ Int32 Atlas2D_LoadTextureElement(TextureLoc texLoc) { } else { // TODO: does this even work?? UInt8 scan0[Bitmap_DataSize(64, 64)]; - Bitmap_Create(&element, size, size, 64 * Bitmap_PixelBytesSize, scan0); + Bitmap_Create(&element, size, size, scan0); return Atlas2D_LoadTextureElement_Raw(texLoc, &element); } }