102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
frame.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
12#include "pcdogs/game/globals.h"
19#include "pcdogs/level/types.h"
25#include <math.h>
26#include <stdarg.h>
27#include <string.h>
28
29// Original pcdogs.exe: 0x4096d0
30// Adjusts the dynamic level scale from frame rate in supported game modes. Values below ten and NaNs do nothing; frame rates above thirty update only the standalone scale.
31void Graphics_AdjustLevelScale(float measured_fps)
32{
33 int32_t mode = g_currentGameMode;
34
35 if (mode < 0 || mode > (int32_t)GAME_MODE_FPS_DEBUG || mode == (int32_t)GAME_MODE_EXTENDED_RENDER_DISTANCE || mode == (int32_t)GAME_MODE_KEEP_LEVEL_SCALE || mode == (int32_t)GAME_MODE_MENU_STRINGS)
36 return;
37
38 /* FCOMP sets C0 on unordered input as well as values below ten. */
39 if (!(measured_fps >= 10.0f))
40 return;
41
42 float level_scale;
43
44 __builtin_memcpy(&level_scale, &g_levelScaleFactor, sizeof(level_scale));
45
46 if (measured_fps > 30.0f)
47 {
48 g_dynamic_level_scale = __ftol_msvc((long double)level_scale);
49
50 return;
51 }
52
53 int32_t fps_adjusted_level_scale = __ftol_msvc((long double)g_oneOverThirtyFPS *
54 (long double)level_scale * (long double)measured_fps);
55
56 g_dynamic_level_scale = fps_adjusted_level_scale;
57 render_list_state->dynamic_level_scale = fps_adjusted_level_scale;
58}
59
60// Original pcdogs.exe: 0x418780
61// Clears the Direct3D depth buffer to one and returns the device result.
63{
64 IDirect3DDevice7 *device = g_d3dDevice7;
65
66 return device->lpVtbl->Clear(device, 0, NULL, D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
67}
68
69// Original pcdogs.exe: 0x418a80
70// Covers the window with a solid rectangle while disabling and then restoring supported depth writes.
71void Graphics_ClearScreenWithColor(uint32_t rgb_color)
72{
73 uint32_t window_height = (uint32_t)Graphics_WindowHeight;
74 uint32_t window_width = (uint32_t)Graphics_WindowWidth;
75
78 ((window_width + 1u) & 0xffffu) | (window_height << 16), MATH_Q12_ONE,
79 // Initialize default position to (0,0,0)
80 rgb_color);
82}
83
85{
86 g_sortedListCursor = link;
87 struct Graphics_Render_PolygonBatchRecord *record = *link;
88 if (!record)
89 return;
90
92 do
93 {
94 Graphics_DrawQuad(record);
95 /* Drawing can replace the current record. Follow its link. */
96 link = &g_sortedListCurrentRecord->next_in_bucket;
97 g_sortedListCursor = link;
98 record = *link;
100 } while (record);
101}
102
103// Original pcdogs.exe: 0x42cbe0
105{
106 Graphics_DrawBucket(&render_list_state->sorted_list_head);
107
110
111 for (size_t i = 0; i < sizeof(state->sorted_list_buckets) / sizeof(state->sorted_list_buckets[0]); ++i)
112 {
113 if (state->sorted_list_buckets[i])
115 }
116
120}
121
122// Original pcdogs.exe: 0x42cca0
123// Starts a rendering pass, fills the window with the supplied clear color while depth writes are disabled, and ends the pass.
124void Graphics_BeginRendering(uint32_t clear_color)
125{
127 // Disable fog temporarily for background clear
130 // Draw background rectangle filling entire window
131 (((uint32_t)Graphics_WindowWidth + 1u) & 0xffffu) | (((uint32_t)Graphics_WindowHeight + 1u) << 16),
132 MATH_Q12_ONE, clear_color);
134
135 /* tailcall */
136 return Graphics_EndRendering();
137}
138
139// Original pcdogs.exe: 0x42cd40
140// Increments the rendering-pass counter and clears depth only when entering the outermost pass.
142{
143 char previous_pass_depth = g_renderingFrameCounter;
144
145 g_renderingFrameCounter = previous_pass_depth + 1;
146
147 if (previous_pass_depth)
148 return;
149
150 /* tailcall */
152}
153
154// Original pcdogs.exe: 0x42cd60
155// Decrements an active rendering-pass counter and invokes the no-op backend when the outermost pass ends without fog capability.
157{
158 char previous_pass_depth = g_renderingFrameCounter;
159
160 if (!previous_pass_depth)
161 return;
162
163 // Decrement rendering frame counter
164 g_renderingFrameCounter = previous_pass_depth - 1;
165
166 // When counter reaches 0, check graphics capability flags
167 if (previous_pass_depth == 1 && !Graphics_HasFogCapability())
168 /* tailcall */
169 return Runtime_NoOp();
170}
171
172// Original pcdogs.exe: 0x43dfa0
173// Returns the graphics capability bit indicating fog support.
178
179// Original pcdogs.exe: 0x43e0a0
181{
186
187 IDirect3DDevice7 *device = g_d3dDevice7;
188 if (device->lpVtbl->BeginScene(device) < 0)
189 {
190 IDirectDrawSurface7 *primary = g_ddrawPrimarySurface;
191 if (primary && primary->lpVtbl->IsLost(primary))
192 {
193 IDirectDraw7 *draw = g_directDraw7;
194 HRESULT error = draw->lpVtbl->RestoreAllSurfaces(draw);
195 if (error < 0)
196 Debug_Log(D3D_FormatDirectXError(error, NULL), "DirectX Error: RestoreAllSurfaces();");
197 }
198
199 device = g_d3dDevice7;
200 HRESULT error = device->lpVtbl->BeginScene(device);
201 if (error < 0)
202 {
203 Debug_Log(D3D_FormatDirectXError(error, NULL), "HWBegFrame(): BeginScene();");
204 return 1;
205 }
206 }
207
208 uint32_t transition_flags = Game_FrameTransitionFlags;
209 if (!(transition_flags & GAME_TRANSITION_FADE_COUNTDOWN))
210 {
211 uint32_t now = Timer_GetElapsedTickCount();
213 {
214 device = g_d3dDevice7;
215 device->lpVtbl->EndScene(device);
216 return 1;
217 }
218
220 transition_flags = Game_FrameTransitionFlags;
221 }
222
223 uint32_t capabilities = g_graphicsCapabilities;
224 uint8_t frame_result = !(capabilities & GRAPHICS_CAP_FRAME_TRANSITION) && !(transition_flags & GAME_TRANSITION_RESET_CLEAR);
225 if (capabilities & GRAPHICS_CAP_FRAME_TRANSITION)
226 {
227 transition_flags &= ~(uint32_t)(GAME_TRANSITION_LOAD_LEVEL | GAME_TRANSITION_INITIALIZE_LEVEL);
229 Game_FrameTransitionFlags = transition_flags;
230 }
231 if (capabilities & GRAPHICS_CAP_RESET_FRAME_COUNTER)
232 {
233 g_frameCounter = 0;
235 }
236
240
241 do
242 {
243 device = g_d3dDevice7;
244 } while (device->lpVtbl->EndScene(device) != DD_OK);
245
246 IDirectDrawSurface7 *primary = g_ddrawPrimarySurface;
247 if (primary && g_ddrawBackBuffer)
248 {
249 HRESULT flip = primary->lpVtbl->Flip(primary, NULL, DDFLIP_WAIT);
250 if (flip == DDERR_SURFACELOST)
251 {
252 primary = g_ddrawPrimarySurface;
253 if (primary && primary->lpVtbl->IsLost(primary))
254 {
255 IDirectDraw7 *draw = g_directDraw7;
256 draw->lpVtbl->RestoreAllSurfaces(draw);
257 }
258 }
259 else
260 {
261 while (flip < 0)
262 {
263 primary = g_ddrawPrimarySurface;
264 flip = primary->lpVtbl->Flip(primary, NULL, DDFLIP_WAIT | DDFLIP_NOVSYNC);
265 }
266 }
267 }
268
271
274 {
275 /* FCOMP sets C0 for both elapsed < 2 and unordered elapsed times. */
276 if (!(g_fpsElapsedSeconds >= 2.0f))
277 {
278 long double frame_seconds = (long double)Graphics_RenderFrame_EndTimeSec - Graphics_RenderFrame_StartTimeSec;
280 g_fpsElapsedSeconds = (float)(frame_seconds + g_fpsElapsedSeconds);
281 }
282 else
283 {
284 g_currentFPS = (float)((long double)g_fpsFrameCount / g_fpsElapsedSeconds);
286 g_fpsFrameCount = 0;
287 g_fpsElapsedSeconds = 0.0f;
288 }
289 }
290 return frame_result;
291}
292
293// Original pcdogs.exe: 0x440310
294// Clears the background to black during the designated rendering state or to the root node's stored background color otherwise.
296{
298 {
300
301 return;
302 }
303
304 // Background clear color: root mesh node +0xB4 (packed 0x00RRGGBB)
306}
307
308// Original pcdogs.exe: 0x4432b0
309// Sets the default viewport, installs actor update callbacks and initializes collision-plane lookup tables.
void Actor_ApplyVerticalMovement(struct Actor_State *actor)
Definition movement.c:604
int32_t Actor_RejectCollisionProbe(void *subject, void *other_object, struct Collision_Polygon *collision_poly, int32_t collision_depth)
Definition lifecycle.c:599
void Actor_ProcessMovementBehavior(struct Actor_State *actor)
Definition movement.c:827
void Actor_RunNullCallback(struct Actor_State *unused_actor)
Definition lifecycle.c:591
void Actor_ProcessMovementCommands(struct Actor_State *actor)
Definition movement.c:621
void Actor_FollowAttachedMovement(struct Actor_State *actor)
#define g_actorPhaseCallback6
Definition globals.h:48
#define g_actorPhaseCallback0
Definition globals.h:33
#define g_actorPhaseCallback7
Definition globals.h:51
#define g_actorPreprocessCallback0
Definition globals.h:54
#define g_actorPhaseCallback5
Definition globals.h:45
#define Actor_DefaultUpdateCallbackSlot
Definition globals.h:30
#define g_actorPhaseCallback4
Definition globals.h:42
void Camera_SetViewport(int16_t viewport_x, int16_t viewport_y, int16_t viewport_w, int16_t viewport_h, int32_t apply_projection)
Definition projection.c:243
void Checkers_UpdateStateMachine(void)
void Collision_InitializePlaneLookupTables(void)
Definition geometry.c:43
void Debug_RenderOverlay(void)
Definition diagnostics.c:39
void Debug_Log(char const *category, char const *message)
Definition diagnostics.c:17
static void Graphics_DrawBucket(struct Graphics_Render_PolygonBatchRecord **link)
Definition frame.c:84
int32_t Graphics_HasFogCapability()
Definition frame.c:174
void Graphics_InitializeDispatchTables()
Definition frame.c:310
void Graphics_EndRendering()
Definition frame.c:156
void Graphics_AdjustLevelScale(float measured_fps)
Definition frame.c:31
void Graphics_DrawSortedLists(void)
Definition frame.c:104
void Graphics_IncrementPassCounter()
Definition frame.c:141
HRESULT Graphics_ClearDepthBuffer()
Definition frame.c:62
void Graphics_BeginRendering(uint32_t clear_color)
Definition frame.c:124
void Graphics_ClearScreenWithColor(uint32_t rgb_color)
Definition frame.c:71
uint8_t Graphics_RenderFrame(void)
Definition frame.c:180
void Graphics_ClearBackground()
Definition frame.c:295
#define GAME_TRANSITION_RESET_CLEAR
Definition constants.h:9
#define GAME_TRANSITION_CLEANUP_LEVEL
Definition constants.h:24
#define GAME_TRANSITION_RUNNING
Definition constants.h:17
#define GAME_TRANSITION_FADE_COUNTDOWN
Definition constants.h:25
#define GAME_TRANSITION_LOAD_LEVEL
Definition constants.h:26
#define GAME_TRANSITION_INITIALIZE_LEVEL
Definition constants.h:18
@ GAME_MODE_EXTENDED_RENDER_DISTANCE
Definition enums.h:14
@ GAME_MODE_MENU_STRINGS
Definition enums.h:23
@ GAME_MODE_FPS_DEBUG
Definition enums.h:32
@ GAME_MODE_KEEP_LEVEL_SCALE
Definition enums.h:20
void Game_UpdateAndRenderScene(void)
Definition loop.c:72
#define Game_FrameTransitionFlags
Definition globals.h:47
#define g_currentGameMode
Definition globals.h:74
#define GRAPHICS_CAP_FRAME_TRANSITION
Definition constants.h:14
#define GRAPHICS_FRAME_INTERVAL_MS
Definition constants.h:29
#define GRAPHICS_BLACK_BACKGROUND_THRESHOLD
Definition constants.h:94
#define GRAPHICS_CAP_RESET_FRAME_COUNTER
Definition constants.h:20
void Graphics_DrawRectangle(int32_t packed_origin_xy, int32_t packed_size_wh, int32_t blend_flags, uint32_t rgb_color)
Definition primitives.c:629
int32_t Graphics_DrawQuad(struct Graphics_Render_PolygonBatchRecord *batch)
Definition primitives.c:528
#define Graphics_RenderFrame_StartTimeSec
Definition globals.h:278
#define Graphics_WindowWidth
Definition globals.h:140
#define Graphics_RenderFrame_LastFrameTick
Definition globals.h:275
#define Graphics_RenderFrame_EndTimeSec
Definition globals.h:281
#define g_graphicsCapabilities
Definition globals.h:284
#define g_sortedListCurrentRecord
Definition globals.h:272
#define render_list_state
Definition globals.h:152
#define Graphics_WindowHeight
Definition globals.h:143
#define g_sortedListCursor
Definition globals.h:269
#define g_dynamic_level_scale
Definition globals.h:106
#define g_currentLevelID
Definition globals.h:43
#define g_levelScaleFactor
Definition globals.h:112
#define MATH_Q12_ONE
Definition constants.h:9
#define pkg_resource_current_level_data
Definition globals.h:117
char * D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
void D3D_SetZWriteEnableIfSupported(BOOL enable)
uint32_t Timer_GetElapsedTickCount()
Definition timer.c:24
long double Timer_GetGameTime(void)
Definition timer.c:17
#define g_ddrawPrimarySurface
Definition globals.h:225
#define g_ddrawBackBuffer
Definition globals.h:222
void Runtime_NoOp()
Definition startup.c:60
int64_t __ftol_msvc(long double value)
#define g_objectNodeRoot
Definition globals.h:12
struct Graphics_Render_PolygonBatchRecord * sorted_list_buckets[0x4000]
Definition types.h:693
#define g_d3dDevice7
Definition values.h:792
#define g_fpsElapsedSeconds
Definition values.h:495
#define g_renderingStateFlag
Definition values.h:627
#define g_frameCounter
Definition values.h:501
#define g_directDraw7
Definition values.h:810
#define g_fpsFrameCount
Definition values.h:498
#define g_renderFrameIndex
Definition values.h:555
#define g_renderingFrameCounter
Definition values.h:432
#define g_currentFPS
Definition values.h:492
#define g_renderBatchTriangleCount
Definition values.h:537
#define g_renderBatchPrimitiveCount
Definition values.h:543
#define g_renderFrameCount
Definition values.h:552
#define g_showFPSCounter
Definition values.h:483
#define g_oneOverThirtyFPS
Definition values.h:66