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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
/* Roaming-blobs-on-mars-collect-some-dust-on-a-tropical-island-and-go-pearl-
diving-before-population-goes-out-of-control. */
/* Each frame, a background virtual screen is copied to a virtual screen; */
/* sprites (well, pixels) are written on that virtual screen; and the */
/* virtual screen is copied to video memory. The background is updated as */
/* appropriate. This simple animation technique works well for 320x200 */
/* because it's so small. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <vga.h>
#include <vgagl.h>
/* This can be changed into any 256 color mode. */
/* For planar 256 color modes, enable page flipping. */
/* Even 16 color modes work (ugly colors). */
#define VGAMODE G800x600x256
#define USE_PAGEFLIPPING
/* #define USE_SMALLOC */
/* This is the size of the animated window. */
#define MAPWIDTH 800
#define MAPHEIGHT 560
#define MAXMOVERS 2000
#define MAXCITIES 1000
#define NUMBEROFCITIES 20
#define NUMBEROFMOVERS 1400
#define MOVERTHRESHOLD 1400
#define MOVERLIFETIME 1000
#define COLORTIME 2000
#define randomn( n ) (random() % n)
#define red(x) (32 + x)
#define green(x) (64 + x)
#define yellow(x) (96 + x)
#define blue(x) (128 + x)
#define magenta(x) (160 + x)
#define cyan(x) (192 + x)
#define white(x) (224 + x)
/* Data types */
typedef struct {
int x;
int y;
} Position;
#define STAT_ACTIVE 1
typedef struct {
int stat;
int x;
int y;
int vx;
int vy;
int color;
int time;
} Mover;
typedef struct {
int x;
int y;
int pop;
int hit;
} City;
/* Global variables */
int map[MAPWIDTH][MAPHEIGHT];
/* Map encoding i: */
/* (0 - 0xffff Mover number i) */
/* 0x10000... Part of city (i - 0x10000) */
Mover mover[MAXMOVERS];
int nu_movers = 0;
City city[MAXCITIES];
int nu_cities = 0;
int mytime = 0; /* used to be "time" but collids w/libc function time() */
int pop = 0;
int framerate, framecount, frameclock;
GraphicsContext *physicalscreen;
GraphicsContext *backscreen;
GraphicsContext *background;
void error(char *s)
{
printf("%s\n", s);
vga_setmode(TEXT);
exit(0);
}
void setcustompalette(void)
{
/* colors 0-31 are a RGB mix (bits 0 and 1 red, 2 green, 3 and 4 blue) */
/* 32-63 black to red */
/* 64-95 black to green */
/* 96-127 black to yellow */
/* 128-159 black to blue */
/* 160-191 black to magenta */
/* 192-223 black to cyan */
/* 224-255 black to white */
Palette pal;
int i;
for (i = 0; i < 256; i++) {
int r, g, b;
r = g = b = 0;
if ((i & 32) > 0)
r = (i & 31) << 1;
if ((i & 64) > 0)
g = (i & 31) << 1;
if ((i & 128) > 0)
b = (i & 31) << 1;
if (i < 32) {
r = (i & 3) << 4; /* 2 bits */
g = (i & 4) << 3; /* 1 bit */
b = (i & 24) << 1; /* 2 bits */
}
pal.color[i].red = r;
pal.color[i].green = g;
pal.color[i].blue = b;
}
gl_setpalette(&pal);
}
void initfont(void)
{
void *font;
#ifdef USE_SMALLOC
font = smalloc(256 * 8 * 8 * BYTESPERPIXEL);
#else
font = malloc(256 * 8 * 8 * BYTESPERPIXEL);
#endif
gl_expandfont(8, 8, white(24), gl_font8x8, font);
gl_setfont(8, 8, font);
}
int fsize(FILE * f)
{
int oldpos, size;
oldpos = ftell(f);
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, oldpos, SEEK_SET);
return size;
}
void loadfile(char **buf, char *fname)
{
FILE *f;
int size;
f = fopen(fname, "rb");
size = fsize(f);
*buf = malloc(size);
fread(*buf, 1, size, f);
fclose(f);
}
/* Map */
void clearmap(void)
{
int x, y;
for (y = 0; y < MAPHEIGHT; y++)
for (x = 0; x < MAPWIDTH; x++)
map[x][y] = 0;
}
Position
findfreeposition(void)
{
int x, y;
Position p;
do {
x = randomn(MAPWIDTH);
y = randomn(MAPHEIGHT);
}
while (map[x][y] != 0);
p.x = x;
p.y = y;
return p;
}
/* Movers */
void initmovers(void)
{
int i;
for (i = 0; i < MAXMOVERS; i++)
mover[i].stat = 0;
}
int findfreemoverslot(void)
{
int i;
for (i = 0; i < MAXMOVERS; i++)
if (!(mover[i].stat & STAT_ACTIVE))
return i;
error("Mover table overflow");
return 0;
}
void addrandommover(void)
{
Position p = findfreeposition();
int i = findfreemoverslot();
int c;
mover[i].x = p.x;
mover[i].y = p.y;
do {
mover[i].vx = randomn(3) - 1;
mover[i].vy = randomn(3) - 1;
}
while (mover[i].vx == 0 && mover[i].vy == 0);
mover[i].stat = STAT_ACTIVE;
switch (randomn(4)) {
case 0:
c = blue(20);
break;
case 1:
c = green(20);
break;
case 2:
c = magenta(20);
break;
default:
c = cyan(20);
break;
}
mover[i].time = 0;
mover[i].color = c;
nu_movers++;
}
void killmover(int i)
{
mover[i].stat = 0;
nu_movers--;
}
void drawmover(int i)
{
gl_setpixel(mover[i].x, mover[i].y, mover[i].color);
}
/* Cities */
void initcities(void)
{
nu_cities = 0;
}
void addcity(int x, int y)
{
int i = nu_cities++;
map[x][y] = i + 0x10000;
city[i].x = x;
city[i].y = y;
city[i].pop = 1;
city[i].hit = 0;
}
int cityat(int x, int y)
{
if (map[x][y] >= 0x10000)
return map[x][y] - 0x10000;
else
return -1;
}
int citycolor(void)
{
static int colortable[5] =
{yellow(31), blue(31), white(31), green(31), cyan(31)};
return colortable[(mytime / COLORTIME) % 5]
- (mytime % COLORTIME) * 25 / COLORTIME;
}
void growcity(int cx, int cy, int x, int y, int ct)
{
/* add city unit at (x, y) adjacent to city unit (cx, cy) */
int c;
map[x][y] = ct + 0x10000;
c = citycolor();
gl_setpixel(x, y, c);
city[ct].pop++;
city[ct].hit = 20;
pop++;
}
/* Main components */
void createbackground(void)
{
/* Create fancy dark red background */
int x, y;
for (y = 0; y < MAPHEIGHT; y++)
for (x = 0; x < MAPWIDTH; x++) {
int i = 0;
int n = 0;
int c;
if (x > 0) {
i += gl_getpixel(x - 1, y) - red(0);
n++;
}
if (y > 0) {
i += gl_getpixel(x, y - 1) - red(0);
n++;
}
c = (i + randomn(16)) / (n + 1);
if (c > 9)
c = 9;
gl_setpixel(x, y, red(0) + c);
}
}
void drawbackground(void)
{
/* Build up background from map data */
int x, y;
gl_setcontext(background);
gl_clearscreen(0);
createbackground();
for (y = 0; y < MAPHEIGHT; y++)
for (x = 0; x < MAPWIDTH; x++) {
int c = cityat(x, y);
if (c != -1)
gl_setpixel(x, y, citycolor());
}
}
void createcities(void)
{
int i;
for (i = 0; i < NUMBEROFCITIES; i++) {
Position p = findfreeposition();
addcity(p.x, p.y);
}
}
void writestat(void)
{
char s[41];
int i, x, y;
int maxpopcity, maxpop;
sprintf(s, "Pop %7d Time %7d Rate %5d.%d", pop, mytime,
framerate / 10, framerate % 10);
gl_setwritemode(WRITEMODE_OVERWRITE);
gl_write(0, HEIGHT - 8, s);
maxpop = -1;
maxpopcity = 0;
for (i = 0; i < nu_cities; i++)
if (city[i].pop > maxpop) {
maxpop = city[i].pop;
maxpopcity = i;
}
gl_enableclipping();
gl_circle(city[maxpopcity].x, city[maxpopcity].y, 10,
blue(31));
gl_disableclipping();
gl_setwritemode(WRITEMODE_MASKED);
x = city[maxpopcity].x;
y = city[maxpopcity].y;
sprintf(s, "%d", maxpop);
/* clipping */
if (x + strlen(s) * 8 > MAPWIDTH)
x = MAPWIDTH - strlen(s) * 8;
if (y + 8 > MAPHEIGHT)
y = MAPHEIGHT - 8;
gl_write(x, y, s);
}
void drawscreen(void)
{
int i;
/* Copy background to backscreen. */
gl_setcontext(background);
gl_copyscreen(backscreen);
/* Now draw the objects in backscreen. */
gl_setcontext(backscreen);
for (i = 0; i < MAXMOVERS; i++)
if (mover[i].stat & STAT_ACTIVE) {
drawmover(i);
}
writestat();
/* Copy backscreen to physical screen. */
gl_copyscreen(physicalscreen);
}
void move(void)
{
int i;
gl_setcontext(background);
for (i = 0; i < MAXMOVERS; i++)
if (mover[i].stat & STAT_ACTIVE) {
int nx, ny;
int c;
if (++mover[i].time == MOVERLIFETIME) {
killmover(i);
continue;
}
for (;;) {
nx = mover[i].x + mover[i].vx;
ny = mover[i].y + mover[i].vy;
if (nx < 0 || nx >= MAPWIDTH) {
mover[i].vx = -mover[i].vx;
continue;
}
if (ny < 0 || ny >= MAPHEIGHT) {
mover[i].vy = -mover[i].vy;
continue;
}
break;
}
c = cityat(nx, ny);
if (c != -1) { /* found city */
killmover(i);
growcity(nx, ny, mover[i].x, mover[i].y, c);
continue; /* next mover */
}
mover[i].x = nx;
mover[i].y = ny;
}
if (pop >= MAPWIDTH * MAPHEIGHT * 255 / 256) {
/* start all over again */
printf("fun: new run.\n");
pop = 0;
mytime = 0;
clearmap();
initcities();
createcities();
drawbackground();
}
}
void createmovers(void)
{
int i;
for (i = 0; i < NUMBEROFMOVERS; i++)
addrandommover();
}
int main(void)
{
vga_init();
clearmap();
initmovers();
createcities();
createmovers();
vga_setmode(VGAMODE);
gl_setcontextvga(VGAMODE);
physicalscreen = gl_allocatecontext();
gl_getcontext(physicalscreen);
#ifdef USE_PAGEFLIPPING
/* Try to enable page flipping. */
gl_enablepageflipping(physicalscreen);
#endif
setcustompalette();
/* initfont() here caused trouble with planar 256 color modes. */
gl_setcontextvgavirtual(VGAMODE);
backscreen = gl_allocatecontext();
gl_getcontext(backscreen);
#ifdef USE_SMALLOC
free(backscreen->vbuf);
backscreen->vbuf = smalloc(BYTEWIDTH * HEIGHT);
gl_setcontext(backscreen);
#endif
initfont();
gl_setcontextvgavirtual(VGAMODE);
background = gl_allocatecontext();
gl_getcontext(background);
#ifdef USE_SMALLOC
free(background->vbuf);
background->vbuf = smalloc(BYTEWIDTH * HEIGHT);
gl_setcontext(background);
#endif
drawbackground();
framerate = 0;
framecount = 0;
frameclock = clock();
for (;;) {
int i;
drawscreen();
move();
for (i = 0; i < 4; i++)
if (nu_movers < MOVERTHRESHOLD)
addrandommover();
mytime++;
/* Update frame rate every 3 seconds. */
framecount++;
if (clock() - frameclock >= CLOCKS_PER_SEC) {
framerate = framecount * CLOCKS_PER_SEC / (clock() - frameclock);
framecount = 0;
frameclock = clock();
}
}
#ifndef USE_SMALLOC
gl_freecontext(backscreen);
gl_freecontext(background);
#endif
vga_setmode(TEXT);
exit(0);
}
|