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
|
#include <stdafx.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
HWND hWnd;
const int upd_time = 10;
const int size = 5;
const int h = 50;
const int w = 50;
const int count = h * w;
int rgb_map[count]; // 0 = red, 1 = green, 2 = blue
const int change_map[] = { 1, 2, 0 }; // red -> green, green -> blue, blue -> red
VOID OnPaint(HDC hdc)
{
Graphics graphics(hdc);
for(int i = 0; i < count; ++i)
{
SolidBrush solidBrush(Color((0 == rgb_map[i]) ? 255 : 0
, (1 == rgb_map[i]) ? 255 : 0
, (2 == rgb_map[i]) ? 255 : 0));
graphics.FillRectangle(&solidBrush
, (i % w) * size, (i / w) * size, size, size);
}
}
void OnTimer() // Here the color changes (the rest is more or less windows stuff)
{
int choose_map[count];
int choose_count = count;
for(int c = 0; c < choose_count; ++c)
{
choose_map[c] = c;
}
while(choose_count > 0)
{
const int sel_idx = choose_map[rand() % choose_count];
--choose_count;
choose_map[sel_idx] = choose_map[choose_count];
const int x = (sel_idx % w);
const int y = (sel_idx / w);
const int env_idx[] =
{
y * w + x - 1
, y * w + x + 1
, (y - 1) * w + x
, (y + 1) * w + x
};
const int env_count = sizeof(env_idx) / sizeof(*env_idx);
for(int i = 0; i < env_count; ++i)
{
if(env_idx[i] < 0)
;
else if(env_idx[i] < count)
{
if(change_map[rgb_map[sel_idx]] == rgb_map[env_idx[i]])
rgb_map[env_idx[i]] = rgb_map[sel_idx];
}
}
}
InvalidateRect(hWnd, NULL, false);
}
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, INT iCmdShow)
{
MSG msg;
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
srand (time(NULL));
for(int i = 0; i < count; ++i)
{
rgb_map[i] = rand() % 3;
}
// Initialize GDI+.
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = TEXT("ColorGame");
RegisterClass(&wndClass);
hWnd = CreateWindow(
TEXT("ColorGame"), // window class name
TEXT("Color Game"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL); // creation parameters
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
SetTimer(hWnd, 1, upd_time, (TIMERPROC) NULL);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);
return msg.wParam;
} // WinMain
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch(message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
OnPaint(hdc);
EndPaint(hWnd, &ps);
return 0;
case WM_TIMER:
OnTimer();
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // WndProc
|