没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
SDL Guide 中文译版
内容目录
SDL Guide 中文译版...........................................................................................................................1
第一章 基础...................................................................................................................................2
例 1-1 初始化 SDL.....................................................................................................................3
第二章 图像和视频.......................................................................................................................3
例 2-1 初始化视频......................................................................................................................3
例 2-2 初始化最佳视频模式......................................................................................................4
例 2-3 读取并显示 BMP 文件....................................................................................................4
例 2-4 getpixel()..........................................................................................................................5
例 2-5 putpixel()..........................................................................................................................6
例 2-6 使用上面的 putpixel()在屏幕中心画一个黄点.............................................................7
例 2-7 初始化 SDL 加 OpenGL..................................................................................................8
例 2-8 SDL 和 OpenGL............................................................................................................10
第三章上 游戏杆输入处理.........................................................................................................20
例 3-1 初始化 SDL 并带游戏杆支持......................................................................................20
例 3-2 打印所有游戏杆的名字................................................................................................20
例 3-3 启动第一个游戏杆........................................................................................................21
例 3-4 游戏杆轴事件................................................................................................................22
例 3-5 ........................................................................................................................................22
例 3-6 游戏杆按钮事件............................................................................................................22
例 3-7 轨迹球事件....................................................................................................................23
例 3-8 游戏杆 hat 事件.............................................................................................................23
例 3-9 查询游戏杆特性............................................................................................................24
第三章下 键盘输入.....................................................................................................................24
例 3-10 读键盘消息..................................................................................................................25
例 3-11 解释按键消息..............................................................................................................25
例 3-12 正确的运动控制..........................................................................................................29
第四章 样例.................................................................................................................................31
最快的图像平面块传送...........................................................................................................31
过滤和处理事件.......................................................................................................................34
打开音频设备...........................................................................................................................36
播放音频...................................................................................................................................36
列出所有 CDROM...................................................................................................................37
打开缺省 CDROM 驱动器......................................................................................................38
列出 CD 上所有音轨................................................................................................................39
播放 CD....................................................................................................................................39
基于时间的游戏主循环...........................................................................................................39
1
SDL 即 Simple DirectMedia Layer,类似 DirectX,是完整的游戏、多媒体开发包,但不
同的是它跨越几乎所有的平台,有各种语言的接口,多种语言的文档,而这一切都是广大志
愿者完成的。
类似 DirectX,是完整的游戏、多媒体开发包,但不同的是它跨越几乎所有的平台,有各种
语言的接口,多种语言的文档,而这一切都是广大志愿者完成的。目前扩展部分还没有正式
的文档,以下为核心部分文档的向导部分。
SDL Guide
SDL 向导中文译版
序言
关于 SDL
SDL 为方便制作能跨跃 Linux、BSD、MacOS、Win32 和 BeOS 平台,使用本地高性能媒体
接口,并且让您可以只需使用一份源代码级 API 而设计。SDL 是相当低层的 API,但使用它
可以让你以极大的灵活性写出完全跨平台的程序。
关于 SDL 文档
SDLdoc 项目即要重新编写 SDL 文档并同步更新。项目组有使用 SDL 的志愿者组成。
最新版本可在 http://sdldoc.sourceforge.net 下载
第一章 基础
初始化
SDL 由八个子系统组成――音频、CDROM、事件处理、文件 I/O、游戏杆、线程、记
时器和视频。使用前必须调用 SDL_Init 或 SDL_InitSubSystem 初始化。SDL_Init 必须早于其
他所有 SDL 调用,它将自动初始化事件处理、文件 I/O 和线程子系统,并根据参数选择启动
其他子系统。例如,初始化缺省和视频子系统:
SDL_Init(SDL_INIT_VIDEO);
初始化缺省、视频和记时器子系统:
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_Init 对应 SDL_Quit(和 SDL_QuitSubSystem)。SDL_Quit 关闭所有子系统,必须在程
序关闭前调用。
除此之外,我们还必须进行错误处理。很多 SDL 函数返回一个值指示成功与否。例如
SDL_Init 失败时返回-1。每当 SDL 出错时,错误信息被保存,并可用 SDL_GetError 取得。
例 1-1 初始化 SDL
#include "SDL.h" /* All SDL App's need this */
2
#include <stdio.h>int main()
{
printf("Initializing SDL."); /* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1))
{
printf("Could not initialize SDL: %s.", SDL_GetError());
exit(-1);
}
printf("SDL initialized.");
printf("Quiting SDL."); /* Shutdown all subsystems */
SDL_Quit();
printf("Quiting....");
exit(0);
}
第二章 图像和视频
初始化 SDL Video 显示
视频是最常用的部分,也是 SDL 最完整的子系统。下面的初始化过程是每个 SDL 程序都要
做的,即使可能有些不同。
例 2-1 初始化视频
SDL_Surface *screen;
/* Initialize the SDL library */
if( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr,
"Couldn''''t initialize SDL: %s\n", SDL_GetError());
exit(1);
}
/* Clean up on exit */
atexit(SDL_Quit);
/*
* Initialize the display in a 640x480 8-bit palettized mode,
* requesting a software surface
*/
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if ( screen == NULL ) {
3
fprintf(stderr, "Couldn''''t set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
初始化最佳视频模式
如果你希望某种色深(颜色数)但如果用户的显示器不支持也可以接受其他色深,使用加
SDL_ANYFORMAT 参数的 SDL_SetVideoMode。您还可以用 SDL_VideoModeOK 来找到与
请求模式最接近的模式。
例 2-2 初始化最佳视频模式
/* Have a preference for 8-bit, but accept any depth */
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE|SDL_ANYFORMAT);
if ( screen == NULL ) {
fprintf(stderr, "Couldn''''t set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
printf("Set 640x480 at %d bits-per-pixel mode\n",
screen->format->BitsPerPixel);
读取并显示 BMP 文件
当 SDL 已经初始化,视频模式已经选择,下面的函数将读取并显示指定的 BMP 文件。
例 2-3 读取并显示 BMP 文件
void display_bmp(char *file_name)
{
SDL_Surface *image;
/* Load the BMP file into a surface */
image = SDL_LoadBMP(file_name);
if (image == NULL) {
fprintf(stderr, "Couldn''''t load %s: %s\n", file_name, SDL_GetError());
return;
}
/*
* Palettized screen modes will have a default palette (a standard
* 8*8*4 colour cube), but if the image is palettized as well we can
* use that palette for a nicer colour matching
*/
4
if (image->format->palette && screen->format->palette) {
SDL_SetColors(screen, image->format->palette->colors, 0,
image->format->palette->ncolors);
}
/* Blit onto the screen surface */
if(SDL_BlitSurface(image, NULL, screen, NULL) < 0)
fprintf(stderr, "BlitSurface error: %s\n", SDL_GetError());
SDL_UpdateRect(screen, 0, 0, image->w, image->h);
/* Free the allocated BMP surface */
SDL_FreeSurface(image);
}
直接在显示上绘图
下面两个函数实现在图像平面的像素读写。它们被细心设计成可以用于所有色深。记住在使
用前要先锁定图像平面,之后要解锁。
在像素值和其红、绿、蓝值间转换,使用 SDL_GetRGB()和 SDL_MapRGB()。
例 2-4 getpixel()
/*
* Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!
*/
Uint32 getpixel(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;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
5
剩余39页未读,继续阅读
hfw_1987
- 粉丝: 36
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页