123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- /*
- Copyright (C) 2011 Weber Yann, Schuck Clement
-
- This file is part of Tansive.
-
- Tansive is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
-
- Tansive is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Tansive. If not, see <http://www.gnu.org/licenses/>.
- */
- #include"visu.h"
-
- void rasterCircleRGBA(SDL_Surface* surface,int x0, int y0, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
- {
- int f = 1 - radius;
- int ddF_x = 1;
- int ddF_y = -2 * radius;
- int x = 0;
- int y = radius;
-
- setPixelRGBA(surface,x0, y0 + radius,r,g,b,a);
- setPixelRGBA(surface,x0, y0 - radius,r,g,b,a);
- setPixelRGBA(surface,x0 + radius, y0,r,g,b,a);
- setPixelRGBA(surface,x0 - radius, y0,r,g,b,a);
-
- while(x < y)
- {
- if(f >= 0)
- {
- y--;
- ddF_y += 2;
- f += ddF_y;
- }
- x++;
- ddF_x += 2;
- f += ddF_x;
- setPixelRGBA(surface,x0 + x, y0 + y,r,g,b,a);
- setPixelRGBA(surface,x0 - x, y0 + y,r,g,b,a);
- setPixelRGBA(surface,x0 + x, y0 - y,r,g,b,a);
- setPixelRGBA(surface,x0 - x, y0 - y,r,g,b,a);
- setPixelRGBA(surface,x0 + y, y0 + x,r,g,b,a);
- setPixelRGBA(surface,x0 - y, y0 + x,r,g,b,a);
- setPixelRGBA(surface,x0 + y, y0 - x,r,g,b,a);
- setPixelRGBA(surface,x0 - y, y0 - x,r,g,b,a);
- }
- }
-
- void rasterCircleRGB(SDL_Surface* surface,int x0, int y0, int radius, Uint8 r, Uint8 g, Uint8 b)
- {
- rasterCircleRGBA(surface, x0,y0,radius,r,g,b,255);
- }
-
- void setPixel(SDL_Surface* surface, int x, int y, Uint32 pixel)
- {
-
- if(x >= surface->w || y >= surface->h || x < 0 || y < 0)
- {
- #ifdef DEBUG
- fprintf(stderr, "Out of range\n\tx=%d\ty=%d\n",x,y);
- #endif
- }
- else
- {
- int BPP = surface->format->BytesPerPixel;
-
- 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;
-
- }
- }
-
- }
-
- void setPixelRGB(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b)
- {
- Uint32 pixel = SDL_MapRGB(surface->format, r,g,b);
- setPixel(surface, x, y, pixel);
- }
-
- void setPixelRGBA(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
- {
- Uint32 pixel = SDL_MapRGBA(surface->format, r,g,b,a);
- setPixel(surface, x, y, pixel);
- }
-
- void setPixelRGBP(SDL_Surface* surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, float p)
- {
- Uint8 a = (int)(p*255.0);
- Uint32 pixel = SDL_MapRGBA(surface->format, r,g,b,a);
- setPixel(surface, x, y, pixel);
- }
-
- void draw_map(SDL_Surface* screen, SDL_Surface* surface, t_map* mp)
- {
- int j,i;
-
- for(i=0;i<mp->x;i++)
- {
- for(j=0;j<mp->y;j++)
- {
- if(mp->map[i][j].type == MAP_ELT_VEGET)
- setPixelRGB(surface, i,j,0,255,0);
- else if(mp->map[i][j].type == MAP_ELT_CREAT)
- {
- rasterCircleRGB(surface, i, j, mp->map[i][j].creat->genome_tree->gnome.perception,255 ,255 , 0);
- setPixelRGB(surface, i,j,255,0,0);
- }
- }
- }
-
- }
-
- void draw_map_zoom(SDL_Surface* screen, SDL_Surface* surface, t_map* mp, int xc, int yc, int zoom)
- {
-
- int x1 = xc-(mp->x/zoom)/2, y1 = yc-(mp->y/zoom)/2, x2 = xc+(mp->x/zoom)/2, y2 = yc+(mp->y/zoom)/2;
- int i,j,k = 0,l = 0, m,n;
-
- if(x1<0)
- {
- x1*=-1;
- x2+=x1;
- x1=0;
- }
- else if(x2>mp->x-1)
- {
- x1-=x2-mp->x-1;
- x2=mp->x-1;
- }
-
- if(y1<0)
- {
- y1*=-1;
- y2+=y1;
- y1=0;
- }
- else if(y2>mp->y-1)
- {
- y1-=y2-mp->y-1;
- y2=mp->y-1;
- }
-
- l=0;
- for(j=y1;j<y2; j++)
- {
- k = 0;
- for(i=x1; i<x2; i++)
- {
- if(mp->map[i][j].type == MAP_ELT_VEGET)
- {
- /*for(n=l; n<l+zoom; n++)
- {
- for(m=k; m<k+zoom; m++)
- {
- setPixelRGB(surface,m,n,0,255,0);
- }
- }*/
- rasterCircleRGB(surface, k+zoom/2,l+zoom/2, zoom/2, 0,255,0);
- }
- else if(mp->map[i][j].type == MAP_ELT_CREAT)
- {
- /*for(n=l; n<l+zoom; n++)
- {
- for(m=k; m<k+zoom; m++)
- {
- setPixelRGB(surface,m,n,255,0,0);
- }
- }*/
- rasterCircleRGBA(surface,k+zoom/2, l+zoom/2, mp->map[i][j].creat->genome_tree->gnome.perception*zoom,255 ,255 , 0,128);
- rasterCircleRGB(surface, k+zoom/2,l+zoom/2, zoom/2, 255,0,0);
- }
-
- k+= zoom;
- }
- l+= zoom;
- }
-
- //SDL_BlitSurface(surface, NULL, screen, &bckgrndPos);
- }
-
- void draw_zoom_area(SDL_Surface* screen, SDL_Surface* surface, int xc, int yc, t_map* mp)
- {
- int zoom = 3;
- int x1 = xc-(mp->x/zoom)/2, y1 = yc-(mp->y/zoom)/2, x2 = xc+(mp->x/zoom)/2, y2 = yc+(mp->y/zoom)/2;
- int i,j;
-
- if(x1<0)
- {
- x1*=-1;
- x2+=x1;
- x1=0;
- }
- else if(x2>mp->x-1)
- {
- x1-=x2-mp->x-1;
- x2=mp->x-1;
- }
-
- if(y1<0)
- {
- y1*=-1;
- y2+=y1;
- y1=0;
- }
- else if(y2>mp->y-1)
- {
- y1-=y2-mp->y-1;
- y2=mp->y-1;
- }
-
- j=y1;
- for(i=x1; i<x2; i++)
- setPixelRGBP(surface,i,j,0,0,255,0.5);
- j=y2;
- for(i=x1; i<x2; i++)
- setPixelRGBP(surface,i,j,0,0,255,0.5);
-
- i=x1;
- for(j=y1;j<y2;j++)
- setPixelRGBP(surface,i,j,0,0,255,0.5);
- i=x2;
- for(j=y1;j<y2;j++)
- setPixelRGBP(surface,i,j,0,0,255,0.5);
- }
|