-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.c
140 lines (124 loc) · 3.88 KB
/
world.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GLUT/glut.h>
#include <unistd.h>
#include <time.h>
#include "cells.c"
#include <limits.h>
int world_width = 200;
int world_height = 200;
int screen_width = 1000;
int screen_height = 1000;
cell ***cellArray;
int world_x_origin = 0;
int world_y_origin = 0;
int cell_size = 20;
unsigned long int update = 562826243;
// 562826243 interesting landscape
// 58969546 perpetual crying
// 862062994 pouring sand
cell *newCell(int x, int y, int state){
cell *myCell = malloc(sizeof(cell));
myCell->x = x;
myCell->y = y;
myCell->state = state;
myCell->nextState = state;
return myCell;
}
void drawCell(cell *c, int cell_size, int x_origin, int y_origin){
int x_ind = c->x;
int y_ind = c->y;
c->state = c->nextState;
if(c->state == 1){
// glColor3ub(rand()%255, rand()%255, rand()%255 );
glColor3ub(34, 102, 0);
// glColor3ub(0, 0, 0);
}
else{
// glColor3ub(255, 255, 255);
glColor3ub(133, 150, 235);
}
glBegin(GL_POLYGON);
glVertex3f(x_origin + x_ind * cell_size, y_origin + y_ind * cell_size, 0);
glVertex3f(x_origin + (x_ind + 1) * cell_size, y_origin + y_ind * cell_size, 0);
glVertex3f(x_origin + (x_ind + 1) * cell_size, y_origin + (y_ind + 1) * cell_size,0);
glVertex3f(x_origin + x_ind * cell_size, y_origin + (y_ind + 1) * cell_size, 0);
glEnd();
}
void reInitializeCells(){
for(int i=0; i<world_width; i++){
for(int j=0; j<world_height; j++){
cellArray[i][j] = newCell(i,j,rand()%2);
}
}
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
usleep(50000);
for(int i=0; i<world_width;i++){
for(int j=0; j<world_height; j++){
updateCell(cellArray[i][j]
, cellArray[(i+1)%world_width][j]
, cellArray[i][(j+1)%world_height]
, cellArray[(i+world_width-1)%world_width][j]
, cellArray[i][(j+world_height-1)%world_height]
, update);
}
}
for(int i=0; i<world_width;i++){
for(int j=0; j<world_height; j++){
drawCell(cellArray[i][j], cell_size, world_x_origin, world_y_origin);
}
}
glFlush();
}
void keyPressed (unsigned char key, int x, int y) {
if(key == 'q'){
printf("dawnaballz\n");
exit(0);
}
if(key == 'u'){
update = rand()%ULONG_MAX;
printf("%lu\n",update);
reInitializeCells();
}
}
void onClick(int button, int state, int x, int y){
int closest_x_ind = (int)(x * world_width / screen_width);
int closest_y_ind = world_height-(int)(y * world_height / screen_height);
if(state == GLUT_DOWN){
for(int i=closest_x_ind-10; i<closest_x_ind+10;i++){
for(int j=closest_y_ind-10;j<closest_y_ind+5; j++){
if(rand()%7 >= 1){
cellArray[(i+world_width)%world_width][(j+world_height)%world_height]->state = 1;
}
}
}
}
}
int main(int argc, char *argv[])
{
cellArray = malloc(world_width*world_height*sizeof(cell));
for(int i=0; i<world_width; i++){
cellArray[i] = malloc(world_height*sizeof(cell));
for(int j=0; j<world_height; j++){
cellArray[i][j] = newCell(i,j,rand()%2);
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screen_width, screen_height * 2 * world_height / world_width);
glutInitWindowPosition(200, 0);
glutCreateWindow("Dawna Linsdell");
glClearColor(1.0, 1.0, 1.0, 1.0);
glOrtho(0.f, world_width * cell_size, 0.f, world_height * cell_size, 0.f, 2.f);
glViewport(0, 0, world_width, world_height);
glutKeyboardFunc(keyPressed);
glutDisplayFunc(display);
glutMouseFunc(onClick);
glClear(GL_COLOR_BUFFER_BIT);
glutIdleFunc(display);
glutMainLoop();
return 0;
}