⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdl很好的教程.txt

📁 用sdl开发小型游戏的
💻 TXT
📖 第 1 页 / 共 2 页
字号:

让我们在做一个彩色的屏幕(如你看见的截图)。我们通过一个循环把所有坐标绘制上去。在循环前添加锁定屏幕的函数,在循环后添加解锁的函数。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 + -