29 #include <gdk/gdkwin32.h>
37 SDL_Surface* mapsurface;
38 static SDL_Surface* lightmap;
39 static SDL_Surface* fogmap;
40 static char *redrawbitmap;
51 static void do_SDL_error(
const char *SDL_function,
const char *file,
int line)
54 file, line, SDL_GetError());
66 static void putpixel(SDL_Surface *surface,
int x,
int y, Uint32 pixel)
68 int bpp = surface->format->BytesPerPixel;
70 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
82 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
83 p[0] = (pixel >> 16) & 0xff;
84 p[1] = (pixel >> 8) & 0xff;
88 p[1] = (pixel >> 8) & 0xff;
89 p[2] = (pixel >> 16) & 0xff;
105 static void overlay_grid(
int re_init,
int ax,
int ay)
108 static SDL_Surface* grid_overlay;
110 static int first_pass;
116 SDL_PixelFormat* fmt;
122 if( re_init == TRUE) {
124 SDL_FreeSurface( grid_overlay);
131 if( grid_overlay == NULL) {
132 grid_overlay= SDL_CreateRGBSurface( SDL_HWSURFACE|SDL_SRCALPHA,
135 mapsurface->format->BitsPerPixel,
136 mapsurface->format->Rmask,
137 mapsurface->format->Gmask,
138 mapsurface->format->Bmask,
139 mapsurface->format->Amask);
140 if( grid_overlay == NULL) {
141 do_SDL_error(
"CreateRGBSurface", __FILE__, __LINE__);
144 grid_overlay= SDL_DisplayFormatAlpha( grid_overlay);
153 if( first_pass== 0) {
159 fmt= grid_overlay->format;
163 pixel= (Uint32*)grid_overlay->pixels+y*grid_overlay->pitch/4+x;
165 if( x == 0 || y == 0 ||
169 *pixel= SDL_MapRGBA( fmt, 255, 0, 0, SDL_ALPHA_OPAQUE);
171 *pixel= SDL_MapRGBA( fmt, 0, 0, 0, SDL_ALPHA_TRANSPARENT);
185 SDL_BlitSurface( grid_overlay, NULL, mapsurface, &dst);
194 SDL_BlitSurface( grid_overlay, &dst, mapsurface, &dst);
210 void init_SDL( GtkWidget* sdl_window,
int just_lightmap)
213 char SDL_windowhack[32];
215 if( just_lightmap == 0) {
216 g_assert( sdl_window != NULL);
217 if( SDL_WasInit( SDL_INIT_VIDEO) != 0) {
219 SDL_FreeSurface( lightmap);
222 SDL_FreeSurface( mapsurface);
232 snprintf(SDL_windowhack,
sizeof(SDL_windowhack),
"SDL_WINDOWID=%ld",
233 GDK_WINDOW_XWINDOW(gtk_widget_get_window(sdl_window)) );
235 sprintf( SDL_windowhack,
"SDL_WINDOWID=%ld",
236 GDK_WINDOW_HWND(gtk_widget_get_window(sdl_window)) );
238 putenv( SDL_windowhack);
240 if( SDL_Init( SDL_INIT_VIDEO) < 0) {
241 LOG(
LOG_CRITICAL,
"gtk-v2::init_SDL",
"Could not initialize SDL: %s", SDL_GetError());
247 SDL_HWSURFACE|SDL_DOUBLEBUF);
249 if( mapsurface == NULL) {
250 do_SDL_error(
"SetVideoMode", __FILE__, __LINE__);
254 SDL_FreeSurface( fogmap);
259 mapsurface->format->BitsPerPixel,
260 mapsurface->format->Rmask,
261 mapsurface->format->Gmask,
262 mapsurface->format->Bmask,
263 mapsurface->format->Amask);
265 if( fogmap == NULL) {
266 do_SDL_error(
"SDL_CreateRGBSurface", __FILE__, __LINE__);
273 if( SDL_SetAlpha( fogmap, SDL_SRCALPHA|SDL_RLEACCEL, 128) < 0) {
274 do_SDL_error(
"SDL_SetAlpha", __FILE__, __LINE__);
278 if( just_lightmap != 0 && lightmap) {
279 SDL_FreeSurface( lightmap);
284 mapsurface->format->BitsPerPixel,
285 mapsurface->format->Rmask,
286 mapsurface->format->Gmask,
287 mapsurface->format->Bmask,
288 mapsurface->format->Amask);
289 if( lightmap == NULL) {
290 do_SDL_error(
"SDL_CreateRGBSurface", __FILE__, __LINE__);
296 lightmap= SDL_DisplayFormatAlpha( lightmap);
297 if( lightmap == NULL) {
298 do_SDL_error(
"DisplayFormatAlpha", __FILE__, __LINE__);
303 overlay_grid( TRUE, 0, 0);
343 int startx,
int starty,
int endx,
int endy,
344 int destx,
int desty)
348 for (x=startx; x<endx; x++) {
349 top= ((x*(tr-tl))/
width)+tl;
350 bottom= ((x*(br-bl))/
width)+bl;
351 for (y=starty; y<endy; y++) {
352 val=((y*(bottom-top))/
height)+top;
360 putpixel(lightmap, destx+x-startx, desty+y-starty,
361 SDL_MapRGBA(lightmap->format, 0, 0, 0, val));
369 #define ALPHA_FUDGE(x) (2*(x) / 3)
370 #define GENDARK(x,y) ( (((x)&(y) & 1) == 1)?255:0 )
404 static void do_sdl_per_pixel_lighting(
int x,
int y,
int mx,
int my)
407 int dark0, dark1, dark2, dark3, dark4;
415 if (y-1 < 0 || !
mapdata_cell(mx, my-1)->have_darkness) {
433 if (x-1 < 0 || !
mapdata_cell(mx-1, my)->have_darkness) {
453 SDL_FillRect(mapsurface,&dst, SDL_MapRGB(mapsurface->format, 0, 0, 0));
455 SDL_FillRect(lightmap,NULL, SDL_MapRGBA(lightmap->format, 0, 0, 0,
mapdata_cell(mx, my)->darkness));
456 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
480 if (dark1 == dark0) {
489 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
497 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
504 if (dark3 == dark0) {
509 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
517 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
528 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
530 if (dark4 == dark0) {
535 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
542 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
545 if (dark2 == dark0) {
550 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0, ALPHA_FUDGE(dark0)));
558 SDL_FillRect(lightmap, &dst, SDL_MapRGBA(lightmap->format, 0, 0, 0,
563 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
567 static int *darkx=NULL, *darky=NULL,darkx_allocated=0;
594 SDL_LockSurface( lightmap);
598 putpixel(lightmap, dx, dy, SDL_MapRGBA(lightmap->format, 0, 0, 0,(darkx[dx] + darky[dy])/2));
602 int dark5, dark6, dark7, dark8;
605 dark5 = (dark1+dark2)>>1;
613 dark6 = (dark2+dark3)>>1;
620 dark7 = (dark3+dark4)>>1;
625 if ( (x-1 < 0) || (y-1 < 0)
627 dark8 = (dark4+dark1)>>1;
656 SDL_UnlockSurface(lightmap);
657 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
673 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,weight,weightC;
697 for (i=0; i<=layer; i++) {
707 for (i=0; i<8; i++) {
727 for (i=0; i<8; i++) {
728 if ( (slevels[i]>0) && (!partdone[i]) &&
729 ((lowest<0) || (slevels[i]<slevels[lowest]))
749 for (i=0; i<8; i++) {
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) {
765 smoothface=sfaces[lowest];
774 if ( (!
pixmaps[smoothface]->map_image) ||
782 if (SDL_BlitSurface(
pixmaps[smoothface]->fog_image,
783 &src, mapsurface, &dst)) {
784 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
787 if (SDL_BlitSurface(
pixmaps[smoothface]->map_image,
788 &src, mapsurface, &dst)) {
789 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
797 if (SDL_BlitSurface(
pixmaps[smoothface]->fog_image,
798 &src, mapsurface, &dst)) {
799 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
802 if (SDL_BlitSurface(
pixmaps[smoothface]->map_image,
803 &src, mapsurface, &dst)) {
804 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
821 static void update_redrawbitmap(
void)
878 static void display_mapcell(
int ax,
int ay,
int mx,
int my)
884 dst.x = ax*scaled_image_size;
885 dst.y = ay*scaled_image_size;
886 dst.w = scaled_image_size;
887 dst.h = scaled_image_size;
888 SDL_FillRect(mapsurface, &dst, SDL_MapRGB(mapsurface->format, 0, 0, 0));
894 for (layer=0; layer<
MAXLAYERS; layer++) {
899 if (face > 0 &&
pixmaps[face]->map_image != NULL) {
903 src.x = w-scaled_image_size;
904 src.y = h-scaled_image_size;
905 src.w = scaled_image_size;
906 src.h = scaled_image_size;
907 dst.x = ax*scaled_image_size;
908 dst.y = ay*scaled_image_size;
910 if (SDL_BlitSurface(
pixmaps[face]->fog_image, &src, mapsurface, &dst)) {
911 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
914 if (SDL_BlitSurface(
pixmaps[face]->map_image, &src, mapsurface, &dst)) {
915 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
926 drawsmooth_sdl (mx,my,layer,dst);
931 if (face > 0 &&
pixmaps[face]->map_image != NULL) {
938 int dx, dy, sourcex, sourcey, offx, offy;
941 offx = dx?(scaled_image_size-dx):0;
944 sourcex = sx * scaled_image_size - offx ;
951 offy = dy?(scaled_image_size-dy):0;
954 sourcey = sy * scaled_image_size - offy;
962 src.w = scaled_image_size - offx;
963 src.h = scaled_image_size - offy;
964 dst.x = ax*scaled_image_size + offx;
965 dst.y = ay*scaled_image_size + offy;
967 if (SDL_BlitSurface(
pixmaps[face]->fog_image, &src, mapsurface, &dst)) {
968 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
971 if (SDL_BlitSurface(
pixmaps[face]->map_image, &src, mapsurface, &dst)) {
972 do_SDL_error(
"BlitSurface", __FILE__, __LINE__);
980 dst.x = ax*scaled_image_size;
981 dst.y = ay*scaled_image_size;
982 dst.w = scaled_image_size;
983 dst.h = scaled_image_size;
990 SDL_FillRect(mapsurface,&dst, SDL_MapRGB(mapsurface->format, 0, 0, 0));
992 SDL_SetAlpha(lightmap, SDL_SRCALPHA|SDL_RLEACCEL,
mapdata_cell(mx, my)->darkness);
993 SDL_BlitSurface(lightmap, NULL, mapsurface, &dst);
996 do_sdl_per_pixel_lighting(ax, ay, mx, my);
1025 int x, y, num_spaces = 0, num_drawn = 0;
1026 if (redrawbitmap == NULL) {
1029 update_redrawbitmap();
1047 SDL_Flip(mapsurface);
1061 if (dx > 15 || dx < -15 || dy > 15 || dy < 15)
1068 SDL_LockSurface( mapsurface);
1071 memmove( mapsurface->pixels + offset, mapsurface->pixels,
1073 }
else if( dy > 0) {
1075 memmove( mapsurface->pixels, mapsurface->pixels + offset,
1081 for( y= 0; y < mapsurface->h; y++) {
1083 char* start_of_row= mapsurface->pixels + mapsurface->pitch * y;
1085 memmove( start_of_row + offset, start_of_row,
1086 mapsurface->pitch - offset);
1088 char* start_of_row= mapsurface->pixels + mapsurface->pitch * y;
1090 memmove( start_of_row, start_of_row + offset,
1091 mapsurface->pitch - offset);
1095 SDL_UnlockSurface( mapsurface);