From: David Gow Date: Mon, 21 Apr 2014 04:35:36 +0000 (+0800) Subject: Merge branch 'master' of git.ucc.asn.au:ipdf/code X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=aaa65d90ac812f924cbbc39bef7a5f8b6cad2da3;hp=-c Merge branch 'master' of git.ucc.asn.au:ipdf/code Conflicts: src/Makefile src/main.h src/real.h src/screen.cpp src/view.h This was one hell of a merge. I have more code coming, but it breaks other things, so it'll take a little while. --- aaa65d90ac812f924cbbc39bef7a5f8b6cad2da3 diff --combined bin/ipdf index cf8ff78,86a7077..10e25fe Binary files differ diff --combined src/Makefile index f3c287c,ef5bff1..90adae9 --- a/src/Makefile +++ b/src/Makefile @@@ -1,10 -1,8 +1,10 @@@ #Makefile ARCH := $(shell uname -m) -CXX = g++ -std=gnu++0x -Wall -Werror -Wshadow -pedantic -g +# TODO: stb_truetype doesn't compile with some of these warnings. +CXX = g++ -std=gnu++0x -g +# -Wall -Werror -Wshadow -pedantic MAIN = main.o - OBJ = log.o document.o view.o screen.o vfpu.o stb_truetype.o -OBJ = log.o real.o document.o view.o screen.o vfpu.o ++OBJ = log.o real.o document.o view.o screen.o vfpu.o stb_truetype.o LIB_x86_64 = ../contrib/lib/libSDL2-2.0.so.0 -lGL LIB_i386 = ../contrib/lib32/libSDL2-2.0.so.0 -lGL @@@ -23,7 -21,7 +23,7 @@@ MAINRPATH := $(MAINRPATH_$(ARCH) TESTRPATH := $(TESTRPATH_$(ARCH)) CFLAGS := $(CFLAGS_$(ARCH)) - + DEF = -DREAL=1 LINKOBJ = $(OBJPATHS) @@@ -33,6 -31,15 +33,15 @@@ BIN = ../bin/ipd 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) @@@ -46,7 -53,7 +55,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 --combined src/main.h index 46c1544,dedc7c2..bfd9fac --- a/src/main.h +++ b/src/main.h @@@ -13,32 -13,22 +13,31 @@@ inline void OverlayBMP(Document & doc, { View view(doc, bounds, c); Screen scr; - scr.RenderBMP(input); + if (input != NULL) + scr.RenderBMP(input); view.Render(); + if (output != NULL) + scr.ScreenShot(output); scr.Present(); - sleep(5); - scr.RenderBMP(input); - view.Render(); - scr.ScreenShot(output); } 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)) { View view(doc,bounds, c); Screen scr; + scr.DebugFontInit("DejaVuSansMono.ttf"); scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf { static bool oldButtonDown = false; static int oldx, oldy; + if (buttons > 1 && !oldButtonDown) + { + oldButtonDown = true; + view.ToggleGPUTransform(); + oldx = x; + oldy = y; + return; + } if (buttons && !oldButtonDown) { // We're beginning a drag. @@@ -66,22 -56,10 +65,22 @@@ } ); + double init_time = SDL_GetPerformanceCounter(); while (scr.PumpEvents()) { scr.Clear(); view.Render(); + scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (SDL_GetPerformanceCounter() - init_time)* 1000.0/SDL_GetPerformanceFrequency(), SDL_GetPerformanceFrequency()/(SDL_GetPerformanceCounter() - init_time)); + scr.DebugFontPrintF("View bounds: (%f, %f) - (%f, %f)\n", view.GetBounds().x, view.GetBounds().y, view.GetBounds().w, view.GetBounds().h); + if (view.UsingGPUTransform()) + { + scr.DebugFontPrint("Doing coordinate transform on the GPU.\n"); + } + else + { + scr.DebugFontPrint("Doing coordinate transform on the CPU.\n"); + } scr.Present(); + init_time = SDL_GetPerformanceCounter(); } } diff --combined src/real.h index 2a4877c,f65bb98..e6e69bc --- a/src/real.h +++ b/src/real.h @@@ -3,20 -3,28 +3,27 @@@ #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 - + namespace IPDF + { + extern const char * g_real_name[]; - #ifdef REAL_SINGLE + #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 --combined src/screen.cpp index 2764ef7,7d43e41..07d3f94 --- a/src/screen.cpp +++ b/src/screen.cpp @@@ -2,6 -2,8 +2,8 @@@ #include "screen.h" #include "SDL_opengl.h" + #include // for access(2) + #include // for access(2) using namespace IPDF; using namespace std; @@@ -38,7 -40,6 +40,7 @@@ void Screen::Clear(float r, float g, fl { glClearColor(r,g,b,a); glClear(GL_COLOR_BUFFER_BIT); + DebugFontClear(); } void Screen::ResizeViewport(int width, int height) @@@ -82,7 -83,7 +84,7 @@@ bool Screen::PumpEvents( m_last_mouse_y = evt.button.y; if (m_mouse_handler) { - m_mouse_handler(evt.button.x, evt.button.y, evt.button.state, 0); + m_mouse_handler(evt.button.x, evt.button.y, evt.button.state?evt.button.button:0, 0); } break; case SDL_MOUSEWHEEL: @@@ -100,7 -101,6 +102,7 @@@ filename[0] = (char)evt.key.keysym.sym; ScreenShot(filename); } + break; } default: break; @@@ -140,8 -140,8 +142,7 @@@ void Screen::ScreenShot(const char * fi unsigned char * pixels = new unsigned char[w*h*4]; if (pixels == NULL) Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4); - glReadBuffer(GL_FRONT); - glPixelStorei(GL_PACK_ALIGNMENT, 1); + - for (int y = 0; y < h; ++y) { glReadPixels(0,h-y-1,w, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[y*w*4]); @@@ -172,6 -172,11 +173,11 @@@ */ 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()); @@@ -224,79 -229,3 +230,79 @@@ glDisable(GL_TEXTURE_2D); SDL_FreeSurface(bmp); } + +void Screen::DebugFontInit(const char *name, float font_size) +{ + unsigned char font_atlas_data[1024*1024]; + FILE *font_file = fopen(name, "rb"); + fseek(font_file, 0, SEEK_END); + size_t font_file_size = ftell(font_file); + fseek(font_file, 0, SEEK_SET); + unsigned char *font_file_data = (unsigned char*)malloc(font_file_size); + fread(font_file_data, 1, font_file_size, font_file); + fclose(font_file); + stbtt_BakeFontBitmap(font_file_data,0, font_size, font_atlas_data,1024,1024, 32,96, m_debug_font_rects); + free(font_file_data); + glGenTextures(1, &m_debug_font_atlas); + glBindTexture(GL_TEXTURE_2D, m_debug_font_atlas); + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024,1024, 0, GL_ALPHA, GL_UNSIGNED_BYTE, font_atlas_data); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + m_debug_font_size = font_size; +} + +void Screen::DebugFontClear() +{ + m_debug_font_x = m_debug_font_y = 0; + DebugFontPrint("\n"); +} + +void Screen::DebugFontPrint(const char* str) +{ + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0,ViewportWidth(), ViewportHeight(), 0, -1, 1); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + + + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBindTexture(GL_TEXTURE_2D, m_debug_font_atlas); + glBegin(GL_QUADS); + while (*str) { + if (*str >= 32 && *str < 128) { + stbtt_aligned_quad q; + stbtt_GetBakedQuad(m_debug_font_rects, 1024,1024, *str-32, &m_debug_font_x,&m_debug_font_y,&q,1); + glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y0); + glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y0); + glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y1); + glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y1); + } + else if (*str == '\n') + { + m_debug_font_x = 0; + m_debug_font_y += m_debug_font_size; + } + ++str; + } + glEnd(); + glDisable(GL_BLEND); + glDisable(GL_TEXTURE_2D); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); +} + +void Screen::DebugFontPrintF(const char *fmt, ...) +{ + char buffer[BUFSIZ]; + va_list va; + va_start(va, fmt); + vsnprintf(buffer, BUFSIZ, fmt,va); + va_end(va); + DebugFontPrint(buffer); +} diff --combined src/view.cpp index 0354ee9,d8a875b..26e9507 --- a/src/view.cpp +++ b/src/view.cpp @@@ -11,17 -11,19 +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,19 -35,9 +35,20 @@@ m_bounds.y = y - top; m_bounds.w *= scaleAmt; m_bounds.h *= scaleAmt; + Debug("View Bounds => %s", m_bounds.Str().c_str()); } +Rect View::TransformToViewCoords(const Rect& inp) const +{ + Rect out; + out.x = (inp.x - m_bounds.x) / m_bounds.w; + out.y = (inp.y - m_bounds.y) / m_bounds.h; + + out.w = inp.w / m_bounds.w; + out.h = inp.h / m_bounds.h; + return out; +} + void View::DrawGrid() { // Draw some grid lines at fixed pixel positions @@@ -85,14 -77,7 +88,14 @@@ void View::Render( glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(Float(m_bounds.x), Float(m_bounds.x)+Float(m_bounds.w), Float(m_bounds.y) + Float(m_bounds.h), Float(m_bounds.y), -1.f, 1.f); + if (m_use_gpu_transform) + { + glOrtho(Float(m_bounds.x), Float(m_bounds.x)+Float(m_bounds.w), Float(m_bounds.y) + Float(m_bounds.h), Float(m_bounds.y), -1.f, 1.f); + } + else + { + glOrtho(0,1,1,0,-1,1); + } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@@ -107,15 -92,7 +110,15 @@@ { if (m_document.m_objects.types[id] != RECT_FILLED) continue; - Rect obj_bounds = m_document.m_objects.bounds[id]; + Rect obj_bounds; + if (m_use_gpu_transform) + { + obj_bounds = m_document.m_objects.bounds[id]; + } + else + { + obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]); + } glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y)); glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y)); glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h)); @@@ -128,15 -105,7 +131,15 @@@ { if (m_document.m_objects.types[id] != RECT_OUTLINE) continue; - Rect obj_bounds = m_document.m_objects.bounds[id]; + Rect obj_bounds; + if (m_use_gpu_transform) + { + obj_bounds = m_document.m_objects.bounds[id]; + } + else + { + obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]); + } glBegin(GL_LINE_LOOP); glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y)); glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y)); diff --combined src/view.h index b099c18,bbd575e..c6c230d --- a/src/view.h +++ b/src/view.h @@@ -10,24 -10,19 +10,27 @@@ namespace IPD { 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_use_gpu_transform(false) {} - : m_document(document), m_bounds(bounds), m_colour(colour) ++ : m_document(document), m_bounds(bounds), m_colour(colour), m_use_gpu_transform(false) + { + Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str()); + } virtual ~View() {} void Render(); void Translate(Real x, Real y); void ScaleAroundPoint(Real x, Real y, Real scaleAmt); + + Rect TransformToViewCoords(const Rect& inp) const; + + const Rect& GetBounds() const { return m_bounds; } + + const bool UsingGPUTransform() const { return m_use_gpu_transform; } + void ToggleGPUTransform() { m_use_gpu_transform = (!m_use_gpu_transform); } private: void DrawGrid(); + bool m_use_gpu_transform; Document & m_document; Rect m_bounds; Colour m_colour;