Wed, 17 Jul 2013 18:59:28 +0300
updated .gitignore
6
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | #include "bytestream.h" |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | #include "misc.h" |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | #include <string.h> |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | union { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | uint32 i; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | float f; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | } g_floatunion; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | Bytestream::Bytestream( ulong len ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | m_data = null; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | resize( len ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | clear(); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | Bytestream::Bytestream( const char* data, ulong len ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | m_data = null; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | init( data, len ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | void Bytestream::resize( ulong newsize ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | char* olddata = null; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | ulong oldsize; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | if( m_data ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | oldsize = m_size; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | olddata = new char[oldsize]; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | memcpy( olddata, m_data, oldsize ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | delete[] m_data; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | m_data = new uint8[newsize]; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | m_size = newsize; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | if( olddata ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | memcpy( m_data, olddata, min<ulong> ( oldsize, newsize )); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | void Bytestream::init( const char* data, ulong len ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | resize( len ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | memcpy( m_data, data, len ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | m_ptr = &m_data[0]; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | m_len = len; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
45 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | size_t Bytestream::len() const { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | return m_len; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | void Bytestream::clear() { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | m_ptr = &m_data[0]; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | m_len = 0; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | uint8& Bytestream::subscript( ulong idx ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | return m_data[idx]; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | const uint8& Bytestream::const_subscript( ulong idx ) const { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | return m_data[idx]; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | void Bytestream::seek( ulong pos ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | m_ptr = m_data + pos; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | void Bytestream::rewind() { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | m_ptr = m_data; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | ulong Bytestream::bytesLeft() const { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | return ( m_len - ( m_ptr - &m_data[0] )); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | ulong Bytestream::spaceLeft() const { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | return ( m_size - m_len ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | bool Bytestream::readByte( uint8& val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | if( bytesLeft() < 1 ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | val = *m_ptr++; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | bool Bytestream::readShort( uint16& val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | if( bytesLeft() < 2 ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | val = 0; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | for( int i = 0; i < 2; ++i ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
97 | val |= *m_ptr++ << ( i * 8 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | bool Bytestream::readLong( uint32& val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | if( bytesLeft() < 4 ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
105 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
106 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | val = 0; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | for( int i = 0; i < 4; ++i ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | val |= *m_ptr++ << ( i * 8 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
115 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | bool Bytestream::readFloat( float& val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | if( !readLong( g_floatunion.i )) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
118 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | val = g_floatunion.f; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
121 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | bool Bytestream::readString( str& val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | if( bytesLeft() < 1 ) // need at least the null terminator |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | uint8_t c; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | while( readByte( c ) && c != '\0' ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | val += ( char ) c; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
134 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
135 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | void Bytestream::doWrite( uint8_t val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | *m_ptr++ = val; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | m_len++; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | void Bytestream::growToFit( ulong bytes ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | if( spaceLeft() < bytes ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | resize( m_size + bytes + 128 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | bool Bytestream::readBytes( uint8 numbytes, uint8* val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | while( numbytes-- ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
150 | if( !readByte( *val++ )) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
153 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
155 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | void Bytestream::writeBytes( uint8 numbytes, const uint8* val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | growToFit( numbytes ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | while( numbytes-- ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | writeByte( *val++ ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
163 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | void Bytestream::writeByte( uint8 val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | growToFit( 1 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
166 | doWrite( val ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
167 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
170 | void Bytestream::writeShort( uint16 val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
171 | growToFit( 2 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
173 | for( int i = 0; i < 2; ++i ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
174 | doWrite(( val >> ( i * 8 )) & 0xFF ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
176 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
177 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
178 | void Bytestream::writeLong( uint32 val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
179 | growToFit( 4 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
180 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
181 | for( int i = 0; i < 4; ++i ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
182 | doWrite(( val >> ( i * 8 )) & 0xFF ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
184 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
185 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | void Bytestream::writeFloat( float val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | g_floatunion.f = val; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | writeLong( g_floatunion.i ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | void Bytestream::writeString( str val ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | growToFit( val.length() + 1 ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
195 | for( qchar c : val ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
196 | doWrite( c.toAscii() ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | doWrite( '\0' ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
199 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
200 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
201 | // ============================================================================= |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
202 | bool Bytestream::tryMerge( const Bytestream& other ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
203 | if( spaceLeft() < other.len() ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
204 | return false; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
205 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
206 | for( ulong i = 0; i < other.len(); ++i ) |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
207 | writeByte( other[i] ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
208 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
209 | return true; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
210 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
211 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
212 | void Bytestream::merge( const Bytestream& other ) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
213 | growToFit( other.len() ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
214 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
215 | if( !tryMerge( other )) { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
216 | // Shouldn't happen |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
217 | fprint( stderr, "ByteStream: Not enough space for merge (%1 bytes left, need %2)", |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
218 | spaceLeft(), other.len() ); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
219 | abort(); |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
220 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
221 | } |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
222 | |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
223 | const uint8* Bytestream::data() const { |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
224 | return m_data; |
67b6ef6917ba
Now capable of actually launching demos
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
225 | } |