neingeist
/
cscape
Archived
1
0
Fork 0

initial CVS import

master
neingeist 22 years ago
commit e4d8ca2d45

@ -0,0 +1,11 @@
LDFLAGS=`sdl-config --libs`
CFLAGS=-g `sdl-config --cflags`
all: cscape maptest
cscape: map.o mapcolors.o sdl.o
maptest: map.o mapcolors.o sdl.o
clean:
rm -f *.o maptest cscape

@ -0,0 +1,113 @@
#include "math.h"
#include "SDL.h"
#include "sdl.h"
#define MEERESSPIEGEL 48
#define BLICKWEITE 55
#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++)
sintab[x] = trunc(127 * sin(x*M_PI/180));
for (x=0; x < 90; x++)
costab[x] = sintab[x+90];
for (x=90; x < 360; x++)
costab[x] = -sintab[x-90];
}
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);
sleep(30); SDL_Quit(); exit(0);
}
/*
int posx = 0;
int posy = 0;
int richtung = 0;
int flughoehe;
int iy, ix, iy1, iyp, ixp;
int x, y, z, s, i, j;
int map_color;
int rng[] = new int[320];
while (true) {
// Schnarch...
try {Thread.sleep(100);} catch (InterruptedException e) {}
/* Richtung wechseln? */
/* if (java.lang.Math.random()<0.5)
if (java.lang.Math.random()<1.0)
if (richtung <= 0) richtung = 357;
else richtung -= 3;
else
if (richtung >= 357) richtung = 0;
else richtung += 3;
*/
/* Vorwärts? */
/* posy = posy + costab[richtung] / 32;
posx = posx + sintab[richtung] / 32;
if (posy<=0) posy += 256;
if (posx<=0) posx += 256;
*/
/* Absolute Höhe berechnen */
/* flughoehe = 100;
*/
/*
for (i=0;i<320;i++) rng[i] = 200;
System.arraycopy(emptyscreen,0,screen,0,320*200);
for (iy=posy; iy<=posy+blickweite; iy++) {
iy1 = 2 * (iy-posy) + 1;
s = 4 + 300 / iy1;
for (ix=posx-(iy-posy); ix<=posx+(iy-posy); ix++) {
ixp = posx + ((ix-posx) * costab[richtung] + (iy-posy) * sintab[richtung])
/ 128;
iyp = posy + ((iy-posy) * costab[richtung] - (ix-posx) * sintab[ri
chtung]) / 128;
x = 160 + 360*(ix-posx) / iy1;
if ((x>=0) && (x+s<=318) && ((256*iyp+ixp) > 0) && ((256*iyp+ixp)
< 256*256) ) {
map_color = map[256*iyp+ixp];
z = map[256*iyp+ixp];
if (z < meeresspiegel) z = meeresspiegel;
y = 100 + 30 * (flughoehe-z) / iy1;
if ((y>=0) && (y<=199)) {
for (j=x;j<=x+s;j++)
for (i=rng[j];i>=y;i--) {
if ((320*i+j>=0) && (320*i+j<320*200))
screen[320*i+j] = (byte)map_color;
if (y<rng[j]) rng[j] = y;
}
*/

49
map.c

@ -0,0 +1,49 @@
#include <stdlib.h>
void map_init (char *map, int x, int y) {
int i;
for (i=0;i<x*y;i++) {
map[i]=0;
}
}
char map_ncol (int mc, int n, int dvd);
void map_generate (char *map, int x1,int y1,int x2,int y2) {
int xn,yn,dxy,p1,p2,p3,p4;
if ((x2-x1<2) && (y2-y1<2)) return;
p1 = map[x1*256+y1];
p2 = map[x1*256+y2];
p3 = map[x2*256+y1];
p4 = map[x2*256+y2];
xn = (x2+x1)/2;
yn = (y2+y1)/2;
dxy = 5*(x2-x1+y2-y1)/3;
if (map[xn*256+y1] <= 0) map[xn*256+y1] = map_ncol(p1+p3,dxy,2);
if (map[x1*256+yn] <= 0) map[x1*256+yn] = map_ncol(p1+p2,dxy,2);
if (map[x2*256+yn] <= 0) map[x2*256+yn] = map_ncol(p3+p4,dxy,2);
if (map[xn*256+y2] <= 0) map[xn*256+y2] = map_ncol(p2+p4,dxy,2);
map[xn*256+yn] = map_ncol(p1+p2+p3+p4,dxy,4);
map_generate(map, x1,y1,xn,yn);
map_generate(map, xn,y1,x2,yn);
map_generate(map, x1,yn,xn,y2);
map_generate(map, xn,yn,x2,y2);
}
char map_ncol (int mc, int n, int dvd) {
char loc = 0;
loc = (mc+n-(int) (2.0*n*rand()/(RAND_MAX+1.0)) )/dvd;
if (loc < 5) loc = 5;
return loc;
}

