model.cpp

Mon, 18 Mar 2013 03:38:51 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Mon, 18 Mar 2013 03:38:51 +0200
changeset 23
69a91c1ff583
parent 22
335e430a6b4f
permissions
-rw-r--r--

begin work on subfile caching

0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 #include <stdio.h>
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 #include <stdlib.h>
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 #include "common.h"
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 #include "io.h"
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 #include "gui.h"
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 #include "draw.h"
23
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
7 #include "bbox.h"
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 // Clear everything from the model
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 void closeModel () {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 // Remove all loaded files and the objects they contain
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 for (ushort i = 0; i < g_LoadedFiles.size(); i++) {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 OpenFile* f = g_LoadedFiles[i];
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14
22
335e430a6b4f So much for that pointer class, caused more problems than it solved. For instance splitting a second quad after a first one had been split would trigger a peculiar crash...
Santeri Piippo <crimsondusk64@gmail.com>
parents: 20
diff changeset
15 for (ushort j = 0; j < f->objects.size(); ++j)
19
6c5977e43e73 Added pointer serializing so I can keep track of all LDObject* members. This way I can replace them all properly when needed.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 7
diff changeset
16 delete (LDObject*)f->objects[j];
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 delete f;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 // Clear the array
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 g_LoadedFiles.clear();
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 g_CurrentFile = NULL;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 g_qWindow->R->hardRefresh();
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26 }
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 void newModel () {
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 // Create a new anonymous file and set it to our current
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 if (g_LoadedFiles.size())
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 closeModel (); // Close any open file first, though
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 OpenFile* f = new OpenFile;
7
098e3c4949c6 Set window title dynamically based on filename
Santeri Piippo <crimsondusk64@gmail.com>
parents: 0
diff changeset
34 f->zFileName = "";
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 g_LoadedFiles.push_back (f);
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 g_CurrentFile = f;
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 g_qWindow->R->hardRefresh();
20
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
39 }
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
40
23
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
41 void openModel (str zPath) {
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
42 if (g_CurrentFile)
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
43 closeModel ();
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
44
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
45 OpenFile* pFile = IO_OpenLDrawFile (zPath);
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
46 g_CurrentFile = pFile;
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
47
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
48 // Recalculate the bounding box
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
49 g_BBox.calculate();
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
50
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
51 // Rebuild the object tree view now.
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
52 g_qWindow->buildObjList ();
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
53 g_qWindow->setTitle ();
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
54 }
69a91c1ff583 begin work on subfile caching
Santeri Piippo <crimsondusk64@gmail.com>
parents: 22
diff changeset
55
20
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
56 void saveModel () {
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
57 if (!g_CurrentFile)
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
58 return;
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
59
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
60 FILE* fp = fopen (g_CurrentFile->zFileName, "w");
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
61 if (!fp)
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
62 return;
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
63
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
64 // Write all entries now
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
65 for (ulong i = 0; i < g_CurrentFile->objects.size(); ++i) {
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
66 LDObject* obj = g_CurrentFile->objects[i];
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
67
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
68 // LDraw requires lines to have DOS line endings
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
69 str zLine = str::mkfmt ("%s\r\n",obj->getContents ().chars ());
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
70
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
71 fwrite (zLine.chars(), 1, ~zLine, fp);
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
72 }
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
73
2ca638886082 Yay! This thing can save now.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 19
diff changeset
74 fclose (fp);
0
c51cce84a9ac Initial commit
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 }

mercurial