2 "$Id: sdl.c 9200 2008-06-01 17:12:43Z anmaster $";
37 #include <SDL_image.h>
41 #include <glade/glade.h>
45 #include <gdk/gdkwin32.h>
47 #include <gdk/gdkkeysyms.h>
55 SDL_Surface* mapsurface;
56 static SDL_Surface* lightmap;
57 static SDL_Surface* fogmap;
58 static char *redrawbitmap;
73 #include <SDL_image.h>
76 static void do_SDL_error(
const char *SDL_function,
const char *file,
int line)
79 file, line, SDL_GetError());
91 static void putpixel(SDL_Surface *surface,
int x,
int y, Uint32 pixel)
93 int bpp = surface->format->BytesPerPixel;
95 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
103 *(Uint16 *)p = pixel;
107 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
108 p[0] = (pixel >> 16) & 0xff;
109 p[1] = (pixel >> 8) & 0xff;
113 p[1] = (pixel >> 8) & 0xff;
114 p[2] = (pixel >> 16) & 0xff;
119 *(Uint32 *)p = pixel;
130 static void overlay_grid(
int re_init,
int ax,
int ay)
133 static SDL_Surface* grid_overlay;
135 static int first_pass;
141 SDL_PixelFormat* fmt;
150 SDL_FreeSurface( grid_overlay);
156 if( grid_overlay == NULL)
158 grid_overlay= SDL_CreateRGBSurface( SDL_HWSURFACE|SDL_SRCALPHA,
161 mapsurface->format->BitsPerPixel,
162 mapsurface->format->Rmask,
163 mapsurface->format->Gmask,
164 mapsurface->format->Bmask,
165 mapsurface->format->Amask);
166 if( grid_overlay == NULL)
167 do_SDL_error(
"CreateRGBSurface", __FILE__, __LINE__);
169 grid_overlay= SDL_DisplayFormatAlpha( grid_overlay);
185 fmt= grid_overlay->format;
191 pixel= (Uint32*)grid_overlay->pixels+y*grid_overlay->pitch/4+x;
193 if( x == 0 || y == 0 ||
197 *pixel= SDL_MapRGBA( fmt, 255, 0, 0, SDL_ALPHA_OPAQUE);
201 *pixel= SDL_MapRGBA( fmt, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
215 SDL_BlitSurface( grid_overlay, NULL, mapsurface, &dst);
226 SDL_BlitSurface( grid_overlay, &dst, mapsurface, &dst);
242 void init_SDL( GtkWidget* sdl_window,
int just_lightmap)
245 char SDL_windowhack[32];
247 if( just_lightmap == 0) {
248 g_assert( sdl_window != NULL);
249 if( SDL_WasInit( SDL_INIT_VIDEO) != 0) {
251 SDL_FreeSurface( lightmap);
253 SDL_FreeSurface( mapsurface);
262 snprintf(SDL_windowhack,
sizeof(SDL_windowhack),
"SDL_WINDOWID=%ld",
263 GDK_WINDOW_XWINDOW(sdl_window->window) );
265 sprintf( SDL_windowhack,
"SDL_WINDOWID=%ld",
266 GDK_WINDOW_HWND(sdl_window->window) );
268 putenv( SDL_windowhack);
270 if( SDL_Init( SDL_INIT_VIDEO) < 0)
272 LOG(
LOG_CRITICAL,
"gtk::init_SDL",
"Could not initialize SDL: %s", SDL_GetError());
277 SDL_HWSURFACE|SDL_DOUBLEBUF);
279 if( mapsurface == NULL)
281 do_SDL_error(
"SetVideoMode", __FILE__, __LINE__);
285 SDL_FreeSurface( fogmap);
287 fogmap= SDL_CreateRGBSurface( SDL_HWSURFACE|SDL_SRCALPHA,
map_image_size,
289 mapsurface->format->BitsPerPixel,
290 mapsurface->format->Rmask,
291 mapsurface->format->Gmask,
292 mapsurface->format->Bmask,
293 mapsurface->format->Amask);
297 do_SDL_error(
"SDL_CreateRGBSurface", __FILE__, __LINE__);
304 if( SDL_SetAlpha( fogmap, SDL_SRCALPHA|SDL_RLEACCEL, 128) < 0)
306 do_SDL_error(
"SDL_SetAlpha", __FILE__, __LINE__);
310 if( just_lightmap != 0 && lightmap)
311 SDL_FreeSurface( lightmap);
313 lightmap= SDL_CreateRGBSurface( SDL_HWSURFACE|SDL_SRCALPHA,
map_image_size,
315 mapsurface->format->BitsPerPixel,
316 mapsurface->format->Rmask,
317 mapsurface->format->Gmask,
318 mapsurface->format->Bmask,
319 mapsurface->format->Amask);
320 if( lightmap == NULL)
322 do_SDL_error(
"SDL_CreateRGBSurface", __FILE__, __LINE__);
329 lightmap= SDL_DisplayFormatAlpha( lightmap);
330 if( lightmap == NULL)
332 do_SDL_error(
"DisplayFormatAlpha", __FILE__, __LINE__);
338 overlay_grid(
TRUE, 0, 0);
374 int startx,
int starty,
int endx,
int endy,
375 int destx,
int desty){
378 for (x=startx;x<endx;x++){
379 top= ((x*(tr-tl))/
width)+tl;
380 bottom= ((x*(br-bl))/
width)+bl;
381 for (y=starty;y<endy;y++){
382 val=((y*(bottom-top))/
height)+top;
388 putpixel(lightmap, destx+x-startx, desty+y-starty,
389 SDL_MapRGBA(lightmap->format, 0, 0, 0, val));
397 #define ALPHA_FUDGE(x) (2*(x) / 3)
398 #define GENDARK(x,y) ( (((x)&(y) & 1) == 1)?255:0 )
432 static void do_sdl_per_pixel_lighting(
int x,
int y,
int mx,
int my)
469 SDL_FillRect(mapsurface,&dst, SDL_MapRGB(mapsurface->format, 0, 0, 0));
471 SDL_FillRect(lightmap,NULL, SDL_MapRGBA(lightmap->format, 0, 0, 0,
the_map.
cells[mx][my].
darkness));
472 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
496 if (dark1 == dark0) {
505 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
514 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
515 ALPHA_FUDGE((map_image_half_size - i) * dark1 + i * dark0)/map_image_half_size));
521 if (dark3 == dark0) {
526 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
535 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
536 ALPHA_FUDGE(dark0*(map_image_size-i) + dark3*(i-map_image_half_size)) / map_image_half_size));
544 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
546 if (dark4 == dark0) {
551 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
559 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
560 ALPHA_FUDGE(dark4*(map_image_half_size-i) + dark0*i) / map_image_half_size));
562 if (dark2 == dark0) {
567 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
576 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
577 ALPHA_FUDGE(dark0*(map_image_size-i) + dark2*(i-map_image_half_size)) / map_image_half_size));
581 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
585 static int *darkx=NULL, *darky=NULL,darkx_allocated=0;
592 if (map_image_size > darkx_allocated) {
593 darkx = realloc(darkx, map_image_size *
sizeof(
int));
594 darky = realloc(darky, map_image_size *
sizeof(
int));
601 darkx[dx] = (dark0*(map_image_size-dx) + dark2*(dx-map_image_half_size)) /
map_image_half_size;
606 darky[dy] = (dark0*(map_image_size-dy) + dark3*(dy-map_image_half_size)) /
map_image_half_size;
608 SDL_LockSurface( lightmap);
612 putpixel(lightmap, dx, dy, SDL_MapRGBA(lightmap->format, 0, 0, 0,(darkx[dx] + darky[dy])/2));
615 int dark5, dark6, dark7, dark8;
629 if ( (x-1 < 0) || (y-1 < 0)
634 map_image_size, map_image_size,
635 map_image_half_size, map_image_half_size, map_image_size, map_image_size,
639 map_image_size, map_image_size,
640 0, map_image_half_size, map_image_half_size, map_image_size,
641 map_image_half_size, 0);
644 map_image_size, map_image_size,
645 map_image_half_size, 0, map_image_size, map_image_half_size,
646 0, map_image_half_size);
649 map_image_size, map_image_size,
650 0, 0, map_image_half_size, map_image_half_size,
651 map_image_half_size, map_image_half_size);
657 SDL_UnlockSurface(lightmap);
658 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
674 static void drawsmooth_sdl (
int mx,
int my,
int layer,SDL_Rect dst){
675 static int dx[8]={0,1,1,1,0,-1,-1,-1};
676 static int dy[8]={-1,-1,0,1,1,1,0,-1};
677 static int bweights[8]={2,0,4,0,8,0,1,0};
678 static int cweights[8]={0,2,0,4,0,8,0,1};
679 static int bc_exclude[8]={
689 int partdone[8]={0,0,0,0,0,0,0,0};
692 int i,lowest,weight,weightC;
697 for (i=0;i<=layer;i++)
730 if ( (slevels[i]>0) && (!partdone[i]) &&
731 ((lowest<0) || (slevels[i]<slevels[lowest]))
750 if ( (slevels[i]==slevels[lowest]) &&
751 (sfaces[i]==sfaces[lowest])){
753 weight=weight+bweights[i];
754 weightC&=~bc_exclude[i];
757 weightC&=~cweights[i];
762 if (sfaces[lowest]<=0)
764 smoothface=sfaces[lowest];
773 if ( (!
pixmaps[smoothface]->map_image) ||
777 src.x=map_image_size*weight;
780 if (SDL_BlitSurface(
pixmaps[smoothface]->fog_image,
781 &src, mapsurface, &dst))
782 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
784 if (SDL_BlitSurface(
pixmaps[smoothface]->map_image,
785 &src, mapsurface, &dst))
786 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
790 src.x=map_image_size*weightC;
793 if (SDL_BlitSurface(
pixmaps[smoothface]->fog_image,
794 &src, mapsurface, &dst))
795 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
797 if (SDL_BlitSurface(
pixmaps[smoothface]->map_image,
798 &src, mapsurface, &dst))
799 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
815 static void update_redrawbitmap(
void)
835 redrawbitmap[x + 1 + (y+1) * use_config[CONFIG_MAPWIDTH]] = 1;
846 redrawbitmap[x + (y+1) * use_config[CONFIG_MAPWIDTH]] = 1;
847 redrawbitmap[x + 2 + (y+1) * use_config[CONFIG_MAPWIDTH]] = 1;
848 redrawbitmap[x + 1 + (y) * use_config[CONFIG_MAPWIDTH]] = 1;
849 redrawbitmap[x + 1 + (y+2) * use_config[CONFIG_MAPWIDTH]] = 1;
853 redrawbitmap[x + (y) * use_config[CONFIG_MAPWIDTH]] = 1;
854 redrawbitmap[x + 2 + (y) * use_config[CONFIG_MAPWIDTH]] = 1;
855 redrawbitmap[x + (y+2) * use_config[CONFIG_MAPWIDTH]] = 1;
856 redrawbitmap[x + 2 + (y+2) * use_config[CONFIG_MAPWIDTH]] = 1;
860 redrawbitmap[x + 1 + (y+1) * use_config[CONFIG_MAPWIDTH]] = 1;
883 SDL_FillRect(mapsurface, &dst, SDL_MapRGB(mapsurface->format, 0, 0, 0));
889 for (layer=0; layer<
MAXLAYERS; layer++) {
894 if (face > 0 &&
pixmaps[face]->map_image != NULL) {
905 if (SDL_BlitSurface(
pixmaps[face]->fog_image, &src, mapsurface, &dst))
906 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
908 if (SDL_BlitSurface(
pixmaps[face]->map_image, &src, mapsurface, &dst))
909 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
919 drawsmooth_sdl (mx,my,layer,dst);
923 if (face > 0 &&
pixmaps[face]->map_image != NULL) {
930 int dx, dy, sourcex, sourcey, offx, offy;
933 offx = dx?(map_image_size -dx):0;
936 sourcex = sx * map_image_size - offx ;
943 offy = dy?(map_image_size -dy):0;
946 sourcey = sy * map_image_size - offy;
954 src.w = map_image_size - offx;
955 src.h = map_image_size - offy;
956 dst.x = ax*map_image_size + offx;
957 dst.y = ay*map_image_size + offy;
959 if (SDL_BlitSurface(
pixmaps[face]->fog_image, &src, mapsurface, &dst))
960 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
962 if (SDL_BlitSurface(
pixmaps[face]->map_image, &src, mapsurface, &dst))
963 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
980 SDL_FillRect(mapsurface,&dst, SDL_MapRGB(mapsurface->format, 0, 0, 0));
983 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
986 do_sdl_per_pixel_lighting(ax, ay, mx, my);
1015 int x, y, num_spaces=0, num_drawn=0;
1016 struct timeval tv1, tv2, tv3;
1017 long elapsed1, elapsed2;
1020 gettimeofday(&tv1, NULL);
1022 update_redrawbitmap();
1031 if (redraw || redrawbitmap[x + 1 + (y+1) * use_config[CONFIG_MAPWIDTH]]) {
1041 gettimeofday(&tv2, NULL);
1043 SDL_Flip(mapsurface);
1046 gettimeofday(&tv3, NULL);
1047 elapsed1 = (tv2.tv_sec - tv1.tv_sec)*1000000 + (tv2.tv_usec - tv1.tv_usec);
1048 elapsed2 = (tv3.tv_sec - tv2.tv_sec)*1000000 + (tv3.tv_usec - tv2.tv_usec);
1052 if ((elapsed1 + elapsed2)>10000)
1053 LOG(
LOG_INFO,
"gtk::sdl_gen_map",
"gen took %7ld, flip took %7ld, total = %7ld",
1054 elapsed1, elapsed2, elapsed1 + elapsed2);
1071 SDL_LockSurface( mapsurface);
1074 memmove( mapsurface->pixels + offset, mapsurface->pixels,
1075 mapsurface->pitch * (mapsurface->h + dy*map_image_size) );
1079 memmove( mapsurface->pixels, mapsurface->pixels + offset,
1080 mapsurface->pitch * (mapsurface->h - dy*map_image_size) );
1085 for( y= 0; y < mapsurface->h; y++) {
1087 char* start_of_row= mapsurface->pixels + mapsurface->pitch * y;
1088 int offset= ( mapsurface->format->BytesPerPixel * map_image_size * -dx);
1089 memmove( start_of_row + offset, start_of_row,
1090 mapsurface->pitch - offset);
1093 char* start_of_row= mapsurface->pixels + mapsurface->pitch * y;
1094 int offset= ( mapsurface->format->BytesPerPixel * map_image_size * dx);
1095 memmove( start_of_row, start_of_row + offset,
1096 mapsurface->pitch - offset);
1100 SDL_UnlockSurface( mapsurface);
#define CAN_SMOOTH(__SQUARE, __LEVEL)
void init_SDL(GtkWidget *sdl_window, int just_lightmap)
#define CFG_LT_PIXEL_BEST
PixmapInfo * pixmaps[MAXPIXMAPNUM]
void LOG(LogLevel level, const char *origin, const char *format,...)
int sdl_mapscroll(int dx, int dy)
const char *const rcsid_gtk_sdl_c
static void display_mapcell(int ax, int ay, int mx, int my)
sint16 use_config[CONFIG_NUMS]
sint16 mapdata_face(int x, int y, int layer)
void drawquarterlightmap_sdl(int tl, int tr, int bl, int br, int width, int height, int startx, int starty, int endx, int endy, int destx, int desty)
sint16 mapdata_bigface(int x, int y, int layer, int *ww, int *hh)
struct MapCellLayer heads[MAXLAYERS]
void sdl_gen_map(int redraw)