@ -0,0 +1,32 @@
#include "SDL.h"
char farben[] =
{ 0, 0, 0,48,48,48, 1, 0,43, 1, 3,43, 2, 5,44, 2, 7,44, 3, 9,45, 4,11,46,
5,13,47, 6,15,48, 7,17,49, 8,19,50, 9,21,51,10,22,52,11,24,52,12,26,54,
13,28,54,14,30,56,15,32,56,16,34,58,17,34,58,17,36,58,18,38,60,19,40,60,
20,42,62,21,44,62,10,31, 0,11,31, 0,11,31, 1,11,32, 1,12,32, 1,12,32, 2,
12,33, 2,13,33, 2,14,33, 3,15,33, 3,15,34, 3,15,34, 4,15,35, 4,16,35, 4,
16,35, 5,16,36, 5,17,36, 5,17,36, 6,18,37, 6,18,38, 7,19,38, 8,20,39, 8,
20,40, 9,21,40,10,22,41,10,22,42,11,23,42,12,24,43,12,24,44,13,25,44,14,
25,45,14,26,46,15,27,46,16,27,47,17,28,47,18,28,48,19,29,49,19,30,49,20,
30,50,21,31,51,21,32,51,22,32,52,23,33,53,23,34,53,24,34,54,25,35,55,25,
36,55,26,36,56,27,37,57,27,38,57,27,39,57,27,41,57,27,42,57,27,43,57,27,
44,57,27,45,57,27,46,57,27,47,57,27,49,57,27,50,57,27,51,57,27,52,57,27,
53,57,27,55,57,27,56,57,27,57,57,27,58,57,27,58,57,26,58,57,25,58,57,24,
58,56,23,58,55,22,58,54,20,58,53,19,58,51,18,58,50,17,58,50,16,58,49,15,
58,48,14,58,47,13,58,46,12,58,45,11,58,44,11,58,44,10,58,43,10,58,42, 9,
57,41, 8,57,40, 8,56,39, 7,56,38, 6,55,37, 5,55,35, 4,54,33, 4,54,31, 2,
32,32,32,63,63,63,63,63,63,63,63,63,63,63,63,48,48,48,63,63,63,63,63,63 };
void map_setpalette(SDL_Surface *screen) {
SDL_Color colors[128];
int i;
for(i=0;i<=127;i++) {
colors[i].r=farben[i*3+0]<<2;
colors[i].g=farben[i*3+1]<<2;
colors[i].b=farben[i*3+2]<<2;
}
SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 128);
}

@ -0,0 +1,40 @@
#include <stdlib.h>
#include "SDL.h"
#include "sdl.h"
#define MAPSIZE 256
void map_show(SDL_Surface *screen, char *map, int mapsize) {
int i,j;
/* Lock the screen for direct access to the pixels */
if(SDL_MUSTLOCK(screen))
if(SDL_LockSurface(screen) < 0) {
fprintf(stderr, "Can't lock screen: %s\n",
SDL_GetError());
return;
}
for(i=0;i<mapsize;i++)
for(j=0;j<mapsize;j++)
sdl_putpixel(screen, i, j, map[i*mapsize+j]);
if(SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
SDL_UpdateRect(screen, 0, 0, mapsize-1, mapsize-1);
}
int main(void) {
char map[MAPSIZE*MAPSIZE];
SDL_Surface *screen;
map_init(map,MAPSIZE,MAPSIZE);
map_generate(map,0,0,MAPSIZE-1,MAPSIZE-1);
screen = sdl_init();
map_setpalette(screen);
map_show(screen, map, MAPSIZE);
sleep(30);
SDL_Quit(); exit(0);
}

47
sdl.c

@ -0,0 +1,47 @@
#include <SDL.h>
SDL_Surface* sdl_init() {
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if(screen == NULL) {
fprintf(stderr, "Unable to set video mode: %s\n",
SDL_GetError());
exit(1);
}
return screen;
}
void sdl_putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}

@ -0,0 +1,2 @@
SDL_Surface* sdl_init();
void sdl_putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel);