分类:
2012-11-07 10:18:18
原文地址:SDL使用 作者:kathy870513
图2 |
图3 |
图4 |
图5 |
图6 |
图7 |
图8 |
图9 |
#include #if defined(_MSC_VER) #include "SDL.h" #else #include "SDL/SDL.h" #endif SDL_Surface *screen; void render() { // 独占资源,将surface 锁定 if (SDL_MUSTLOCK(screen)) if (SDL_LockSurface(screen) < 0) return; // 获取当前时间,以毫秒计时 int tick = SDL_GetTicks(); // 声明变量 int i, j, yofs, ofs; // 对窗口进行绘制 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对窗口进行更新 SDL_UpdateRect(screen, 0, 0, 640, 480); } // Entry point int main(int argc, char *argv[]) { // 初始化SDL子系统,这里只对视频进行初始化 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } // 注册SDL_Quit,当退出时调用,使得退出时程序自动清理 atexit(SDL_Quit); // 使用32位象素创建640x480的窗口 screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); // 若失败,则退出 if ( screen == NULL ) { fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError()); exit(1); } // 主循环 while (1) { // Render stuff render(); // SDL中的事件轮询机制 SDL_Event event; while (SDL_PollEvent(&event)) { //对消息进行处理 switch (event.type) { // 如果按下某键的消息响应 case SDL_KEYDOWN: break; //如果某键按下后弹起的消息响应 case SDL_KEYUP: //若按下ESC键,则退出 if (event.key.keysym.sym == SDLK_ESCAPE) return 0; break; //退出消息响应 case SDL_QUIT: return(0); } } } return 0; } |
图10 |
#include “SDL.h” |
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 #include #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 #include #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 #include #include “SDL.h” |
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* keys; |
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(); } |