?? sdl很好的教程.txt
字號:
讓我們在做一個彩色的屏幕(如你看見的截圖)。我們通過一個循環把所有坐標繪制上去。在循環前添加鎖定屏幕的函數,在循環后添加解鎖的函數。Drawpixel函數在屏幕surface(緩沖)上繪制彩色的像素(每個像素顏色不同),然后使用SDL_Flip把緩沖(screen surface)繪制到實際的計算機屏幕上。
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);
注意:實際上一直往屏幕上繪制像素是很慢的。通常只有在需要時,才繪制需要的某一部分。更多的請看以后的教程。
把 // DRAWING GOES HERE 替換成上述代碼,并允許程序。你會看見一個彩色的窗口,但只存在很短的時間。為了存在時間長一點,添加一個循環:
for(i=0;i<100;i++)
{
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);
}
循環100次,然后退出。但還有更好的方法:
我們把繪圖代碼放入一個函數:
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);
}
在main()函數中,我們創建一個游戲循環。游戲循環是一個循環,直至退出。我們的游戲循環是一個while循環,當done等于0時循環。
int done=0;
while(done == 0)
{
// CODE
}
在游戲循環中,我們檢測是否ESC鍵或窗口上的X按鈕被按下了。如果按下了,則令done等于1,那么循環就會結束。
一切的SDL事件使用SDL_Event結構表示。我們需要一個SDL_Event變量來檢測時間:
SDL_Event event;
我們不停的獲取事件(直至沒有事件發生):
while ( SDL_PollEvent(&event) )
{
}
在每個 while(...) {...}中, SDL_Event 會包含事件的信息。然后我們確定事件的類型。
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
// CODE
}
如果我們得到了退出的事件(關閉按鈕被按下),我們令done等于1。如果一個按鍵被按下,我們在確定哪個鍵被按下:
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
所有的鍵盤上的按鍵名字都以SDLK_開頭。查看 SDL_keysym.h文件來得到更多的SDLK_ 鍵名字。事件檢測之后:
DrawScene(screen);
好了,下列是全部的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/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;
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=282166
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -