-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
160 lines (153 loc) · 3.02 KB
/
Grid.cpp
File metadata and controls
160 lines (153 loc) · 3.02 KB
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
#include "stdafx.h"
#include "Grid.h"
#define GRID_TILE_COUNT 11*19
HWND grid[GRID_TILE_COUNT]; //0 === Top Left; GRID_TILE_COUNT - 1 === Bottom Right
//this is NOT transformed - north is up
//[y * 11 + x] - flattened horizontal first
HWND createTile(HWND m_hwnd, int id)
{
WCHAR buffer[8];
swprintf_s(buffer, L"%d\n", id);
int x = (id % 11 + 5) * 25, y = (id / 11 + 1) * 25;
return CreateWindow(
L"BUTTON",
buffer,
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_OWNERDRAW, // Styles
x, // x position in window
y, // y position in window
25, // Button width
25, // Button height
m_hwnd, // Parent window
(HMENU) (GRID_BUTTON | id),
(HINSTANCE) GetWindowLong(m_hwnd, GWLP_HINSTANCE),
NULL
);
}
BOOL initGrid(HWND m_hwnd)
{
for (int i = 0; i < GRID_TILE_COUNT; i++)
{
if ((grid[i] = createTile(m_hwnd, i)) == nullptr)
{
return FALSE;
}
}
if (!DestroyWindow(grid[208]) || !DestroyWindow(grid[207]) || !DestroyWindow(grid[206]) || !DestroyWindow(grid[200]) || !DestroyWindow(grid[199]) || !DestroyWindow(grid[198]))
{
return FALSE;
}
grid[208] = grid[207] = grid[206] = grid[200] = grid[199] = grid[198] = nullptr;
return TRUE;
}
BOOL rotateGrid(Orientation o)
{
unsigned char id, xx, yy;
switch (o)
{
case NorthUp:
for (int y = 0; y < 19; y++)
{
for (int x = 0; x < 11; x++)
{
xx = x + 5;
yy = y + 1;
id = y * 11 + x;
if (grid[id] != nullptr)
{
if (!MoveWindow(grid[id], xx * 25, yy * 25, 25, 25, 0))
{
return FALSE;
}
}
}
}
break;
case NorthRight:
for (int y = 0; y < 19; y++)
{
for (int x = 0; x < 11; x++)
{
xx = 19 - y;
yy = x + 5;
id = y * 11 + x;
if (grid[id] != nullptr)
{
if (!MoveWindow(grid[id], xx * 25, yy * 25, 25, 25, 0))
{
return FALSE;
}
}
}
}
break;
case NorthDown:
for (int y = 0; y < 19; y++)
{
for (int x = 0; x < 11; x++)
{
xx = 15 - x;
yy = 19 - y;
id = y * 11 + x;
if (grid[id] != nullptr)
{
if (!MoveWindow(grid[id], xx * 25, yy * 25, 25, 25, 0))
{
return FALSE;
}
}
}
}
break;
case NorthLeft:
for (int y = 0; y < 19; y++)
{
for (int x = 0; x < 11; x++)
{
xx = y + 1;
yy = 15 - x;
id = y * 11 + x;
if (grid[id] != nullptr)
{
if (!MoveWindow(grid[id], xx * 25, yy * 25, 25, 25, 0))
{
return FALSE;
}
}
}
}
break;
default:
break;
}
return TRUE;
}
BOOL redrawTile(BYTE index)
{
if (index >= GRID_TILE_COUNT)
{
return FALSE;
}
return RedrawWindow(grid[index], nullptr, NULL, RDW_INVALIDATE | RDW_ERASE);
}
BOOL redrawTwoTiles(BYTE newPos, BYTE oldPos)
{
if (newPos >= GRID_TILE_COUNT || oldPos >= GRID_TILE_COUNT)
{
return FALSE;
}
return redrawTile(newPos) && redrawTile(oldPos);
}
BOOL redrawAllTiles()
{
for (BYTE i = 0; i < GRID_TILE_COUNT; i++)
{
if (grid[i] != nullptr)
{
if (!redrawTile(i))
{
return FALSE;
}
}
}
return TRUE;
}