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) {
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]);

View File

@ -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);
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);
}
}

View File

@ -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);

View File

@ -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 */

View File

@ -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

View File

@ -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);

View File

@ -3,11 +3,14 @@
#include "DisplayDevice.h"
#include "ExtMath.h"
#include "ErrorHandler.h"
#include "Drawer2D.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
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;
}