在我的SDL系列文章中的上一篇我已经介绍了关于SDL的安装过程及其详细步骤,下面就开始我们的SDL征途吧。
这一篇我着重为大家讲解如何利用SDL做一个简单的颜色自动变换调色板。
根据SDL官方文档我们很容易就能够获得建立一个屏幕的步骤:
首先我们要初始化一个Video,而后获得一个SDL_Surface;
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
如果函数调用成功则一切OK,
SDL给用户提供了很多画图的API,它们在SDL_gfxPrimitives.h文件中
在这里我们只用其中的一个来实现我们的功能
- boxRGBA(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2,
- Sint16 y2, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
这个函数就可以实现在dst上显示出我们所要绘制的图案了...是不是感觉很high呀...就这么简单
下面我就把具体的代码贴出来供大家分享下...
- #include <SDL.h>
- #include <SDL_gfxPrimitives.h>
- #include <SDL_image.h>
- #include <SDL_rotozoom.h>
- #include <SDL_ttf.h>
- #include <stdio.h>
- int main(int argc,char *argv[])
- {
- if(SDL_Init(SDL_INIT_VIDEO) < 0 )
- {
- printf("Init error\n");
- return -1;
- }
- SDL_Surface *screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
- if(!screen)
- {
- printf("Init video mode error\n");
- return -1;
- }
- int i = 255;
- for(; i >= 0; i--)
- {
- SDL_FillRect(screen, &screen->clip_rect, 0x0);
- boxRGBA(screen, 100, 100, 300, 300, 255, 0, 0, i);
- SDL_Delay(20);
- SDL_UpdateRects(screen,1,&screen->clip_rect);
- }
- SDL_Quit();
- return 0;
- }
本文转自 驿落黄昏 51CTO博客,原文链接:http://blog.51cto.com/yiluohuanghun/832855,如需转载请自行联系原作者