#include <Windows.h>
#include <stdlib.h>
#include <vector>
#include <thread>
#include <mutex>
using namespace std;
enum {
SPECTRUM_DATA_CNT = 63,
};
struct SDisplay {
int Width;
int Height;
HDC OffScreenDC;
HBITMAP OffScreenBMP;
HBRUSH BlackBrush;
HBRUSH WhiteBrush;
};
struct SSpectrum {
uint8_t Current[SPECTRUM_DATA_CNT];
uint8_t Draw[SPECTRUM_DATA_CNT];
recursive_mutex Mutex;
};
static struct {
SDisplay Display;
SSpectrum Spectrum;
} _G;
static void _DoPaint() {
SDisplay& D = _G.Display;
HDC& hdc = D.OffScreenDC;
int w = D.Width;
int h = D.Height;
// uint8_t
int col_w = w / SPECTRUM_DATA_CNT - 2;
SelectObject(hdc, D.BlackBrush);
Rectangle(hdc, 0, 0, w, h);
SSpectrum& S = _G.Spectrum;
unique_lock<recursive_mutex> lock(S.Mutex);
SelectObject(hdc, D.WhiteBrush);
for (int i = 0; i < SPECTRUM_DATA_CNT; i++) {
int x = i * w / SPECTRUM_DATA_CNT;
int y = h - S.Draw[i] * h / 256;
Rectangle(hdc, x, y, x + col_w, h);
}
}
static void _OnPaint(HWND hWnd) {
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
RECT rc;
GetClientRect(hWnd, &rc);
int w = rc.right - rc.left;
int h = rc.bottom - rc.top;
SDisplay& d = _G.Display;
HDC& hdc = d.OffScreenDC;
HBITMAP& hbmp = d.OffScreenBMP;
if (d.Width != w || d.Height != h) {
d.Width = w;
d.Height = h;
if (hdc == NULL) {
hdc = CreateCompatibleDC(ps.hdc);
}
HBITMAP newBmp = CreateCompatibleBitmap(hdc, w, h);
HBITMAP oldBmp = (HBITMAP)SelectObject(hdc, newBmp);
if (hbmp && hbmp == oldBmp) {
DeleteObject(hbmp);
}
hbmp = newBmp;
}
_DoPaint();
BitBlt(ps.hdc, 0, 0, w, h, hdc, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_PAINT:
_OnPaint(hWnd);
printf("WM_PAINT\n");
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
static const wchar_t* _GetWindowClassName() {
return L"SpectrumWindow";
}
static void _InitWindowClass() {
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = _GetWindowClassName();
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
}
static void _RandomSpectrum() {
SSpectrum& S = _G.Spectrum;
unique_lock<recursive_mutex> lock(S.Mutex);
for (int i = 0; i < SPECTRUM_DATA_CNT; i++) {
S.Current[i] = rand() % 256;
if (S.Draw[i] > S.Current[i]) {
uint8_t d = S.Draw[i] - S.Current[i];
if (d > 10) {
d = 10;
}
S.Draw[i] -= d;
}
else if (S.Draw[i] < S.Current[i]) {
uint8_t d = S.Current[i] - S.Draw[i];
if (d > 10) {
d = 10;
}
S.Draw[i] += d;
}
}
}
int main() {
_InitWindowClass();
_G.Display.BlackBrush = CreateSolidBrush(RGB(0, 0, 0));
_G.Display.WhiteBrush = CreateSolidBrush(RGB(255, 255, 255));
HWND hWnd = CreateWindow(_GetWindowClassName(), L"Spectrum", WS_OVERLAPPEDWINDOW,
0, 0, 1024, 600, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
thread([hWnd] {
for (int i = 0; i < 40*60; i++) {
this_thread::sleep_for(chrono::milliseconds(25));
_RandomSpectrum();
InvalidateRect(hWnd, NULL, FALSE);
}
}).detach();
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}