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
|
#include <World.h>
#include <cstdio>
World::World(void){
line=NULL;
lineCount=entCount=0;
toLeft=toRight=behind=infront=NULL;
}
World::World(const float width,World *l,World *r){
unsigned int i;
double f;
lineCount=width/HLINE+11;
if((line=(struct line_t *)calloc(lineCount,sizeof(struct line_t)))==NULL){
std::cout<<"Failed to allocate memory!"<<std::endl;
abort();
}
toLeft=l;
toRight=r;
behind=infront=NULL;
entCount=0;
if(toLeft){
if(toLeft->toRight){
std::cout<<"There's already a world to the left!"<<std::endl;
abort();
}else{
toLeft->toRight=this;
}
}
if(toRight){
if(toRight->toLeft){
std::cout<<"There's already a world to the right!"<<std::endl;
abort();
}else{
toRight->toLeft=this;
}
}
line[0].start=(grand()%100)/100.0f-0.8f; // lazy
if(line[0].start>-0.5f)line[0].start=-0.7f;
for(i=10;i<lineCount;i+=10){
line[i].start=((double)((grand()%50)+400))/1000.0f-1;
}
for(i=0;i<lineCount;i++){
if(!(i%10)||!i){
f=line[i+10].start-line[i].start;
f/=10.0f;
}else{
line[i].start=line[i-1].start+f;
}
}
}
static float drawOffsetX=0,
drawOffsetY=0;
void World::draw(void){
unsigned int i;
float x,y,hline=HLINE;
static World *root,*cur;
root=cur=this;
LOOP:
if(cur->behind){
drawOffsetX+=(cur->getWidth()-cur->behind->getWidth())/2;
drawOffsetY+=.3;
hline/=2;
cur=cur->behind;
goto LOOP;
//behind->draw();
}
LOOP2:
glBegin(GL_QUADS);
for(i=0;i<cur->lineCount-10;i++){
x=(hline*i)-1+drawOffsetX;
y=cur->line[i].start+drawOffsetY;
glColor3ub(0,200,0);
glVertex2f(x ,y);
glVertex2f(x+hline,y);
y-=hline*2;
glVertex2f(x+hline,y);
glVertex2f(x ,y);
glColor3ub(150,100,50);
glVertex2f(x ,y);
glVertex2f(x+hline,y);
glVertex2f(x+hline,-1);
glVertex2f(x ,-1);
}
glEnd();
if(root!=cur){
cur=cur->infront;
drawOffsetX-=(cur->getWidth()-cur->behind->getWidth())/2;
drawOffsetY-=.3;
hline*=2;
goto LOOP2;
}else{
drawOffsetX=drawOffsetY=0;
for(i=0;i<entCount;i++){
((Entity **)entity)[i]->draw();
}
}
}
void World::detect(vec2 *v,const float width){
unsigned int i;
// hey
// oh hai
for(i=0;i<lineCount-10;i++){
if(v->y<line[i].start){
if(v->x>(HLINE*i)-1&&v->x<(HLINE*i)-1+HLINE){
v->y=line[i].start;
return;
}else if(v->x+width>(HLINE*i)-1&&v->x+width<(HLINE*i)-1+HLINE){
v->y=line[i].start;
return;
}
}else if(v->y>line[i].start+HLINE/4){
v->y-=HLINE/8;
}
}
}
float World::getWidth(void){
return (lineCount-11)*HLINE;
}
void World::saveToFile(FILE *f,World *parent){
fwrite(&lineCount,sizeof(unsigned int) ,1 ,f);
fwrite(&line ,sizeof(struct line_t),lineCount,f);
if(toLeft!=NULL&&toLeft!=parent->toLeft){
toLeft->saveToFile(f,toLeft);
}
if(toRight!=NULL&&toRight!=parent->toRight){
toRight->saveToFile(f,toRight);
}
}
void World::loadFromFile(FILE *f,World *parent){
fread(&lineCount,sizeof(unsigned int) ,1 ,f);
line=(struct line_t *)malloc(lineCount*sizeof(struct line_t *));
fread(&line ,sizeof(struct line_t),lineCount,f);
if(toLeft!=NULL&&toLeft!=parent->toLeft){
toLeft->loadFromFile(f,toLeft);
}
std::cout<<toRight<<" "<<parent->toRight<<std::endl;
if(toRight!=NULL&&toRight!=parent->toRight){
puts("A");
toRight->loadFromFile(f,toRight);
}
}
void World::addLayer(const float width){
if(behind){
behind->addLayer(width);
}else{
behind=new World(width,NULL,NULL);
behind->infront=this;
}
}
void World::addEntity(void *e){
entity[entCount++]=e;
}
|