没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
瑾供交流学习之用,本人不保证不会出现翻译错误 : )
1
关于 sdl
sdl
sdl
sdl 的翻译 (
(
(
( 主要是介绍部分 )
)
)
)
1. 介绍 ................................................................................................
1
2. 图形和视频 .......................................................................................
2
2.1SDL 的视频介绍 .......................................................................
2
2.1.1 初始化视频显示 ...............................................................
2
2.1.2 初始化最好的视频模式 ...................................................
3
2.1.3 读取和显示一个 bmp 文件 ...............................................
3
2.1.4 直接绘图来显示 ...............................................................
4
2.2 在 sdl 中使用 opengl .................................................................
5
2.2.1 初始化 ..............................................................................
6
2.2.2 绘制 .................................................................................
8
3 输入处理 .........................................................................................
17
3.1 控制杆处理 .............................................................................
17
3.1.1 初始化 ............................................................................
17
3.1.2 询问 ...............................................................................
17
3.1.3 打开一个控制杆接受控制杆事件 ...................................
18
3.1.4 优化的控制杆功能 .........................................................
20
3.2 处理键盘 ................................................................................
21
3.2.1 键盘相关结构体 .............................................................
21
3.2.2SDLKey ...........................................................................
21
3.2.3SDLMod ..........................................................................
22
3.2.4SDL_keysym ...................................................................
22
瑾供交流学习之用,本人不保证不会出现翻译错误 : )
2
3.2.5SDL_KeyboardEvent ........................................................
22
3.2.6 读取键盘事件 .................................................................
22
3.2.7 一个更加详细的查看 .....................................................
23
3.2.8 游戏模式输入 .................................................................
26
1.
1.
1.
1. 介绍
SDL 是一个由 8 个子系统组成 —— 音频 、 CDROM 、 事件驱动 、 文件 I/O 、 操作杆驱动
、
线程 、 时钟和视频 。 在你使用这些子系统之前 , 你必须要初始化他们 , 使用 SDL_Init ( 或 者
SDL_InitSubSystem 。 SDL_Init 必须在其他 SDL 功能被调用之前调用 。 所以初始化默认的子
系统和视频子系统你需要调用 SDL_Init(SDL_INIT_VIDEO) , 而初始化默认的子系统 、 视频
系统和时间系统你需要调用 SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_TIMER ) )
SDL_Init 的退出使用 SDL_Quit (和 SDL_QuitSubSystem ) SDL_Quit 关掉所有子系统
,
包括默认的,它应该被调用在 SDL 应用退出之前。
SDL_Init 和 SDL_Quit 坚固的嵌入你的程序工具箱 , 你能写你的第一个和最基础的 SD
L
应用。但是,我们必须准备处理错误。许多 SDL 功能返回一个值指出这个功能是成功或者
失败 , 例如 , SDL_Init 返回 -1 如果不能初始化一个子系统 。 SDL 提供一个有用的设备允许
你精确的定位错误的类型,每次一个 SDL 内错误发生一个错误信息被存储,使 用
SDL_GetError 可以找到。经常使用这个,你能知道错误的很多。
例子 1 —— 1 初始化 SDL
#include "SDL.h" /* All SDL App's need this */
#include <stdio.h>
int
main() {
printf("Initializing SDL.\n");
/* Initialize defaults, Video and Audio */
if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
}
printf("SDL initialized.\n");
printf("Quiting SDL.\n");
/* Shutdown
all
subsystems */
SDL_Quit();
printf("Quiting....\n");
exit(0);
}
瑾供交流学习之用,本人不保证不会出现翻译错误 : )
3
2.
2.
2.
2. 图形和视频
2.1SDL
2.1SDL
2.1SDL
2.1SDL 的视频介绍
的视频介绍
的视频介绍
的视频介绍
视频也许是最常用的 SDL 的应用地方,所以它有最全套的子系统。这里是一些例子来
显示这个基础。
2.1.1
2.1.1
2.1.1
2.1.1 初始化视频显示
例子 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 ) {
fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
SDL_GetError());
exit(1);
}
2.1.2
2.1.2
2.1.2
2.1.2 初始化最好的视频模式
如果你有一个偏好一个可靠的像素深度但是将要接受其他的,那么如下使 用
SDL_SetVideoMode 和 SDL_ANYFORMAT 。你也能使用 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());
瑾供交流学习之用,本人不保证不会出现翻译错误 : )
4
exit(1);
}
printf("Set 640x480 at %d bits-per-pixel mode\n",
screen->format->BitsPerPixel);
2.1.3
2.1.3
2.1.3
2.1.3 读取和显示一个 bmp
bmp
bmp
bmp 文件
下面的功能读取和显示一个作为参数给出 的 bm p 文件 , 前 提 SD
L
被初始化视频模式被设置 。
例子 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
*/
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);
}
2.1.4
2.1.4
2.1.4
2.1.4 直接绘图来显示
以下的两个功能能够用于得到和设置单独的一个平面的像素 。 他们仔细的被写和任何当
前 SDL 的深度工作 。 基础要锁定这个表面再调用他们 , 在调用其他 SDL 功能之前需要解锁 。
瑾供交流学习之用,本人不保证不会出现翻译错误 : )
5
在像素值和他们的红 、 绿 、 蓝部件之间转换 , 使用 SDL_GetRGB() 和 SDL_MapRGB() 。
例子 2 —— 4getpixel()
/*
* 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];
else
return p[0] | p[1] << 8 | p[2] << 16;
case
4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
下面的代码使用 putpixel() 功能来设置一个黄色像素在屏幕中间
例子 2 —— 6 使用 putpixel()
/* Code to set a yellow pixel at the center of the screen */
int
x, y;
Uint32 yellow;
/* Map the color yellow to this display (R=0xff, G=0xFF, B=0x00)
Note: If the display is palettized, you must set the palette first.
*/
yellow = SDL_MapRGB(screen->format, 0xff, 0xff, 0x00);
剩余29页未读,继续阅读
资源评论
nervfzb
- 粉丝: 2
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功