Permalink
Browse files

yet more c90 rewrite

  • Loading branch information...
UnknownShadow200 committed Nov 4, 2018
1 parent 514174f commit feaf213fa216a1c3fe6e91d5f18772f22930596d
Showing with 271 additions and 213 deletions.
  1. +8 −5 src/Deflate.c
  2. +2 −2 src/Deflate.h
  3. +2 −1 src/Drawer2D.h
  4. +43 −36 src/Entity.c
  5. +1 −2 src/EntityComponents.c
  6. +14 −11 src/ErrorHandler.c
  7. +4 −3 src/Formats.c
  8. +8 −13 src/Menus.c
  9. +13 −5 src/Model.c
  10. +8 −14 src/Screens.c
  11. +2 −0 src/String.h
  12. +2 −2 src/TexturePack.c
  13. +1 −1 src/TexturePack.h
  14. +162 −117 src/Widgets.c
  15. +1 −1 src/Widgets.h
@@ -23,7 +23,8 @@ void GZipHeader_Init(struct GZipHeader* header) {
}
ReturnCode GZipHeader_Read(struct Stream* s, struct GZipHeader* header) {
ReturnCode res; uint8_t tmp;
uint8_t tmp;
ReturnCode res;
switch (header->State) {
case GZIP_STATE_HEADER1:
@@ -104,7 +105,8 @@ void ZLibHeader_Init(struct ZLibHeader* header) {
}
ReturnCode ZLibHeader_Read(struct Stream* s, struct ZLibHeader* header) {
ReturnCode res; uint8_t tmp;
uint8_t tmp;
ReturnCode res;
switch (header->State) {
case ZLIB_STATE_COMPRESSIONMETHOD:
@@ -196,7 +198,8 @@ static void Huffman_Build(struct HuffmanTable* table, uint8_t* bitLens, int coun
offset += bl_count[i];
/* Last codeword is actually: code + (bl_count[i] - 1) */
/* When decoding we peform < against this value though, so we need to add 1 here */
/* When decoding we peform < against this value though, so need to add 1 here */
/* This way, don't need to special case bit lengths with 0 codewords when decoding */
if (bl_count[i]) {
table->EndCodewords[i] = code + bl_count[i];
} else {
@@ -306,7 +309,7 @@ void Inflate_Init(struct InflateState* state, struct Stream* source) {
state->LastBlock = false;
state->Bits = 0;
state->NumBits = 0;
state->NextIn = state->Input;
state->NextIn = state->Input;
state->AvailIn = 0;
state->Output = NULL;
state->AvailOut = 0;
@@ -420,7 +423,7 @@ void Inflate_Process(struct InflateState* state) {
} break;
case 1: { /* Fixed/static huffman compressed */
Huffman_Build(&state->Table.Lits, fixed_lits, INFLATE_MAX_LITS);
Huffman_Build(&state->Table.Lits, fixed_lits, INFLATE_MAX_LITS);
Huffman_Build(&state->TableDists, fixed_dists, INFLATE_MAX_DISTS);
state->State = Inflate_NextCompressState(state);
} break;
@@ -75,8 +75,8 @@ NOINLINE_ void Inflate_MakeStream(struct Stream* stream, struct InflateState* st
#define DEFLATE_HASH_SIZE 0x1000UL
#define DEFLATE_HASH_MASK 0x0FFFUL
struct DeflateState {
uint32_t Bits; /* Holds bits across byte boundaries*/
uint32_t NumBits; /* Number of bits in Bits buffer*/
uint32_t Bits; /* Holds bits across byte boundaries */
uint32_t NumBits; /* Number of bits in Bits buffer */
uint32_t InputPosition;
uint8_t* NextOut; /* Pointer within Output buffer to next byte that can be written */
@@ -40,8 +40,9 @@ void Drawer2D_Make2DTexture(struct Texture* tex, Bitmap* bmp, Size2D used, int X
bool Drawer2D_ValidColCodeAt(const String* text, int i);
bool Drawer2D_ValidColCode(char c);
/* Whether text is empty or consists purely of valid colour codes. */
bool Drawer2D_IsEmptyText(const String* text);
/* Returns the last valid colour code in the given input, or \0 if no valid colour code was found. */
/* Returns the last valid colour code in the given input, or \0 if not found. */
char Drawer2D_LastCol(const String* text, int start);
bool Drawer2D_IsWhiteCol(char c);
@@ -528,6 +528,7 @@ static void Player_DrawName(struct Player* player) {
struct Model* model = e->Model;
Vector3 pos;
float scale;
VertexP3fT2fC4b vertices[4];
if (player->NameTex.X == PLAYER_NAME_EMPTY_TEX) return;
if (!player->NameTex.ID) Player_MakeNameTexture(player);
@@ -548,7 +549,6 @@ static void Player_DrawName(struct Player* player) {
size.X *= tempW * 0.2f; size.Y *= tempW * 0.2f;
}
VertexP3fT2fC4b vertices[4];
TextureRec rec = { 0.0f, 0.0f, player->NameTex.U2, player->NameTex.V2 };
PackedCol col = PACKEDCOL_WHITE;
Particle_DoRender(&size, &pos, &rec, col, vertices);
@@ -664,26 +664,27 @@ static void Player_ClearHat(Bitmap* bmp, uint8_t skinType) {
}
}
static void Player_EnsurePow2(struct Player* player, Bitmap* bmp) {
int width = Math_NextPowOf2(bmp->Width);
int height = Math_NextPowOf2(bmp->Height);
static void Player_EnsurePow2(struct Player* p, Bitmap* bmp) {
uint32_t stride;
int width, height;
Bitmap scaled;
int y;
width = Math_NextPowOf2(bmp->Width);
height = Math_NextPowOf2(bmp->Height);
if (width == bmp->Width && height == bmp->Height) return;
Bitmap_Allocate(&scaled, width, height);
int y;
uint32_t stride = (uint32_t)(bmp->Width) * BITMAP_SIZEOF_PIXEL;
p->Base.uScale = (float)bmp->Width / width;
p->Base.vScale = (float)bmp->Height / height;
stride = bmp->Width * BITMAP_SIZEOF_PIXEL;
for (y = 0; y < bmp->Height; y++) {
uint32_t* src = Bitmap_GetRow(bmp, y);
uint32_t* dst = Bitmap_GetRow(&scaled, y);
Mem_Copy(dst, src, stride);
}
struct Entity* entity = &player->Base;
entity->uScale = (float)bmp->Width / width;
entity->vScale = (float)bmp->Height / height;
Mem_Free(bmp->Scan0);
*bmp = scaled;
}
@@ -831,16 +832,18 @@ static void LocalPlayer_SetLocation(struct Entity* e, struct LocationUpdate* upd
}
void LocalPlayer_Tick(struct Entity* e, double delta) {
if (!World_Blocks) return;
struct LocalPlayer* p = (struct LocalPlayer*)e;
struct HacksComp* hacks = &p->Hacks;
float xMoving = 0, zMoving = 0;
bool wasOnGround;
Vector3 headingVelocity;
if (!World_Blocks) return;
e->StepSize = hacks->FullBlockStep && hacks->Enabled && hacks->CanAnyHacks && hacks->CanSpeed ? 1.0f : 0.5f;
p->OldVelocity = e->Velocity;
float xMoving = 0, zMoving = 0;
LocalInterpComp_AdvanceState(&p->Interp);
bool wasOnGround = e->OnGround;
p->OldVelocity = e->Velocity;
wasOnGround = e->OnGround;
LocalInterpComp_AdvanceState(&p->Interp);
LocalPlayer_HandleInput(&xMoving, &zMoving);
hacks->Floating = hacks->Noclip || hacks->Flying;
if (!hacks->Floating && hacks->CanBePushed) PhysicsComp_DoEntityPush(e);
@@ -851,7 +854,7 @@ void LocalPlayer_Tick(struct Entity* e, double delta) {
}
PhysicsComp_UpdateVelocityState(&p->Physics);
Vector3 headingVelocity = Vector3_RotateY3(xMoving, 0, zMoving, e->HeadY * MATH_DEG2RAD);
headingVelocity = Vector3_RotateY3(xMoving, 0, zMoving, e->HeadY * MATH_DEG2RAD);
PhysicsComp_PhysicsTick(&p->Physics, headingVelocity);
/* Fixes high jump, when holding down a movement key, jump, fly, then let go of fly key */
@@ -861,7 +864,7 @@ void LocalPlayer_Tick(struct Entity* e, double delta) {
AnimatedComp_Update(e, p->Interp.Prev.Pos, p->Interp.Next.Pos, delta);
TiltComp_Update(&p->Tilt, delta);
Player_CheckSkin((struct Player*)p);
Player_CheckSkin(&p->Base);
SoundComp_Tick(wasOnGround);
}
@@ -928,7 +931,7 @@ struct EntityVTABLE localPlayer_VTABLE = {
void LocalPlayer_Init(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
Player_Init(&p->Base);
Player_SetName((struct Player*)p, &Game_Username, &Game_Username);
Player_SetName(&p->Base, &Game_Username, &Game_Username);
p->Collisions.Entity = &p->Base;
HacksComp_Init(&p->Hacks);
@@ -944,31 +947,35 @@ void LocalPlayer_Init(void) {
static bool LocalPlayer_IsSolidCollide(BlockID b) { return Block_Collide[b] == COLLIDE_SOLID; }
static void LocalPlayer_DoRespawn(void) {
struct LocalPlayer* p = &LocalPlayer_Instance;
Vector3 spawn = p->Spawn;
Vector3I P;
struct LocationUpdate update;
struct AABB bb;
Vector3 spawn = p->Spawn;
Vector3I pos;
BlockID block;
float height, spawnY;
int y;
if (!World_Blocks) return;
Vector3I_Floor(&P, &spawn);
Vector3I_Floor(&pos, &spawn);
/* Spawn player at highest valid position */
if (World_IsValidPos_3I(P)) {
if (World_IsValidPos_3I(pos)) {
AABB_Make(&bb, &spawn, &p->Base.Size);
int y;
for (y = P.Y; y <= World_Height; y++) {
float spawnY = Respawn_HighestFreeY(&bb);
for (y = pos.Y; y <= World_Height; y++) {
spawnY = Respawn_HighestFreeY(&bb);
if (spawnY == RESPAWN_NOT_FOUND) {
BlockID block = World_GetPhysicsBlock(P.X, y, P.Z);
float height = Block_Collide[block] == COLLIDE_SOLID ? Block_MaxBB[block].Y : 0.0f;
block = World_GetPhysicsBlock(pos.X, y, pos.Z);
height = Block_Collide[block] == COLLIDE_SOLID ? Block_MaxBB[block].Y : 0.0f;
spawn.Y = y + height + ENTITY_ADJUSTMENT;
break;
}
bb.Min.Y += 1.0f; bb.Max.Y += 1.0f;
}
}
spawn.Y += 2.0f / 16.0f;
struct LocationUpdate update; LocationUpdate_MakePosAndOri(&update, spawn, p->SpawnRotY, p->SpawnHeadX, false);
spawn.Y += 2.0f/16.0f;
LocationUpdate_MakePosAndOri(&update, spawn, p->SpawnRotY, p->SpawnHeadX, false);
p->Base.VTABLE->SetLocation(&p->Base, &update, false);
p->Base.Velocity = Vector3_Zero;
@@ -1063,7 +1070,7 @@ static void NetPlayer_SetLocation(struct Entity* e, struct LocationUpdate* updat
static void NetPlayer_Tick(struct Entity* e, double delta) {
struct NetPlayer* p = (struct NetPlayer*)e;
Player_CheckSkin((struct Player*)p);
Player_CheckSkin(&p->Base);
NetInterpComp_AdvanceState(&p->Interp);
AnimatedComp_Update(e, p->Interp.Prev.Pos, p->Interp.Next.Pos, delta);
}
@@ -1086,16 +1093,16 @@ static void NetPlayer_RenderName(struct Entity* e) {
distance = Model_RenderDistance(e);
threshold = Entities_NameMode == NAME_MODE_ALL_UNSCALED ? 8192 * 8192 : 32 * 32;
if (distance <= (float)threshold) Player_DrawName((struct Player*)p);
if (distance <= (float)threshold) Player_DrawName(&p->Base);
}
struct EntityVTABLE netPlayer_VTABLE = {
NetPlayer_Tick, Player_Despawn, NetPlayer_SetLocation, Entity_GetCol,
NetPlayer_RenderModel, NetPlayer_RenderName, Player_ContextLost, Player_ContextRecreated,
};
void NetPlayer_Init(struct NetPlayer* player, const String* displayName, const String* skinName) {
Mem_Set(player, 0, sizeof(struct NetPlayer));
Player_Init(&player->Base);
Player_SetName((struct Player*)player, displayName, skinName);
player->Base.VTABLE = &netPlayer_VTABLE;
void NetPlayer_Init(struct NetPlayer* p, const String* displayName, const String* skinName) {
Mem_Set(p, 0, sizeof(struct NetPlayer));
Player_Init(&p->Base);
Player_SetName(&p->Base, displayName, skinName);
p->Base.VTABLE = &netPlayer_VTABLE;
}
@@ -166,8 +166,7 @@ void HacksComp_Init(struct HacksComp* hacks) {
hacks->NoclipSlide = true;
hacks->CanBePushed = true;
String str = String_FromArray(hacks->__HacksFlagsBuffer);
hacks->HacksFlags = str;
String_InitArray(hacks->HacksFlags, hacks->__HacksFlagsBuffer);
}
bool HacksComp_CanJumpHigher(struct HacksComp* hacks) {
@@ -485,14 +485,21 @@ static void X11_MessageBox(const char* title, const char* text, X11Window* w) {
X11Button ok = { 0 };
X11Textbox body = { 0 };
XFontStruct* font;
Atom wmDelete;
int x, y, width, height;
XSizeHints hints = { 0 };
int mouseX = -1, mouseY = -1, over;
XEvent e;
X11Window_Init(w);
XMapWindow(dpy, w->win);
XStoreName(dpy, w->win, title);
Atom wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, w->win, &wmDelete, 1);
XFontStruct* font = XQueryFont(dpy, XGContextFromGC(w->gc));
font = XQueryFont(dpy, XGContextFromGC(w->gc));
if (!font) return;
/* Compute size of widgets */
@@ -504,10 +511,10 @@ static void X11_MessageBox(const char* title, const char* text, X11Window* w) {
ok.Height = ok.Text.Height + 10;
/* Compute size and position of window */
int width = body.Width + 20;
int height = body.Height + 20 + ok.Height + 20;
int x = DisplayWidth (dpy, DefaultScreen(dpy))/2 - width/2;
int y = DisplayHeight(dpy, DefaultScreen(dpy))/2 - height/2;
width = body.Width + 20;
height = body.Height + 20 + ok.Height + 20;
x = DisplayWidth (dpy, DefaultScreen(dpy))/2 - width/2;
y = DisplayHeight(dpy, DefaultScreen(dpy))/2 - height/2;
XMoveResizeWindow(dpy, w->win, x, y, width, height);
/* Adjust bounds of widgets */
@@ -518,7 +525,6 @@ static void X11_MessageBox(const char* title, const char* text, X11Window* w) {
XFreeFontInfo(NULL, font, 1);
XUnmapWindow(dpy, w->win); /* Make window non resizeable */
XSizeHints hints = { 0 };
hints.flags = PSize | PMinSize | PMaxSize;
hints.min_width = hints.max_width = hints.base_width = width;
hints.min_height = hints.max_height = hints.base_height = height;
@@ -527,9 +533,6 @@ static void X11_MessageBox(const char* title, const char* text, X11Window* w) {
XMapRaised(dpy, w->win);
XFlush(dpy);
XEvent e;
int mouseX = -1, mouseY = -1;
for (;;) {
XNextEvent(dpy, &e);
@@ -538,7 +541,7 @@ static void X11_MessageBox(const char* title, const char* text, X11Window* w) {
case ButtonPress:
case ButtonRelease:
if (e.xbutton.button != Button1) break;
int over = X11Button_Contains(&ok, mouseX, mouseY);
over = X11Button_Contains(&ok, mouseX, mouseY);
if (ok.Clicked && e.type == ButtonRelease) {
if (over) return;
@@ -305,9 +305,10 @@ static ReturnCode Nbt_ReadTag(uint8_t typeId, bool readTagName, struct Stream* s
uint32_t i, count;
if (typeId == NBT_END) return 0;
tag.TagID = typeId; tag.Parent = parent;
tag.Name = String_Init(tag.NameBuffer, 0, NBT_STRING_SIZE);
tag.TagID = typeId;
tag.Parent = parent;
tag.DataSize = 0;
String_InitArray(tag.Name, tag.NameBuffer);
if (readTagName) {
res = Nbt_ReadString(stream, &tag.Name);
@@ -343,7 +344,7 @@ static ReturnCode Nbt_ReadTag(uint8_t typeId, bool readTagName, struct Stream* s
}
break;
case NBT_STR:
tag.Value.Str.Text = String_Init(tag.Value.Str.Buffer, 0, NBT_STRING_SIZE);
String_InitArray(tag.Value.Str.Text, tag.Value.Str.Buffer);
res = Nbt_ReadString(stream, &tag.Value.Str.Text);
break;
Oops, something went wrong.

0 comments on commit feaf213

Please sign in to comment.