您的位置:首页 > 其它

提供了SDL非常多的基础功能、包括画线画圆各种函数包和缩放图形!

2012-07-13 11:07 429 查看
可以在这里下载:http://www.ferzkopp.net/joomla/content/view/19/14/

名为SDL_gfx、这里是它的API文档http://www.ferzkopp.net/Software/SDL_gfx-2.0/Docs/html/index.html

当然吗如果迩仅仅是需要缩放的话而不需要其它功能也可以自己写个简单的缩放图形的函数、这里是

这里提供完整的缩放代码:

Uint32 ReadPixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
return *p;
break;

case 2:
return *(Uint16 *)p;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;

case 4:
return *(Uint32 *)p;
break;

default:
return 0;       /* shouldn't happen, but avoids warnings */
}
}

void DrawPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
}
}

SDL_Surface* SDL_ScaleSurface(SDL_Surface *Surface, Uint16 Width, Uint16 Height)
{
SDL_Surface *_ret = SDL_CreateRGBSurface(Surface->flags, Width, Height, Surface->format->BitsPerPixel,
Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);

double    _stretch_factor_x = (static_cast<double>(Width)  / static_cast<double>(Surface->w)),
_stretch_factor_y = (static_cast<double>(Height) / static_cast<double>(Surface->h));

for(Sint32 y = 0; y < Surface->h; y++) //Run across all Y pixels.
for(Sint32 x = 0; x < Surface->w; x++) //Run across all X pixels.
for(Sint32 o_y = 0; o_y < _stretch_factor_y; ++o_y) //Draw _stretch_factor_y pixels for each Y pixel.
for(Sint32 o_x = 0; o_x < _stretch_factor_x; ++o_x) //Draw _stretch_factor_x pixels for each X pixel.
DrawPixel(_ret, static_cast<Sint32>(_stretch_factor_x * x) + o_x,
static_cast<Sint32>(_stretch_factor_y * y) + o_y, ReadPixel(Surface, x, y));

return _ret;
}


最后迩只需要简单的调用SDL_ScaleSurface就可以了、这个代码的实现非常的简单、先输入长宽、然后计算输出的表面长宽比、如果是小于的话、就两个像素变成一个像素、如果是大于原来的表面的话、就复制两个像素代表原来的一个像素、总的来说就是这样子吧其中返回的是计算后的生成的表面、如果原来的表面没有用的话、赶紧使用SDL_FreeSurface释放掉它吧、不然又要占内存空间了、这里还依赖了两个函数写像素和读像素

原文链接:

http://www.sdltutorials.com/sdl-scale-surface

http://www.libsdl.org/cgi/docwiki.cgi/Pixel_Access
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