-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDGProcessor.cpp
233 lines (224 loc) · 8.39 KB
/
CDGProcessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "stdafx.h"
#include <stdio.h>
#include "CDGGlobals.h"
#include "CDGPrefs.h"
#include "CDGRender.h"
#include "CDGWindows.h"
#include "CDGBitmaps.h"
#include "CDGPalette.h"
#include "CDGInstructionHandlers.h"
#include "CDGBackgroundFunctions.h"
// Handle of the processing thread.
HANDLE g_hCDGProcessingThread = NULL;
// Cross thread communication events.
HANDLE g_hStopCDGProcessingEvent = NULL;
HANDLE g_hStoppedCDGProcessingEvent = NULL;
HANDLE g_hStopCDGThreadEvent = NULL;
HANDLE g_hSongLoadedEvent = NULL;
// Current CDG data.
CDGPacket* g_pCDGData = NULL;
DWORD g_nCDGPackets = 0;
// Current CDG instruction index.
DWORD g_nCDGPC = 0;
/// <summary>
/// Clear and deallocate the CDG instruction buffer.
/// </summary>
void ClearExistingCDGData() {
if (g_pCDGData) {
free(g_pCDGData);
g_pCDGData = NULL;
}
g_nCDGPackets = 0;
}
/// <summary>
/// Reads the CDG instructions from the corresponding CDG for the given file, if one exists.
/// </summary>
/// <param name="pFileBeingPlayed">Path to current file being played. Might be MP3, WAV, whatever.</param>
/// <returns>True if successful.</returns>
bool ReadCDGData(const WCHAR* pFileBeingPlayed) {
ClearExistingCDGData();
bool result = false;
WCHAR pathBuffer[MAX_PATH + 1];
wcscpy_s(pathBuffer, pFileBeingPlayed);
pathBuffer[MAX_PATH] = '\0';
size_t pathLength = wcslen(pathBuffer);
_wcslwr_s(pathBuffer);
// This is a plain filesystem path. We want to replace the extension with cdg, or
// add cdg if there is no extension.
WCHAR* pDot = wcsrchr(pathBuffer, '.');
WCHAR* pSlash = wcsrchr(pathBuffer, '\\');
if (pDot > pSlash)
*pDot = '\0';
wcscat_s(pathBuffer, L".cdg");
FILE* pFile = NULL;
errno_t error = _wfopen_s(&pFile, pathBuffer, L"rb");
if (!error && pFile) {
fseek(pFile, 0, SEEK_END);
int size = ftell(pFile);
g_nCDGPackets = size / sizeof(CDGPacket);
fseek(pFile, 0, SEEK_SET);
g_pCDGData = (CDGPacket*)malloc(g_nCDGPackets * sizeof(CDGPacket));
if (g_pCDGData) {
fread(g_pCDGData, sizeof(CDGPacket), g_nCDGPackets, pFile);
result = true;
}
fclose(pFile);
}
return result;
}
/// <summary>
/// Process enough CDG instructions to bring us up to the current time.
/// </summary>
/// <param name="songPosition">Current song position (milliseconds). Will be compared with the current CDG program counter to
/// determine how many instructions need processed.</param>
/// <param name="pRedrawRect">Rectangle that will receive an accumulated redraw region for all processed instructions.</param>
/// <param name="pInvalidRect">Rectangle that will receive an accumulated invalidate region for all processed instructions.</param>
/// <returns>Flags indicating what level of screen redraw/invalidation is required.</returns>
CDG_REFRESH_FLAGS ProcessCDGPackets(long songPosition,RECT *pRedrawRect,RECT *pInvalidRect) {
CDG_REFRESH_FLAGS result = CDG_REFRESH_NONE;
HANDLE waitHandles[] = { g_hStopCDGProcessingEvent, g_hStopCDGThreadEvent };
// Get current song position in milliseconds (see comment about rewind tolerance).
if (songPosition>=0) {
// Account for WinAmp timing drift bug (see comment about time scaler)
// and general lag (see comment about hysteresis).
songPosition = (int)(songPosition * g_nTimeScaler) + HYSTERESIS_MS;
DWORD cdgFrameIndex = (DWORD)(songPosition / CDG_FRAME_DURATION_MS);
if (cdgFrameIndex > g_nCDGPackets)
cdgFrameIndex = g_nCDGPackets;
// If the target frame is BEFORE the current CDGPC, the user has
// (possibly) rewound the song.
// Due to the XORing nature of CDG graphics, we have to start from the start
// and reconstruct the screen up until this point!
int difference = cdgFrameIndex - g_nCDGPC;
if (difference < -REWIND_TOLERANCE_MS)
g_nCDGPC = 0;
for (; g_nCDGPC < cdgFrameIndex;) {
int waitResult = ::WaitForMultipleObjects(2, waitHandles, FALSE, 0);
if (waitResult == WAIT_TIMEOUT) {
CDGPacket* pCDGPacket = g_pCDGData + g_nCDGPC;
if ((pCDGPacket->command & 0x3F) == 0x09) {
BYTE instr = pCDGPacket->instruction & 0x3F;
switch (instr) {
case CDG_INSTR_MEMORY_PRESET:
result |= MemoryPreset(pCDGPacket->data[0] & 0x0F, pInvalidRect);
break;
case CDG_INSTR_BORDER_PRESET:
result|=BorderPreset(pCDGPacket->data[0] & 0x0F, pInvalidRect);
break;
case CDG_INSTR_TILE_BLOCK:
case CDG_INSTR_TILE_BLOCK_XOR:
result |= TileBlock(pCDGPacket->data, instr == CDG_INSTR_TILE_BLOCK_XOR,pRedrawRect);
break;
case CDG_INSTR_SCROLL_COPY:
case CDG_INSTR_SCROLL_PRESET:
result |= Scroll(pCDGPacket->data[0] & 0x0F, (pCDGPacket->data[1] >> 4) & 0x03, pCDGPacket->data[1] & 0x0F, (pCDGPacket->data[2] >> 4) & 0x03, pCDGPacket->data[2] & 0x0F, instr == CDG_INSTR_SCROLL_COPY,pRedrawRect);
break;
case CDG_INSTR_TRANSPARENT_COLOR:
// Not implemented.
break;
case CDG_INSTR_LOAD_COLOR_TABLE_LOW:
case CDG_INSTR_LOAD_COLOR_TABLE_HIGH:
result |= LoadColorTable(pCDGPacket->data, instr == CDG_INSTR_LOAD_COLOR_TABLE_HIGH, pInvalidRect);
break;
default:
break;
}
}
g_nCDGPC++;
}
else
break;
}
}
return result;
}
/// <summary>
/// Resets the CDG processor, zero-ing the program counter, etc.
/// </summary>
void ResetProcessor() {
g_nCDGPC = 0;
ResetPalette();
ClearForegroundBuffer();
SetBackgroundColorIndex(0);
}
/// <summary>
/// Thread for CDG processing.
/// </summary>
/// <param name="pParams">Thread params.</param>
/// <returns>Thread result.</returns>
DWORD WINAPI CDGProcessor(LPVOID pParams) {
HANDLE waitHandles[] = { g_hStopCDGProcessingEvent, g_hStopCDGThreadEvent,g_hSongLoadedEvent };
static RECT invalidRect,redrawRect;
// Run indefinitely, until an event is raised to get us out of the loop (Winamp quit, song stopped, etc).
for (;;) {
ResetProcessor();
int waitResult = ::WaitForMultipleObjects(2, waitHandles + 1, FALSE, INFINITE);
if (waitResult == 0) {
break;
}
for (;;) {
waitResult = ::WaitForMultipleObjects(2, waitHandles, FALSE, SCREEN_REFRESH_MS);
if (waitResult == WAIT_TIMEOUT) {
::ZeroMemory(&invalidRect, sizeof(RECT));
::ZeroMemory(&redrawRect, sizeof(RECT));
if (g_nCDGPC < g_nCDGPackets) {
CDG_REFRESH_FLAGS result = ProcessCDGPackets(::SendMessage(g_hWinampWindow, WM_WA_IPC, 0, IPC_GETOUTPUTTIME), &redrawRect, &invalidRect);
// Each call to ProcessCDGPackets will return a byte, which is an accumulation of the results from the
// CDG instructions that were processed.
// If the 1 bit is set, then the area defined by redrawRect needs redrawn and repainted.
// If the 2 bit is set, then the entire background needs repainted.
// If the 4 bit is set, then the area defined by invalidRect needs repainted.
if (result & CDG_REFRESH_REDRAW_RECT) {
DrawForeground(&redrawRect);
if (invalidRect.right > 0)
::UnionRect(&invalidRect, &invalidRect, &redrawRect);
else
memcpy(&invalidRect, &redrawRect, sizeof(RECT));
}
if (result & (CDG_REFRESH_REDRAW_RECT | CDG_REFRESH_INVALIDATE_RECT)) {
RenderForegroundBackBuffer(&invalidRect);
CDGRectToClientRect(&invalidRect);
::WaitForSingleObject(g_hPaintMutex,INFINITE);
::InvalidateRect(g_hForegroundWindow, &invalidRect,FALSE);
::ReleaseMutex(g_hPaintMutex);
}
if (result & CDG_REFRESH_ENTIRE_BACKGROUND) {
::WaitForSingleObject(g_hPaintMutex, INFINITE);
::InvalidateRect(g_hBackgroundWindow, NULL, FALSE);
::ReleaseMutex(g_hPaintMutex);
}
}
}
else
break;
}
::SetEvent(g_hStoppedCDGProcessingEvent);
}
return 0;
}
/// <summary>
/// Starts the CDG processor.
/// </summary>
/// <returns>True if successful.</returns>
bool StartCDGProcessor() {
ResetProcessor();
g_hStopCDGProcessingEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
g_hStoppedCDGProcessingEvent = ::CreateEvent(NULL, FALSE, TRUE, NULL);
g_hStopCDGThreadEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL);
g_hSongLoadedEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
g_hCDGProcessingThread = ::CreateThread(NULL, 0, CDGProcessor, NULL, 0, NULL);
return !!g_hCDGProcessingThread;
}
/// <summary>
/// Stops the CDG processor.
/// </summary>
void StopCDGProcessor() {
::SetEvent(g_hStopCDGProcessingEvent);
::SetEvent(g_hStopCDGThreadEvent);
::WaitForSingleObject(g_hCDGProcessingThread, INFINITE);
::CloseHandle(g_hStopCDGProcessingEvent);
::CloseHandle(g_hStoppedCDGProcessingEvent);
::CloseHandle(g_hStopCDGThreadEvent);
::CloseHandle(g_hSongLoadedEvent);
ClearExistingCDGData();
}