Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <math.h>
- #include <stdio.h>
- #include <SDL.h>
- int bsize = 2;
- int scw = 1280;
- int sch = 1024;
- int mapa = 32;
- int mapb = 32;
- int mapc = 32;
- void filldata (int *hei, int *col)
- {
- int idx;
- int x,y,z;
- for (y=0; y<mapb; y++)
- {
- for (x=0; x<mapa; x++)
- {
- int val;
- for (z=0; z<mapc; z++)
- {
- idx = y*mapa*mapb + x*mapa + z;
- double d = sqrt((x-10)*(x-10) + (y-10)*(y-10));
- int h = 30 - 3*d;
- if ((h>0)&&(z<h)) val = z;
- else val = -1;
- hei[idx] = val;
- col[idx] = 3*sqrt(x*x+y*y+z*z);
- }
- }
- }
- }
- void fillpalette (SDL_Surface *screen)
- {
- SDL_Color colors[256];
- int i;
- /* Fill colors with color information */
- for(i=0;i<=256;i++)
- {
- colors[i].r=i;
- colors[i].g=i/3;
- colors[i].b=i/3;
- }
- SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
- }
- void printdata (int *hei)
- {
- int x,y,z;
- FILE *out = fopen ("testout.dat", "w");
- for (y=0; y<mapb; y++)
- {
- for (x=0; x<mapa; x++)
- {
- for (z=0; z<mapc; z++)
- {
- int idx = y*mapa*mapb + x*mapa + z;
- fprintf (out, "%i %i %i\n", x, y, hei[idx]);
- }
- }
- }
- fclose (out);
- }
- void blacken_screen (SDL_Surface *screen)
- {
- SDL_Rect rect;
- rect.x = 0;
- rect.y = 0;
- rect.w = scw;
- rect.h = sch;
- SDL_FillRect (screen, &rect, 0);
- }
- void checkevents(double *ang, int *x, int *y, int *k)
- {
- SDL_Event event;
- if (SDL_PollEvent(&event))
- {
- switch (event.type) {
- case SDL_QUIT:
- exit(0);
- }
- }
- Uint8 *keystate = SDL_GetKeyState (NULL);
- if (keystate[SDLK_LEFT]) *ang += 0.02;
- if (keystate[SDLK_RIGHT]) *ang -= 0.02;
- if (keystate[SDLK_UP]) *k -= 1;
- if (keystate[SDLK_DOWN]) *k += 1;
- if (keystate[SDLK_a] || keystate[SDLK_d] || keystate[SDLK_w] || keystate[SDLK_s])
- {
- int xd, yd;
- xd = 0;
- yd = 0;
- if (keystate[SDLK_a]) xd = -3;
- if (keystate[SDLK_d]) xd = 3;
- if (keystate[SDLK_w]) yd = 3;
- if (keystate[SDLK_s]) yd = -3;
- *x += yd*cos(*ang) + xd*sin(*ang);
- *y += -yd*sin(*ang) + xd*cos(*ang);
- }
- }
- int main ()
- {
- int hei[mapa*mapb*mapc-1];
- int col[mapa*mapb*mapc-1];
- filldata (hei, col);
- printdata (hei);
- if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
- {
- fprintf(stderr, "Cannot initialize SDL: %s\n", SDL_GetError());
- exit(1);
- }
- SDL_Surface *screen;
- screen = SDL_SetVideoMode(scw, sch, 8, SDL_SWSURFACE|SDL_HWPALETTE);
- if ( screen == NULL )
- {// Если установить разрешение не удалось
- fprintf(stderr, "Cannot set resolution %ix%i: %s\n", scw, sch, SDL_GetError());
- exit(1);
- }
- fillpalette (screen);
- int posx = 0;
- int posy = 0;
- int posz = 40;
- double ang = 0;
- int k = 1;
- double dd = 1;
- double de = dd * 128; // 128 is the max "distance"
- int x,y,z;
- double amax = M_PI/3.4; // FOV
- double a;
- int bin1 = mapb*mapb*mapb-mapa*mapa;
- int bin2 = mapa*mapa-mapa;
- int bin3 = mapc-1;
- int sx;
- while (1)
- {
- SDL_LockSurface(screen);
- blacken_screen (screen);
- double sinang = sin(ang);
- double cosang = cos(ang);
- for (sx=0;sx<scw;sx++)
- {
- double d;
- a = -amax + 2*amax*sx/scw;
- double tana = tan (a);
- for (d=de;d>=0;d-=dd)
- {
- double y1 = d*/*cos(amax)**/tana;
- double x1 = d;//*cos(amax);
- x = posx + x1*cosang+y1*sinang;
- y = posy - x1*sinang+y1*cosang;
- z = posz - k*d;//*sin(amax);
- x = abs(x);
- y = abs(y);
- int idx = ((y*mapa*mapb)&bin1) + ((x*mapa)&bin2);
- int zi;
- for (zi=0; zi<mapc; zi++)
- {
- int idx2 = idx+zi;
- int h = hei[idx2];
- if (h==-1) break;
- int sy = sch/2 + 150*(z-h)/(d+1);
- int sy2 = sch/2 + 150*(z-h-bsize)/(d+1);
- if (sy2<sch)
- {
- if (sy>=sch) sy = sch-1;
- if (sy2<0) sy2 = 0;
- int syi;
- for (syi=sy; syi>=sy2; syi--)
- {
- *((Uint8*)screen->pixels+sx+scw*syi) = col[idx2];
- }
- }
- }
- }
- }
- SDL_UnlockSurface (screen);
- SDL_Flip (screen);
- checkevents(&ang, &posx, &posy, &k);
- }
- return atexit(SDL_Quit);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement