-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_ice.c
206 lines (162 loc) · 5.14 KB
/
game_of_ice.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
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
#include <omp.h>
#include "SDL.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define PARR_SZ 1280
#define QARR_SZ 720
typedef union {
Uint32 c;
struct {
Uint8 a, r, g, b;
};
} px_t;
SDL_Surface *init_display(void)
{
SDL_Surface *screen;
if( SDL_Init(SDL_INIT_VIDEO) == -1 ) {
fprintf(stderr, "SDL init error: %s\n", SDL_GetError());
exit(-1);
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(PARR_SZ, QARR_SZ, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
if( screen == NULL ) {
fprintf(stderr, "SDL video error: %s\n", SDL_GetError());
exit(-1);
}
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
SDL_EventState(SDL_MOUSEBUTTONUP, SDL_IGNORE);
SDL_EventState(SDL_MOUSEBUTTONDOWN, SDL_IGNORE);
SDL_EventState(SDL_JOYAXISMOTION, SDL_IGNORE);
SDL_EventState(SDL_JOYBALLMOTION, SDL_IGNORE);
SDL_EventState(SDL_JOYBUTTONUP, SDL_IGNORE);
SDL_EventState(SDL_JOYBUTTONDOWN, SDL_IGNORE);
return screen;
}
inline void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
((Uint32 *)surface->pixels)[y * surface->w + x] = pixel;
}
inline void addpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
px_t ap = *((px_t *)&pixel);
px_t *bp = &(((px_t *)surface->pixels)[y * surface->w + x]);
bp->a = (255 - ap.a) < bp->a ? 255 : ap.a + bp->a;
bp->r = (255 - ap.r) < bp->r ? 255 : ap.r + bp->r;
bp->g = (255 - ap.g) < bp->g ? 255 : ap.g + bp->g;
bp->b = (255 - ap.b) < bp->b ? 255 : ap.b + bp->b;
}
inline void subpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) {
px_t ap = *((px_t *)&pixel);
px_t *bp = &(((px_t *)surface->pixels)[y * surface->w + x]);
bp->a = ap.a > bp->a ? 0 : bp->a - ap.a;
bp->r = ap.r > bp->r ? 0 : bp->r - ap.r;
bp->g = ap.g > bp->g ? 0 : bp->g - ap.g;
bp->b = ap.b > bp->b ? 0 : bp->b - ap.b;
}
Uint8 *new_gamestate(int a_sz, int b_sz)
{
int i, j;
Uint8 *a;
a = malloc(a_sz * b_sz);
if(a == NULL) {
fprintf(stderr, "Can't init gamestate.\n");
exit(-1);
}
for(j=0; j < b_sz; j++)
for(i=0; i < a_sz; i++)
{
a[i + j*(a_sz)] = 0;
}
return a;
}
inline void draw_state(SDL_Surface *s, Uint8 *a)
{
int i, j;
Uint32 red_color, white_color;
red_color = SDL_MapRGB(s->format, 3, 2, 1);
white_color = SDL_MapRGB(s->format, 255, 255, 255);
#pragma omp for
for(j = 0; j < QARR_SZ; j++)
for(i = 0; i < PARR_SZ; i++)
{
subpixel(s, i, j, red_color);
addpixel(s, i, j, a[i + j*PARR_SZ] ? white_color : 0);
}
}
void seed_gamestate(Uint8 *a)
{
int i, j;
#pragma omp for
for(j=0; j < QARR_SZ; j++)
{
a[j*PARR_SZ] = lrand48() & 0x1;
a[PARR_SZ + j*PARR_SZ - 1] = lrand48() & 0x1;
}
#pragma omp for
for(i=0; i < PARR_SZ; i++)
{
a[i] = lrand48() & 0x1;
a[i + (QARR_SZ-1)*PARR_SZ] = lrand48() & 0x1;
}
}
void run_game(const Uint8 *restrict a, Uint8 *restrict b)
{
int i, j, o, t;
#pragma omp for
for(j=1; j < QARR_SZ-1; j++)
for(i=1; i < PARR_SZ-1; i++)
{
o = i + j*PARR_SZ;
// Edge cases are random noise, thus ignored
t = 0;
t += a[o - 1 - PARR_SZ];
t += a[o - PARR_SZ];
t += a[o + 1 - PARR_SZ];
t += a[o - 1];
t += a[o + 1];
t += a[o - 1 + PARR_SZ];
t += a[o + PARR_SZ];
t += a[o + 1 + PARR_SZ];
b[o] = a[o] * (t == 2 ? 1 : 0);
b[o] |= (t == 3) ? 1 : 0;
}
}
int main(int argc, char **argv)
{
SDL_Surface *s;
SDL_Event event;
int skip_count = 0;
Uint8 *a, *b;
srand48(0);
s = init_display();
SDL_FillRect(s, NULL, 0);
a = new_gamestate(PARR_SZ, QARR_SZ);
b = new_gamestate(PARR_SZ, QARR_SZ);
while(1)
{ SDL_PollEvent(&event);
if(event.type == SDL_KEYDOWN \
&& event.key.keysym.sym == SDLK_q) {
break;
}
#pragma omp parallel shared(a, b)
{
seed_gamestate(a);
run_game(a, b);
seed_gamestate(b);
run_game(b, a);
}
skip_count++;
if(skip_count % 2 == 0)
{
draw_state(s, a);
SDL_Flip(s);
skip_count = 0;
} else if(skip_count % 1 == 0) {
draw_state(s, b);
SDL_Flip(s);
}
}
free(a);
free(b);
return 0;
}