Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <math.h>
- #include <stdio.h>
- #include <time.h>
- #include <sys/time.h>
- #include <SDL.h>
- #include <SDL_ttf.h>
- int scw = 1280;
- int sch = 1024;
- int mapw = 512;
- int maph = 512;
- char* my_itoa(unsigned int val)
- {
- static char buf[11];
- short i=10;
- for(;val&&i;--i,val/=10)buf[i]="0123456789"[val%10];
- return &buf[i+1];
- }
- double mygettime()
- {
- struct timeval tv;
- if(gettimeofday(&tv, 0) < 0)
- {
- perror("oops");
- }
- return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
- }
- void filldata (int *hei)
- {
- int p = 0;
- int x,y;
- for (y=0; y<maph; y++)
- {
- for (x=0; x<mapw; x++)
- {
- double x1 = (x-mapw/2)*0.3;
- double y1 = (y-maph/2)*0.3;
- // col[p] = 3*(cos(x*0.2) + sin (y*0.3)) + 88;
- hei[p] = 1000*sin(sqrt(x1*x1+y1*y1))/(1+sqrt(x1*x1+y1*y1));
- p++;
- }
- }
- }
- 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/2;
- colors[i].b=i/2;
- }
- SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
- }
- 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);
- }
- }
- 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);
- }
- double get_filtered_val (int *val, double xf, double yf)
- {
- int x = xf;
- int y = yf;
- double diffx = xf - x;
- double diffy = yf - y;
- double oppx = 1 - diffx;
- double oppy = 1 - diffy;
- if ((x==511) || (y==511)) return val[mapw*x+y];
- return (val[x*mapw+y]*oppx + val[(x+1)*mapw+y]*diffx)*oppy + \
- (val[x*mapw+y+1]*oppx + val[(x+1)*mapw+y+1]*diffx)*diffy;
- }
- double uint8_get_filtered_val (Uint8 *val, double xf, double yf)
- {
- int x = xf;
- int y = yf;
- double diffx = xf - x;
- double diffy = yf - y;
- double oppx = 1 - diffx;
- double oppy = 1 - diffy;
- if ((x==511) || (y==511)) return val[mapw*x+y];
- return (val[x*mapw+y]*oppx + val[(x+1)*mapw+y]*diffx)*oppy + \
- (val[x*mapw+y+1]*oppx + val[(x+1)*mapw+y+1]*diffx)*diffy;
- }
- int main ()
- {
- if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
- {
- fprintf(stderr, "Cannot initialize SDL: %s\n", SDL_GetError());
- exit(1);
- }
- if (TTF_Init()==-1)
- {
- printf ("Error Init TTF\n");
- exit(1);
- }
- TTF_Font *font = TTF_OpenFont ("tahoma.ttf", 24);
- SDL_Color fontcolor;
- fontcolor.r = 255;
- fontcolor.g = 255;
- fontcolor.b = 255;
- SDL_Rect dest;
- dest.x = 20;
- dest.y = 20;
- SDL_Surface *textrend = TTF_RenderText_Solid (font, "FPS: ", fontcolor);
- SDL_Surface *screen;
- screen = SDL_SetVideoMode(scw, sch, 8, SDL_SWSURFACE);
- if ( screen == NULL )
- {// Если установить разрешение не удалось
- fprintf(stderr, "Cannot set resolution %ix%i: %s\n", scw, sch, SDL_GetError());
- exit(1);
- }
- fillpalette (screen);
- int hei[mapw*maph-1];
- // int col[mapw*maph-1];
- filldata (hei);
- // SDL_Surface *text = SDL_LoadBMP ("./testtexture.bmp");
- SDL_Surface *text = SDL_LoadBMP ("./testtext3.bmp");
- if (!text) printf ("Error\n");
- Uint8 *col = text->pixels;
- int posx = 0;
- int posy = 0;
- double ang = 0;
- int horiz = -100;
- int k = 1;
- double dd = 2.0;
- double de = dd * 128;
- double x,y,z;
- double amax = M_PI/3.4;
- double a;
- int sx;
- int maxh;
- int bin1 = mapw*(maph-1);
- int bin2 = maph-1;
- double time1 = mygettime();
- double time2;
- int frames = 0;
- while (1)
- {
- SDL_LockSurface(screen);
- blacken_screen (screen);
- int startidx = ((posx*mapw) & bin1) + (posy&bin2);
- int starth = hei[startidx];
- int posz = 250+starth/2;
- // int posz = 400;
- double sinang = sin(ang);
- double cosang = cos(ang);
- for (sx=0;sx<scw;sx++)
- {
- double d;
- a = -amax + (2*amax)*sx/scw;
- //a = amax*sx/scw;
- double tana = tan (a);
- maxh = sch-1;
- for (d=0;d<=de;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);
- int xdiv = x/mapw;
- int ydiv = y/maph;
- x = fabs (x - xdiv*mapw);
- y = fabs (y - ydiv*maph);
- float h = get_filtered_val (hei, x, y);
- int sy = sch/2 + 40*(z-h)/(d+1);
- if (sy<0) sy = 0;
- if (sy>sch-1) sy = sch-1;
- if (sy<maxh)
- {
- int startpos = maxh;
- maxh=sy;
- int syi;
- Uint8 color = uint8_get_filtered_val (col, x, y);
- for (syi=startpos; syi>=sy; syi--)
- {
- *((Uint8*)screen->pixels+sx+scw*syi) = color;
- }
- }
- }
- }
- SDL_UnlockSurface(screen);
- SDL_BlitSurface (textrend, NULL, screen, &dest);
- SDL_Flip (screen);
- checkevents (&ang, &posx, &posy, &k);
- frames++;
- time2 = mygettime() - time1;
- if (time2 > 1.0)
- {
- SDL_FreeSurface (textrend);
- char fpsstring[30] = "FPS: ";
- strcat (fpsstring, my_itoa (frames));
- textrend = TTF_RenderText_Solid (font, fpsstring, fontcolor);
- frames = 0;
- time1 = mygettime();
- }
- }
- return atexit(SDL_Quit);
- }
Advertisement
Add Comment
Please, Sign In to add comment