Mon, 18 Mar 2013 03:38:51 +0200
begin work on subfile caching
0 | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include "common.h" | |
4 | #include "io.h" | |
5 | #include "gui.h" | |
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 | 8 | |
9 | // Clear everything from the model | |
10 | void closeModel () { | |
11 | // Remove all loaded files and the objects they contain | |
12 | for (ushort i = 0; i < g_LoadedFiles.size(); i++) { | |
13 | OpenFile* f = g_LoadedFiles[i]; | |
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 | 17 | |
18 | delete f; | |
19 | } | |
20 | ||
21 | // Clear the array | |
22 | g_LoadedFiles.clear(); | |
23 | g_CurrentFile = NULL; | |
24 | ||
25 | g_qWindow->R->hardRefresh(); | |
26 | } | |
27 | ||
28 | void newModel () { | |
29 | // Create a new anonymous file and set it to our current | |
30 | if (g_LoadedFiles.size()) | |
31 | closeModel (); // Close any open file first, though | |
32 | ||
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 | 35 | g_LoadedFiles.push_back (f); |
36 | g_CurrentFile = f; | |
37 | ||
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 | 75 | } |