Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2003:
[Freeciv-Dev] Re: (PR#3374) struct Sprite "hack" in SDL client
Home

[Freeciv-Dev] Re: (PR#3374) struct Sprite "hack" in SDL client

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#3374) struct Sprite "hack" in SDL client
From: "Raimar Falke" <rf13@xxxxxxxxxxxxxxxxx>
Date: Thu, 27 Feb 2003 01:22:49 -0800
Reply-to: rt@xxxxxxxxxxxxxx

On Sun, Feb 09, 2003 at 07:34:17PM -0800, Jason Short via RT wrote:
> 
> The SDL client uses struct Sprite * basically like a void* - there is no
> struct involved; the pointer just points at an SDL_Surface.
> 
> This is bad because it is not typesafe.  The correct thing is probably
> to create a real Sprite structure, even if it does just have one element
> (an SDL_Surface*).  However, this is not quite as easy as it seems.

It is easy. See the attached patch.

Rafal: please apply.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "- Amiga Y2K fixes (a bit late, wouldn't you say?)"
    -- Linus Torvalds about linux 2.4.0 at 4 Jan 2001

diff -Nurd -X clean/diff_ignore sdl_base/client/gui-sdl/graphics.c 
sdl_test/client/gui-sdl/graphics.c
--- sdl_base/client/gui-sdl/graphics.c  Wed Feb 26 19:53:52 2003
+++ sdl_test/client/gui-sdl/graphics.c  Thu Feb 27 10:04:35 2003
@@ -1761,6 +1761,15 @@
   }
 }
 
+static struct Sprite *ctor_sprite(SDL_Surface * psurface)
+{
+  struct Sprite *result = fc_malloc(sizeof(*result));
+
+  result->psurface = psurface;
+
+  return result;
+}
+
 /**************************************************************************
   Create a new sprite by cropping and taking only the given portion of
   the image.
@@ -1781,13 +1790,13 @@
     pNew = SDL_ConvertSurface(pTmp, pTmp->format, pTmp->flags);
 
     if (!pNew) {
-      return GET_SPRI(pTmp);
+      return ctor_sprite(pTmp);
     }
 
     FREESURFACE(pTmp);
   }
 
-  return GET_SPRI(pNew);
+  return ctor_sprite(pNew);
 }
 
 /**************************************************************************
@@ -1890,7 +1899,7 @@
 
     if (SDL_BlitSurface(pBuf, NULL, pNew, NULL)) {
       FREESURFACE(pNew);
-      return GET_SPRI(pBuf);
+      return ctor_sprite(pBuf);
     }
 
     FREESURFACE(pBuf);
@@ -1899,7 +1908,7 @@
                    getpixel(pNew, pNew->w - 1, pNew->h - 1));
   }
 
-  return GET_SPRI(pNew);
+  return ctor_sprite(pNew);
 }
 
 /**************************************************************************
@@ -1907,7 +1916,9 @@
 **************************************************************************/
 void free_sprite(struct Sprite *s)
 {
-  FREESURFACE(GET_SURF(s));
+    //  FREESURFACE(GET_SURF(s));
+  s->psurface=NULL;
+  free(s);
 }
 
 
diff -Nurd -X clean/diff_ignore sdl_base/client/gui-sdl/graphics.h 
sdl_test/client/gui-sdl/graphics.h
--- sdl_base/client/gui-sdl/graphics.h  Sat Feb 22 13:52:19 2003
+++ sdl_test/client/gui-sdl/graphics.h  Thu Feb 27 09:46:37 2003
@@ -30,11 +30,10 @@
 #define        RECT_LIMIT      80
 
 struct Sprite {
-  struct SDL_Surface;
+  struct SDL_Surface *psurface;
 };
 
