FROM: http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php

Event Driven Programming

Last Updated 1/04/14

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.

            //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. Since we just started the application at this point, it is obviously initialized to false.

We also declare an SDL_Event union. A SDL event is some thing like a key pressmouse motionjoy button press, etc. 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. 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 will keep taking events off the queue until it is empty. 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. 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.

                //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. If it is still false it will keep going until the user Xs out the window.
Download the media and source code for this tutorial here.

Back to SDL Tutorials

最新文章

  1. oracle DML(数据管理语言)sql 基本语句
  2. EditPlus 3.1
  3. Delphi String 常用字串符处理函数
  4. php验证是否是md5编码的代码
  5. Google map v3 using simple tool file google.map.util.js v 1.2
  6. Java基础笔记-面向对象2
  7. CLR和.Net对象
  8. .net remoting 实现通用消息处理窗口
  9. 2.5. Integer 16 、Integer 32、Integer 64(Core Data 应用程序实践指南)
  10. HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二
  11. 107个JS常用方法(持续更新中)
  12. Spring 事务传播实践分析
  13. 04-HTML-图片标签
  14. svn 锁的处理
  15. buntu下cutecom图像界面串口调试工具使用
  16. 1-1.flutter学习笔记(一)git入门(msysgit图文安装)
  17. 网页图表Highcharts实践教程之标签组与载入动画
  18. java多线程15 :wait()和notify() 的生产者/消费者模式
  19. LeetCode141:Linked List Cycle
  20. docker1.9 network跨主机安装

热门文章

  1. 刷题[CISCN2019 华北赛区 Day2 Web1]Hack World
  2. RabbitMQ 3.6.12延迟队列简单示例
  3. MySQL 5.7二进制日志
  4. Dynamically allocated memory 动态分配内存【malloc】Memory leaks 内存泄漏
  5. spring-boot-route(二)读取配置文件的几种方式
  6. 04 sublime text 3在线安装package control插件,之后安装主题插件和ConvertToUTF8 插件
  7. python中def用法
  8. appium 环境安装指引
  9. 多测师_肖sir _python 练习题(一)100以内奇数,偶数,质数胡计算
  10. java基础小程序—万年历