From 8922b377d2c01d1d451e91e7401efd0a4e178155 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 21 Dec 2018 14:51:42 +1100 Subject: [PATCH] Fix sounds not working --- src/Audio.c | 4 ++-- src/Bitmap.c | 28 ++++++++++++++++-------- src/Model.c | 7 ------ src/Model.h | 8 +++++++ src/Program.c | 1 + src/Stream.c | 50 ++++++++++++++++++++++++++++++++++++++++-- src/Stream.h | 60 ++++++++++++++++----------------------------------- src/Vectors.c | 4 ++-- src/Vectors.h | 21 ++++++++++++------ 9 files changed, 113 insertions(+), 70 deletions(-) diff --git a/src/Audio.c b/src/Audio.c index 2a61726be..584f51aef 100644 --- a/src/Audio.c +++ b/src/Audio.c @@ -250,7 +250,7 @@ static void Sounds_Play(uint8_t type, struct Soundboard* board) { int i, volume; ReturnCode res; - if (type == SOUND_NONE || !Audio_SoundsVolume == 0) return; + if (type == SOUND_NONE || !Audio_SoundsVolume) return; snd = Soundboard_PickRandom(board, type); if (!snd) return; @@ -279,7 +279,7 @@ static void Sounds_Play(uint8_t type, struct Soundboard* board) { } l = Audio_GetFormat(output->Handle); - if (l->Channels == 0 || AudioFormat_Eq(l, &fmt)) { + if (!l->Channels || AudioFormat_Eq(l, &fmt)) { Sounds_PlayRaw(output, snd, &fmt, volume); return; } } diff --git a/src/Bitmap.c b/src/Bitmap.c index b9ca41ae3..de1f4b209 100644 --- a/src/Bitmap.c +++ b/src/Bitmap.c @@ -507,7 +507,7 @@ ReturnCode Png_Decode(Bitmap* bmp, struct Stream* stream) { /*########################################################################################################################* *------------------------------------------------------PNG encoder--------------------------------------------------------* *#########################################################################################################################*/ -static void Png_Filter(uint8_t filter, uint8_t* cur, uint8_t* prior, uint8_t* best, int lineLen) { +static void Png_Filter(uint8_t filter, const uint8_t* cur, const uint8_t* prior, uint8_t* best, int lineLen) { /* 3 bytes per pixel constant */ uint8_t a, b, c; int i, p, pa, pb, pc; @@ -562,16 +562,25 @@ static void Png_Filter(uint8_t filter, uint8_t* cur, uint8_t* prior, uint8_t* be } } -static void Png_EncodeRow(const BitmapCol* src, uint8_t* cur, uint8_t* prior, uint8_t* best, int lineLen) { - uint8_t* dst = cur; +static void Png_MakeRow(const BitmapCol* src, uint8_t* dst, int lineLen, bool alpha) { + uint8_t* end = dst + lineLen; + + if (alpha) { + for (; dst < end; src++, dst += 4) { + dst[0] = src->R; dst[1] = src->G; dst[2] = src->B; dst[3] = src->A; + } + } else { + for (; dst < end; src++, dst += 3) { + dst[0] = src->R; dst[1] = src->G; dst[2] = src->B; + } + } +} + +static void Png_EncodeRow(const uint8_t* cur, const uint8_t* prior, uint8_t* best, int lineLen) { + uint8_t* dst; int bestFilter, bestEstimate = Int32_MaxValue; int x, filter, estimate; - for (x = 0; x < lineLen; x += 3) { - dst[0] = src->R; dst[1] = src->G; dst[2] = src->B; - src++; dst += 3; - } - dst = best + 1; /* NOTE: Waste of time trying the PNG_NONE filter */ for (filter = PNG_FILTER_SUB; filter <= PNG_FILTER_PAETH; filter++) { @@ -645,7 +654,8 @@ ReturnCode Png_Encode(Bitmap* bmp, struct Stream* stream, Png_RowSelector select uint8_t* prev = (y & 1) == 0 ? prevLine : curLine; uint8_t* cur = (y & 1) == 0 ? curLine : prevLine; - Png_EncodeRow(src, cur, prev, bestLine, lineSize); + Png_MakeRow(src, cur, lineSize, false); /* TODO: alpha */ + Png_EncodeRow(cur, prev, bestLine, lineSize); /* +1 for filter byte */ if ((res = Stream_Write(&zlStream, bestLine, lineSize + 1))) return res; } diff --git a/src/Model.c b/src/Model.c index 804956d28..d8826fe9f 100644 --- a/src/Model.c +++ b/src/Model.c @@ -386,13 +386,6 @@ static struct ModelTex* textures_head; #define Model_RetSize(x,y,z) static Vector3 P = { (x)/16.0f,(y)/16.0f,(z)/16.0f }; *size = P; #define Model_RetAABB(x1,y1,z1, x2,y2,z2) static struct AABB BB = { (x1)/16.0f,(y1)/16.0f,(z1)/16.0f, (x2)/16.0f,(y2)/16.0f,(z2)/16.0f }; *bb = BB; -#define BoxDesc_Dim(p1, p2) p1 < p2 ? p2 - p1 : p1 - p2 - -#define BoxDesc_Tex(x, y) x,y -#define BoxDesc_Dims(x1,y1,z1,x2,y2,z2) BoxDesc_Dim(x1,x2), BoxDesc_Dim(y1,y2), BoxDesc_Dim(z1,z2) -#define BoxDesc_Bounds(x1,y1,z1,x2,y2,z2) x1/16.0f,y1/16.0f,z1/16.0f, x2/16.0f,y2/16.0f,z2/16.0f -#define BoxDesc_Rot(x, y, z) x/16.0f,y/16.0f,z/16.0f -#define BoxDesc_Box(x1,y1,z1,x2,y2,z2) BoxDesc_Dims(x1,y1,z1,x2,y2,z2), BoxDesc_Bounds(x1,y1,z1,x2,y2,z2) static void Models_ContextLost(void* obj) { Gfx_DeleteVb(&Model_Vb); diff --git a/src/Model.h b/src/Model.h index 832bbe3b2..48e217e90 100644 --- a/src/Model.h +++ b/src/Model.h @@ -125,6 +125,14 @@ struct BoxDesc { float RotX,RotY,RotZ; /* Rotation origin point */ }; +#define BoxDesc_Dim(p1, p2) p1 < p2 ? p2 - p1 : p1 - p2 +/* Macros for making initialising a BoxDesc easier to understand. See Model.c for how these get used. */ +#define BoxDesc_Tex(x, y) x,y +#define BoxDesc_Dims(x1,y1,z1,x2,y2,z2) BoxDesc_Dim(x1,x2), BoxDesc_Dim(y1,y2), BoxDesc_Dim(z1,z2) +#define BoxDesc_Bounds(x1,y1,z1,x2,y2,z2) x1/16.0f,y1/16.0f,z1/16.0f, x2/16.0f,y2/16.0f,z2/16.0f +#define BoxDesc_Rot(x, y, z) x/16.0f,y/16.0f,z/16.0f +#define BoxDesc_Box(x1,y1,z1,x2,y2,z2) BoxDesc_Dims(x1,y1,z1,x2,y2,z2), BoxDesc_Bounds(x1,y1,z1,x2,y2,z2) + /* Builds a box model assuming the follow texture layout: let SW = sides width, BW = body width, BH = body height ********************************************************************************************* diff --git a/src/Program.c b/src/Program.c index 6c1f459b7..84e25769b 100644 --- a/src/Program.c +++ b/src/Program.c @@ -74,6 +74,7 @@ int main(int argc, char** argv) { argsCount = Platform_GetCommandLineArgs(argc, argv, args); /* NOTE: Make sure to comment this out before pushing a commit */ /* String rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565"); */ + /* String rawArgs = String_FromConst("UnknownShadow200"); */ /* argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4); */ Platform_SetWorkingDir(); diff --git a/src/Stream.c b/src/Stream.c index 080f1cd77..5c63245c7 100644 --- a/src/Stream.c +++ b/src/Stream.c @@ -364,7 +364,7 @@ void Stream_ReadonlyBuffered(struct Stream* s, struct Stream* source, void* data /*########################################################################################################################* -*-------------------------------------------------CRC32Stream/ReadU32-----------------------------------------------------* +*-----------------------------------------------------CRC32Stream---------------------------------------------------------* *#########################################################################################################################*/ static ReturnCode Stream_Crc32Write(struct Stream* stream, const uint8_t* data, uint32_t count, uint32_t* modified) { struct Stream* source; @@ -388,6 +388,48 @@ void Stream_WriteonlyCrc32(struct Stream* s, struct Stream* source) { s->Meta.CRC32.CRC32 = 0xFFFFFFFFUL; } + +/*########################################################################################################################* +*-------------------------------------------------Read/Write primitives---------------------------------------------------* +*#########################################################################################################################*/ +uint16_t Stream_GetU16_LE(const uint8_t* data) { + return (uint16_t)(data[0] | (data[1] << 8)); +} + +uint16_t Stream_GetU16_BE(const uint8_t* data) { + return (uint16_t)((data[0] << 8) | data[1]); +} + +uint32_t Stream_GetU32_LE(const uint8_t* data) { + return (uint32_t)( + (uint32_t)data[0] | ((uint32_t)data[1] << 8) | + ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24)); +} + +uint32_t Stream_GetU32_BE(const uint8_t* data) { + return (uint32_t)( + ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) | + ((uint32_t)data[2] << 8) | (uint32_t)data[3]); +} + +void Stream_SetU16_LE(uint8_t* data, uint16_t value) { + data[0] = (uint8_t)(value ); data[1] = (uint8_t)(value >> 8 ); +} + +void Stream_SetU16_BE(uint8_t* data, uint16_t value) { + data[0] = (uint8_t)(value >> 8 ); data[1] = (uint8_t)(value ); +} + +void Stream_SetU32_LE(uint8_t* data, uint32_t value) { + data[0] = (uint8_t)(value ); data[1] = (uint8_t)(value >> 8 ); + data[2] = (uint8_t)(value >> 16); data[3] = (uint8_t)(value >> 24); +} + +void Stream_SetU32_BE(uint8_t* data, uint32_t value) { + data[0] = (uint8_t)(value >> 24); data[1] = (uint8_t)(value >> 16); + data[2] = (uint8_t)(value >> 8 ); data[3] = (uint8_t)(value); +} + ReturnCode Stream_ReadU32_LE(struct Stream* s, uint32_t* value) { uint8_t data[4]; ReturnCode res; if ((res = Stream_Read(s, data, 4))) return res; @@ -400,6 +442,7 @@ ReturnCode Stream_ReadU32_BE(struct Stream* s, uint32_t* value) { *value = Stream_GetU32_BE(data); return 0; } + /*########################################################################################################################* *--------------------------------------------------Read/Write strings-----------------------------------------------------* *#########################################################################################################################*/ @@ -429,6 +472,9 @@ ReturnCode Stream_ReadLine(struct Stream* s, String* text) { /* Handle \r\n or \n line endings */ if (cp == '\r') continue; if (cp == '\n') return 0; + + /* ignore byte order mark */ + if (cp == 0xFEFF) continue; String_Append(text, Convert_UnicodeToCP437(cp)); } return readAny ? 0 : ERR_END_OF_STREAM; @@ -456,4 +502,4 @@ ReturnCode Stream_WriteLine(struct Stream* s, String* text) { cur = Platform_NewLine; while (*cur) { buffer[len++] = *cur++; } return Stream_Write(s, buffer, len); -} +} \ No newline at end of file diff --git a/src/Stream.h b/src/Stream.h index 119eeefd9..2033bb203 100644 --- a/src/Stream.h +++ b/src/Stream.h @@ -70,54 +70,32 @@ CC_API void Stream_ReadonlyBuffered(struct Stream* s, struct Stream* source, voi /* Wraps another Stream, calculating a running CRC32 as data is written. */ /* To get the final CRC32, xor it with 0xFFFFFFFFUL */ void Stream_WriteonlyCrc32(struct Stream* s, struct Stream* source); + +/* Reads a little-endian 16 bit unsigned integer from memory. */ +uint16_t Stream_GetU16_LE(const uint8_t* data); +/* Reads a big-endian 16 bit unsigned integer from memory. */ +uint16_t Stream_GetU16_BE(const uint8_t* data); +/* Reads a little-endian 32 bit unsigned integer from memory. */ +uint32_t Stream_GetU32_LE(const uint8_t* data); +/* Reads a big-endian 32 bit unsigned integer from memory. */ +uint32_t Stream_GetU32_BE(const uint8_t* data); + +/* Writes a little-endian 16 bit unsigned integer to memory. */ +void Stream_SetU16_LE(uint8_t* data, uint16_t value); +/* Writes a big-endian 16 bit unsigned integer to memory. */ +void Stream_SetU16_BE(uint8_t* data, uint16_t value); +/* Writes a little-endian 32 bit unsigned integer to memory. */ +void Stream_SetU32_LE(uint8_t* data, uint32_t value); +/* Writes a big-endian 32 bit unsigned integer to memory. */ +void Stream_SetU32_BE(uint8_t* data, uint32_t value); /* Reads a little-endian 32 bit unsigned integer from the stream. */ ReturnCode Stream_ReadU32_LE(struct Stream* s, uint32_t* value); /* Reads a big-endian 32 bit unsigned integer from the stream. */ ReturnCode Stream_ReadU32_BE(struct Stream* s, uint32_t* value); -/* Reads a little-endian 16 bit unsigned integer from memory. */ -static CC_INLINE uint16_t Stream_GetU16_LE(const uint8_t* data) { - return (uint16_t)(data[0] | (data[1] << 8)); -} -/* Reads a big-endian 16 bit unsigned integer from memory. */ -static CC_INLINE uint16_t Stream_GetU16_BE(const uint8_t* data) { - return (uint16_t)((data[0] << 8) | data[1]); -} -/* Reads a little-endian 32 bit unsigned integer from memory. */ -static CC_INLINE uint32_t Stream_GetU32_LE(const uint8_t* data) { - return (uint32_t)( - (uint32_t)data[0] | ((uint32_t)data[1] << 8) | - ((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24)); -} -/* Reads a big-endian 32 bit unsigned integer from memory. */ -static CC_INLINE uint32_t Stream_GetU32_BE(const uint8_t* data) { - return (uint32_t)( - ((uint32_t)data[0] << 24) | ((uint32_t)data[1] << 16) | - ((uint32_t)data[2] << 8) | (uint32_t)data[3]); -} - -/* Writes a little-endian 16 bit unsigned integer to memory. */ -static CC_INLINE void Stream_SetU16_LE(uint8_t* data, uint16_t value) { - data[0] = (uint8_t)(value ); data[1] = (uint8_t)(value >> 8 ); -} -/* Writes a big-endian 16 bit unsigned integer to memory. */ -static CC_INLINE void Stream_SetU16_BE(uint8_t* data, uint16_t value) { - data[0] = (uint8_t)(value >> 8 ); data[1] = (uint8_t)(value ); -} -/* Writes a little-endian 32 bit unsigned integer to memory. */ -static CC_INLINE void Stream_SetU32_LE(uint8_t* data, uint32_t value) { - data[0] = (uint8_t)(value ); data[1] = (uint8_t)(value >> 8 ); - data[2] = (uint8_t)(value >> 16); data[3] = (uint8_t)(value >> 24); -} -/* Writes a big-endian 32 bit unsigned integer to memory. */ -static CC_INLINE void Stream_SetU32_BE(uint8_t* data, uint32_t value) { - data[0] = (uint8_t)(value >> 24); data[1] = (uint8_t)(value >> 16); - data[2] = (uint8_t)(value >> 8 ); data[3] = (uint8_t)(value); -} - /* Reads a line of UTF8 encoded character from the stream. */ /* NOTE: Reads one byte at a time. May want to use Stream_ReadonlyBuffered. */ CC_API ReturnCode Stream_ReadLine(struct Stream* s, String* text); /* Writes a line of UTF8 encoded text to the stream. */ CC_API ReturnCode Stream_WriteLine(struct Stream* s, String* text); -#endif +#endif \ No newline at end of file diff --git a/src/Vectors.c b/src/Vectors.c index ee9520ad1..7a2818dd8 100644 --- a/src/Vectors.c +++ b/src/Vectors.c @@ -142,7 +142,7 @@ Vector3 Vector3_GetDirVector(float yawRad, float pitchRad) { }*/ -struct Matrix Matrix_Identity = { +const struct Matrix Matrix_Identity = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, @@ -188,7 +188,7 @@ void Matrix_Scale(struct Matrix* result, float x, float y, float z) { result->Row0.X = x; result->Row1.Y = y; result->Row2.Z = z; } -void Matrix_Mul(struct Matrix* result, struct Matrix* left, struct Matrix* right) { +void Matrix_Mul(struct Matrix* result, const struct Matrix* left, const struct Matrix* right) { /* Originally from http://www.edais.co.uk/blog/?p=27 */ float lM11 = left->Row0.X, lM12 = left->Row0.Y, lM13 = left->Row0.Z, lM14 = left->Row0.W, diff --git a/src/Vectors.h b/src/Vectors.h index ae29ebc1b..7f580db4a 100644 --- a/src/Vectors.h +++ b/src/Vectors.h @@ -11,7 +11,7 @@ typedef struct Vector3_ { float X, Y, Z; } Vector3; typedef struct Vector3I_ { int X, Y, Z; } Vector3I; struct Vector4 { float X, Y, Z, W; }; struct Matrix { struct Vector4 Row0, Row1, Row2, Row3; }; -extern struct Matrix Matrix_Identity; +extern const struct Matrix Matrix_Identity; Vector3 Vector3_Create1(float value); Vector3 Vector3_Create3(float x, float y, float z); @@ -62,14 +62,21 @@ Vector3 Vector3_GetDirVector(float yawRad, float pitchRad); NOTE: This is not an identity function. Returned pitch is always within [-90, 90] degrees.*/ /*void Vector3_GetHeading(Vector3 dir, float* yawRad, float* pitchRad);*/ -void Matrix_RotateX(struct Matrix* result, float angle); -void Matrix_RotateY(struct Matrix* result, float angle); -void Matrix_RotateZ(struct Matrix* result, float angle); -void Matrix_Translate(struct Matrix* result, float x, float y, float z); -void Matrix_Scale(struct Matrix* result, float x, float y, float z); +/* Returns a matrix representing a counter-clockwise rotation around X axis. */ +CC_API void Matrix_RotateX(struct Matrix* result, float angle); +/* Returns a matrix representing a counter-clockwise rotation around Y axis. */ +CC_API void Matrix_RotateY(struct Matrix* result, float angle); +/* Returns a matrix representing a counter-clockwise rotation around Z axis. */ +CC_API void Matrix_RotateZ(struct Matrix* result, float angle); +/* Returns a matrix representing a translation to the given coordinates. */ +CC_API void Matrix_Translate(struct Matrix* result, float x, float y, float z); +/* Returns a matrix representing a scaling by the given factors. */ +CC_API void Matrix_Scale(struct Matrix* result, float x, float y, float z); #define Matrix_MulBy(dst, right) Matrix_Mul(dst, dst, right) -void Matrix_Mul(struct Matrix* result, struct Matrix* left, struct Matrix* right); +/* Multiplies two matrices together. */ +/* NOTE: result can be the same pointer as left or right. */ +CC_API void Matrix_Mul(struct Matrix* result, const struct Matrix* left, const struct Matrix* right); void Matrix_Orthographic(struct Matrix* result, float width, float height, float zNear, float zFar); void Matrix_OrthographicOffCenter(struct Matrix* result, float left, float right, float bottom, float top, float zNear, float zFar);