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 = 800;
- int sch = 600;
- 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;
- hei[p] = 100*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(float *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);
- }
- 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];
- filldata (hei);
- SDL_Surface *text = SDL_LoadBMP ("./testtext3.bmp");
- if (!text) printf ("Error\n");
- Uint8 *col = text->pixels;
- int posx = 0;
- int posy = 0;
- float ang = 0;
- int k = 4;
- int dd = 65536;
- int de = dd * 128; // 228 is the max "distance"
- int d;
- long x,y,z;
- float amax = M_PI/3.4;
- float a;
- float da;
- int sx;
- int maxh;
- int bin1 = (mapw*(maph-1))<<16;
- int bin2 = ((maph-1))<<16;
- double time1 = mygettime();
- double time2;
- int frames = 0;
- // long foo = d/200;
- // int bar = dd/200;
- while (1)
- {
- SDL_LockSurface(screen);
- blacken_screen (screen);
- int startidx = ((posx*mapw) & bin1) + (posy&bin2);
- int starth = hei[startidx];
- int posz = 100;
- a = -amax;
- da = 2*amax/scw;
- int dx = dd*(cos(ang)-a*sin(ang));
- int dy = -dd*(sin(ang)+a*cos(ang));
- int ddx = dd*sin(ang)*da;
- int ddy = dd*cos(ang)*da;
- for (sx=0;sx<scw;sx++)
- {
- int d;
- int p = sx + scw*(sch-1);
- int dz = dd*k;
- x = posx<<16;
- y = posy<<16;
- z = posz<<16;
- for (d=dd/2; d<=de; d+=dd)
- {
- x += dx;
- y += dy;
- z -= dz;
- int idx = (((x*mapw)&bin1)+(y&bin2))>>16;
- int h = hei[idx]<<16;
- Uint8 color = col[idx];
- while (h>z)
- {
- *((Uint8*)screen->pixels+p) = color;
- z += d/200;
- dz -= dd/100;
- p -= scw;
- }
- }
- a += da;
- dx += ddx;
- dy += ddy;
- }
- 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();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment