Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TexCubeDX9.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: TexDX9.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Direct3D Texture Cache
13 */
14 
15 #include "MemDebug.h"
16 #include "TexCubeDX9.h"
17 #include "VideoDX9.h"
18 #include "Bitmap.h"
19 #include "Color.h"
20 
21 // +--------------------------------------------------------------------+
22 
23 void Print(const char* fmt, ...);
24 void VideoDX9Error(const char* msg, HRESULT err);
25 
26 #ifndef RELEASE
27 #define RELEASE(x) if (x) { x->Release(); x=NULL; }
28 #endif
29 
30 // +--------------------------------------------------------------------+
31 
33 : video(v), texture(0)
34 {
35  d3d = video->Direct3D();
36  d3ddevice = video->D3DDevice();
37 
38  for (int i = 0; i < 6; i++) {
39  faces[i] = 0;
40  last_modified[i] = 0;
41  }
42 }
43 
45 {
46  RELEASE(texture);
47 }
48 
49 // +--------------------------------------------------------------------+
50 
51 bool
52 TexCubeDX9::LoadTexture(Bitmap* bmp, int face_index)
53 {
54  if (!d3ddevice) return false;
55 
56  if (faces[face_index] == bmp && last_modified[face_index] >= bmp->LastModified())
57  return true; // already loaded and hasn't been modified
58 
59  HRESULT hr = D3D_OK;
60 
61  // create the texture, if necessary
62  if (!texture) {
63  hr = d3ddevice->CreateCubeTexture(bmp->Width(),
64  1, // one mip level
65  0, // no specific usage
66  D3DFMT_A8R8G8B8, // format matching Color::rgba
67  D3DPOOL_MANAGED,
68  &texture,
69  0);
70 
71  if (FAILED(hr) || !texture) {
72  VideoDX9Error("LoadTexture - could not create cube texture", hr);
73  return false;
74  }
75  }
76 
77  // lock the surface for writing
78  D3DLOCKED_RECT locked_rect;
79  D3DCUBEMAP_FACES face = (D3DCUBEMAP_FACES) face_index;
80  hr = texture->LockRect(face, 0, &locked_rect, 0, 0);
81 
82  if (FAILED(hr)) {
83  VideoDX9Error("LoadTexture - could not lock texture surface", hr);
84  RELEASE(texture);
85  return false;
86  }
87 
88  // load the bitmap into the texture surface
89  for (int i = 0; i < bmp->Height(); i++) {
90  BYTE* src = (BYTE*) (bmp->HiPixels() + i * bmp->Width());
91  BYTE* dst = (BYTE*) locked_rect.pBits + i * locked_rect.Pitch;
92 
93  CopyMemory(dst, src, bmp->Width() * sizeof(Color));
94  }
95 
96  // unlock the surface
97  texture->UnlockRect(face, 0);
98 
99  faces[face_index] = bmp;
100  last_modified[face_index] = bmp->LastModified();
101 
102  return true;
103 }
104 
105 // +--------------------------------------------------------------------+
106 
107 IDirect3DCubeTexture9*
109 {
110  if (texture) {
111  // need to refresh anything?
112  for (int i = 0; i < 6; i++) {
113  if (faces[i] && last_modified[i] < faces[i]->LastModified()) {
114  LoadTexture(faces[i], i);
115  }
116  }
117  }
118 
119  return texture;
120 }