您的位置:首页 > 编程语言 > C语言/C++

C++ SDL教程翻译 Lesson 03 Event Driven Programming

2017-06-21 20:27 525 查看
初二的我又来啦!这是第三篇翻译,嗯~

末尾还是有附加内容,翻译中所说的源代码下载请去结尾,我给出了地址。

警告:转载此翻译文请注明来自CSDN博客,CSDN博客是此翻译唯一出处。

中文版:

课程3 事件驱动编程

现在,我们将开始处理用户输入并允许用户从窗口中输出。

除了在屏幕上显示图像外,游戏还要求用户输入。

您可以使用SDL的事件处理系统。

//Main loop flag
bool quit = false;

//Event handler
SDL_Event e;


在我们的代码中,SDL已被初始化,并加载了媒体(正如上一篇教程中提到的),我们声明了一个退出标志,它可以跟踪用户是否已经退出。

因为我们在这个时候才开始应用,所以它显然被初始化为false的。

我们还声明一个SDL_Event联合。SDL事件是一些东西组成,如按键、鼠标移动、按钮的按下等等。

在这个应用程序中,我们将寻找退出事件来结束应用程序。

//While application is running
while( !quit )
{


在之前的教程中,我们让程序在关闭前等待几秒钟。

在这个应用程序中,我们让应用程序等待到用户退出。

因此,当用户没有退出时,我们要应用程序循环。

保持运行应用程序活动的这个循环被称为主循环,它有时被称为游戏循环。

它是任何游戏应用程序的核心。

//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}


在主循环的顶部有我们的事件循环。

它所做的就是一直处理事件队列,直到它是空的。

当你按下一个键,或移动鼠标,或点击屏幕,你就会把事件放到事件队列上。

然后事件队列会将它们存储在事件发生的顺序中,等待您处理它们。

当您想要找出发生了什么事件,以便您可以处理它们时,您可以通过调用SDL_PollEvent来轮询事件队列以获得最近的事件。

SDL_PollEvent所做的就是从事件队列中获取最近的事件,并将事件中的数据放入我们传递给该函数的SDL_Event中。

SDL_PollEvent将会把事件从队列中取走,直到它是空的。

当队列为空时,SDL_PollEvent将返回0。因此,这段代码所做的就是将轮询事件保持在事件队列中,直到它为空。

如果事件队列中的事件是SDL_QUIT事件(当用户从窗口退出时,事件是事件),我们将退出标志设置为true,这样我们就可以退出应用程序。

//Apply the image
SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );

//Update the surface
SDL_UpdateWindowSurface( gWindow );
}


在完成了对框架的事件处理之后,我们将要绘制到屏幕并更新它(正如上一教程所讨论的那样)。

如果退出标志被设置为true,应用程序将在循环结束时退出。

如果它仍然是false,它将继续运行直到用户从窗口退出。

请下载本教程的媒体和源代码。

英文版

Lesson 03 Event Driven Programming

课程3 事件驱动编程

Here we’ll start handling user input by allow the user to X out the window.

现在,我们将开始处理用户输入并允许用户从窗口中输出。

Besides just putting images on the screen, games require that you handle input from the user.

除了在屏幕上显示图像外,游戏还要求用户输入。

You can do that with SDL using the event handling system.

您可以使用SDL的事件处理系统。

//Main loop flag
bool quit = false;

//Event handler
SDL_Event e;


In our code after SDL is initialized and the media is loaded (as mentioned in the previous tutorial), we declare a quit flag that keeps track of whether the user has quit or not.

在我们的代码中,SDL已被初始化,并加载了媒体(正如上一篇教程中提到的),我们声明了一个退出标志,它可以跟踪用户是否已经退出。

Since we just started the application at this point, it is obviously initialized to false.

因为我们在这个时候才开始应用,所以它显然被初始化为false的。

We also declare an SDL_Event union. A SDL event is some thing like a key press, mouse motion, joy button press, etc.

