objwriter.cxx

changeset 26
54eaea6dc27c
parent 20
d7b13805d1e0
child 32
d11a034aabfd
equal deleted inserted replaced
25:2a600af97c6b 26:54eaea6dc27c
46 #include "str.h" 46 #include "str.h"
47 #include "objwriter.h" 47 #include "objwriter.h"
48 48
49 #include "bots.h" 49 #include "bots.h"
50 50
51 extern bool g_GotMainLoop;
52
51 ObjWriter::ObjWriter (str path) { 53 ObjWriter::ObjWriter (str path) {
54 MainLoopBuffer = new DataBuffer;
55 OnEnterBuffer = new DataBuffer;
56
52 numWrittenBytes = 0; 57 numWrittenBytes = 0;
53 fp = fopen (path, "w"); 58 fp = fopen (path, "w");
54 CHECK_FILE (fp, path, "writing"); 59 CHECK_FILE (fp, path, "writing");
55 } 60 }
56 61
57 ObjWriter::~ObjWriter () { 62 ObjWriter::~ObjWriter () {
58 fclose (fp); 63 fclose (fp);
64 delete MainLoopBuffer;
65 delete OnEnterBuffer;
59 } 66 }
60 67
61 void ObjWriter::WriteString (char* s) { 68 void ObjWriter::WriteString (char* s) {
62 Write<long> (strlen (s)); 69 Write<long> (strlen (s));
63 for (unsigned int u = 0; u < strlen (s); u++) 70 for (unsigned int u = 0; u < strlen (s); u++)
69 } 76 }
70 77
71 void ObjWriter::WriteString (str s) { 78 void ObjWriter::WriteString (str s) {
72 WriteString (s.chars()); 79 WriteString (s.chars());
73 } 80 }
81
82 void ObjWriter::WriteBuffers () {
83 // If there was no mainloop defined, write a dummy one now.
84 if (!g_GotMainLoop) {
85 MainLoopBuffer->Write<long> (DH_MAINLOOP);
86 MainLoopBuffer->Write<long> (DH_ENDMAINLOOP);
87 }
88
89 // Write the onenter and mainloop buffers, IN THAT ORDER
90 for (int i = 0; i < 2; i++) {
91 DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer;
92 for (unsigned int x = 0; x < buf->writepos; x++) {
93 unsigned char c = *(buf->buffer+x);
94 Write<unsigned char> (c);
95 }
96
97 // Clear the buffer afterwards for potential next state
98 delete buf;
99 buf = new DataBuffer;
100 }
101
102 // Next state definitely has no mainloop yet
103 g_GotMainLoop = false;
104 }

mercurial