From db3e5d10090eae170001bfe37dbcd3b2e722dc99 Mon Sep 17 00:00:00 2001 From: Arthur O'Dwyer Date: Sat, 16 Jun 2018 20:32:09 -0700 Subject: [PATCH] Minor fixes for Mac and MinGW. Eliminate a format-string warning by using less template magic: https://stackoverflow.com/questions/12573968/how-to-use-gccs-printf-format-attribute-with-c11-variadic-templates Fix one `%Ld` in `rogueviz.cpp` that should have been `%lld`. Rename `savepng.c` into `savepng.cpp` so that we don't have to care about the system's C compiler; it builds fine using the same C++ options as HyperRogue itself. Figure out how to get `-DCAP_PNG` and `-DCAP_ROGUEVIZ` working on Mac, and document them. Assume that roughly the same things should also work on MinGW. Consistency on `STDSIZE`, even though it doesn't suffice for C++17 conformance. It *almost* suffices; the one place where Clang still complains even with `-std=c++17 -DSTDSIZE -Wno-sign-compare` is here: ./fieldpattern.cpp:757:51: error: non-constant-expression cannot be narrowed from type 'unsigned long' to 'int' in initializer list [-Wc++11-narrowing] ex.primes.emplace_back(primeinfo{nextprime, size(fp.matrices) / S7, (bool) fp.wsquare}); ^~~~~~~~~~~~~~~~~~~~~~ So, we fix that up too, in this patch. --- Makefile.mac | 34 ++++++++++++++++++++++++++-------- Makefile.mgw | 26 ++++++++++++++++++++++---- fieldpattern.cpp | 3 ++- hyper.h | 16 +++++++++++++--- langen.cpp | 4 +++- rogueviz-video.cpp | 2 +- savepng.c => savepng.cpp | 6 ++++++ sysconfig.h | 2 +- 8 files changed, 74 insertions(+), 19 deletions(-) rename savepng.c => savepng.cpp (98%) diff --git a/Makefile.mac b/Makefile.mac index 46847ecf..59422f8c 100644 --- a/Makefile.mac +++ b/Makefile.mac @@ -1,4 +1,4 @@ -# This Makefile works for Mac OS X (Yosemite). +# This Makefile works for Mac OS X (El Capitan). # # Run "brew install sdl" to install SDL in /usr/local. # Run "brew install sdl_gfx". @@ -7,15 +7,31 @@ # Run "make -f Makefile.mac" to build HyperRogue as ./hyper. CXXFLAGS += -std=c++11 -march=native -DMAC -# CXXFLAGS += -DCAP_ROGUEVIZ CXXFLAGS += -W -Wall -Wextra -pedantic -CXXFLAGS += -Wno-format-pedantic -Wno-unused-parameter -Wno-char-subscripts -Wno-missing-field-initializers -Wno-vla-extension -CXXFLAGS += ${EXTRA_CXXFLAGS} +CXXFLAGS += -Wno-format-pedantic -Wno-unused-parameter -Wno-missing-field-initializers -Wno-vla-extension CXXFLAGS += -I/usr/local/include +CXXFLAGS += ${EXTRA_CXXFLAGS} + LDFLAGS += -L/usr/local/lib -hyper: hyper.o - $(CXX) $(CXXFLAGS) hyper.o $(LDFLAGS) -lSDL -lSDLMain -lSDL_gfx -lSDL_mixer -lSDL_ttf -framework AppKit -framework OpenGL -o hyper +OBJS = hyper.o + +ifeq (a,b) +# Enable PNG screenshots. Requires "brew install libpng". +CXXFLAGS += -DCAP_PNG +LDFLAGS += -lpng +OBJS += savepng.o +else +CXXFLAGS += -DCAP_PNG=0 +endif + +ifeq (a,b) +# Enable RogueViz. +CXXFLAGS += -DCAP_ROGUEVIZ +endif + +hyper: $(OBJS) + $(CXX) $(CXXFLAGS) $(OBJS) $(LDFLAGS) -lSDL -lSDLMain -lSDL_gfx -lSDL_mixer -lSDL_ttf -framework AppKit -framework OpenGL -o hyper hyper.o: *.cpp language-data.cpp $(CXX) $(CXXFLAGS) -O2 -c hyper.cpp @@ -23,11 +39,13 @@ hyper.o: *.cpp language-data.cpp langen: langen.cpp language-??.cpp language-ptbr.cpp $(CXX) $(CXXFLAGS) -O0 -Wno-embedded-directive langen.cpp -o langen -# Generation of language-data.cpp language-data.cpp: langen ./langen > language-data.cpp +savepng.o: savepng.cpp + $(CXX) $(CXXFLAGS) -O2 -c savepng.cpp + .PHONY: clean clean: - rm -f langen language-data.cpp hyper.o hyper + rm -f hyper hyper.o langen language-data.cpp savepng.o diff --git a/Makefile.mgw b/Makefile.mgw index c1b5f044..f91e1020 100644 --- a/Makefile.mgw +++ b/Makefile.mgw @@ -7,11 +7,26 @@ CXXFLAGS += -std=c++11 -mwindows -DWINDOWS CXXFLAGS += -D_A_VOLID=8 -CXXFLAGS += -DCAP_PNG=0 CXXFLAGS += ${EXTRA_CXXFLAGS} -hyper.exe: hyper.obj hyper.res - $(CXX) $(CXXFLAGS) hyper.obj hyper.res -lSDL -lSDL_mixer -lopengl32 -lSDL_ttf -lSDL_gfx -lglew32 -o hyper.exe +OBJS = hyper.obj + +ifeq (a,b) +# Enable PNG screenshots. Requires libpng. +CXXFLAGS += -DCAP_PNG +LDFLAGS += -lpng +OBJS += savepng.obj +else +CXXFLAGS += -DCAP_PNG=0 +endif + +ifeq (a,b) +# Enable RogueViz. +CXXFLAGS += -DCAP_ROGUEVIZ +endif + +hyper.exe: $(OBJS) hyper.res + $(CXX) $(CXXFLAGS) $(OBJS) hyper.res -lSDL -lSDL_mixer -lopengl32 -lSDL_ttf -lSDL_gfx -lglew32 -o hyper.exe hyper.obj: *.cpp language-data.cpp hyper.res $(CXX) $(CXXFLAGS) -O2 -c hyper.cpp -o hyper.obj @@ -25,7 +40,10 @@ langen.exe: langen.cpp language-??.cpp language-ptbr.cpp language-data.cpp: langen.exe ./langen.exe > language-data.cpp +savepng.obj: savepng.cpp + $(CXX) $(CXXFLAGS) -O2 -c savepng.cpp -o savepng.obj + .PHONY: clean clean: - rm -f langen.exe language-data.cpp hyper.obj hyper.res hyper.exe + rm -f hyper.exe hyper.obj hyper.res langen.exe language-data.cpp savepng.obj diff --git a/fieldpattern.cpp b/fieldpattern.cpp index dd71ba96..7c85080c 100644 --- a/fieldpattern.cpp +++ b/fieldpattern.cpp @@ -754,7 +754,8 @@ void nextPrime(fgeomextra& ex) { fp.Prime = nextprime; if(fp.solve() == 0) { fp.build(); - ex.primes.emplace_back(primeinfo{nextprime, size(fp.matrices) / S7, (bool) fp.wsquare}); + int cells = fp.matrices.size() / S7; + ex.primes.emplace_back(primeinfo{nextprime, cells, (bool) fp.wsquare}); break; } nextprime++; diff --git a/hyper.h b/hyper.h index 00ea3740..5d118ff2 100644 --- a/hyper.h +++ b/hyper.h @@ -6,6 +6,8 @@ #define VERNUM 10402 #define VERNUM_HEX 0xA0B2 +#include + namespace hr { using namespace std; @@ -368,7 +370,7 @@ string its(int i); int hrand(int i); #ifndef STDSIZE -template int size(const T& x) {return int(x.size()); } +template int size(const T& x) {return x.size(); } #endif // initialize the achievement system. @@ -3432,7 +3434,15 @@ int score_default(int id); void handle_event(SDL_Event& ev); #ifndef XPRINTF -template void Xprintf(const char *fmt, T... t) { printf(fmt, t...); } +#ifdef __GNUC__ +__attribute__((__format__ (__printf__, 1, 2))) +#endif +void Xprintf(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); +} #endif void pop_game(); @@ -3502,4 +3512,4 @@ void gdpush(int t); extern int fontscale; -} \ No newline at end of file +} diff --git a/langen.cpp b/langen.cpp index 724c6aa8..1600ae50 100644 --- a/langen.cpp +++ b/langen.cpp @@ -14,7 +14,9 @@ #include using namespace std; -template int size(T x) { return x.size(); } +#ifndef STDSIZE +template int size(const T& x) { return x.size(); } +#endif #define NUMLAN 7 diff --git a/rogueviz-video.cpp b/rogueviz-video.cpp index f3d66066..80eaf4c4 100644 --- a/rogueviz-video.cpp +++ b/rogueviz-video.cpp @@ -23,7 +23,7 @@ void rvvideo(const string &fname) { reached = (2*reached-1) / 3; else reached *= 2; } - printf("reached = %Ld\n", reached); + printf("reached = %lld\n", reached); vector seq; while(reached>1) { seq.push_back(llts(reached)); diff --git a/savepng.c b/savepng.cpp similarity index 98% rename from savepng.c rename to savepng.cpp index 72a05de3..bc74ce50 100644 --- a/savepng.c +++ b/savepng.cpp @@ -35,6 +35,9 @@ static void png_write_SDL(png_structp png_ptr, png_bytep data, png_size_t length SDL_RWwrite(rw, data, sizeof(png_byte), length); } +#ifdef __cplusplus +extern "C" +#endif SDL_Surface *SDL_PNGFormatAlpha(SDL_Surface *src) { SDL_Surface *surf; @@ -56,6 +59,9 @@ SDL_Surface *SDL_PNGFormatAlpha(SDL_Surface *src) return surf; } +#ifdef __cplusplus +extern "C" +#endif int SDL_SavePNG_RW(SDL_Surface *surface, SDL_RWops *dst, int freedst) { png_structp png_ptr; diff --git a/sysconfig.h b/sysconfig.h index 67871b14..be93aeb0 100644 --- a/sysconfig.h +++ b/sysconfig.h @@ -88,7 +88,7 @@ #define CAP_SDL (!ISMOBILE) #endif -#ifdef CAP_COMPASS +#ifndef CAP_COMPASS #define CAP_COMPASS ISMOBILE #endif