#include <stdio.h> #include <stdlib.h> #include “SDL.h?/td> |
SDL_Surface *back; SDL_Surface *image; SDL_Surface *screen; int xpos = 0, ypos = 0; |
int InitImages() { back = SDL_LoadBMP("bg.bmp"); image = SDL_LoadBMP("image.bmp"); return 0; } |
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect); |
void DrawIMG(SDL_Surface *img, int x, int y) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_BlitSurface(img, NULL, screen, &dest); } |
![]() |
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2) { SDL_Rect dest; dest.x = x; dest.y = y; SDL_Rect dest2; dest2.x = x2; dest2.y = y2; dest2.w = w; dest2.h = h; SDL_BlitSurface(img, &dest2, screen, &dest); } |
void DrawBG() { Slock(screen); DrawIMG(back, 0, 0); Sulock(screen); } |
![]() |
void DrawScene() { Slock(screen); DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2); DrawIMG(image, xpos, ypos); SDL_Flip(screen); Sulock(screen); } |
Uint8* keysQ?/td> |
int done=0; while(done == 0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { ypos -= 1; } if ( keys[SDLK_DOWN] ) { ypos += 1; } if ( keys[SDLK_LEFT] ) { xpos -= 1; } if ( keys[SDLK_RIGHT] ) { xpos += 1; } DrawScene(); } |
![]() |
#include “SDL.h?/td> |
SDL_INIT_TIMER SDL_INIT_AUDIO SDL_INIT_VIDEO SDL_INIT_CDROM SDL_INIT_JOYSTICK SDL_INIT_NOPARACHUATE SDL_INIT_EVENTTHREAD SDL_INIT_EVERYTHING |
if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) { printf(“Unable to init SDL: %s\n? SDL_GetError()); return 1; } |
atexit(SDL_Quit); |
SDL_Surface *screen; |
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); |
If ( screen == NULL ) { printf(“Unable to set 640x480 video: %s\n? SDL_GetError()); return 1; } |
Uint8 ?相当于unsigned char Uint16 ?16?2字节) unsigned integer Uint32 ?32?4字节) unsigned integer Uint64 - 64?8字节) unsigned integer Sint8 ?相当于signed char Sint16 ?16?2字节) signed integer Sint32 ?32?4字节) signed integer Sint64 - 64?8字节) signed integer |
Uint32 init = SDL_WasInit(SDL_INIT_AUDIO); If (init & SDL_INIT_AUDIO) { sound = 1; } else { sound = 0; } |
void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); switch (screen->format->BytesPerPixel) { case 1: // Assuming 8-bpp { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: // Probably 15-bpp or 16-bpp { Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: // Slow 24-bpp mode, usually not used { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { bufp[0] = color; bufp[1] = color >> 8; bufp[2] = color >> 16; } else{ bufp[2] = color; bufp[1] = color >> 8; bufp[0] = color >> 16; } } break; case 4: // Probably 32-bpp { Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; } } |
void Slock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } } } void Sulock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); } } |
#include <stdio.h> #include <stdlib.h> #include "SDL.h" // The functions are not shown to save space void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B); void Slock(SDL_Surface *screen); void Sulock(SDL_Surface *screen); int main(int argc, char *argv[]) { if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_Surface *screen; screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if ( screen == NULL ) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); exit(1); } // DRAWING GOES HERE return 0; } |
void DrawScene(SDL_Surface *screen) { Slock(screen); for(int x=0;x<640;x++) { for(int y=0;y<480;y++) { DrawPixel(screen, x,y,y/2,y/2,x/3); } } Sulock(screen); SDL_Flip(screen); } |
SDL_Event event; |
while ( SDL_PollEvent(&event)) { if ( event.type == SDL_QUIT) { //code here? } if ( event.type == SDL_KEYDOWN) { //code here? } //?. } |
#include <stdio.h> #include <stdlib.h> #include "SDL.h" void Slock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { if ( SDL_LockSurface(screen) < 0 ) { return; } } } void Sulock(SDL_Surface *screen) { if ( SDL_MUSTLOCK(screen) ) { SDL_UnlockSurface(screen); } } void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) { Uint32 color = SDL_MapRGB(screen->format, R, G, B); switch (screen->format->BytesPerPixel) { case 1: // Assuming 8-bpp { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; *bufp = color; } break; case 2: // Probably 15-bpp or 16-bpp { Uint16 *bufp; bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; *bufp = color; } break; case 3: // Slow 24-bpp mode, usually not used { Uint8 *bufp; bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; if(SDL_BYTEORDER == SDL_LIL_ENDIAN) { bufp[0] = color; bufp[1] = color >> 8; bufp[2] = color >> 16; } else { bufp[2] = color; bufp[1] = color >> 8; bufp[0] = color >> 16; } } break; case 4: // Probably 32-bpp { Uint32 *bufp; bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; *bufp = color; } break; } } void DrawScene(SDL_Surface *screen) { Slock(screen); for(int x=0;x<640;x++) { for(int y=0;y<480;y++) { DrawPixel(screen, x,y,y/2,y/2,x/3); } } Sulock(screen); SDL_Flip(screen); } int main(int argc, char *argv[]) { if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_Surface *screen; screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if ( screen == NULL ) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); exit(1); } int done=0; while(done == 0) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } DrawScene(screen); } return 0; } |
![]() |
![]() ? |
![]() ? |
![]() ? |
![]() ? |
![]() ? |
![]() ? |
![]() ? |
![]() ? |
#include <stdlib.h> #if defined(_MSC_VER) #include "SDL.h" #else #include "SDL/SDL.h" #endif SDL_Surface *screen; void render() { // 独占资源Q将surface 锁定 if (SDL_MUSTLOCK(screen)) if (SDL_LockSurface(screen) < 0) return; // 获取当前旉Q以毫秒计时 int tick = SDL_GetTicks(); // 声明变量 int i, j, yofs, ofs; // 对窗口进行绘?br /> yofs = 0; for (i = 0; i < 480; i++) { for (j = 0, ofs = yofs; j < 640; j++, ofs++) { ((unsigned int*)screen->pixels)[ofs] = i * i + j * j + tick; } yofs += screen->pitch / 4; } // 解除锁定 if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); // 使用SDL对窗口进行更?br /> SDL_UpdateRect(screen, 0, 0, 640, 480); } // Entry point int main(int argc, char *argv[]) { // 初始化SDL子系l,q里只对视频q行初始?br /> if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } // 注册SDL_QuitQ当退出时调用Q得退出时E序自动清理 atexit(SDL_Quit); // 使用32位象素创?40x480的窗?br /> screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); // 若失败,则退?br /> if ( screen == NULL ) { fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError()); exit(1); } // d@?br /> while (1) { // Render stuff render(); // SDL中的事g轮询机制 SDL_Event event; while (SDL_PollEvent(&event)) { //Ҏ(gu)息进行处?br /> switch (event.type) { // 如果按下某键的消息响?br /> case SDL_KEYDOWN: break; //如果某键按下后弹L(fng)消息响应 case SDL_KEYUP: //若按下ESC键,则退?br /> if (event.key.keysym.sym == SDLK_ESCAPE) return 0; break; //退出消息响?br /> case SDL_QUIT: return(0); } } } return 0; } |
![]() ?0 |
什么是SDLQ?br />?Simple DirectMedia LayerQ?LGPL 许可证?/p>
免费的跨q_多媒体应用编E接?
用于游戏、游戏开发工兗模拟器、样本演C、多媒体应用{?
它能做什么?
视频、音频、事件、CDROM支持、线E、计时器、各U图象文件格式读取、快速绘图、韟뀁游戏杆?/p>
持、网l、MPEG解码{等Q且CPU字节序无关?
大体上与DirectX比较对应关系如下Q?/p>
SDL_Video、SDL_Image、OpenGL —?DirectDraw、Direct3D
SDL_Audio、SDL_Mixer —?DirectSound
SDL_Joystick、SDL_Base —?DirectInput
SDL_Net —?DirectPlay
SMPEG、SDL_Video、SDL_Audio、SDL_Sound、SDL_Filter —?DirectShow
字体、窗口管理等其他实用工具和大量样?
支持哪些q_Q?br />Linux 随系l安?
Win32 需一个到几个较小的DLL
BeOS
MacOS, MacOS X
其他非官方移?
可以在哪些编E语a中用?
几乎所有!SDL本n?C 写成Q有各种语言的接口。简单的函数调用Q不需要COM?/p>
个h观点Q不代表M团体和他人)(j)
ȝ来说QSDLq不怎么优秀Q但它是数的DX替代品之一。有Z(x)问ؓ(f)什么非得不用DXQ也没有非得
不用Q至懒人L希望用更的功夫做更多的 事。对大多Ch来说Q无论DX、OpenGLq是SDLQ都?/p>
低层APIQ尤其DXQ如果?zhn)_NDXQ那么恭喜,(zhn)的g知识一定也不错。留?j)的话就会(x)?C天的?/p>
戏很多都提供选项QDirect3D、OpenGL、Glideq是SoftwareQ既是说很多游戏厂商都根据需要设计了(jin)
自己的高层APIQ?底层是可以替换的。那么也许有水^高的厂商都是充分开发硬件功能才有高性能
的表玎ͼ那么我D个例子,很多2D游戏的渲染虽然用?DirectDrawQ但却是UY件在内存帧缓冲区?/p>
染,最后调用DirectDraw图象Swap到屏q。对?D游戏加今天的机器配置QGDI加直 接~冲操作?/p>
l够,需要更先进的渲染能力时p使用3D技术,q也是在DX8中DirectDraw和Direct3D都被Z3D
?DirectGraphics替换掉的原因。但最主要的是Q选择SDL意味着跨^台?/p>
回到正题。象DX一PSDL的各个部分是可以单独使用的,但必LSDL_Base。窗口消息管理方式很?/p>
董,写过Win32E序的一定还记得switch...caseQ没错SDL用就是这个,但完全可以不用它的。SDL?C
风格很浓Q就像DirectX的COM风格很浓一P用非 C c语a的h?x)更愿意装一下再用?/p>
与DX相比QSDL有更快的启动速度Q方便的调试Q调试过DXE序吗?:( Q,z的接口Q很的q行?/p>
库,当然首要的是跨^台。SDL直接支持很多媒体文g格式Q与DX比v来非常的方便。但SDL即简单直?/p>
讉K媒体层,不象DX支持那么多功能,当然也因Z是每个^台都能提供那么多功能?/p>
样例代码上说Q比起DXQSDL的样例非常短精(zhn),E序程是直U式Q效果上一点也不差。非C语言?/p>
译版本保留?jin)C的风|没有利用先进的语aҎ(gu),是个遗憾。至于帮助文Ӟl对不如DirectXQ很
多有用的信息是头文g里的注释。但毕竟 C 接口比COM单得多,看看函数名和样例也就?x)用了(jin)。但?/p>
有中文资料,不爱看英文的可能?x)头痛。可能的话大家分工翻译一下,毕竟比DX文档多?jin)?/p>
详细信息在哪里?
http://www.libsdl.org/ SDL首页
http://www.delphi-jedi.org/ Delphi接口目首页
http://jsdl.sourceforge.net/ Java接口首页
http://csgl.sourceforge.net C#接口首页
http://phpsdl.sourceforge.net php接口首页
使用SDL的游戏有哪些Q?br />http://www.libsdl.org/games.php
有个列表QM非常?