-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoord.c
69 lines (60 loc) · 1.82 KB
/
coord.c
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
#include "coord.h"
#include "const.h"
Coord getNextCoordFromCoord(Direction direction, Coord coord) {
Coord nextCoord;
nextCoord.x = coord.x;
nextCoord.y = coord.y;
switch (direction) {
case UP:
if (coord.y > 0) {
nextCoord.y -= 1;
}
return nextCoord;
case DOWN:
if (coord.y < SIZE) {
nextCoord.y += 1;
}
return nextCoord;
case LEFT:
if (coord.x > 0) {
nextCoord.x -= 1;
}
return nextCoord;
case RIGHT:
if (coord.x < SIZE) {
nextCoord.x += 1;
}
return nextCoord;
}
}
Coord getNextCoordFromPixel(Direction direction, SDL_Rect* curPos) {
Coord nextCoord;
nextCoord.x = curPos->x / WINDOW_SCALE;
nextCoord.y = curPos->y / WINDOW_SCALE;
switch (direction) {
case UP:
if (nextCoord.y > 0) {
nextCoord.y = (curPos->y - WINDOW_SCALE) / WINDOW_SCALE;
nextCoord.x = (curPos->x / WINDOW_SCALE);
}
return nextCoord;
case DOWN:
if (nextCoord.y < SIZE) {
nextCoord.y = (curPos->y + WINDOW_SCALE) / WINDOW_SCALE;
nextCoord.x = (curPos->x / WINDOW_SCALE);
}
return nextCoord;
case LEFT:
if (nextCoord.x > 0) {
nextCoord.y = (curPos->y / WINDOW_SCALE);
nextCoord.x = (curPos->x - WINDOW_SCALE) / WINDOW_SCALE;
}
return nextCoord;
case RIGHT:
if (nextCoord.x < SIZE) {
nextCoord.y = (curPos->y / WINDOW_SCALE);
nextCoord.x = (curPos->x + WINDOW_SCALE) / WINDOW_SCALE;
}
return nextCoord;
}
}