-
Notifications
You must be signed in to change notification settings - Fork 5
/
level.c
267 lines (255 loc) · 8.5 KB
/
level.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
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
#include "level.h"
#include <stdlib.h>
void add_to_string(char* buffer,char* add)
{
memcpy(&buffer[strlen(buffer)],add,strlen(add)+1);
}
char* create_level_string(char* buffer,int width,int height,int circles,int triangles,int quads)
{
int t = spRand()%TEXTURE_COUNT+1;
char temp[16];
buffer[0] = 0;
add_to_string(buffer,ltostr(t,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(width,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(height,temp,36));
int i;
for (i = 0; i < circles; i++)
{
int r = spRand()%(spMin(width,height) >> 3);
int x = LEVEL_BORDER+r+spRand()%(width-2*LEVEL_BORDER-2*r);
int y = LEVEL_BORDER+r+spRand()%(height-2*LEVEL_BORDER-2*r);
if (spRand()%5 == 0)
add_to_string(buffer,"-"); //negative
add_to_string(buffer,"*"); //circle
add_to_string(buffer,ltostr(x,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(y,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(r,temp,36));
}
for (i = 0; i < triangles; i++)
{
int r = spRand()%(spMin(width,height) >> 2);
int x = LEVEL_BORDER+r+spRand()%(width-2*LEVEL_BORDER-2*r);
int y = LEVEL_BORDER+r+spRand()%(height-2*LEVEL_BORDER-2*r);
int a1 = spRand()%(SP_PI*2);
int a2 = spRand()%(SP_PI*2);
int a3 = spRand()%(SP_PI*2);
Sint32 x1 = x + ( r * spCos( a1 ) >> SP_ACCURACY );
Sint32 y1 = y + ( r * spSin( a1 ) >> SP_ACCURACY );
Sint32 x2 = x + ( r * spCos( a2 ) >> SP_ACCURACY );
Sint32 y2 = y + ( r * spSin( a2 ) >> SP_ACCURACY );
Sint32 x3 = x + ( r * spCos( a3 ) >> SP_ACCURACY );
Sint32 y3 = y + ( r * spSin( a3 ) >> SP_ACCURACY );
if (spRand()%5 == 0)
add_to_string(buffer,"-"); //negative
add_to_string(buffer,"^"); //triangle
add_to_string(buffer,ltostr(x1,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(y1,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(x2,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(y2,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(x3,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(y3,temp,36));
}
for (i = 0; i < quads; i++)
{
int r = spRand()%(spMin(width,height) >> 2);
int angle = spRand()%(SP_PI*2);
int x = LEVEL_BORDER+r*3/2+spRand()%(width-2*LEVEL_BORDER-3*r); //sqrt(2) ~ 2/3
int y = LEVEL_BORDER+r*3/2+spRand()%(height-2*LEVEL_BORDER-3*r);//sqrt(2) ~ 2/3
Sint32 x1 = -r >> 1;
Sint32 x2 = x1;
Sint32 x3 = r >> 1;
Sint32 x4 = x3;
Sint32 y1 = -r >> 1;
Sint32 y2 = r >> 1;
Sint32 y3 = y2;
Sint32 y4 = y1;
Sint32 nx1 = x + ( x1 * spCos( angle ) - y1 * spSin( angle ) >> SP_ACCURACY );
Sint32 ny1 = y + ( y1 * spCos( angle ) + x1 * spSin( angle ) >> SP_ACCURACY );
Sint32 nx2 = x + ( x2 * spCos( angle ) - y2 * spSin( angle ) >> SP_ACCURACY );
Sint32 ny2 = y + ( y2 * spCos( angle ) + x2 * spSin( angle ) >> SP_ACCURACY );
Sint32 nx3 = x + ( x3 * spCos( angle ) - y3 * spSin( angle ) >> SP_ACCURACY );
Sint32 ny3 = y + ( y3 * spCos( angle ) + x3 * spSin( angle ) >> SP_ACCURACY );
Sint32 nx4 = x + ( x4 * spCos( angle ) - y4 * spSin( angle ) >> SP_ACCURACY );
Sint32 ny4 = y + ( y4 * spCos( angle ) + x4 * spSin( angle ) >> SP_ACCURACY );
if (spRand()%5 == 0)
add_to_string(buffer,"-"); //negative
add_to_string(buffer,"#"); //quad
add_to_string(buffer,ltostr(nx1,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(ny1,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(nx2,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(ny2,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(nx3,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(ny3,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(nx4,temp,36));
add_to_string(buffer," ");
add_to_string(buffer,ltostr(ny4,temp,36));
}
return buffer;
}
char* ltostr(unsigned int l,char* buffer,int base)
{
char temp[16];
temp[15] = 0;
int pos = 15;
while (l > 0 && pos > 0)
{
pos--;
int rest = l % base;
l = l / base;
if (rest >= 0 && rest <= 9)
temp[pos] = rest+'0';
else
temp[pos] = rest-10+'a';
}
memcpy(buffer,&temp[pos],strlen(&temp[pos])+1);
return buffer;
}
SDL_Surface* create_level(char* level_string,int alt_width,int alt_height,int color)
{
printf("Parse level: %s\n",level_string);
//Lets overread the texture...
char* mom = level_string;
strtol(mom,&mom,36);
//Reading the width
int width = strtol(mom,&mom,36);
//Reading the height
int height = strtol(mom,&mom,36);
//However we need to ignore this
width = LEVEL_WIDTH;
height = LEVEL_HEIGHT;
Sint32 zoom = SP_ONE;
int border_shift = 0;
if (alt_width > 0 && alt_height > 0)
{
Sint32 xZoom = alt_width*SP_ONE / (width-2*LEVEL_BORDER);
Sint32 yZoom = alt_height*SP_ONE / (height-2*LEVEL_BORDER);
if (xZoom < yZoom)
zoom = xZoom;
else
zoom = yZoom;
width = (width-2*LEVEL_BORDER)*zoom >> SP_ACCURACY;
height = (height-2*LEVEL_BORDER)*zoom >> SP_ACCURACY;
border_shift = LEVEL_BORDER*zoom;
}
printf("Zoom %i, Width %i, Height %i\n",zoom,width,height);
SDL_Surface* level = spCreateSurface(width,height);
spSelectRenderTarget(level);
spClearTarget(SP_ALPHA_COLOR);
spSetAlphaTest(0);
int negative = 0;
while (mom[0] != 0)
{
Sint32 x1,y1,x2,y2,x3,y3,x4,y4;
//Reading the kind
switch (mom[0])
{
case '-':
mom++;
negative = 1;
break;
case '*': //circle
mom++;
x1 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y1 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
x2 = strtol(mom,&mom,36)*zoom >> SP_ACCURACY;
spEllipse(x1,y1,0,x2,x2,negative?SP_ALPHA_COLOR:color);
negative = 0;
break;
case '^': //triangle
mom++;
x1 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y1 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
x2 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y2 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
x3 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y3 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
spTriangle(x1,y1,0,x2,y2,0,x3,y3,0,negative?SP_ALPHA_COLOR:color);
negative = 0;
break;
case '#': //quads
mom++;
x1 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y1 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
x2 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y2 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
x3 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y3 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
x4 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
y4 = strtol(mom,&mom,36)*zoom - border_shift >> SP_ACCURACY;
spQuad(x1,y1,0,x2,y2,0,x3,y3,0,x4,y4,0,negative?SP_ALPHA_COLOR:color);
negative = 0;
break;
default:
mom++;
negative = 0;
}
}
if (alt_width <= 0 || alt_height <= 0)
spRectangleBorder(width/2,height/2,0,width,height,LEVEL_BORDER,LEVEL_BORDER,SP_ALPHA_COLOR);
spSetAlphaTest(1);
spSelectRenderTarget(spGetWindowSurface());
return level;
}
Uint16 level_color = 0;
Uint16 get_level_color()
{
return level_color;
}
Uint16 border_color = 0;
Uint16 get_border_color()
{
return border_color;
}
void texturize_level(SDL_Surface* level,char* level_string)
{
int t = strtol(level_string,NULL,36);
char buffer[256];
sprintf(buffer,"./textures/texture%i.png",(t-1)%TEXTURE_COUNT+1);
SDL_Surface* texture = spLoadSurface(buffer);
spSelectRenderTarget(level);
Uint16* level_pixel = spGetTargetPixel();
SDL_LockSurface(texture);
int texture_width = texture->pitch/texture->format->BytesPerPixel;
Uint16* texture_pixel = (Uint16*)texture->pixels;
int x,y;
int r=0,g=0,b=0;
int c = 0;
for (x = 0; x < texture->w; x+=2)
for (y = 0; y < texture->h; y+=2)
{
r += spGetRawRFromColor( texture_pixel[x+y*texture_width] );
g += spGetRawGFromColor( texture_pixel[x+y*texture_width] );
b += spGetRawBFromColor( texture_pixel[x+y*texture_width] );
c++;
}
r /= c;
g /= c;
b /= c;
level_color = (r<<11) | (g<<5) | b;
Sint32 h = spGetHFromColor(level_color);
Sint32 s = spGetSFromColor(level_color);
Sint32 v = spGetVFromColor(level_color);
border_color = spGetHSV(h,spMin(255,s*3/2),spMin(255,v*3/2));
for (x = 0; x < level->w; x++)
for (y = 0; y < level->h; y++)
if (level_pixel[x+y*level->w]!= SP_ALPHA_COLOR)
level_pixel[x+y*level->w] = texture_pixel[(x & TEXTURE_MASK) + (y & TEXTURE_MASK)*texture_width];
SDL_UnlockSurface(texture);
spSelectRenderTarget(spGetWindowSurface());
spDeleteSurface(texture);
}