Merge branch 'master' of git.ucc.asn.au:/ipdf/code
authorSam Moore <[email protected]>
Wed, 26 Mar 2014 16:27:44 +0000 (00:27 +0800)
committerSam Moore <[email protected]>
Wed, 26 Mar 2014 16:27:44 +0000 (00:27 +0800)
Conflicts:
src/main.cpp

Fixed that and also fixed other things.

src/CMakeLists.txt [new file with mode: 0644]
src/Makefile
src/document.cpp
src/ipdf.h
src/main.cpp
src/view.cpp

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644 (file)
index 0000000..8582b7f
--- /dev/null
@@ -0,0 +1,43 @@
+cmake_minimum_required(VERSION 2.8)
+
+include(FindPkgConfig)
+pkg_search_module(SDL2 REQUIRED sdl2)
+
+project(ipdf)
+
+set(CMAKE_CXX_FLAGS "-std=gnu++0x")
+
+include_directories(${SDL2_INCLUDE_DIRS})
+
+add_library(ipdflib
+common.h
+log.h
+log.cpp
+ipdf.h
+view.h
+view.cpp
+document.h
+document.cpp
+screen.h
+screen.cpp
+)
+
+add_executable(ipdf
+main.cpp
+)
+
+target_link_libraries(ipdf
+ipdflib
+GL
+${SDL2_LIBRARIES}
+)
+
+add_executable(ipdf-testsaveload
+tests/saveload.cpp
+)
+
+target_link_libraries(ipdf-testsaveload
+ipdflib
+GL
+${SDL2_LIBRARIES}
+)
index 0a1bc96..6d7f100 100644 (file)
@@ -22,7 +22,7 @@ runtests : tests/runtests.sh
        cd tests; ./runtests.sh
 
 
-$(BIN) : $(LINKOBJ)
+$(BIN) : $(LINKOBJ) ../obj/$(MAIN)
        @mkdir -p $(dir $@)
        $(CXX) -o $(BIN) $(LINKOBJ) ../obj/$(MAIN) $(LIB)
 
@@ -36,7 +36,7 @@ clean_bin :
        $(RM) $(BIN)
 
 clean :
-       $(RM) $(BIN) $(DEPS) $(LINKOBJ)
+       $(RM) $(BIN) $(DEPS) $(LINKOBJ) ../obj/$(MAIN)
        $(RM) tests/*~
        $(RM) tests/*.test
        $(RM) tests/*.out
index e8556ee..d745c15 100644 (file)
@@ -5,6 +5,27 @@
 using namespace IPDF;
 using namespace std;
 
+// Loads an std::vector<T> of size num_elements from a file.
+template<typename T>
+static void LoadStructVector(FILE *src_file, size_t num_elems, std::vector<T>& dest)
+{
+       size_t structsread = 0;
+       dest.resize(num_elems);
+       structsread = fread(dest.data(), sizeof(T), num_elems, src_file);
+       if (structsread != num_elems)
+               Fatal("Only read %u structs (expected %u)!", structsread, num_elems);
+}
+
+// Saves an std::vector<T> to a file. Size must be saves separately.
+template<typename T>
+static void SaveStructVector(FILE *dst_file, std::vector<T>& src)
+{
+       size_t written = 0;
+       written = fwrite(src.data(), sizeof(T), src.size(), dst_file);
+       if (written != src.size())
+               Fatal("Only wrote %u structs (expected %u)!", written, src.size());
+}
+
 void Document::Save(const string & filename)
 {
        Debug("Saving document to file \"%s\"...", filename.c_str());
@@ -19,16 +40,10 @@ void Document::Save(const string & filename)
                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);
+       SaveStructVector<ObjectType>(file, m_objects.types);
 
        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);
-
-
+       SaveStructVector<Rect>(file, m_objects.bounds);
 
        int err = fclose(file);
        if (err != 0)
@@ -57,18 +72,11 @@ 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);
+       LoadStructVector<ObjectType>(file, ObjectCount(), m_objects.types);
        
        Debug("Object bounds...");
-       read = fread(m_objects.bounds.data(), sizeof(Rect), m_objects.bounds.size(), file);
-       if (read != ObjectCount())
-               Fatal("Only read %u objects!", read);
+       LoadStructVector<Rect>(file, ObjectCount(), m_objects.bounds);
        
        Debug("Successfully loaded %u objects from \"%s\"", ObjectCount(), filename.c_str());
 }
index 4041bca..8ee30c3 100644 (file)
@@ -16,6 +16,12 @@ namespace IPDF
        typedef unsigned ObjectID;
        typedef enum {RECT_FILLED, RECT_OUTLINE} ObjectType;
 
+       enum DocChunkTypes
+       {
+               CT_OBJTYPES,
+               CT_OBJBOUNDS
+       };
+
        struct Rect
        {
                Real x; Real y; Real w; Real h;
index 72f29e3..339ea8d 100644 (file)
@@ -3,6 +3,7 @@
 int main(int argc, char ** argv)
 {      
        Document doc;
+       srand(time(NULL));
        if (argc > 1)
        {
                for (int i = 2; i < argc; ++i)
@@ -11,6 +12,11 @@ int main(int argc, char ** argv)
                }
                doc.Load(argv[1]);
        }
+       else
+       {
+               Debug("Add random object");
+               doc.Add(RECT_FILLED, Rect(Random()*0.5, Random()*0.5, Random()*0.5, Random()*0.5));
+       }
        MainLoop(doc);
        return 0;
 }
index bd9b2fa..9e832d2 100644 (file)
@@ -27,6 +27,8 @@ void View::Render()
        glBegin(GL_QUADS);
        for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
        {
+               if (m_document.m_objects.types[id] == RECT_FILLED)
+                       continue;
                Rect obj_bounds = m_document.m_objects.bounds[id];
                glVertex2f(obj_bounds.x, obj_bounds.y);
                glVertex2f(obj_bounds.x + obj_bounds.w, obj_bounds.y);
@@ -35,4 +37,17 @@ void View::Render()
        }
        glEnd();
 
+       for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
+       {
+               if (m_document.m_objects.types[id] == RECT_OUTLINE)
+                       continue;
+               Rect obj_bounds = m_document.m_objects.bounds[id];
+               glBegin(GL_LINE_LOOP);
+               glVertex2f(obj_bounds.x, obj_bounds.y);
+               glVertex2f(obj_bounds.x + obj_bounds.w, obj_bounds.y);
+               glVertex2f(obj_bounds.x + obj_bounds.w, obj_bounds.y + obj_bounds.h);
+               glVertex2f(obj_bounds.x, obj_bounds.y + obj_bounds.h);
+               glEnd();
+       }
+
 }

UCC git Repository :: git.ucc.asn.au