From: Sam Moore Date: Wed, 16 Apr 2014 16:22:06 +0000 (+0800) Subject: Implement Pointless Broken Things X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=07db15ca49833542130f7c29c6a6dc7560fab59d;p=ipdf%2Fcode.git Implement Pointless Broken Things Trying to overlay the screenshot on an existing bmp to make it easier to combine the outputs of different implementations (ie: Recompile program with a different typedef instead of using templates). Rendering the existing bmp is proving to be a pain. This is also using ancient opengl. Cannot see what is wrong. I get something in the window but it does not resemble the bmp I saved at all. And then the screenshot of *that* is just totally black. --- diff --git a/src/ipdf.h b/src/ipdf.h index d6dc955..7150944 100644 --- a/src/ipdf.h +++ b/src/ipdf.h @@ -35,6 +35,13 @@ namespace IPDF } }; + struct Colour + { + float r; float g; float b; float a; + Colour() = default; + Colour(float _r, float _g, float _b, float _a) : r(_r), g(_g), b(_b), a(_a) {} + }; + struct Objects { std::vector types; diff --git a/src/main.cpp b/src/main.cpp index 1b2073e..598de72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,20 +4,61 @@ int main(int argc, char ** argv) { Document doc; srand(time(NULL)); - if (argc > 1) + + enum {OUTPUT_TO_BMP, LOOP} mode = LOOP; + + Rect bounds(0,0,1,1); + Colour c(0,0,0,1); + const char * output_to_bmp = NULL; + const char * input_filename = NULL; + + int i = 0; + while (++i < argc) { - for (int i = 2; i < argc; ++i) + if (argv[i][0] != '-') { - if (fork() == 0) doc.Load(argv[i]); + input_filename = argv[i]; + continue; } - doc.Load(argv[1]); + switch (argv[i][1]) + { + case 'o': + mode = OUTPUT_TO_BMP; + if (++i >= argc) + Fatal("No argument following -o switch"); + output_to_bmp = argv[i]; + break; + case 'c': + { + Debug("Reading paint colour"); + for (int j = 1; j <= 4; ++j) + { + if (i+j >= argc) + Fatal("No %d colour component following -c switch", j); + char * e; + float * comp = (j == 1) ? (&c.r) : ((j == 2) ? (&c.g) : ((j == 3) ? (&c.b) : &(c.a))); + *comp = strtof(argv[i+j], &e); + if (*e != '\0') + Fatal("Colour component %d not a valid float", j); + } + i += 4; + break; + } + } + } + + if (input_filename != NULL) + { + doc.Load(input_filename); } else { - Debug("Add random object"); - //doc.Add(RECT_FILLED, Rect(Random()*0.5, Random()*0.5, Random()*0.5, Random()*0.5)); - doc.Add(RECT_FILLED, Rect(0.25,0.25, 0.5, 0.5)); + doc.Add(RECT_FILLED, Rect(0.2,0.2,0.6,0.6)); } - MainLoop(doc); + + if (mode == LOOP) + MainLoop(doc, bounds, c); + else if (mode == OUTPUT_TO_BMP) + OverlayBMP(doc, output_to_bmp, bounds, c); return 0; } diff --git a/src/main.h b/src/main.h index 63c68b3..afcffe8 100644 --- a/src/main.h +++ b/src/main.h @@ -3,14 +3,27 @@ #include "document.h" #include "view.h" #include "screen.h" +#include using namespace std; using namespace IPDF; -inline void MainLoop(Document & doc) +inline void OverlayBMP(Document & doc, const char * filename, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f)) { - View view(doc); + View view(doc, bounds, c); + Screen scr; + //view.Render(); + scr.RenderBMP(filename); + scr.Present(); + //MainLoop(doc, bounds, c); + sleep(3); + scr.ScreenShot(filename); +} + +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.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf { diff --git a/src/screen.cpp b/src/screen.cpp index e97995c..624e55d 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -142,3 +142,55 @@ void Screen::ScreenShot(const char * filename) const delete [] pixels; Debug("Succeeded!"); } + +/** + * Render a BMP + * NOT PART OF THE DOCUMENT FORMAT + */ +void Screen::RenderBMP(const char * filename) const +{ + SDL_Surface * bmp = SDL_LoadBMP(filename); + if (bmp == NULL) + Fatal("Failed to load BMP from %s - %s", filename, SDL_GetError()); + + int w = bmp->w; + int h = bmp->h; + + GLenum texture_format; + switch (bmp->format->BytesPerPixel) + { + case 4: //contains alpha + texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGBA : GL_BGRA; + break; + case 3: //does not contain alpha + texture_format = (bmp->format->Rmask == 0x000000FF) ? GL_RGB : GL_BGR; + break; + default: + Fatal("Could not understand SDL_Surface format (%d colours)", bmp->format->BytesPerPixel); + break; + } + Debug("SDL_Surface %d BytesPerPixel, format %d (RGB = %d, BGR = %d, RGBA = %d, BGRA = %d)", bmp->format->BytesPerPixel, texture_format, GL_RGB, GL_BGR, GL_RGBA, GL_BGRA); + + + GLuint texID; + + glGenTextures(1, &texID); + glBindTexture(GL_TEXTURE_2D, texID); + glTexImage2D(GL_TEXTURE_2D, 0, bmp->format->BytesPerPixel, w, h, 0, texture_format, GL_UNSIGNED_BYTE, bmp->pixels); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texID); + + glBegin(GL_QUADS); + glTexCoord2i(0,0); glVertex2f(-0.5f*w, -0.5f*h); + glTexCoord2i(1,0); glVertex2f(0.5f*w, -0.5f*h); + glTexCoord2i(1,1); glVertex2f(0.5f*w, 0.5f*h); + glTexCoord2i(0,1); glVertex2f(-0.5*w,0.5*h); + glEnd(); + + glDisable(GL_TEXTURE_2D); + + //SDL_FreeSurface(bmp); +} diff --git a/src/screen.h b/src/screen.h index f5ae753..e641826 100644 --- a/src/screen.h +++ b/src/screen.h @@ -45,6 +45,7 @@ namespace IPDF void SetMouseCursor(MouseCursors cursor); void ScreenShot(const char * filename) const; + void RenderBMP(const char * filename) const; private: void ResizeViewport(int width, int height); diff --git a/src/view.cpp b/src/view.cpp index 2b6641a..cd4cc35 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -80,10 +80,7 @@ void View::Render() glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - - - - glColor4f(0.f,0.f,0.f,1.f); + glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a); glBegin(GL_QUADS); for (unsigned id = 0; id < m_document.ObjectCount(); ++id) { diff --git a/src/view.h b/src/view.h index 4e2306a..24ae6d9 100644 --- a/src/view.h +++ b/src/view.h @@ -9,7 +9,8 @@ namespace IPDF class View { public: - View(Document & document) : m_document(document), m_bounds(0,0,1,1) {} + 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) {} virtual ~View() {} void Render(); @@ -21,6 +22,7 @@ namespace IPDF void DrawGrid(); Document & m_document; Rect m_bounds; + Colour m_colour; }; }