⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl_rotozoom.c

📁 国外一套很好的游戏代码,款经典的小游戏 希望大家提出宝贵意见 让我们一起进步
💻 C
📖 第 1 页 / 共 2 页
字号:
	    }	    pc = (tColorRGBA *) ((Uint8 *) pc + gap);	}    }}/*   8bit Rotozoomer without smoothing Rotates and zoomes 8bit palette/Y 'src' surface to 'dst' surface. */void transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos){    int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay, sw, sh;    tColorY *pc, *sp;    int gap;    /*     * Variable setup      */    xd = ((src->w - dst->w) << 15);    yd = ((src->h - dst->h) << 15);    ax = (cx << 16) - (icos * cx);    ay = (cy << 16) - (isin * cx);    sw = src->w - 1;    sh = src->h - 1;    pc = dst->pixels;    gap = dst->pitch - dst->w;    /*     * Clear surface to colorkey      */    memset(pc, (unsigned char) (src->format->colorkey & 0xff), dst->pitch * dst->h);    /*     * Iterate through destination surface      */    for (y = 0; y < dst->h; y++) {	dy = cy - y;	sdx = (ax + (isin * dy)) + xd;	sdy = (ay - (icos * dy)) + yd;	for (x = 0; x < dst->w; x++) {	    dx = (short) (sdx >> 16);	    dy = (short) (sdy >> 16);	    if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {		sp = (tColorY *) (src->pixels);		sp += (src->pitch * dy + dx);		*pc = *sp;	    }	    sdx += icos;	    sdy += isin;	    pc++;	}	pc += gap;    }}/*   rotozoomSurface() Rotates and zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. 'angle' is the rotation in degrees. 'zoom' a scaling factor. If 'smooth' is 1 then the destination 32bit surface is anti-aliased. If the surface is not 8bit or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.*/#define VALUE_LIMIT	0.001/* Local rotozoom-size function with trig result return */void rotozoomSurfaceSizeTrig(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight,			     double *canglezoom, double *sanglezoom){    double x, y, cx, cy, sx, sy;    double radangle;    int dstwidthhalf, dstheighthalf;    /*     * Determine destination width and height by rotating a centered source box      */    radangle = angle * (M_PI / 180.0);    *sanglezoom = sin(radangle);    *canglezoom = cos(radangle);    *sanglezoom *= zoom;    *canglezoom *= zoom;    x = width / 2;    y = height / 2;    cx = *canglezoom * x;    cy = *canglezoom * y;    sx = *sanglezoom * x;    sy = *sanglezoom * y;    dstwidthhalf = MAX((int)		       ceil(MAX(MAX(MAX(fabs(cx + sy), fabs(cx - sy)), fabs(-cx + sy)), fabs(-cx - sy))), 1);    dstheighthalf = MAX((int)			ceil(MAX(MAX(MAX(fabs(sx + cy), fabs(sx - cy)), fabs(-sx + cy)), fabs(-sx - cy))), 1);    *dstwidth = 2 * dstwidthhalf;    *dstheight = 2 * dstheighthalf;}/* Publically available rotozoom-size function */void rotozoomSurfaceSize(int width, int height, double angle, double zoom, int *dstwidth, int *dstheight){    double dummy_sanglezoom, dummy_canglezoom;    rotozoomSurfaceSizeTrig(width, height, angle, zoom, dstwidth, dstheight, &dummy_sanglezoom, &dummy_canglezoom);}/* Publically available rotozoom function */SDL_Surface *rotozoomSurface(SDL_Surface * src, double angle, double zoom, int smooth){    SDL_Surface *rz_src;    SDL_Surface *rz_dst;    double zoominv;    double sanglezoom, canglezoom, sanglezoominv, canglezoominv;    int dstwidthhalf, dstwidth, dstheighthalf, dstheight;    int is32bit;    int i, src_converted;    /*     * Sanity check      */    if (src == NULL)	return (NULL);    /*     * Determine if source surface is 32bit or 8bit      */    is32bit = (src->format->BitsPerPixel == 32);    if ((is32bit) || (src->format->BitsPerPixel == 8)) {	/*	 * Use source surface 'as is' 	 */	rz_src = src;	src_converted = 0;    } else {	/*	 * New source surface is 32bit with a defined RGBA ordering 	 */	rz_src =	    SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);	SDL_BlitSurface(src, NULL, rz_src, NULL);	src_converted = 1;	is32bit = 1;    }    /*     * Sanity check zoom factor      */    if (zoom < VALUE_LIMIT) {	zoom = VALUE_LIMIT;    }    zoominv = 65536.0 / (zoom * zoom);    /*     * Check if we have a rotozoom or just a zoom      */    if (fabs(angle) > VALUE_LIMIT) {	/*	 * Angle!=0: full rotozoom 	 */	/*	 * ----------------------- 	 */	/* Determine target size */	rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, zoom, &dstwidth, &dstheight, &canglezoom, &sanglezoom);	/*	 * Calculate target factors from sin/cos and zoom 	 */	sanglezoominv = sanglezoom;	canglezoominv = canglezoom;	sanglezoominv *= zoominv;	canglezoominv *= zoominv;	/* Calculate half size */	dstwidthhalf = dstwidth / 2;	dstheighthalf = dstheight / 2;	/*	 * Alloc space to completely contain the rotated surface 	 */	rz_dst = NULL;	if (is32bit) {	    /*	     * Target surface is 32bit with source RGBA/ABGR ordering 	     */	    rz_dst =		SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,				     rz_src->format->Rmask, rz_src->format->Gmask,				     rz_src->format->Bmask, rz_src->format->Amask);	} else {	    /*	     * Target surface is 8bit 	     */	    rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);	}	/*	 * Lock source surface 	 */	SDL_LockSurface(rz_src);	/*	 * Check which kind of surface we have 	 */	if (is32bit) {	    /*	     * Call the 32bit transformation routine to do the rotation (using alpha) 	     */	    transformSurfaceRGBA(rz_src, rz_dst, dstwidthhalf, dstheighthalf,				 (int) (sanglezoominv), (int) (canglezoominv), smooth);	    /*	     * Turn on source-alpha support 	     */	    SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);	} else {	    /*	     * Copy palette and colorkey info 	     */	    for (i = 0; i < rz_src->format->palette->ncolors; i++) {		rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];	    }	    rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;	    /*	     * Call the 8bit transformation routine to do the rotation 	     */	    transformSurfaceY(rz_src, rz_dst, dstwidthhalf, dstheighthalf,			      (int) (sanglezoominv), (int) (canglezoominv));	    SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);	}	/*	 * Unlock source surface 	 */	SDL_UnlockSurface(rz_src);    } else {	/*	 * Angle=0: Just a zoom 	 */	/*	 * -------------------- 	 */	/*	 * Calculate target size	 */	zoomSurfaceSize(rz_src->w, rz_src->h, zoom, zoom, &dstwidth, &dstheight);	/*	 * Alloc space to completely contain the zoomed surface 	 */	rz_dst = NULL;	if (is32bit) {	    /*	     * Target surface is 32bit with source RGBA/ABGR ordering 	     */	    rz_dst =		SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,				     rz_src->format->Rmask, rz_src->format->Gmask,				     rz_src->format->Bmask, rz_src->format->Amask);	} else {	    /*	     * Target surface is 8bit 	     */	    rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);	}	/*	 * Lock source surface 	 */	SDL_LockSurface(rz_src);	/*	 * Check which kind of surface we have 	 */	if (is32bit) {	    /*	     * Call the 32bit transformation routine to do the zooming (using alpha) 	     */	    zoomSurfaceRGBA(rz_src, rz_dst, smooth);	    /*	     * Turn on source-alpha support 	     */	    SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);	} else {	    /*	     * Copy palette and colorkey info 	     */	    for (i = 0; i < rz_src->format->palette->ncolors; i++) {		rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];	    }	    rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;	    /*	     * Call the 8bit transformation routine to do the zooming 	     */	    zoomSurfaceY(rz_src, rz_dst);	    SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);	}	/*	 * Unlock source surface 	 */	SDL_UnlockSurface(rz_src);    }    /*     * Cleanup temp surface      */    if (src_converted) {	SDL_FreeSurface(rz_src);    }    /*     * Return destination surface      */    return (rz_dst);}/*   zoomSurface() Zoomes a 32bit or 8bit 'src' surface to newly created 'dst' surface. 'zoomx' and 'zoomy' are scaling factors for width and height. If 'smooth' is 1 then the destination 32bit surface is anti-aliased. If the surface is not 8bit or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.*/#define VALUE_LIMIT	0.001void zoomSurfaceSize(int width, int height, double zoomx, double zoomy, int *dstwidth, int *dstheight){    /*     * Sanity check zoom factors      */    if (zoomx < VALUE_LIMIT) {	zoomx = VALUE_LIMIT;    }    if (zoomy < VALUE_LIMIT) {	zoomy = VALUE_LIMIT;    }    /*     * Calculate target size      */    *dstwidth = (int) ((double) width * zoomx);    *dstheight = (int) ((double) height * zoomy);    if (*dstwidth < 1) {	*dstwidth = 1;    }    if (*dstheight < 1) {	*dstheight = 1;    }}SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smooth){    SDL_Surface *rz_src;    SDL_Surface *rz_dst;    int dstwidth, dstheight;    int is32bit;    int i, src_converted;    /*     * Sanity check      */    if (src == NULL)	return (NULL);    /*     * Determine if source surface is 32bit or 8bit      */    is32bit = (src->format->BitsPerPixel == 32);    if ((is32bit) || (src->format->BitsPerPixel == 8)) {	/*	 * Use source surface 'as is' 	 */	rz_src = src;	src_converted = 0;    } else {	/*	 * New source surface is 32bit with a defined RGBA ordering 	 */	rz_src =	    SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);	SDL_BlitSurface(src, NULL, rz_src, NULL);	src_converted = 1;	is32bit = 1;    }    /* Get size if target */    zoomSurfaceSize(rz_src->w, rz_src->h, zoomx, zoomy, &dstwidth, &dstheight);    /*     * Alloc space to completely contain the zoomed surface      */    rz_dst = NULL;    if (is32bit) {	/*	 * Target surface is 32bit with source RGBA/ABGR ordering 	 */	rz_dst =	    SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 32,				 rz_src->format->Rmask, rz_src->format->Gmask,				 rz_src->format->Bmask, rz_src->format->Amask);    } else {	/*	 * Target surface is 8bit 	 */	rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight, 8, 0, 0, 0, 0);    }    /*     * Lock source surface      */    SDL_LockSurface(rz_src);    /*     * Check which kind of surface we have      */    if (is32bit) {	/*	 * Call the 32bit transformation routine to do the zooming (using alpha) 	 */	zoomSurfaceRGBA(rz_src, rz_dst, smooth);	/*	 * Turn on source-alpha support 	 */	SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255);    } else {	/*	 * Copy palette and colorkey info 	 */	for (i = 0; i < rz_src->format->palette->ncolors; i++) {	    rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];	}	rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;	/*	 * Call the 8bit transformation routine to do the zooming 	 */	zoomSurfaceY(rz_src, rz_dst);	SDL_SetColorKey(rz_dst, SDL_SRCCOLORKEY | SDL_RLEACCEL, rz_src->format->colorkey);    }    /*     * Unlock source surface      */    SDL_UnlockSurface(rz_src);    /*     * Cleanup temp surface      */    if (src_converted) {	SDL_FreeSurface(rz_src);    }    /*     * Return destination surface      */    return (rz_dst);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -