Skip to content

SDLHelloWord

Zhao Yipeng edited this page Dec 6, 2017 · 11 revisions

SDL HelloWorld!

英文原文地址:http://www.willusher.io/sdl2%20tutorials/2013/08/17/lesson-1-hello-world

这一节我们将学习简单的将一张图片绘制到屏幕上的方法。具体来讲是绘制下面这张图片。

hello

你可以通过右键另存为下载这张图片。如果你丢了资源或者想要偷看一下我的代码,就从这里抓好了。但切忌复制粘贴!

创建一个SDL Project

首先在delphi当中File->New->Other,创建一个Console Application,保存为SDLHelloWorld,添加一个新的单元,保存为App.pas。

启动SDL

使用SDL,我们必须先初始化我们想要使用的SDL的各个子系统。将一组“或”关系的标志作为参数传入到SDL_Init可以完成我们要使用的子系统的初始化。现在我们只需要使用视频子系统,但是将来我们会增加更多的标志,因为我们需要更多的特性。注意使用视频子系统的时候,事件处理子系统会被自动初始化,文件I/O和线程系统也会被默认启动。如果所有的初始化都正确SDL_Init会返回0,如果不是我们将会打印错误并退出。

修改SDLHelloWorld.dpr:

program SDLHelloWorld;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  sdl2 in '..\..\Pascal-SDL-2-Headers-master\sdl2.pas',
  App in 'App.pas';

begin
  Run;
end.

App.pas如下:

unit App;

interface

uses
  sdl2,
  res_path;

function Run: Integer;

implementation

function Run: Integer;
var
  win: PSDL_Window;
  ren: PSDL_Renderer;
  bmp: PSDL_Surface;
  tex: PSDL_Texture;
  imagePath: string;
  i: Integer;
begin

  // First we need to start up SDL, and make sure it went ok
  if (SDL_Init(SDL_INIT_VIDEO) <> 0) then
  begin
    Writeln('SDL_Init Error: ', SDL_GetError);
    Exit(1);
  end;

  // Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
  win := SDL_CreateWindow('Hello World!', 100, 100, 640, 480, SDL_WINDOW_SHOWN);
  // Make sure creating our window went ok
  if (win = nil) then
  begin
    Writeln('SDL_CreateWindow Error: ', SDL_GetError);
    Exit(1)
  end;

  // Create a renderer that will draw to the window, -1 specifies that we want to load whichever
  // video driver supports the flags we're passing
  // Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
  // SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
  // synchronized with the monitor's refresh rate
  ren := SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED or SDL_RENDERER_PRESENTVSYNC);
  if (ren = nil) then
  begin
    SDL_DestroyWindow(win);
    Writeln('SDL_CreateRenderer Error: ', SDL_GetError);
    SDL_Quit();
    Exit(1);
  end;

  // SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
  // this lets us choose when to upload or remove textures from the GPU
  imagePath := getResourcePath('Lesson1') + 'hello.bmp';
  bmp := SDL_LoadBMP(PAnsiChar(AnsiString(imagePath)));
  if (bmp = nil) then
  begin
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    Writeln('SDL_LoadBMP Error: ', SDL_GetError);
    SDL_Quit();
    Exit(1);
  end;

  // To use a hardware accelerated texture for rendering we can create one from
  // the surface we loaded
  tex := SDL_CreateTextureFromSurface(ren, bmp);
  // We no longer need the surface
  SDL_FreeSurface(bmp);
  if (tex = nil) then
  begin
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
    Writeln('SDL_CreateTextureFromSurface Error: ', SDL_GetError);
    SDL_Quit();
    Exit(1);
  end;

  // A sleepy rendering loop, wait for 3 seconds and render and present the screen each time
  for i := 0 to 2 do
  begin
    // First clear the renderer
    SDL_RenderClear(ren);
    // Draw the texture
    SDL_RenderCopy(ren, tex, nil, nil);
    // Update the screen
    SDL_RenderPresent(ren);
    // Take a quick break after all that hard work
    SDL_Delay(1000);
  end;

  // Clean up our objects and quit
  SDL_DestroyTexture(tex);
  SDL_DestroyRenderer(ren);
  SDL_DestroyWindow(win);
  SDL_Quit();
end;

end.

Clone this wiki locally