From: Sam Moore Date: Wed, 26 Mar 2014 10:50:14 +0000 (+0800) Subject: Add ObjectType for Object Type X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=901ea3355265e7f413674a62f7c29806ae35a565 Add ObjectType for Object Type --- diff --git a/src/Makefile b/src/Makefile index 59e111b..3e8d8a0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ #Makefile CXX = g++ -std=gnu++0x -Wall -Werror -Wshadow -pedantic -g -OBJ = log.o main.o document.o view.o screen.o +OBJ = log.o tests/saveload.o document.o view.o screen.o LIB = `sdl2-config --libs` -lGL OBJPATHS = $(OBJ:%=../obj/%) DEPS := $(OBJPATHS:%.o=%.d) diff --git a/src/document.cpp b/src/document.cpp index 3c1a4ab..e8556ee 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -18,11 +18,18 @@ void Document::Save(const string & filename) if (written != 1) Fatal("Failed to write number of objects!"); + Debug("Object types..."); + written = fwrite(m_objects.types.data(), sizeof(ObjectType), m_objects.types.size(), file); + if (written != ObjectCount()) + Fatal("Only wrote %u objects!", written); + Debug("Object bounds..."); written = fwrite(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file); if (written != ObjectCount()) Fatal("Only wrote %u objects!", written); + + int err = fclose(file); if (err != 0) Fatal("Failed to close file \"%s\" - %s", filename.c_str(), strerror(err)); @@ -50,7 +57,13 @@ void Document::Load(const string & filename) Fatal("Failed to read number of objects!"); Debug("Number of objects: %u", ObjectCount()); + m_objects.types.resize(ObjectCount()); m_objects.bounds.resize(ObjectCount()); + + Debug("Object types..."); + read = fread(m_objects.types.data(), sizeof(ObjectType), m_objects.types.size(), file); + if (read != ObjectCount()) + Fatal("Only read %u objects!", read); Debug("Object bounds..."); read = fread(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file); @@ -60,9 +73,10 @@ void Document::Load(const string & filename) Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str()); } -void Document::Add(Real x, Real y, Real w, Real h) +void Document::Add(ObjectType type, const Rect & bounds) { - m_objects.bounds.push_back(Rect(x, y, w, h)); + m_objects.types.push_back(type); + m_objects.bounds.push_back(bounds); m_count++; } @@ -71,7 +85,7 @@ void Document::DebugDumpObjects() Debug("Objects for Document %p are:", this); for (unsigned id = 0; id < ObjectCount(); ++id) { - Debug("%u.\t%s", id, m_objects.bounds[id].Str().c_str()); + Debug("%u. \tType: %u\tBounds: %s", id, m_objects.types[id], m_objects.bounds[id].Str().c_str()); } } diff --git a/src/document.h b/src/document.h index ffb761d..89e854a 100644 --- a/src/document.h +++ b/src/document.h @@ -13,7 +13,7 @@ namespace IPDF void Load(const std::string & filename = ""); void Save(const std::string & filename); - void Add(Real x, Real y, Real w, Real h); + void Add(ObjectType type, const Rect & bounds); void DebugDumpObjects(); unsigned ObjectCount() const {return m_count;} diff --git a/src/ipdf.h b/src/ipdf.h index e150088..4041bca 100644 --- a/src/ipdf.h +++ b/src/ipdf.h @@ -14,6 +14,7 @@ namespace IPDF } typedef unsigned ObjectID; + typedef enum {RECT_FILLED, RECT_OUTLINE} ObjectType; struct Rect { @@ -30,6 +31,7 @@ namespace IPDF struct Objects { + std::vector types; std::vector bounds; }; diff --git a/src/tests/saveload.cpp b/src/tests/saveload.cpp index a2d58cb..32dd1d7 100644 --- a/src/tests/saveload.cpp +++ b/src/tests/saveload.cpp @@ -2,6 +2,7 @@ #include "../document.h" #include "../view.h" +#include "../screen.h" using namespace std; using namespace IPDF; @@ -14,7 +15,7 @@ int main(int argc, char ** argv) Document doc; for (unsigned id = 0; id < test_objects; ++id) { - doc.Add(Random(), Random(), Random(), Random()); + doc.Add((ObjectType)(rand() % 2), Rect(Random(), Random(), Random(), Random())); } doc.Save("test.ipdf"); @@ -28,5 +29,14 @@ int main(int argc, char ** argv) } + View view(doc); + Screen scr; + + while (scr.PumpEvents()) + { + view.Render(); + scr.Present(); + } + return 0; } diff --git a/src/view.cpp b/src/view.cpp index 40262ac..bd9b2fa 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -10,12 +10,7 @@ void View::Render() static bool debug_output_done = false; if (!debug_output_done) { - Debug("Bounds are %s", m_bounds.Str().c_str()); - Debug("Objects are:"); - for (unsigned id = 0; id < m_document.ObjectCount(); ++id) - { - Debug("%u\t%s", id, m_document.m_objects.bounds[id].Str().c_str()); - } + m_document.DebugDumpObjects(); debug_output_done = true; }