forked from halildurmus/win32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.dart
607 lines (500 loc) · 14.7 KB
/
snake.dart
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Copyright (c) 2020, Dart | Windows. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// The classic Snake game, as popularized by the Nokia phones of the 1990s.
// Original C implementation by David Jones, available here:
// https://github.com/davidejones/winsnake
// Note: This code isn't very idiomatic for Dart, since it's an almost direct
// translation of the C code. Nevertheless, it demonstrates some useful
// concepts, including pointer arithmetic and use of virtual memory in Win32.
// ignore_for_file: constant_identifier_names
import 'dart:ffi';
import 'dart:math' show Random;
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
late int hWnd;
const IDT_TIMER1 = 1;
const IDT_TIMER2 = 2;
final rng = Random();
final bitmapInfo = calloc<BITMAPINFO>();
Pointer bitmapMemory = nullptr;
late int bitmapWidth;
late int bitmapHeight;
const bytesPerPixel = 4;
bool isRunning = false;
int gameOverRow = 0;
bool wipeDown = true;
int appleX = 0;
int appleY = 0;
int blocksPerWidth = 0;
int blocksPerHeight = 0;
int timerAmount = 100;
Point direction = Point();
List<Point> snakePoints = <Point>[];
List<List<int>> data = [<int>[]];
class Point {
int x;
int y;
@override
String toString() => '($x, $y)';
Point([this.x = 0, this.y = 0]);
factory Point.clone(Point orig) => Point(orig.x, orig.y);
}
/// Fill rectangle with supplied 24-bit color in RGB format
void drawRect(int rectX, int rectY, int width, int height, int color) {
final red = (color >> 16) & 0xFF;
final green = (color >> 8) & 0xFF;
final blue = color & 0xFF;
final pitch = bitmapWidth * bytesPerPixel;
final ptr = Pointer<Uint8>.fromAddress(bitmapMemory.address);
var rowOffset = 0;
for (var y = 0; y < bitmapHeight; y++) {
var pixelOffset = rowOffset;
for (var x = 0; x < bitmapWidth; x++) {
if ((x >= rectX && y >= rectY) &&
(x <= (rectX + width) && y <= (rectY + height))) {
ptr.elementAt(pixelOffset).value = blue;
pixelOffset++;
ptr.elementAt(pixelOffset).value = green;
pixelOffset++;
ptr.elementAt(pixelOffset).value = red;
pixelOffset++;
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
} else {
// move along
pixelOffset += bytesPerPixel;
}
}
rowOffset += pitch;
}
}
// Return a random integer from (rangeMin <= n < rangeMax)
int randRange(int rangeMin, int rangeMax) =>
rng.nextInt(rangeMax - rangeMin) + rangeMin;
// Position an apple at a random location on the gameboard
void setApple() {
// clear old apple just in case
data[appleY][appleX] = 0;
// get a random x, y coordinate on the gameboard
final x = randRange(0, bitmapWidth ~/ 10);
final y = randRange(0, bitmapHeight ~/ 10);
// set to 1 to represent apple
if (data[y][x] == 0) {
data[y][x] = 1;
appleX = x;
appleY = y;
} else {
// something else already here, find a new location
setApple();
}
}
void collectApple() {
// play sound
Beep(750, 100);
// increase speed and increase snake
final newPoint = Point()
..x = snakePoints[snakePoints.length - 1].x
..y = snakePoints[snakePoints.length - 1].y;
snakePoints.add(newPoint);
KillTimer(hWnd, IDT_TIMER1);
timerAmount -= 5;
if (timerAmount <= 20) {
timerAmount = 20;
}
SetTimer(hWnd, IDT_TIMER1, timerAmount, nullptr);
// set new apple
setApple();
}
void moveSnake() {
// set initial position and length
// unset old
// set direction on new
final lastBlock = Point();
for (var i = 0; i < snakePoints.length; i++) {
data[snakePoints[i].y][snakePoints[i].x] = 0;
}
// set
for (var i = 0; i < snakePoints.length; i++) {
final tempX = snakePoints[i].x;
final tempY = snakePoints[i].y;
if (i == 0) {
// snake head
if (direction.x == 1) {
// right
snakePoints[i].x += 1;
if (snakePoints[i].x >= (bitmapWidth ~/ 10)) {
snakePoints[i].x = 0;
}
} else if (direction.x == -1) {
// left
snakePoints[i].x -= 1;
if (snakePoints[i].x < 0) {
snakePoints[i].x = bitmapWidth ~/ 10;
}
} else if (direction.y == 1) {
// down
snakePoints[i].y += 1;
if (snakePoints[i].y >= (bitmapHeight ~/ 10)) {
snakePoints[i].y = 0;
}
} else if (direction.y == -1) {
// up
snakePoints[i].y -= 1;
if (snakePoints[i].y < 0) {
snakePoints[i].y = (bitmapHeight ~/ 10) - 1;
}
}
} else {
// snake body
// move to where last block was ahead of this one
snakePoints[i].x = lastBlock.x;
snakePoints[i].y = lastBlock.y;
}
lastBlock
..x = tempX
..y = tempY;
data[snakePoints[i].y][snakePoints[i].x] = 2;
}
}
void setVectorToMemory() {
var vecX = 0;
var vecY = 0;
final pitch = bitmapWidth * bytesPerPixel;
final ptr = Pointer<Uint8>.fromAddress(bitmapMemory.address);
var rowOffset = 0;
for (var y = 0; y < bitmapHeight; y++) {
var pixelOffset = rowOffset;
for (var x = 0; x < bitmapWidth; x++) {
if (data[vecY][vecX] == 1) {
// Apple
//blue
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
//green
ptr.elementAt(pixelOffset).value = 255;
pixelOffset++;
//red
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
} else if (data[vecY][vecX] == 2) {
// snake
//blue
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
//green
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
//red
ptr.elementAt(pixelOffset).value = 255;
pixelOffset++;
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
} else if (data[vecY][vecX] == 3) {
// ???
//blue
ptr.elementAt(pixelOffset).value = 255;
pixelOffset++;
//green
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
//red
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
} else if (data[vecY][vecX] == 4) {
//set to purple
//blue
ptr.elementAt(pixelOffset).value = 226;
pixelOffset++;
//green
ptr.elementAt(pixelOffset).value = 43;
pixelOffset++;
//red
ptr.elementAt(pixelOffset).value = 138;
pixelOffset++;
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
} else {
//blue
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
//green
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
//red
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
ptr.elementAt(pixelOffset).value = 0;
pixelOffset++;
}
vecX = x ~/ 10;
}
rowOffset += pitch;
vecY = y ~/ 10;
}
}
void resetGame() {
appleX = 0;
appleY = 0;
timerAmount = 100;
// init snake direction
direction.x = 1;
direction.y = 0;
// init snake
snakePoints.clear();
final p1 = Point()
..x = 3
..y = 0;
snakePoints.add(p1);
final p2 = Point()
..x = 2
..y = 0;
snakePoints.add(p2);
final p3 = Point()
..x = 1
..y = 0;
snakePoints.add(p3);
final p4 = Point()
..x = 0
..y = 0;
snakePoints.add(p4);
for (var i = 0; i < snakePoints.length; i++) {
data[snakePoints[i].y][snakePoints[i].x] = 2;
}
setApple();
setVectorToMemory();
}
void gameOver() {
Beep(350, 300);
KillTimer(hWnd, IDT_TIMER1);
gameOverRow = 0;
SetTimer(hWnd, IDT_TIMER2, 20, nullptr);
}
void gameOverUpdateComplete() {
KillTimer(hWnd, IDT_TIMER2);
resetGame();
}
void gameOverUpdate() {
if (wipeDown) {
for (var x = 0; x <= data[gameOverRow].length; x++) {
data[gameOverRow][x] = 4;
}
setVectorToMemory();
gameOverRow++;
if (gameOverRow >= blocksPerHeight - 1) {
gameOverRow = blocksPerHeight - 1;
wipeDown = false;
}
} else {
for (var x = data[gameOverRow].length; x > -1; x--) {
data[gameOverRow][x] = 0;
}
setVectorToMemory();
gameOverRow--;
if (gameOverRow < 0) {
gameOverUpdateComplete();
wipeDown = true;
}
}
}
void init(int width, int height) {
// we are initializing the bitmap memory buffer here
// this can be called on resize too but for now stick to fixed window
if (bitmapMemory != nullptr) {
VirtualFree(bitmapMemory, 0, MEM_RELEASE);
}
bitmapWidth = width;
bitmapHeight = height;
bitmapInfo.ref.bmiHeader.biSize = sizeOf<BITMAPINFO>();
bitmapInfo.ref.bmiHeader.biWidth = width;
bitmapInfo.ref.bmiHeader.biHeight = height;
bitmapInfo.ref.bmiHeader.biPlanes = 1;
bitmapInfo.ref.bmiHeader.biBitCount = 32;
bitmapInfo.ref.bmiHeader.biCompression = BI_RGB;
final bitmapMemorySize = (width * height) * bytesPerPixel;
bitmapMemory =
VirtualAlloc(nullptr, bitmapMemorySize, MEM_COMMIT, PAGE_READWRITE);
// init other variables here
blocksPerWidth = (width / 10).ceil();
blocksPerHeight = (height / 10).ceil();
data = List.generate(
blocksPerHeight, (_) => List.generate(blocksPerWidth, (_) => 0));
resetGame();
}
void draw(int hdc, RECT rect, int x, int y, int width, int height) {
// update memory state bitmap to window
// this is a rect to rect copy
final windowWidth = rect.right - rect.left;
final windowHeight = rect.bottom - rect.top;
StretchDIBits(
hdc, // destination device context
0, // x-coordinate of dest rectangle origin
0, // y-coordinate of dest rectangle origin
bitmapWidth, // destination width in logical units
bitmapHeight, // destination height in logical units
0, // x-coordinate of source rectangle origin
windowHeight + 1, // y-coordinate of source rectangle origin
windowWidth, // source width in pixels
-windowHeight, // source height in pixels
bitmapMemory, // pointer to the image bits
bitmapInfo, // pointer to DIB
DIB_RGB_COLORS, // color table is literal RGB values
SRCCOPY // copy directly to dest rectangle
);
}
/// Updates game simulation (one tick of the game loop)
void gameTick() {
// set everything in our 10x10 grid
// move snake
moveSnake();
// is the snake head colliding with apple?
if (snakePoints[0].x == appleX && snakePoints[0].y == appleY) {
collectApple();
}
// check if snake is colliding with itself
for (var i = 0; i < snakePoints.length; i++) {
if (i > 0) {
if (snakePoints[i].x == snakePoints[0].x &&
snakePoints[i].y == snakePoints[0].y) {
gameOver();
}
}
}
// now write the grid to memory
setVectorToMemory();
}
int mainWindowProc(int hwnd, int uMsg, int wParam, int lParam) {
var result = 0;
switch (uMsg) {
case WM_SIZE:
final rect = calloc<RECT>();
GetClientRect(hwnd, rect);
final width = rect.ref.right - rect.ref.left;
final height = rect.ref.bottom - rect.ref.top;
init(width, height);
free(rect);
case WM_CLOSE:
isRunning = false;
case WM_DESTROY:
KillTimer(hwnd, IDT_TIMER1);
isRunning = false;
case WM_PAINT:
final ps = calloc<PAINTSTRUCT>();
final dc = BeginPaint(hwnd, ps);
final x = ps.ref.rcPaint.left;
final y = ps.ref.rcPaint.top;
final width = ps.ref.rcPaint.right - ps.ref.rcPaint.left;
final height = ps.ref.rcPaint.bottom - ps.ref.rcPaint.top;
final rect = calloc<RECT>();
GetClientRect(hwnd, rect);
gameTick();
draw(dc, rect.ref, x, y, width, height);
EndPaint(hwnd, ps);
free(rect);
free(ps);
case WM_KEYDOWN:
{
switch (wParam) {
case VK_LEFT:
if (direction.x != 1) {
direction.x = -1;
direction.y = 0;
}
case VK_RIGHT:
if (direction.x != -1) {
direction.x = 1;
direction.y = 0;
}
case VK_UP:
if (direction.y != 1) {
direction.x = 0;
direction.y = -1;
}
case VK_DOWN:
if (direction.y != -1) {
direction.x = 0;
direction.y = 1;
}
case VK_ESCAPE:
isRunning = false;
}
}
case WM_TIMER:
{
switch (wParam) {
case IDT_TIMER1:
// process the gameplay timer
gameTick();
return 0;
case IDT_TIMER2:
// process the gameover timer
gameOverUpdate();
return 0;
}
}
default:
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return result;
}
void main() => initApp(winMain);
void winMain(int hInstance, List<String> args, int nShowCmd) {
// Register the window class.
final className = TEXT('WinSnakeWindowClass');
final wc = calloc<WNDCLASS>()
..ref.lpfnWndProc = Pointer.fromFunction<WindowProc>(mainWindowProc, 0)
..ref.hInstance = hInstance
..ref.lpszClassName = className;
if (RegisterClass(wc) != 0) {
// Create the window.
hWnd = CreateWindowEx(
0, // Optional window styles.
className, // Window class
TEXT('Dart WinSnake'), // Window caption
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,
// Size and position
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
nullptr // Additional application data
);
if (hWnd != 0) {
SetTimer(hWnd, IDT_TIMER1, timerAmount, nullptr);
isRunning = true;
while (isRunning) {
// Run the message loop.
final msg = calloc<MSG>();
while (PeekMessage(msg, 0, 0, 0, PM_REMOVE) != 0) {
if (msg.ref.message == WM_QUIT) {
isRunning = false;
}
TranslateMessage(msg);
DispatchMessage(msg);
}
free(msg);
final dc = GetDC(hWnd);
final rect = calloc<RECT>();
GetClientRect(hWnd, rect);
final windowWidth = rect.ref.right - rect.ref.left;
final windowHeight = rect.ref.bottom - rect.ref.top;
draw(dc, rect.ref, 0, 0, windowWidth, windowHeight);
ReleaseDC(hWnd, dc);
free(rect);
}
} else {
MessageBox(0, TEXT('Failed to create window'), TEXT('Error'),
MB_ICONEXCLAMATION | MB_OK);
}
} else {
MessageBox(0, TEXT('Failed to create window'), TEXT('Error'),
MB_ICONEXCLAMATION | MB_OK);
}
}