[Xbubble-commits] xbubble-sdl/src graphic_utils.c, 1.2, 1.3 graphic_utils.h, 1.3, 1.4 theme.c, 1.6, 1.7

Martin Quinson mquinson at alioth.debian.org
Fri Sep 15 09:55:31 UTC 2006


Update of /cvsroot/xbubble/xbubble-sdl/src
In directory haydn:/tmp/cvs-serv7310

Modified Files:
	graphic_utils.c graphic_utils.h theme.c 
Log Message:
Fix the computation of the several canon animations by implementing our own rotocrop function to rotate an image with cropping the borders

Index: graphic_utils.c
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/graphic_utils.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- graphic_utils.c	13 Sep 2006 22:51:22 -0000	1.2
+++ graphic_utils.c	15 Sep 2006 09:55:29 -0000	1.3
@@ -1,9 +1,10 @@
+#include <math.h>
 #include <stdlib.h>
 #include "gettext.h"
 #include "utils.h"
 #include "graphic_utils.h"
 
-#include "SDL_framerate.h"
+#include <SDL/SDL_framerate.h>
 
 SDL_Surface *screen;
 
@@ -45,6 +46,84 @@
 
   return screen;
 }
+/* **************************************************************** */
+/* SDL surface rotation without changing the dimensions             */
+/*                                                                  */
+/* The center of the image is not moved, and parts beyond current   */
+/*  size are cropped.                                               */
+/* This is loosely inspirated from the libsdl-gfx functions.        */
+/* (thanks dudes for your LGPL code)                                */
+/* **************************************************************** */
+
+SDL_Surface *surface_rotocrop(SDL_Surface *surf, double angle) {
+      
+   SDL_Surface *src,*dst; 
+   int is_src_converted;
+   
+   /* Make sure the src is 32bits */
+   if (surf->format->BitsPerPixel == 32) {
+      src = surf;
+      is_src_converted = 0;
+   } else {
+      src = SDL_CreateRGBSurface(SDL_SWSURFACE, surf->w, surf->h, 32,
+				 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
+      SDL_BlitSurface(surf, NULL, src, NULL);
+      is_src_converted = 1;
+      warn("Surface given to rotocrop not 32bits. This is sub-optimal.");
+   }
+   /* Alloc the dest surface */
+   dst = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
+			      src->format->Rmask, src->format->Gmask,
+			      src->format->Bmask, src->format->Amask);
+   
+   /* Lock the source */
+   SDL_LockSurface(src);
+   
+   /* Rotate the image */
+   int xdst,ydst;       /* iterators on dst surf */
+   int xcenter,ycenter; /* Center of rotation : center of image */
+   int xsrc,ysrc;       /* result of computation: where to take the point from */
+   
+   double cos_angle = cos(angle);
+   double sin_angle = sin(angle);
+   
+   xcenter = src->w/2;
+   ycenter = src->h/2;
+   
+   for (xdst=0; xdst<dst->w; xdst++) {
+      for (ydst=0; ydst<dst->h; ydst++) {
+	 double xdst_center, ydst_center; /* coords from dst point wrt center and not (0,0) */
+	 
+	 xdst_center = (double)(xdst-xcenter);
+	 ydst_center = (double)(ydst-ycenter);
+	 /* compute where to get the point from */	 
+	 xsrc = xdst_center * cos_angle - ydst_center * sin_angle + xcenter;
+	 	 
+	 if ((xsrc>0) && (xsrc<src->w)) { /* If x is out of cropped zone, don't compute y */
+	    
+	    ysrc = xdst_center * sin_angle + ydst_center * cos_angle + ycenter;
+	    if ((ysrc>0) && (ysrc<src->h)) {	      
+	       /* if inside cropped zone, do the copy */
+	       Uint32 *from = (Uint32*) (((char*)src->pixels) + (ysrc*dst->pitch + xsrc*4));
+	       Uint32 *to   = (Uint32*) (((char*)dst->pixels) + (ydst*dst->pitch + xdst*4));
+	       *to = *from;
+	    }
+	 }
+	 /* in case you wonder, SDL fills the surface with transparent color at creation */
+      }
+   }
+   
+   /* Turn on source-alpha support */
+   SDL_SetAlpha(dst, SDL_SRCALPHA, 255);
+   
+   /* unlock the source and free it if it was a temp */
+   SDL_UnlockSurface(src);
+   if (is_src_converted) 
+     SDL_FreeSurface(src);   
+   
+   return dst;
+}
+
 
 /* ********************* */
 /* Frame rate management */

Index: graphic_utils.h
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/graphic_utils.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- graphic_utils.h	14 Sep 2006 08:01:21 -0000	1.3
+++ graphic_utils.h	15 Sep 2006 09:55:29 -0000	1.4
@@ -9,6 +9,8 @@
 extern SDL_Surface *screen; /* game.c needs this to toggle fullscreen */
 SDL_Surface *window_make(int width, int height);
 
+SDL_Surface *surface_rotocrop(SDL_Surface *surf, double angle);
+
 /* to compute the length of an animation, do <nb_sec> * fps_get() */
 int fps_get(void);
 void fps_set(int fps); 

Index: theme.c
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/theme.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- theme.c	14 Sep 2006 12:35:41 -0000	1.6
+++ theme.c	15 Sep 2006 09:55:29 -0000	1.7
@@ -5,7 +5,6 @@
 
 #include <SDL.h>
 #include <SDL/SDL_image.h>
-#include <SDL/SDL_rotozoom.h>
 
 #include "setting.h"
 #include "gettext.h"
@@ -13,6 +12,8 @@
 #include "frame.h"
 
 #include "theme.h"
+#include "graphic_utils.h"
+
 #define ROW_HEIGHT     0.8660254037844386 /* sqrt(3)/2 */
 
 const char *game_fonts[] = FONTS;
@@ -96,9 +97,8 @@
 
   theme->canon_a = set_new( NB_ANGLES );
   frame = frame_load("canon.png",theme->name,zoom);
-//  frame = frame_load("canon2.png",theme->name,zoom*4);
   for ( i = -CANON_ANGLE_MAX; i <= CANON_ANGLE_MAX; i++ ) {
-    frame_t toadd = frame_new_raw(rotozoomSurface(frame->surf, -i*ANGLE_STEP*180.0/3.14159, 1, 1));
+    frame_t toadd = frame_new_raw(surface_rotocrop(frame->surf, -i*ANGLE_STEP));
     if (!toadd) 
       fail("Canon animation creation failed for frame %d (of %d)",i,CANON_ANGLE_MAX);
     set_add( theme->canon_a, toadd);




More information about the Xbubble-commits mailing list