From e164c93218ed4599614f4f6e5e815429a3fedbf7 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Thu, 17 Apr 2014 20:04:35 +0800 Subject: [PATCH] Allow setting type of Real at compilation time Use a REAL preprocessor definition. Pass it from the Makefile in variable DEF Can change using `make DEF=REAL=X` Also allowed changing bounds with arguments in main. Probably not that useful. Might remove later. Tried to make a tester that overlays a series of BMPs but the RenderBMP -> ScreenShot approach only works once; subsequent attempts lead to entirely filled images. --- src/Makefile | 15 ++++++++++++--- src/ipdf.h | 4 ++++ src/main.cpp | 23 +++++++++++++++++++++-- src/main.h | 9 ++++----- src/real.cpp | 11 +++++++++++ src/real.h | 24 ++++++++++++++++-------- src/screen.cpp | 7 +++++++ src/view.cpp | 5 ++++- src/view.h | 5 ++++- 9 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 src/real.cpp diff --git a/src/Makefile b/src/Makefile index c788f85..ef5bff1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ ARCH := $(shell uname -m) CXX = g++ -std=gnu++0x -Wall -Werror -Wshadow -pedantic -g MAIN = main.o -OBJ = log.o document.o view.o screen.o vfpu.o +OBJ = log.o real.o document.o view.o screen.o vfpu.o LIB_x86_64 = ../contrib/lib/libSDL2-2.0.so.0 -lGL LIB_i386 = ../contrib/lib32/libSDL2-2.0.so.0 -lGL @@ -21,7 +21,7 @@ MAINRPATH := $(MAINRPATH_$(ARCH)) TESTRPATH := $(TESTRPATH_$(ARCH)) CFLAGS := $(CFLAGS_$(ARCH)) - +DEF = -DREAL=1 LINKOBJ = $(OBJPATHS) @@ -31,6 +31,15 @@ BIN = ../bin/ipdf all : $(BIN) +single : DEF = -DREAL=0 +single : $(BIN) + +double : DEF = -DREAL=1 +double : $(BIN) + +# The tests will compile with the default REAL definition +# To change that you can run as `make DEFS="REAL=X" tests/` where X is your chosen type +# But remember to make clean first. tests/% : tests/%.cpp ../obj/tests/%.o $(LINKOBJ) $(CXX) -o $@.test $(LINKOBJ) ../obj/$@.o $(LIB) $(TESTRPATH) @@ -44,7 +53,7 @@ $(BIN) : $(LINKOBJ) ../obj/$(MAIN) ../obj/%.o : %.cpp @mkdir -p $(dir $@) - $(CXX) $(CFLAGS) -c -MMD -o $@ $< + $(CXX) $(CFLAGS) $(DEF) -c -MMD -o $@ $< -include $(DEPS) diff --git a/src/ipdf.h b/src/ipdf.h index 7150944..c9a6a23 100644 --- a/src/ipdf.h +++ b/src/ipdf.h @@ -4,6 +4,10 @@ #include "common.h" #include "real.h" +#define C_RED Colour(1,0,0,1) +#define C_GREEN Colour(0,1,0,1) +#define C_BLUE Colour(0,0,1,1) + namespace IPDF { diff --git a/src/main.cpp b/src/main.cpp index 12d8d56..9cf0c68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,16 +2,19 @@ #include // Because we can. int main(int argc, char ** argv) { + Debug("Compiled with REAL = %d => \"%s\"", REAL, g_real_name[REAL]); + Document doc; srand(time(NULL)); enum {OUTPUT_TO_BMP, LOOP} mode = LOOP; - Rect bounds(0,0,1,1); + Colour c(0,0,0,1); const char * input_bmp = NULL; const char * output_bmp = NULL; const char * input_filename = NULL; + float b[4] = {0,0,1,1}; int i = 0; while (++i < argc) @@ -49,6 +52,21 @@ int main(int argc, char ** argv) i += 4; break; } + case 'b': + { + Debug("Reading view bounds"); + for (int j = 1; j <= 4; ++j) + { + if (i+j >= argc) + Fatal("No %d bounds component following -b switch", j); + char * e; + b[j-1] = strtof(argv[i+j], &e); + if (*e != '\0') + Fatal("Bounds component %d not a valid float", j); + } + i += 4; + break; + } } } @@ -58,8 +76,9 @@ int main(int argc, char ** argv) } else { - doc.Add(RECT_FILLED, Rect(0.2,0.2,0.6,0.6)); + doc.Add(RECT_OUTLINE, Rect(0.5,0.5,1,1)); } + Rect bounds(b[0],b[1],b[2],b[3]); if (mode == LOOP) MainLoop(doc, bounds, c); diff --git a/src/main.h b/src/main.h index f1a0172..dedc7c2 100644 --- a/src/main.h +++ b/src/main.h @@ -13,13 +13,12 @@ inline void OverlayBMP(Document & doc, const char * input, const char * output, { View view(doc, bounds, c); Screen scr; - scr.RenderBMP(input); + if (input != NULL) + scr.RenderBMP(input); view.Render(); - sleep(1); - scr.ScreenShot(output); + if (output != NULL) + scr.ScreenShot(output); scr.Present(); - - sleep(1); } inline void MainLoop(Document & doc, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f)) diff --git a/src/real.cpp b/src/real.cpp new file mode 100644 index 0000000..74c774b --- /dev/null +++ b/src/real.cpp @@ -0,0 +1,11 @@ +#include "real.h" + +namespace IPDF +{ + // Maps the REAL to a string + const char * g_real_name[] = { + "single", + "double" + }; + +} diff --git a/src/real.h b/src/real.h index 73aca6d..f65bb98 100644 --- a/src/real.h +++ b/src/real.h @@ -3,20 +3,28 @@ #include "common.h" -namespace IPDF -{ +#define REAL_SINGLE 0 +#define REAL_DOUBLE 1 + +#ifndef REAL + #error "REAL was not defined!" +#endif -#define REAL_SINGLE -//#define REAL_DOUBLE -//#define REAL_HALF -#ifdef REAL_SINGLE +namespace IPDF +{ + extern const char * g_real_name[]; + +#if REAL == REAL_SINGLE typedef float Real; inline float Float(Real r) {return r;} -#elif defined REAL_DOUBLE +#elif REAL == REAL_DOUBLE typedef double Real; inline double Float(Real r) {return r;} -#endif +#else + #error "Type of Real unspecified." +#endif //REAL + } #endif //_REAL_H diff --git a/src/screen.cpp b/src/screen.cpp index 01e7364..7d43e41 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -2,6 +2,8 @@ #include "screen.h" #include "SDL_opengl.h" +#include // for access(2) +#include // for access(2) using namespace IPDF; using namespace std; @@ -170,6 +172,11 @@ void Screen::ScreenShot(const char * filename) const */ void Screen::RenderBMP(const char * filename) const { + if (access(filename, R_OK) == -1) + { + Error("No such file \"%s\" - Nothing to render - You might have done this deliberately?", filename); + return; + } SDL_Surface * bmp = SDL_LoadBMP(filename); if (bmp == NULL) Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError()); diff --git a/src/view.cpp b/src/view.cpp index 6a3dbdf..d8a875b 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -11,17 +11,19 @@ void View::Translate(Real x, Real y) y *= m_bounds.h; m_bounds.x += x; m_bounds.y += y; + Debug("View Bounds => %s", m_bounds.Str().c_str()); } void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt) { + // x and y are coordinates in the window // Convert to local coords. x *= m_bounds.w; y *= m_bounds.h; x += m_bounds.x; y += m_bounds.y; - Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt)); + //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt)); Real top = y - m_bounds.y; Real left = x - m_bounds.x; @@ -33,6 +35,7 @@ void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt) m_bounds.y = y - top; m_bounds.w *= scaleAmt; m_bounds.h *= scaleAmt; + Debug("View Bounds => %s", m_bounds.Str().c_str()); } void View::DrawGrid() diff --git a/src/view.h b/src/view.h index 24ae6d9..bbd575e 100644 --- a/src/view.h +++ b/src/view.h @@ -10,7 +10,10 @@ namespace IPDF { public: View(Document & document, const Rect & bounds = Rect(0,0,1,1), const Colour & colour = Colour(0.f,0.f,0.f,1.f)) - : m_document(document), m_bounds(bounds), m_colour(colour) {} + : m_document(document), m_bounds(bounds), m_colour(colour) + { + Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str()); + } virtual ~View() {} void Render(); -- 2.20.1