From b6ff532a23d64720432133c6a4e9cae6525d995c Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 28 Oct 2017 22:02:39 +1100 Subject: [PATCH] And also for associating bitmaps with device contexts (Completely untested) --- .../Entities/Components/PhysicsComponent.cs | 26 +++++++----- src/Client/Drawer2D.c | 41 ++++++++++++++----- src/Client/Drawer2D.h | 7 ++-- src/Client/GameMode.c | 19 +++++---- src/Client/Platform.h | 12 ++++-- src/Client/Widgets.c | 8 ++-- src/Client/WinPlatform.c | 28 +++++++++---- 7 files changed, 93 insertions(+), 48 deletions(-) diff --git a/ClassicalSharp/Entities/Components/PhysicsComponent.cs b/ClassicalSharp/Entities/Components/PhysicsComponent.cs index 350b13989..56aeb041f 100644 --- a/ClassicalSharp/Entities/Components/PhysicsComponent.cs +++ b/ClassicalSharp/Entities/Components/PhysicsComponent.cs @@ -225,23 +225,27 @@ namespace ClassicalSharp.Entities { } float LowestModifier(AABB bounds, bool checkSolid) { - Vector3I bbMin = Vector3I.Floor(bounds.Min); - Vector3I bbMax = Vector3I.Floor(bounds.Max); + Vector3I min = Vector3I.Floor(bounds.Min); + Vector3I max = Vector3I.Floor(bounds.Max); float modifier = inf; - for (int y = bbMin.Y; y <= bbMax.Y; y++) - for (int z = bbMin.Z; z <= bbMax.Z; z++) - for (int x = bbMin.X; x <= bbMax.X; x++) + AABB blockBB = default(AABB); + min.X = min.X < 0 ? 0 : min.X; max.X = max.X > game.World.MaxX ? game.World.MaxX : max.X; + min.Y = min.Y < 0 ? 0 : min.Y; max.Y = max.Y > game.World.MaxY ? game.World.MaxY : max.Y; + min.Z = min.Z < 0 ? 0 : min.Z; max.Z = max.Z > game.World.MaxZ ? game.World.MaxZ : max.Z; + + for (int y = min.Y; y <= max.Y; y++) + for (int z = min.Z; z <= max.Z; z++) + for (int x = min.X; x <= max.X; x++) { - BlockID block = game.World.SafeGetBlock(x, y, z); + BlockID block = game.World.GetBlock(x, y, z); if (block == 0) continue; byte collide = BlockInfo.Collide[block]; - if (collide == CollideType.Solid && !checkSolid) - continue; + if (collide == CollideType.Solid && !checkSolid) continue; - Vector3 min = new Vector3(x, y, z) + BlockInfo.MinBB[block]; - Vector3 max = new Vector3(x, y, z) + BlockInfo.MaxBB[block]; - AABB blockBB = new AABB(min, max); + Vector3 v = new Vector3(x, y, z); + blockBB.Min = v + BlockInfo.MinBB[block]; + blockBB.Max = v + BlockInfo.MaxBB[block]; if (!blockBB.Intersects(bounds)) continue; modifier = Math.Min(modifier, BlockInfo.SpeedMultiplier[block]); diff --git a/src/Client/Drawer2D.c b/src/Client/Drawer2D.c index 6a87efdae..42fcdd984 100644 --- a/src/Client/Drawer2D.c +++ b/src/Client/Drawer2D.c @@ -95,22 +95,28 @@ void Drawer2D_Free(void) { Drawer2D_FreeFontBitmap(); } -/* Sets the underlying bitmap that drawing operations are performed on. */ -void Drawer2D_Begin(Bitmap* bmp); -/* Frees any resources associated with the underlying bitmap. */ -void Drawer2D_End(void); -/* Draws a 2D flat rectangle. */ -void Drawer2D_Rect(PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height); +void Drawer2D_Begin(Bitmap* bmp) { + Platform_SetBitmap(bmp); + Drawer2D_Cur = bmp; +} -void Drawer2D_Clear(PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height) { - if (x < 0 || y < 0 || (x + width) > Drawer2D_Cur->Width || (y + height) > Drawer2D_Cur->Height) { +void Drawer2D_End(void) { + Platform_SetBitmap(Drawer2D_Cur); + Drawer2D_Cur = NULL; +} + +/* Draws a 2D flat rectangle. */ +void Drawer2D_Rect(Bitmap* bmp, PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height); + +void Drawer2D_Clear(Bitmap* bmp, PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height) { + if (x < 0 || y < 0 || (x + width) > bmp->Width || (y + height) > bmp->Height) { ErrorHandler_Fail("Drawer2D_Clear - tried to clear at invalid coords"); } Int32 xx, yy; UInt32 argb = PackedCol_ToARGB(col); for (yy = 0; yy < height; yy++) { - UInt32* row = Bitmap_GetRow(Drawer2D_Cur, y + yy) + x; + UInt32* row = Bitmap_GetRow(bmp, y + yy) + x; for (xx = 0; xx < width; xx++) { row[xx] = argb; } } } @@ -359,5 +365,18 @@ Size2D Drawer2D_MeasureBitmapText(DrawTextArgs* args) { return total; } -void Drawer2D_DrawText(DrawTextArgs* args, Int32 x, Int32 y); -Size2D Drawer2D_MeasureText(DrawTextArgs* args); \ No newline at end of file +void Drawer2D_DrawText(DrawTextArgs* args, Int32 x, Int32 y) { + if (Drawer2D_UseBitmappedChat) { + Drawer2D_DrawBitmapText(args, x, y); + } else { + Platform_DrawText(args, x, y); + } +} + +Size2D Drawer2D_MeasureText(DrawTextArgs* args) { + if (Drawer2D_UseBitmappedChat) { + return Drawer2D_MeasureBitmapText(args); + } else { + return Platform_MeasureText(args); + } +} \ No newline at end of file diff --git a/src/Client/Drawer2D.h b/src/Client/Drawer2D.h index 50b88b201..05743e65d 100644 --- a/src/Client/Drawer2D.h +++ b/src/Client/Drawer2D.h @@ -11,6 +11,7 @@ Copyright 2017 ClassicalSharp | Licensed under BSD-3 #include "Texture.h" #include "Constants.h" +typedef struct DrawTextArgs_ { String Text; FontDesc Font; bool UseShadow; } DrawTextArgs; void DrawTextArgs_Make(DrawTextArgs* args, STRING_REF String* text, FontDesc* font, bool useShadow); void DrawTextArgs_MakeEmpty(DrawTextArgs* args, FontDesc* font, bool useShadow); @@ -26,14 +27,14 @@ PackedCol Drawer2D_Cols[DRAWER2D_MAX_COLS]; void Drawer2D_Init(void); void Drawer2D_Free(void); -/* Sets the underlying bitmap that drawing operations are performed on. */ +/* Sets the underlying bitmap that text operations are performed on. */ void Drawer2D_Begin(Bitmap* bmp); /* Frees any resources associated with the underlying bitmap. */ void Drawer2D_End(void); /* Draws a 2D flat rectangle. */ -void Drawer2D_Rect(PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height); +void Drawer2D_Rect(Bitmap* bmp, PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height); /* Clears the entire given area to the specified colour. */ -void Drawer2D_Clear(PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height); +void Drawer2D_Clear(Bitmap* bmp, PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height); void Drawer2D_DrawText(DrawTextArgs* args, Int32 x, Int32 y); Size2D Drawer2D_MeasureText(DrawTextArgs* args); diff --git a/src/Client/GameMode.c b/src/Client/GameMode.c index 8fc28454f..d03fef248 100644 --- a/src/Client/GameMode.c +++ b/src/Client/GameMode.c @@ -54,21 +54,24 @@ void GameMode_PickMiddle(BlockID old) { if (Block_Draw[old] == DrawType_Gas) return; if (!(Block_CanPlace[old] || Block_CanDelete[old])) return; if (!Inventory_CanChangeSelected() || Inventory_SelectedBlock == old) return; + UInt32 i; - // Is the currently selected block an empty slot + /* Is the currently selected block an empty slot */ if (Inventory_Get(Inventory_SelectedIndex) == BlockID_Air) { - Inventory_SetSelectedBlock(old); - return; + Inventory_SetSelectedBlock(old); return; } - /* Try to replace same block or empty slots first */ - UInt32 i; + /* Try to replace same block */ for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) { - if (Inventory_Get(i) != old && Inventory_Get(i) != BlockID_Air) continue; + if (Inventory_Get(i) != old) continue; + Inventory_SetSelectedIndex(i); return; + } + /* Try to replace empty slots */ + for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) { + if (Inventory_Get(i) != BlockID_Air) continue; Inventory_Set(i, old); - Inventory_SetSelectedIndex(i); - return; + Inventory_SetSelectedIndex(i); return; } /* Finally, replace the currently selected block */ diff --git a/src/Client/Platform.h b/src/Client/Platform.h index df2d26ae1..07e0313d3 100644 --- a/src/Client/Platform.h +++ b/src/Client/Platform.h @@ -3,10 +3,10 @@ #include "Typedefs.h" #include "DateTime.h" #include "String.h" +#include "2DStructs.h" /* Abstracts platform specific memory management, I/O, etc. Copyright 2017 ClassicalSharp | Licensed under BSD-3 */ -typedef struct DrawTextArgs_ { String Text; FontDesc Font; bool UseShadow; } DrawTextArgs; /* Initalises required state for this platform. */ void Platform_Init(void); @@ -56,12 +56,18 @@ UInt32 Platform_FileLength(void* file); /* Blocks the calling thread for given number of milliseconds. */ void Platform_ThreadSleep(UInt32 milliseconds); +struct DrawTextArgs_; +struct Bitmap_; /* Allocates handle for the given font. */ void Platform_MakeFont(FontDesc* desc); /* Frees handle for the given font. */ void Platform_FreeFont(FontDesc* desc); /* Measures size of given text.*/ -Size2D Platform_MeasureText(DrawTextArgs* args); +Size2D Platform_MeasureText(struct DrawTextArgs_* args); /* Draws text onto the actively selected bitmap. */ -void Platform_DrawText(DrawTextArgs* args, Int32 x, Int32 y); +void Platform_DrawText(struct DrawTextArgs_* args, Int32 x, Int32 y); +/* Sets the bitmap used for text drawing. */ +void Platform_SetBitmap(struct Bitmap_* bmp); +/* Releases the bitmap that was used for text drawing.*/ +void Platform_ReleaseBitmap(struct Bitmap_* bmp); #endif \ No newline at end of file diff --git a/src/Client/Widgets.c b/src/Client/Widgets.c index 0ab972595..c190efde6 100644 --- a/src/Client/Widgets.c +++ b/src/Client/Widgets.c @@ -935,7 +935,7 @@ Int32 SpecialInputWidget_MeasureTitles(SpecialInputWidget* widget) { return totalWidth; } -void SpecialInputWidget_DrawTitles(SpecialInputWidget* widget) { +void SpecialInputWidget_DrawTitles(SpecialInputWidget* widget, Bitmap* bmp) { Int32 x = 0; DrawTextArgs args; DrawTextArgs_MakeEmpty(&args, &widget->Font, false); @@ -947,7 +947,7 @@ void SpecialInputWidget_DrawTitles(SpecialInputWidget* widget) { PackedCol col = i == widget->SelectedIndex ? col_selected : col_inactive; Size2D size = widget->Tabs[i].TitleSize; - Drawer2D_Clear(col, x, 0, size.Width, size.Height); + Drawer2D_Clear(bmp, col, x, 0, size.Width, size.Height); Drawer2D_DrawText(&args, x + SPECIAL_TITLE_SPACING / 2, 0); x += size.Width; } @@ -1011,9 +1011,9 @@ void SpecialInputWidget_Make(SpecialInputWidget* widget, SpecialInputTab* e) { Bitmap_AllocatePow2(&bmp, size.Width, size.Height); Drawer2D_Begin(&bmp); - SpecialInputWidget_DrawTitles(widget); + SpecialInputWidget_DrawTitles(widget, &bmp); PackedCol col = PACKEDCOL_CONST(30, 30, 30, 200); - Drawer2D_Clear(col, 0, titleHeight, size.Width, bodySize.Height); + Drawer2D_Clear(&bmp, col, 0, titleHeight, size.Width, bodySize.Height); SpecialInputWidget_DrawContent(widget, e, titleHeight); widget->Tex = Drawer2D_Make2DTexture(&bmp, size, widget->Base.X, widget->Base.Y); diff --git a/src/Client/WinPlatform.c b/src/Client/WinPlatform.c index 38e16de17..ba2f1f9b9 100644 --- a/src/Client/WinPlatform.c +++ b/src/Client/WinPlatform.c @@ -3,11 +3,14 @@ #include "DisplayDevice.h" #include "ExtMath.h" #include "ErrorHandler.h" +#include "Drawer2D.h" #define WIN32_LEAN_AND_MEAN #include HDC hdc; +HBITMAP hbmp; HANDLE heap; + void Platform_Init(void) { heap = GetProcessHeap(); /* TODO: HeapCreate instead? probably not */ hdc = CreateCompatibleDC(NULL); @@ -189,19 +192,15 @@ void Platform_MakeFont(FontDesc* desc) { font.lfQuality = ANTIALIASED_QUALITY; desc->Handle = CreateFontIndirectA(&font); - if (desc->Handle == NULL) { - ErrorHandler_Fail("Creating font handle failed"); - } + if (desc->Handle == NULL) ErrorHandler_Fail("Creating font handle failed"); } void Platform_FreeFont(FontDesc* desc) { - if (!DeleteObject(desc->Handle)) { - ErrorHandler_Fail("Deleting font handle failed"); - } + if (!DeleteObject(desc->Handle)) ErrorHandler_Fail("Deleting font handle failed"); desc->Handle = NULL; } -Size2D Platform_MeasureText(DrawTextArgs* args) { +Size2D Platform_MeasureText(struct DrawTextArgs_* args) { HDC hDC = GetDC(NULL); RECT r = { 0 }; DrawTextA(hDC, args->Text.buffer, args->Text.length, @@ -209,9 +208,22 @@ Size2D Platform_MeasureText(DrawTextArgs* args) { return Size2D_Make(r.right, r.bottom); } -void Platform_DrawText(DrawTextArgs* args, Int32 x, Int32 y) { +void Platform_DrawText(struct DrawTextArgs_* args, Int32 x, Int32 y) { HDC hDC = GetDC(NULL); RECT r = { 0 }; DrawTextA(hDC, args->Text.buffer, args->Text.length, &r, DT_NOPREFIX | DT_SINGLELINE | DT_NOCLIP); +} + +void Platform_SetBitmap(struct Bitmap_* bmp) { + hbmp = CreateBitmap(bmp->Width, bmp->Height, 1, 32, bmp->Scan0); + if (hbmp == NULL) ErrorHandler_Fail("Creating bitmap handle failed"); + /* TODO: Should we be using CreateDIBitmap here? */ + + if (!SelectObject(hdc, hbmp)) ErrorHandler_Fail("Selecting bitmap handle"); +} + +void Platform_ReleaseBitmap(struct Bitmap_* bmp) { + if (!DeleteObject(hbmp)) ErrorHandler_Fail("Deleting bitmap handle failed"); + hbmp = NULL; } \ No newline at end of file