Guest User

YARaycaster

a guest
Apr 11th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.48 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include <SDL.h>
  7. #include <SDL_ttf.h>
  8.  
  9. int scw = 800;
  10. int sch = 600;
  11.  
  12. int mapw = 512;
  13. int maph = 512;
  14.  
  15. char* my_itoa(unsigned int val)
  16. {
  17.     static char buf[11];
  18.     short i=10;
  19.     for(;val&&i;--i,val/=10)buf[i]="0123456789"[val%10];
  20.     return &buf[i+1];
  21. }
  22.  
  23. double mygettime()
  24. {
  25.   struct timeval tv;
  26.   if(gettimeofday(&tv, 0) < 0)
  27.   {
  28.     perror("oops");
  29.   }
  30.   return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
  31. }
  32.  
  33. void filldata (int *hei)
  34. {
  35.     int p = 0;
  36.     int x,y;
  37.     for (y=0; y<maph; y++)
  38.     {
  39.         for (x=0; x<mapw; x++)
  40.         {
  41.             double x1 = (x-mapw/2)*0.3;
  42.             double y1 = (y-maph/2)*0.3;
  43.             hei[p] = 100*sin(sqrt(x1*x1+y1*y1))/(1+sqrt(x1*x1+y1*y1));
  44.            
  45.             p++;
  46.         }
  47.     }
  48. }
  49.  
  50. void fillpalette (SDL_Surface *screen)
  51. {
  52.     SDL_Color colors[256];
  53.     int i;
  54. //     Fill colors with color information
  55.     for(i=0;i<=256;i++)
  56.     {
  57.         colors[i].r=i;
  58.         colors[i].g=i/2;
  59.         colors[i].b=i/2;
  60.     }
  61.     SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
  62. }
  63.  
  64. void checkevents(float *ang, int *x, int *y, int *k)
  65. {
  66.     SDL_Event event;
  67.     if (SDL_PollEvent(&event))
  68.     {
  69.         switch (event.type) {
  70.         case SDL_QUIT:
  71.             exit(0);
  72.         }
  73.     }
  74.     Uint8 *keystate = SDL_GetKeyState (NULL);
  75.     if (keystate[SDLK_LEFT]) *ang += 0.02;
  76.     if (keystate[SDLK_RIGHT]) *ang -= 0.02;
  77.     if (keystate[SDLK_UP]) *k -= 1;
  78.     if (keystate[SDLK_DOWN]) *k += 1;
  79.     if (keystate[SDLK_a] || keystate[SDLK_d] || keystate[SDLK_w] || keystate[SDLK_s])
  80.     {
  81.         int xd, yd;
  82.         xd = 0;
  83.         yd = 0;
  84.         if (keystate[SDLK_a]) xd = -3;
  85.         if (keystate[SDLK_d]) xd = 3;
  86.         if (keystate[SDLK_w]) yd = 3;
  87.         if (keystate[SDLK_s]) yd = -3;
  88.        
  89.         *x += yd*cos(*ang) + xd*sin(*ang);
  90.         *y += -yd*sin(*ang) + xd*cos(*ang);
  91.     }
  92. }
  93.  
  94. void blacken_screen (SDL_Surface *screen)
  95. {
  96.     SDL_Rect rect;
  97.     rect.x = 0;
  98.     rect.y = 0;
  99.     rect.w = scw;
  100.     rect.h = sch;
  101.  
  102.     SDL_FillRect (screen, &rect, 0);
  103. }
  104.  
  105. int main ()
  106. {
  107.     if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
  108.     {
  109.         fprintf(stderr, "Cannot initialize SDL: %s\n", SDL_GetError());
  110.         exit(1);
  111.     }
  112.  
  113.     if (TTF_Init()==-1)
  114.     {
  115.         printf ("Error Init TTF\n");
  116.         exit(1);
  117.     }
  118.    
  119.     TTF_Font *font = TTF_OpenFont ("tahoma.ttf", 24);
  120.     SDL_Color fontcolor;
  121.     fontcolor.r = 255;
  122.     fontcolor.g = 255;
  123.     fontcolor.b = 255;
  124.  
  125.     SDL_Rect dest;
  126.     dest.x = 20;
  127.     dest.y = 20;
  128.     SDL_Surface *textrend = TTF_RenderText_Solid (font, "FPS: ", fontcolor);
  129.    
  130.     SDL_Surface *screen;
  131.     screen = SDL_SetVideoMode(scw, sch, 8, SDL_SWSURFACE);
  132.     if ( screen == NULL )
  133.     {// Если установить разрешение не удалось
  134.         fprintf(stderr, "Cannot set resolution %ix%i: %s\n", scw, sch, SDL_GetError());
  135.         exit(1);
  136.     }
  137.  
  138.     fillpalette (screen);
  139.     int hei[mapw*maph-1];
  140.     filldata (hei);
  141.     SDL_Surface *text = SDL_LoadBMP ("./testtext3.bmp");
  142.     if (!text) printf ("Error\n");
  143.     Uint8 *col = text->pixels;
  144.  
  145.     int posx = 0;
  146.     int posy = 0;
  147.  
  148.     float ang = 0;
  149.     int k = 4;
  150.  
  151.     int dd = 65536;
  152.     int de = dd * 128; // 228 is the max "distance"
  153.     int d;
  154.  
  155.     long x,y,z;
  156.  
  157.     float amax = M_PI/3.4;
  158.     float a;
  159.     float da;
  160.     int sx;
  161.  
  162.     int maxh;
  163.  
  164.     int bin1 = (mapw*(maph-1))<<16;
  165.     int bin2 = ((maph-1))<<16;
  166.  
  167.     double time1 = mygettime();
  168.     double time2;
  169.     int frames = 0;
  170.  
  171. //    long foo = d/200;
  172. //    int bar = dd/200;
  173.     while (1)
  174.     {
  175.         SDL_LockSurface(screen);
  176.         blacken_screen (screen);
  177.  
  178.         int startidx = ((posx*mapw) & bin1) + (posy&bin2);
  179.         int starth = hei[startidx];
  180.         int posz = 100;
  181.  
  182.         a = -amax;
  183.         da = 2*amax/scw;
  184.        
  185.         int dx = dd*(cos(ang)-a*sin(ang));
  186.         int dy = -dd*(sin(ang)+a*cos(ang));
  187.  
  188.         int ddx = dd*sin(ang)*da;
  189.         int ddy = dd*cos(ang)*da;
  190.  
  191.         for (sx=0;sx<scw;sx++)
  192.         {
  193.             int d;
  194.             int p = sx + scw*(sch-1);
  195.             int dz = dd*k;
  196.            
  197.             x = posx<<16;
  198.             y = posy<<16;
  199.             z = posz<<16;
  200.             for (d=dd/2; d<=de; d+=dd)
  201.             {
  202.                 x += dx;
  203.                 y += dy;
  204.                 z -= dz;
  205.  
  206.                 int idx = (((x*mapw)&bin1)+(y&bin2))>>16;
  207.  
  208.                 int h = hei[idx]<<16;
  209.                 Uint8 color = col[idx];
  210.                 while (h>z)
  211.                 {
  212.                     *((Uint8*)screen->pixels+p) = color;
  213.                     z += d/200;
  214.                     dz -= dd/100;
  215.                     p -= scw;
  216.                 }
  217.             }
  218.             a += da;
  219.             dx += ddx;
  220.             dy += ddy;
  221.         }
  222.  
  223.        
  224.         SDL_UnlockSurface(screen);
  225.         SDL_BlitSurface (textrend, NULL, screen, &dest);
  226.         SDL_Flip (screen);
  227.         checkevents (&ang, &posx, &posy, &k);
  228.        
  229.         frames++;
  230.        
  231.         time2 = mygettime() - time1;
  232.         if (time2 > 1.0)
  233.         {
  234.             SDL_FreeSurface (textrend);
  235.             char fpsstring[30] = "FPS: ";
  236.             strcat (fpsstring, my_itoa (frames));
  237.             textrend = TTF_RenderText_Solid (font, fpsstring, fontcolor);
  238.             frames = 0;
  239.             time1 = mygettime();
  240.         }
  241.     }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment