And also for associating bitmaps with device contexts (Completely untested)

This commit is contained in:
UnknownShadow200 2017-10-28 22:02:39 +11:00
parent 3dfe3847b9
commit b6ff532a23
7 changed files with 93 additions and 48 deletions

View File

@ -225,23 +225,27 @@ namespace ClassicalSharp.Entities {
} }
float LowestModifier(AABB bounds, bool checkSolid) { float LowestModifier(AABB bounds, bool checkSolid) {
Vector3I bbMin = Vector3I.Floor(bounds.Min); Vector3I min = Vector3I.Floor(bounds.Min);
Vector3I bbMax = Vector3I.Floor(bounds.Max); Vector3I max = Vector3I.Floor(bounds.Max);
float modifier = inf; float modifier = inf;
for (int y = bbMin.Y; y <= bbMax.Y; y++) AABB blockBB = default(AABB);
for (int z = bbMin.Z; z <= bbMax.Z; z++) min.X = min.X < 0 ? 0 : min.X; max.X = max.X > game.World.MaxX ? game.World.MaxX : max.X;
for (int x = bbMin.X; x <= bbMax.X; 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; if (block == 0) continue;
byte collide = BlockInfo.Collide[block]; byte collide = BlockInfo.Collide[block];
if (collide == CollideType.Solid && !checkSolid) if (collide == CollideType.Solid && !checkSolid) continue;
continue;
Vector3 min = new Vector3(x, y, z) + BlockInfo.MinBB[block]; Vector3 v = new Vector3(x, y, z);
Vector3 max = new Vector3(x, y, z) + BlockInfo.MaxBB[block]; blockBB.Min = v + BlockInfo.MinBB[block];
AABB blockBB = new AABB(min, max); blockBB.Max = v + BlockInfo.MaxBB[block];
if (!blockBB.Intersects(bounds)) continue; if (!blockBB.Intersects(bounds)) continue;
modifier = Math.Min(modifier, BlockInfo.SpeedMultiplier[block]); modifier = Math.Min(modifier, BlockInfo.SpeedMultiplier[block]);

View File

@ -95,22 +95,28 @@ void Drawer2D_Free(void) {
Drawer2D_FreeFontBitmap(); Drawer2D_FreeFontBitmap();
} }
/* Sets the underlying bitmap that drawing operations are performed on. */ void Drawer2D_Begin(Bitmap* bmp) {
void Drawer2D_Begin(Bitmap* bmp); Platform_SetBitmap(bmp);
/* Frees any resources associated with the underlying bitmap. */ Drawer2D_Cur = bmp;
void Drawer2D_End(void); }
/* Draws a 2D flat rectangle. */
void Drawer2D_Rect(PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height);
void Drawer2D_Clear(PackedCol col, Int32 x, Int32 y, Int32 width, Int32 height) { void Drawer2D_End(void) {
if (x < 0 || y < 0 || (x + width) > Drawer2D_Cur->Width || (y + height) > Drawer2D_Cur->Height) { 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"); ErrorHandler_Fail("Drawer2D_Clear - tried to clear at invalid coords");
} }
Int32 xx, yy; Int32 xx, yy;
UInt32 argb = PackedCol_ToARGB(col); UInt32 argb = PackedCol_ToARGB(col);
for (yy = 0; yy < height; yy++) { 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; } for (xx = 0; xx < width; xx++) { row[xx] = argb; }
} }
} }
@ -359,5 +365,18 @@ Size2D Drawer2D_MeasureBitmapText(DrawTextArgs* args) {
return total; return total;
} }
void Drawer2D_DrawText(DrawTextArgs* args, Int32 x, Int32 y); void Drawer2D_DrawText(DrawTextArgs* args, Int32 x, Int32 y) {
Size2D Drawer2D_MeasureText(DrawTextArgs* args); 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);
}
}

View File

@ -11,6 +11,7 @@ Copyright 2017 ClassicalSharp | Licensed under BSD-3
#include "Texture.h" #include "Texture.h"
#include "Constants.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_Make(DrawTextArgs* args, STRING_REF String* text, FontDesc* font, bool useShadow);
void DrawTextArgs_MakeEmpty(DrawTextArgs* args, 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_Init(void);
void Drawer2D_Free(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); void Drawer2D_Begin(Bitmap* bmp);
/* Frees any resources associated with the underlying bitmap. */ /* Frees any resources associated with the underlying bitmap. */
void Drawer2D_End(void); void Drawer2D_End(void);
/* Draws a 2D flat rectangle. */ /* 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. */ /* 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); void Drawer2D_DrawText(DrawTextArgs* args, Int32 x, Int32 y);
Size2D Drawer2D_MeasureText(DrawTextArgs* args); Size2D Drawer2D_MeasureText(DrawTextArgs* args);

View File

@ -54,21 +54,24 @@ void GameMode_PickMiddle(BlockID old) {
if (Block_Draw[old] == DrawType_Gas) return; if (Block_Draw[old] == DrawType_Gas) return;
if (!(Block_CanPlace[old] || Block_CanDelete[old])) return; if (!(Block_CanPlace[old] || Block_CanDelete[old])) return;
if (!Inventory_CanChangeSelected() || Inventory_SelectedBlock == 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) { if (Inventory_Get(Inventory_SelectedIndex) == BlockID_Air) {
Inventory_SetSelectedBlock(old); Inventory_SetSelectedBlock(old); return;
return;
} }
/* Try to replace same block or empty slots first */ /* Try to replace same block */
UInt32 i;
for (i = 0; i < INVENTORY_BLOCKS_PER_HOTBAR; i++) { 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_Set(i, old);
Inventory_SetSelectedIndex(i); Inventory_SetSelectedIndex(i); return;
return;
} }
/* Finally, replace the currently selected block */ /* Finally, replace the currently selected block */

View File

@ -3,10 +3,10 @@
#include "Typedefs.h" #include "Typedefs.h"
#include "DateTime.h" #include "DateTime.h"
#include "String.h" #include "String.h"
#include "2DStructs.h"
/* Abstracts platform specific memory management, I/O, etc. /* Abstracts platform specific memory management, I/O, etc.
Copyright 2017 ClassicalSharp | Licensed under BSD-3 Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/ */
typedef struct DrawTextArgs_ { String Text; FontDesc Font; bool UseShadow; } DrawTextArgs;
/* Initalises required state for this platform. */ /* Initalises required state for this platform. */
void Platform_Init(void); void Platform_Init(void);
@ -56,12 +56,18 @@ UInt32 Platform_FileLength(void* file);
/* Blocks the calling thread for given number of milliseconds. */ /* Blocks the calling thread for given number of milliseconds. */
void Platform_ThreadSleep(UInt32 milliseconds); void Platform_ThreadSleep(UInt32 milliseconds);
struct DrawTextArgs_;
struct Bitmap_;
/* Allocates handle for the given font. */ /* Allocates handle for the given font. */
void Platform_MakeFont(FontDesc* desc); void Platform_MakeFont(FontDesc* desc);
/* Frees handle for the given font. */ /* Frees handle for the given font. */
void Platform_FreeFont(FontDesc* desc); void Platform_FreeFont(FontDesc* desc);
/* Measures size of given text.*/ /* Measures size of given text.*/
Size2D Platform_MeasureText(DrawTextArgs* args); Size2D Platform_MeasureText(struct DrawTextArgs_* args);
/* Draws text onto the actively selected bitmap. */ /* 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 #endif

View File

@ -935,7 +935,7 @@ Int32 SpecialInputWidget_MeasureTitles(SpecialInputWidget* widget) {
return totalWidth; return totalWidth;
} }
void SpecialInputWidget_DrawTitles(SpecialInputWidget* widget) { void SpecialInputWidget_DrawTitles(SpecialInputWidget* widget, Bitmap* bmp) {
Int32 x = 0; Int32 x = 0;
DrawTextArgs args; DrawTextArgs_MakeEmpty(&args, &widget->Font, false); 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; PackedCol col = i == widget->SelectedIndex ? col_selected : col_inactive;
Size2D size = widget->Tabs[i].TitleSize; 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); Drawer2D_DrawText(&args, x + SPECIAL_TITLE_SPACING / 2, 0);
x += size.Width; x += size.Width;
} }
@ -1011,9 +1011,9 @@ void SpecialInputWidget_Make(SpecialInputWidget* widget, SpecialInputTab* e) {
Bitmap_AllocatePow2(&bmp, size.Width, size.Height); Bitmap_AllocatePow2(&bmp, size.Width, size.Height);
Drawer2D_Begin(&bmp); Drawer2D_Begin(&bmp);
SpecialInputWidget_DrawTitles(widget); SpecialInputWidget_DrawTitles(widget, &bmp);
PackedCol col = PACKEDCOL_CONST(30, 30, 30, 200); 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); SpecialInputWidget_DrawContent(widget, e, titleHeight);
widget->Tex = Drawer2D_Make2DTexture(&bmp, size, widget->Base.X, widget->Base.Y); widget->Tex = Drawer2D_Make2DTexture(&bmp, size, widget->Base.X, widget->Base.Y);

View File

@ -3,11 +3,14 @@
#include "DisplayDevice.h" #include "DisplayDevice.h"
#include "ExtMath.h" #include "ExtMath.h"
#include "ErrorHandler.h" #include "ErrorHandler.h"
#include "Drawer2D.h"
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
HDC hdc; HDC hdc;
HBITMAP hbmp;
HANDLE heap; HANDLE heap;
void Platform_Init(void) { void Platform_Init(void) {
heap = GetProcessHeap(); /* TODO: HeapCreate instead? probably not */ heap = GetProcessHeap(); /* TODO: HeapCreate instead? probably not */
hdc = CreateCompatibleDC(NULL); hdc = CreateCompatibleDC(NULL);
@ -189,19 +192,15 @@ void Platform_MakeFont(FontDesc* desc) {
font.lfQuality = ANTIALIASED_QUALITY; font.lfQuality = ANTIALIASED_QUALITY;
desc->Handle = CreateFontIndirectA(&font); desc->Handle = CreateFontIndirectA(&font);
if (desc->Handle == NULL) { if (desc->Handle == NULL) ErrorHandler_Fail("Creating font handle failed");
ErrorHandler_Fail("Creating font handle failed");
}
} }
void Platform_FreeFont(FontDesc* desc) { void Platform_FreeFont(FontDesc* desc) {
if (!DeleteObject(desc->Handle)) { if (!DeleteObject(desc->Handle)) ErrorHandler_Fail("Deleting font handle failed");
ErrorHandler_Fail("Deleting font handle failed");
}
desc->Handle = NULL; desc->Handle = NULL;
} }
Size2D Platform_MeasureText(DrawTextArgs* args) { Size2D Platform_MeasureText(struct DrawTextArgs_* args) {
HDC hDC = GetDC(NULL); HDC hDC = GetDC(NULL);
RECT r = { 0 }; RECT r = { 0 };
DrawTextA(hDC, args->Text.buffer, args->Text.length, DrawTextA(hDC, args->Text.buffer, args->Text.length,
@ -209,9 +208,22 @@ Size2D Platform_MeasureText(DrawTextArgs* args) {
return Size2D_Make(r.right, r.bottom); 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); HDC hDC = GetDC(NULL);
RECT r = { 0 }; RECT r = { 0 };
DrawTextA(hDC, args->Text.buffer, args->Text.length, DrawTextA(hDC, args->Text.buffer, args->Text.length,
&r, DT_NOPREFIX | DT_SINGLELINE | DT_NOCLIP); &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;
} }