我们还声明一个SDL_Event联合。SDL事件是一些东西组成,如按键、鼠标移动、按钮的按下等等。

In this application we’re going to look for quit events to end the application.

在这个应用程序中,我们将寻找退出事件来结束应用程序。

//While application is running
while( !quit )
{


In the previous tutorials, we had the program wait for a few seconds before closing.

在之前的教程中,我们让程序在关闭前等待几秒钟。

In this application we’re having the application wait until the user quits before closing.

在这个应用程序中,我们让应用程序等待到用户退出。

So we’ll have the application loop while the user has not quit.

因此,当用户没有退出时,我们要应用程序循环。

This loop that keeps running while the application is active is called the main loop, which is sometimes called the game loop.

保持运行应用程序活动的这个循环被称为主循环,它有时被称为游戏循环。

It is the core of any game application.

它是任何游戏应用程序的核心。

//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
}


At the top of our main loop we have our event loop.

在主循环的顶部有我们的事件循环。

What this does is keep processing the event queue until it is empty.

它所做的就是一直处理事件队列,直到它是空的。

When you press a key, move the mouse, or touch a touch screen you put events onto the event queue.

当你按下一个键,或移动鼠标,或点击屏幕,你就会把事件放到事件队列上。

The event queue will then store them in the order the events occured waiting for you to process them.

然后事件队列会将它们存储在事件发生的顺序中,等待您处理它们。

When you want to find out what events occured so you can process them, you poll the event queue to get the most recent event by calling SDL_PollEvent.

当您想要找出发生了什么事件,以便您可以处理它们时,您可以通过调用SDL_PollEvent来轮询事件队列以获得最近的事件。

What SDL_PollEvent does is take the most recent event from the event queue and puts the data from the event into the SDL_Event we passed into the function.

SDL_PollEvent所做的就是从事件队列中获取最近的事件,并将事件中的数据放入我们传递给该函数的SDL_Event中。

SDL_PollEvent will keep taking events off the queue until it is empty.

SDL_PollEvent将会把事件从队列中取走,直到它是空的。

When the queue is empty, SDL_PollEvent will return 0. So what this piece of code does is keep polling events off the event queue until it’s empty.

当队列为空时,SDL_PollEvent将返回0。因此,这段代码所做的就是将轮询事件保持在事件队列中,直到它为空。

If an event from the event queue is an SDL_QUIT event (which is the event when the user Xs out the window), we set the quit flag to true so we can exit the application.

如果事件队列中的事件是SDL_QUIT事件(当用户从窗口退出时,事件是事件),我们将退出标志设置为true,这样我们就可以退出应用程序。

//Apply the image
SDL_BlitSurface( gXOut, NULL, gScreenSurface, NULL );

//Update the surface
SDL_UpdateWindowSurface( gWindow );
}


After we’re done processing the events for our frame, we draw to the screen and update it (as discussed in the previous tutorial).

在完成了对框架的事件处理之后,我们将要绘制到屏幕并更新它(正如上一教程所讨论的那样)。

If the quit flag was set to true, the application will exit at the end of the loop.

如果退出标志被设置为true,应用程序将在循环结束时退出。

If it is still false it will keep going until the user Xs out the window.

如果它仍然是false,它将继续运行直到用户从窗口退出。

Download the media and source code for this tutorial here.

请下载本教程的媒体和源代码。

这是附件下载:

国外教程原地址(可能需要翻墙):

http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/03_event_driven_programming.zip

益文友_Allen(本博主)提供的下载地址(无需翻墙):

上传中···

好吧,这就是Lesson 03的翻译了。

现在我来补充一下。

Lesson 02讲的就是如何关闭SDL窗口···

因为在之前的代码中,创建的SDL是没法关闭的。

看深一点呢,这里面的重点其实是事件响应,这个我也会在SDL学习之路3rd中讲到。

想更深的理解,可以看看附件中的源代码中的注释,翻译一下就可以,那里面也说的很详细了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息