From acdf8aa622c56b4f939b964dda974e725be4db3d Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 25 Jan 2019 11:20:44 +0000 Subject: [PATCH 1/3] Fixed some mistakes Cppcheck pointed out --- src/Draw.cpp | 4 ++-- src/Ending.cpp | 2 +- src/Sound.cpp | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Draw.cpp b/src/Draw.cpp index 08592788..f240ffb9 100644 --- a/src/Draw.cpp +++ b/src/Draw.cpp @@ -316,7 +316,7 @@ void Surface2Surface(int x, int y, RECT *rect, int to, int from) //Target surface if (!surf[to].texture) { - printf("Tried to draw to surface %s, which doesn't exist\n", to); + printf("Tried to draw to surface %d, which doesn't exist\n", to); return; } @@ -350,7 +350,7 @@ void CortBox2(RECT *rect, uint32_t col, int surf_no) //Target surface if (!surf[surf_no].texture) { - printf("Tried to draw a rectangle to surface %s, which doesn't exist\n", surf_no); + printf("Tried to draw a rectangle to surface %d, which doesn't exist\n", surf_no); return; } diff --git a/src/Ending.cpp b/src/Ending.cpp index 05c70be9..2be53f6b 100644 --- a/src/Ending.cpp +++ b/src/Ending.cpp @@ -123,7 +123,7 @@ void PutIllust() void ReloadIllust(int a) { char name[16]; - sprintf(name, "CREDIT%02ld", a); + sprintf(name, "CREDIT%02d", a); ReloadBitmap_Resource(name, SURFACE_ID_CREDITS_IMAGE); } diff --git a/src/Sound.cpp b/src/Sound.cpp index 3b403a10..e5763a9b 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -31,6 +31,7 @@ SOUNDBUFFER::SOUNDBUFFER(size_t bufSize) playing = false; looping = false; + looped = false; frequency = 0.0; volume = 1.0; @@ -274,7 +275,7 @@ bool InitDirectSound() uint8_t *buf = nullptr; size_t len; - for (size_t n = 0; n < SOUND_NO; n++) + for (unsigned int n = 0; n < SOUND_NO; n++) { sprintf(path, "%2.2X.pxt", n); From 414fe76abcd02818dbf7e8540d256bef3f5db575 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 25 Jan 2019 11:50:42 +0000 Subject: [PATCH 2/3] Safer UTF-8 decoder --- src/Font.cpp | 58 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/Font.cpp b/src/Font.cpp index 9449f5a0..3ebb3cfe 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -38,25 +38,47 @@ static unsigned long UTF8ToCode(const unsigned char *string, unsigned int *bytes unsigned int length; unsigned long charcode; - if ((string[0] & 0x80) == 0) + unsigned int zero_bit = 0; + for (unsigned char lead_byte = string[0]; zero_bit < 5 && (lead_byte & 0x80); ++zero_bit, lead_byte <<= 1); + + switch (zero_bit) { - length = 1; - charcode = string[0] & 0x7F; - } - else if ((string[0] & 0xE0) == 0xC0) - { - length = 2; - charcode = ((string[0] & ~0xE0) << 6) | (string[1] & 0x3F); - } - else if ((string[0] & 0xF0) == 0xE0) - { - length = 3; - charcode = ((string[0] & ~0xF0) << (6 * 2)) | ((string[1] & 0x3F) << 6) | (string[2] & 0x3F); - } - else //if (string[0] & 0xF8 == 0xF0) - { - length = 4; - charcode = ((string[0] & ~0xF8) << (6 * 3)) | ((string[1] & 0x3F) << (6 * 2)) | ((string[2] & 0x3F) << 6) | (string[3] & 0x3F); + case 0: + // Single-byte character + length = 1; + charcode = string[0]; + break; + + case 2: + case 3: + case 4: + length = zero_bit; + charcode = string[0] & (1 << (8 - zero_bit)) - 1; + + for (unsigned int i = 1; i < zero_bit; ++i) + { + if ((string[i] & 0xC0) == 0x80) + { + charcode <<= 6; + charcode |= string[i] & ~0xC0; + } + else + { + // Error: Invalid continuation byte + length = 1; + charcode = 0xFFFD; + break; + } + } + + break; + + default: + // Error: Invalid lead byte + length = 1; + charcode = 0xFFFD; + break; + } if (bytes_read) From b325efd981a809493682dceacd519a6be3c4b3bb Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 25 Jan 2019 12:01:18 +0000 Subject: [PATCH 3/3] Improved Japanese build a little --- Makefile | 28 +++++++++++++++------------- src/Font.cpp | 2 ++ src/GenericLoad.cpp | 4 ---- src/Resource.cpp | 15 +++++++++------ 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 94909c7a..130cf8db 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,20 @@ RELEASE = 0 +ifeq ($(RELEASE), 0) +CXXFLAGS := -O0 -g -static -mconsole +FILENAME := debug +else +CXXFLAGS := -O3 -s -static +FILENAME := release +endif + ifeq ($(JAPANESE), 1) +CXXFLAGS += -DJAPANESE ifeq ($(RELEASE), 0) - CXXFLAGS := -DJAPANESE -O0 -g -static -mconsole FILENAME := debugjp else - CXXFLAGS := -DJAPANESE -O3 -s -static FILENAME := releasejp endif - -else - ifeq ($(RELEASE), 0) - CXXFLAGS := -O0 -g -static -mconsole - FILENAME := debug - else - CXXFLAGS := -O3 -s -static - FILENAME := release - endif endif ifeq ($(FIX_BUGS), 1) @@ -76,8 +74,6 @@ RESOURCES = \ BITMAP/CREDIT16.bmp \ BITMAP/CREDIT17.bmp \ BITMAP/CREDIT18.bmp \ - BITMAP/PIXEL.bmp \ - BITMAP/PIXEL_JP.bmp \ ICON/4.bmp \ ORG/ACCESS \ ORG/ANZEN \ @@ -123,6 +119,12 @@ RESOURCES = \ ORG/ZONBIE \ WAVE/WAVE100 +ifeq ($(JAPANESE), 1) + RESOURCES += BITMAP/PIXEL_JP.bmp +else + RESOURCES += BITMAP/PIXEL.bmp +endif + OBJECTS = $(addprefix obj/$(FILENAME)/, $(addsuffix .o, $(SOURCES))) all: build/$(FILENAME).exe diff --git a/src/Font.cpp b/src/Font.cpp index 3ebb3cfe..e0875146 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -4,7 +4,9 @@ #include #include +#ifdef JAPANESE #include +#endif #include #include FT_FREETYPE_H diff --git a/src/GenericLoad.cpp b/src/GenericLoad.cpp index c0034d99..65df95f7 100644 --- a/src/GenericLoad.cpp +++ b/src/GenericLoad.cpp @@ -3,11 +3,7 @@ bool LoadGenericData() { -#ifdef JAPANESE - MakeSurface_Resource("PIXEL_JP", 1); -#else MakeSurface_Resource("PIXEL", 1); -#endif bool bError = false; if (!MakeSurface_File("MyChar", 16)) diff --git a/src/Resource.cpp b/src/Resource.cpp index 44b004ee..3e1fcff7 100644 --- a/src/Resource.cpp +++ b/src/Resource.cpp @@ -64,8 +64,11 @@ #include "Resource/BITMAP/CREDIT16.bmp.h" #include "Resource/BITMAP/CREDIT17.bmp.h" #include "Resource/BITMAP/CREDIT18.bmp.h" -#include "Resource/BITMAP/PIXEL.bmp.h" +#ifdef JAPANESE #include "Resource/BITMAP/PIXEL_JP.bmp.h" +#else +#include "Resource/BITMAP/PIXEL.bmp.h" +#endif #include "Resource/ICON/4.bmp.h" const unsigned char* GetResource(const char *name, size_t *size) @@ -377,13 +380,13 @@ const unsigned char* GetResource(const char *name, size_t *size) } if (!strcmp(name, "PIXEL")) { - *size = sizeof(rPIXEL); - return rPIXEL; - } - if (!strcmp(name, "PIXEL_JP")) - { +#ifdef JAPANESE *size = sizeof(rPIXEL_JP); return rPIXEL_JP; +#else + *size = sizeof(rPIXEL); + return rPIXEL; +#endif } //ICON