-#define GET_SURF(s)    fc__extension((SDL_Surface *)s)
-#define GET_SPRI(s)    fc__extension((struct Sprite *)s)
+#define GET_SURF(m_sprite)     (m_sprite->psurface)
 
 struct canvas_store {
   int rects_count;             /* update rect. list counter */
diff -Nurd -X clean/diff_ignore sdl_base/client/gui-sdl/mapview.c 
sdl_test/client/gui-sdl/mapview.c
--- sdl_base/client/gui-sdl/mapview.c   Thu Feb 27 07:45:23 2003
+++ sdl_test/client/gui-sdl/mapview.c   Thu Feb 27 10:11:57 2003
@@ -1434,9 +1434,9 @@
 static void draw_map_cell(SDL_Surface * pDest, Sint16 map_x, Sint16 map_y,
                          Uint16 map_col, Uint16 map_row, int citymode)
 {
-  SDL_Surface *pTile_sprs[80];
-  SDL_Surface *pCoasts[4] = {NULL, NULL, NULL, NULL};
-  SDL_Surface *pDither[4] = {NULL, NULL, NULL, NULL};
+  struct Sprite *pTile_sprs[80];
+  struct Sprite *pCoasts[4] = {NULL, NULL, NULL, NULL};
+  struct Sprite *pDither[4] = {NULL, NULL, NULL, NULL};
   SDL_Surface *pDitherBufs[4];
   SDL_Surface *pBufSurface = NULL;
   SDL_Rect dst , des = { map_x, map_y, 0, 0 };
@@ -1449,12 +1449,9 @@
   int full_ocean;
   int solid_bg;
   
-  count = fill_tile_sprite_array_iso((struct Sprite **) pTile_sprs,
-                                    (struct Sprite **) pCoasts,
-                                    (struct Sprite **) pDither,
-                                    map_col, map_row, citymode,
-                                    &solid_bg);
-                                    
+  count =
+      fill_tile_sprite_array_iso(pTile_sprs, pCoasts, pDither, map_col,
+                                map_row, citymode, &solid_bg);
 
   if (count == -1) { /* tile is unknown */
     SDL_BlitSurface(GET_SURF(sprites.black_tile),
@@ -1507,27 +1504,27 @@
       {  /* coasts */
         /* top */
         des.x += NORMAL_TILE_WIDTH / 4;
-        SDL_BlitSurface(pCoasts[0], NULL, pBufSurface, &des);
+        SDL_BlitSurface(GET_SURF(pCoasts[0]), NULL, pBufSurface, &des);
         des = dst;
       
         /* bottom */
         des.x += NORMAL_TILE_WIDTH / 4;
         des.y += NORMAL_TILE_HEIGHT / 2;
-        SDL_BlitSurface(pCoasts[1], NULL, pBufSurface, &des);
+        SDL_BlitSurface(GET_SURF(pCoasts[1]), NULL, pBufSurface, &des);
         des = dst;
       
         /* left */
         des.y += NORMAL_TILE_HEIGHT / 4;
-        SDL_BlitSurface(pCoasts[2], NULL, pBufSurface, &des);
+        SDL_BlitSurface(GET_SURF(pCoasts[2]), NULL, pBufSurface, &des);
         des = dst;
       
         /* right */
         des.y += NORMAL_TILE_HEIGHT / 4;
         des.x += NORMAL_TILE_WIDTH / 2;
-        SDL_BlitSurface(pCoasts[3], NULL, pBufSurface, &des);
+        SDL_BlitSurface(GET_SURF(pCoasts[3]), NULL, pBufSurface, &des);
       }
     } else {
-      SDL_BlitSurface(pTile_sprs[0], NULL, pBufSurface, &des);
+      SDL_BlitSurface(GET_SURF(pTile_sprs[0]), NULL, pBufSurface, &des);
       i++;
     }
 
@@ -1567,13 +1564,13 @@
     /*** Rest of terrain and specials ***/
   for (; i < count; i++) {
     if (pTile_sprs[i]) {
-      if (pTile_sprs[i]->w - NORMAL_TILE_WIDTH > 0
-         || pTile_sprs[i]->h - NORMAL_TILE_HEIGHT > 0) {
-       des.x -= (pTile_sprs[i]->w - NORMAL_TILE_WIDTH);
-       des.y -= (pTile_sprs[i]->h - NORMAL_TILE_HEIGHT);
-       SDL_BlitSurface(pTile_sprs[i], NULL, pBufSurface, &des);
+      if (GET_SURF(pTile_sprs[i])->w - NORMAL_TILE_WIDTH > 0
+         || GET_SURF(pTile_sprs[i])->h - NORMAL_TILE_HEIGHT > 0) {
+       des.x -= (GET_SURF(pTile_sprs[i])->w - NORMAL_TILE_WIDTH);
+       des.y -= (GET_SURF(pTile_sprs[i])->h - NORMAL_TILE_HEIGHT);
+       SDL_BlitSurface(GET_SURF(pTile_sprs[i]), NULL, pBufSurface, &des);
       } else {
-       SDL_BlitSurface(pTile_sprs[i], NULL, pBufSurface, &des);
+       SDL_BlitSurface(GET_SURF(pTile_sprs[i]), NULL, pBufSurface, &des);
       }
       des = dst;
     } else {

[Prev in Thread] Current Thread [Next in Thread]