Isle
Loading...
Searching...
No Matches
tglsurface.cpp
Go to the documentation of this file.
1// TglSurface.cpp : implementation file
2
3#include "tglsurface.h"
4
5#include "decomp.h"
6
8
9using namespace Tgl;
10
12// TglSurface
13
14// FUNCTION: LEGO1 0x100abbf0
15// FUNCTION: BETA10 0x1017d490
17{
18 m_pRenderer = 0;
19 m_pDevice = 0;
20 m_pView = 0;
21 m_pScene = 0;
22
23 m_width = 0;
24 m_height = 0;
25
26 m_stopRendering = FALSE;
27 m_isInitialized = FALSE;
28
29 // statistics
30 m_frameCount = 0;
31#ifdef _DEBUG
32 m_triangleCount = 0;
33#endif
34}
35
36// FUNCTION: LEGO1 0x100abd60
37// FUNCTION: BETA10 0x1017d5a2
39{
40 Destroy();
41}
42
43// FUNCTION: LEGO1 0x100abde0
44// FUNCTION: BETA10 0x1017d647
46{
48
49 delete m_pDevice;
50 m_pDevice = 0;
51
52 m_pRenderer = 0;
53 m_pScene = 0;
54}
55
56// ???
57// FUNCTION: LEGO1 0x100abe10
58// FUNCTION: BETA10 0x1017d6b0
59int GetBitsPerPixel(IDirectDrawSurface* pSurface)
60{
61 DDPIXELFORMAT pixelFormat;
62 HRESULT result;
63
64 memset(&pixelFormat, 0, sizeof(pixelFormat));
65 pixelFormat.dwSize = sizeof(pixelFormat);
66
67 result = pSurface->GetPixelFormat(&pixelFormat);
68 assert(result == DD_OK);
69 assert(pixelFormat.dwFlags & DDPF_RGB);
70
71 return pixelFormat.dwRGBBitCount;
72}
73
74// FUNCTION: LEGO1 0x100abe50
75// FUNCTION: BETA10 0x1017d742
76BOOL TglSurface::Create(const CreateStruct& rCreateStruct, Renderer* pRenderer, Group* pScene)
77{
78 DeviceDirect3DCreateData createData = {rCreateStruct.m_direct3d, rCreateStruct.m_d3dDevice};
79 int bitsPerPixel = GetBitsPerPixel(rCreateStruct.m_pFrontBuffer);
80
81 ColorModel colorModel = Ramp;
82 ShadingModel shadingModel = Gouraud;
83 int shadeCount = 32;
84 BOOL dither = TRUE;
85 int textureShadeCount = -1;
86 int textureColorCount = -1;
87 Result result;
88
89 m_pRenderer = pRenderer;
90 m_pScene = pScene;
91 m_pDevice = m_pRenderer->CreateDevice(createData);
92
93 if (!m_pDevice) {
94 assert(0);
95 m_pRenderer = 0;
96 m_pScene = 0;
97 return FALSE;
98 }
99
100 if (bitsPerPixel == 1) {
101 shadeCount = 4;
102 textureShadeCount = 4;
103 }
104 else if (bitsPerPixel == 8) {
105 shadeCount = 32;
106 shadeCount = 16;
107 dither = FALSE;
108 textureShadeCount = shadeCount;
109 textureColorCount = 256;
110 }
111 else if (bitsPerPixel == 16) {
112 shadeCount = 32;
113 dither = FALSE;
114 textureShadeCount = shadeCount;
115 textureColorCount = 256;
116 }
117 else if (bitsPerPixel >= 24) {
118 shadeCount = 256;
119 dither = FALSE;
120 textureShadeCount = 256;
121 textureColorCount = 64;
122 }
123 else {
124 dither = FALSE;
125 }
126
127 if (textureShadeCount != -1) {
128 result = pRenderer->SetTextureDefaultShadeCount(textureShadeCount);
129 assert(Succeeded(result));
130 }
131 if (textureColorCount != -1) {
132 result = pRenderer->SetTextureDefaultColorCount(textureColorCount);
133 assert(Succeeded(result));
134 }
135
136 result = m_pDevice->SetColorModel(colorModel);
137 assert(Succeeded(result));
138 result = m_pDevice->SetShadingModel(shadingModel);
139 assert(Succeeded(result));
140 result = m_pDevice->SetShadeCount(shadeCount);
141 assert(Succeeded(result));
142 result = m_pDevice->SetDither(dither);
143 assert(Succeeded(result));
144
145 m_width = m_pDevice->GetWidth();
146 m_height = m_pDevice->GetHeight();
147
148 m_pView = CreateView(m_pRenderer, m_pDevice);
149 if (!m_pView) {
150 delete m_pDevice;
151 m_pDevice = 0;
152 m_pRenderer = 0;
153 m_pScene = 0;
154 return FALSE;
155 }
156
157 m_frameRateMeter.Reset();
158 m_renderingRateMeter.Reset();
159#ifdef _DEBUG
160 m_triangleRateMeter.Reset();
161#endif
162 m_frameRateMeter.StartOperation();
163
164 m_isInitialized = TRUE;
165
166 return TRUE;
167}
168
169// FUNCTION: LEGO1 0x100ac030
170// FUNCTION: BETA10 0x1017db86
172{
173 delete m_pView;
174 m_pView = 0;
175}
176
177// FUNCTION: LEGO1 0x100ac050
178// FUNCTION: BETA10 0x1017dbd0
180{
181 MxStopWatch renderTimer;
182
183 if (m_isInitialized && !m_stopRendering) {
184 Result result;
185
186#ifdef _DEBUG
187 m_triangleRateMeter.StartOperation();
188#endif
189 m_renderingRateMeter.StartOperation();
190 renderTimer.Start();
191
192 result = m_pView->Render(m_pScene);
193
194 renderTimer.Stop();
195 assert(Succeeded(result));
196 m_renderingRateMeter.EndOperation();
197#ifdef _DEBUG
198 m_triangleRateMeter.EndOperation();
199#endif
200 m_frameRateMeter.EndOperation();
201 m_frameCount++;
202
203#ifdef _DEBUG
204 {
205#if 0
206 // FIXME: Tgl::Device::GetDrawnTriangleCount does not exist
207 unsigned long triangleCount = m_pDevice->GetDrawnTriangleCount();
208#else
209 unsigned long triangleCount = 0;
210#endif
211
212 m_triangleRateMeter.IncreaseOperationCount(triangleCount - m_triangleCount - 1);
213 m_triangleCount = triangleCount;
214 }
215#endif
216
217#if 0
218 // reset rate meters every 20 frames
219 if ((++m_frameCount % 20) == 0)
220#else
221 // reset rate meters every 4 seconds
222 if (m_frameRateMeter.ElapsedSeconds() > 4.0)
223#endif
224 {
225 m_frameRateMeter.Reset();
226 m_renderingRateMeter.Reset();
227#ifdef _DEBUG
228 m_triangleRateMeter.Reset();
229#endif
230 }
231
232 m_frameRateMeter.StartOperation();
233 }
234
235 return renderTimer.ElapsedSeconds();
236}
void EndOperation()
Marks the end of a measured operation and increments the count.
void Reset()
Resets the operation counter and stopwatch to zero.
void StartOperation()
Marks the beginning of a measured operation.
double ElapsedSeconds() const
Returns the total elapsed seconds since the last Reset().
Measures elapsed wall clock time using high resolution performance counters.
Definition: mxstopwatch.h:25
void Stop()
Stops timing and accumulates the elapsed interval to m_elapsedSeconds.
double ElapsedSeconds() const
Returns the total accumulated elapsed time in seconds.
void Start()
Starts (or resumes) timing from the current moment.
[AI] Encapsulates a rendering surface, its associated hardware device, renderer, and view.
Definition: tglsurface.h:32
virtual Tgl::View * CreateView(Tgl::Renderer *, Tgl::Device *)=0
[AI] Abstract view creation.
virtual BOOL Create(const CreateStruct &, Tgl::Renderer *, Tgl::Group *pScene)
[AI] Creates and initializes the surface, rendering device, and optionally installs scene graph.
Definition: tglsurface.cpp:76
virtual void Destroy()
[AI] Destroys all resources (view, device), releasing scene and renderer references.
Definition: tglsurface.cpp:45
virtual ~TglSurface()
[AI] Virtual destructor also triggers Destroy().
Definition: tglsurface.cpp:38
virtual double Render()
[AI] Renders a single frame and returns the render time for that frame.
Definition: tglsurface.cpp:179
TglSurface()
[AI] Constructs a new TglSurface instance with members initialized to their default state.
Definition: tglsurface.cpp:16
virtual void DestroyView()
[AI] Destroys (deletes) the surface's view/camera, and resets the pointer to null.
Definition: tglsurface.cpp:171
virtual Result SetShadeCount(unsigned long)=0
[AI] Specifies the number of shades for rendering (possibly for palette/ramp mode).
virtual Result SetDither(int)=0
[AI] Enables/disables dithering in the renderer.
virtual Result SetColorModel(ColorModel)=0
[AI] Sets the color model for rendering.
virtual unsigned long GetWidth()=0
[AI] Gets the output width in pixels.
virtual Result SetShadingModel(ShadingModel)=0
[AI] Sets the shading model (e.g., Gouraud, flat).
virtual unsigned long GetHeight()=0
[AI] Gets the output height in pixels.
[AI] Scene graph node for parental transforms/color/material/texture; can hold meshes or other groups...
Definition: tgl.h:669
[AI] Main interface/factory for rendering resources and scene graphs.
Definition: tgl.h:188
virtual Device * CreateDevice(const DeviceDirectDrawCreateData &)=0
[AI] Creates a rendering device using DirectDraw parameters.
virtual Result SetTextureDefaultShadeCount(unsigned long)=0
[AI] Sets the default number of shades to generate for textures.
virtual Result SetTextureDefaultColorCount(unsigned long)=0
[AI] Sets the default number of colors to use for textures (likely palette size).
virtual Result Render(const Group *)=0
[AI] Renders a group hierarchy to the view.
#define TRUE
Definition: d3drmdef.h:28
#define FALSE
Definition: d3drmdef.h:27
#define DDPF_RGB
Definition: ddraw.h:2404
#define DD_OK
Definition: ddraw.h:3166
long HRESULT
Definition: ddraw.h:115
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
typedef BOOL(FAR PASCAL *LPDIENUMDEVICEOBJECTSCALLBACKA)(LPCDIDEVICEOBJECTINSTANCEA
[AI] Namespace containing all classes related to the 3D graphics abstraction/rendering engine.
int Succeeded(Result result)
[AI] Returns whether a Tgl::Result indicates success.
Definition: tgl.h:136
ShadingModel
[AI] Represents shading models available for rendering geometry.
Definition: tgl.h:29
@ Gouraud
[AI] Gouraud (vertex-interpolated) shading. [AI]
Definition: tgl.h:33
Result
[AI] Result type used throughout the Tgl API to report operation success or failure.
Definition: tgl.h:126
ColorModel
[AI] Represents available color models for rendering.
Definition: tgl.h:18
@ Ramp
[AI] Uses a color ramp (palette/indexed color). [AI]
Definition: tgl.h:20
[AI] Contains initialization parameters for creating a TglSurface and its underlying DDraw/D3D resour...
Definition: tglsurface.h:40
IDirectDrawSurface * m_pFrontBuffer
[AI] DirectDraw primary/front buffer surface.
Definition: tglsurface.h:44
IDirect3D2 * m_direct3d
[AI] Direct3D2 interface pointer for 3D initialization.
Definition: tglsurface.h:49
IDirect3DDevice2 * m_d3dDevice
[AI] Direct3D2 device interface (hardware or emulated).
Definition: tglsurface.h:50
[AI] Contains Direct3D-specific data needed to create a rendering device.
Definition: tgl.h:97
DWORD dwSize
Definition: ddraw.h:343
DWORD dwRGBBitCount
Definition: ddraw.h:348
DWORD dwFlags
Definition: ddraw.h:344
int GetBitsPerPixel(IDirectDrawSurface *pSurface)
Definition: tglsurface.cpp:59