Archived
1
0
Fork 0
This repository has been archived on 2019-12-21. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
cscape/cscape.c

135 lines
2.8 KiB
C
Raw Normal View History

2002-05-20 12:40:35 +00:00
/* $Revision$ */
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
2002-05-20 11:07:54 +00:00
#include "SDL.h"
#include "sdl.h"
2002-05-20 12:40:35 +00:00
#include "map.h"
2002-05-20 11:07:54 +00:00
#define MEERESSPIEGEL 48
#define BLICKWEITE 55
2002-05-20 16:45:17 +00:00
#define FLUGHOEHE 100
2002-05-20 11:07:54 +00:00
#define MAPSIZE 256
int sintab[360];
int costab[360];
char map[MAPSIZE*MAPSIZE];
SDL_Surface *screen;
void init_tabs() {
int x;
for (x=0;x<360; x++)
2002-05-20 12:40:35 +00:00
sintab[x] = (int) (127.0 * sin(x*M_PI/180));
2002-05-20 11:07:54 +00:00
for (x=0; x < 90; x++)
costab[x] = sintab[x+90];
for (x=90; x < 360; x++)
costab[x] = -sintab[x-90];
}
2002-05-20 16:07:37 +00:00
void fly(SDL_Surface *screen) {
static int
posx = 0,
posy = 0,
richtung = 0,
flughoehe = 0;
int iy, ix, iy1, iyp, ixp;
int x, y, z, s, i, j;
int map_color;
2002-05-20 16:45:17 +00:00
int rng[WIDTH];
2002-05-20 16:07:37 +00:00
if(SDL_MUSTLOCK(screen))
if(SDL_LockSurface(screen) < 0) {
fprintf(stderr, "Can't lock screen: %s\n",
SDL_GetError());
return;
}
/* Richtung wechseln? */
2002-05-20 16:45:17 +00:00
if (rand()<RAND_MAX/2) {
if (rand()<RAND_MAX) {
if (richtung <= 0) { richtung = 357; }
else { richtung -= 3; }
} else {
if (richtung >= 357) { richtung = 0; }
else { richtung += 3; }
}
}
2002-05-20 16:07:37 +00:00
/* Bewegen ... */
posy = posy + costab[richtung] / 32;
posx = posx + sintab[richtung] / 32;
2002-05-20 16:45:17 +00:00
posy %= MAPSIZE; if(posy<0) posy+=MAPSIZE;
posx %= MAPSIZE; if(posx<0) posx+=MAPSIZE;
2002-05-20 16:07:37 +00:00
/* Absolute H<>he berechnen */
2002-05-20 16:45:17 +00:00
flughoehe = FLUGHOEHE + map[posy*MAPSIZE+posx]/2;
2002-05-20 16:07:37 +00:00
/* FIXME */
2002-05-20 16:45:17 +00:00
for (i=0;i<WIDTH;i++) rng[i] = HEIGHT;
2002-05-20 16:07:37 +00:00
SDL_FillRect(screen, NULL, 0);
for (iy=posy; iy<=posy+BLICKWEITE; iy++) {
iy1 = 2 * (iy-posy) + 1;
2002-05-20 16:45:17 +00:00
s = 4 + (WIDTH-20) / iy1;
2002-05-20 16:07:37 +00:00
for (ix=posx-(iy-posy); ix<=posx+(iy-posy); ix++) {
ixp = posx
+ ((ix-posx) * costab[richtung]
2002-05-20 16:45:17 +00:00
+ (iy-posy) * sintab[richtung]) /(MAPSIZE/2);
2002-05-20 16:07:37 +00:00
iyp = posy
+ ((iy-posy) * costab[richtung]
2002-05-20 16:45:17 +00:00
- (ix-posx) * sintab[richtung]) /(MAPSIZE/2);
2002-05-20 16:07:37 +00:00
2002-05-20 16:45:17 +00:00
x = WIDTH/2 + 360*(ix-posx) / iy1;
2002-05-20 16:07:37 +00:00
if(
2002-05-20 16:45:17 +00:00
(x>=0) && (x+s<=WIDTH-2)
&& ((MAPSIZE*iyp+ixp) > 0)
&& ((MAPSIZE*iyp+ixp) < MAPSIZE*MAPSIZE)
2002-05-20 16:07:37 +00:00
) {
2002-05-20 16:45:17 +00:00
map_color = map[MAPSIZE*iyp+ixp];
z = map[MAPSIZE*iyp+ixp];
2002-05-20 16:07:37 +00:00
if(z<MEERESSPIEGEL) z = MEERESSPIEGEL;
2002-05-20 16:45:17 +00:00
y = HEIGHT/2 + 30 * (flughoehe-z) / iy1;
2002-05-20 16:07:37 +00:00
2002-05-20 16:45:17 +00:00
if ((y>=0) && (y<=HEIGHT-1)) {
2002-05-20 16:07:37 +00:00
for (j=x;j<=x+s;j++)
for (i=rng[j];i>=y;i--) {
2002-05-20 16:45:17 +00:00
if ((WIDTH*i+j>=0) && (WIDTH*i+j<WIDTH*HEIGHT))
2002-05-20 16:07:37 +00:00
sdl_putpixel(screen, j, i, map_color);
if (y<rng[j]) rng[j] = y;
}}
}
}
}
if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
2002-05-20 16:45:17 +00:00
SDL_UpdateRect(screen, 0, 0, WIDTH, HEIGHT);
2002-05-20 16:07:37 +00:00
usleep(1000/25);
}
2002-05-20 11:07:54 +00:00
int main(void) {
init_tabs();
map_init(map,MAPSIZE,MAPSIZE);
map_generate(map,0,0,MAPSIZE-1,MAPSIZE-1);
screen = sdl_init();
map_setpalette(screen);
2002-05-20 16:07:37 +00:00
while(1) {
fly(screen);
}
2002-05-20 11:07:54 +00:00
sleep(30); SDL_Quit(); exit(0);
}