#include <windows.h>
#include <scrnsave.h>
#include <time.h>
#include <stdlib.h>
#include <libpmemcto.h>
#include "life.h"
#include "resource.h"
#define DEFAULT_PATH "c:\\temp\\life.cto"
#define TIMER_ID 1
CHAR Path[MAX_PATH];
CHAR szAppName[] = "Life's Screen-Saver";
CHAR szIniFile[] = "lifesaver.ini";
CHAR szParamPath[] = "Data file path";
static int Width;
static int Height;
void
game_draw(HWND hWnd, struct game *gp)
{
RECT rect;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HBITMAP bmp = CreateBitmap(gp->width, gp->height, 1, 8, gp->board);
HBRUSH brush = CreatePatternBrush(bmp);
GetWindowRect(hWnd, &rect);
FillRect(hdc, &rect, brush);
DeleteObject(brush);
DeleteObject(bmp);
EndPaint(hWnd, &ps);
}
static void
load_config(void)
{
DWORD res = GetPrivateProfileString(szAppName, szParamPath,
DEFAULT_PATH, Path, sizeof(Path), szIniFile);
}
BOOL WINAPI
ScreenSaverConfigureDialog(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
static HWND hOK;
static HWND hPath;
HRESULT hr;
BOOL res;
switch (message) {
case WM_INITDIALOG:
load_config();
hPath = GetDlgItem(hDlg, IDC_PATH);
hOK = GetDlgItem(hDlg, IDC_OK);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_OK:
res = WritePrivateProfileString(szAppName,
szParamPath, Path, szIniFile);
case IDC_CANCEL:
EndDialog(hDlg, LOWORD(wParam) == IDC_OK);
return TRUE;
}
}
return FALSE;
}
BOOL
RegisterDialogClasses(HANDLE hInst)
{
return TRUE;
}
LRESULT CALLBACK
ScreenSaverProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam)
{
static HANDLE hTimer;
static struct game *gp;
HDC hdc;
static RECT rc;
switch (iMessage) {
case WM_CREATE:
Width = GetSystemMetrics(SM_CXSCREEN);
Height = GetSystemMetrics(SM_CYSCREEN);
load_config();
gp = game_init(Path, Width, Height, 10);
hTimer = (HANDLE)SetTimer(hWnd, TIMER_ID, 10, NULL);
return 0;
case WM_ERASEBKGND:
hdc = GetDC(hWnd);
GetClientRect(hWnd, &rc);
FillRect(hdc, &rc, GetStockObject(BLACK_BRUSH));
ReleaseDC(hWnd, hdc);
return 0;
case WM_TIMER:
game_next(gp);
InvalidateRect(hWnd, NULL, FALSE);
return 0;
case WM_PAINT:
game_draw(hWnd, gp);
return 0;
case WM_DESTROY:
if (hTimer)
KillTimer(hWnd, TIMER_ID);
pmemcto_close(gp->pcp);
PostQuitMessage(0);
return 0;
}
return (DefScreenSaverProc(hWnd, iMessage, wParam, lParam));
}