Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
VideoDX9VertexBuffer.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: VideoDX9VertexBuffer.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Direct3D Video class for DirectX 9
13 */
14 
15 #include "MemDebug.h"
16 #include "VideoDX9VertexBuffer.h"
17 #include "Color.h"
18 
19 // +--------------------------------------------------------------------+
20 
21 void VideoDX9Error(const char* msg, HRESULT dderr);
22 extern int VD3D_describe_things;
23 
24 #ifndef RELEASE
25 #define RELEASE(x) if (x) { x->Release(); x=NULL; }
26 #endif
27 
28 // +--------------------------------------------------------------------+
29 
31 UINT nverts,
32 UINT vsize,
33 DWORD format,
34 DWORD usage)
35 : video(dx9), vertex_buffer(0),
36 num_verts(nverts), num_locked(0), vert_size(vsize), next_vert(0),
37 is_dynamic(false)
38 {
39  UINT len = num_verts * vert_size;
40 
41  if (video && len) {
42  is_dynamic = (usage & D3DUSAGE_DYNAMIC) ? true : false;
43  D3DPOOL pool = is_dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
44 
45  HRESULT hr = video->D3DDevice()->CreateVertexBuffer(len,
46  usage,
47  format,
48  pool,
49  &vertex_buffer,
50  0);
51 
52  if (FAILED(hr)) {
53  static int report = 10;
54  if (report) {
55  VideoDX9Error("Could not create vertex buffer.", hr);
56  report--;
57  }
58 
59  num_verts = 0;
60  vert_size = 0;
61  next_vert = 0;
62  }
63  }
64 }
65 
67 {
68  RELEASE(vertex_buffer);
69 }
70 
71 // +--------------------------------------------------------------------+
72 
73 BYTE*
75 {
76  if (vertex_buffer && count <= num_verts) {
77  DWORD flags = 0;
78 
79  if (count == 0)
80  count = num_verts;
81 
82  if (is_dynamic) {
83  flags = D3DLOCK_NOOVERWRITE;
84 
85  if (next_vert + count > num_verts) {
86  next_vert = 0;
87  flags = D3DLOCK_DISCARD;
88  }
89  }
90 
91  void* result = 0;
92  HRESULT hr = 0;
93 
94  hr = vertex_buffer->Lock(next_vert * vert_size,
95  count * vert_size,
96  &result,
97  flags);
98 
99  if (SUCCEEDED(hr)) {
100  num_locked = count;
101  return (BYTE*) result;
102  }
103  }
104 
105  return 0;
106 }
107 
108 void
110 {
111  if (vertex_buffer && num_locked > 0) {
112  vertex_buffer->Unlock();
113 
114  next_vert += num_locked;
115  num_locked = 0;
116  }
117 }
118 
119 bool
121 {
122  if (video && vertex_buffer) {
123  HRESULT hr = E_FAIL;
124 
125  if (num_locked > 0)
126  Unlock();
127 
128  hr = video->D3DDevice()->SetStreamSource(stream,
129  vertex_buffer,
130  0,
131  vert_size);
132 
133  if (SUCCEEDED(hr))
134  return true;
135  }
136 
137  return false;
138 }
139 
140 // +--------------------------------------------------------------------+
141 
142 UINT
144 {
145  return num_verts;
146 }
147 
148 UINT
150 {
151  return vert_size;
152 }
153 
154 UINT
156 {
157  return next_vert;
158 }
159 
160 
161 // +--------------------------------------------------------------------+
162 // +--------------------------------------------------------------------+
163 // +--------------------------------------------------------------------+
164 
165 
167 UINT nind,
168 DWORD usage)
169 : video(dx9), index_buffer(0),
170 num_indices(nind), num_locked(0), next_index(0),
171 is_dynamic(false)
172 {
173  UINT len = num_indices * sizeof(WORD);
174 
175  if (video && len) {
176  is_dynamic = (usage & D3DUSAGE_DYNAMIC) ? true : false;
177  D3DPOOL pool = is_dynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
178 
179  HRESULT hr = video->D3DDevice()->CreateIndexBuffer(len,
180  usage,
181  D3DFMT_INDEX16,
182  pool,
183  &index_buffer,
184  0);
185 
186  if (FAILED(hr)) {
187  static int report = 10;
188  if (report) {
189  VideoDX9Error("Could not create index buffer.", hr);
190  report--;
191  }
192 
193  num_indices = 0;
194  next_index = 0;
195  }
196  }
197 }
198 
200 {
201  RELEASE(index_buffer);
202 }
203 
204 // +--------------------------------------------------------------------+
205 
206 WORD*
208 {
209  if (index_buffer && count <= num_indices) {
210  DWORD flags = 0;
211 
212  if (count == 0)
213  count = num_indices;
214 
215  if (is_dynamic) {
216  flags = D3DLOCK_NOOVERWRITE;
217 
218  if (next_index + count > num_indices) {
219  next_index = 0;
220  flags = D3DLOCK_DISCARD;
221  }
222  }
223 
224  void* result = 0;
225  HRESULT hr = 0;
226 
227  hr = index_buffer->Lock(next_index * 2,
228  count * 2,
229  &result,
230  flags);
231 
232  if (SUCCEEDED(hr)) {
233  num_locked = count;
234  return (WORD*) result;
235  }
236  }
237 
238  return 0;
239 }
240 
241 void
243 {
244  if (index_buffer && num_locked > 0) {
245  index_buffer->Unlock();
246 
247  next_index += num_locked;
248  num_locked = 0;
249  }
250 }
251 
252 bool
254 {
255  if (video && index_buffer) {
256  if (num_locked > 0)
257  Unlock();
258 
259  HRESULT hr = video->D3DDevice()->SetIndices(index_buffer);
260 
261  if (SUCCEEDED(hr))
262  return true;
263  }
264 
265  return false;
266 }
267 
268 // +--------------------------------------------------------------------+
269 
270 UINT
272 {
273  return num_indices;
274 }
275 
276 UINT
278 {
279  return next_index;
280 }
281