【转】Event Driven Programming
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;
We also declare an SDL_Event union. A SDL event is some thing like a key press, mouse motion, joy button press, etc. In this application we're going to look for quit events to end the application.
//While application is running
while( !quit )
{
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;
}
}
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 );
}
【转】Event Driven Programming的更多相关文章
- Event Driven Architecture
在微服务中使用领域事件 稍微回想一下计算机硬件的工作原理我们便不难发现,整个计算机的工作过程其实就是一个对事件的处理过程.当你点击鼠标.敲击键盘或者插上U盘时,计算机便以中断的形式处理各种外部事件 ...
- [转]Table-Driven and Data Driven Programming
What is Table-Driven and Data-Driven Programming? Data/Table-Driven programming is the technique of ...
- event driven的一些概念
1. event :Something that happens during your application that requires a response. 2.event object:Th ...
- event driven model
http://www.jdon.com/eda.html http://blog.csdn.net/gykimo/article/details/9182287 事件代表过去发生的事件,事件既是技术架 ...
- 【转】Beginning Game Programming v2.0
Beginning Game Programming v2.0 Last Updated 8/19/18 Greetings everyone, welcome to the ground up re ...
- 理解Nodejs的Event Loop
Node的“event loop”主要是用来处理高输出量的.这很神奇,这也是为什么node可以在单线程的情况下同时处理很多的后台操作.本文就会集中讲述event loop是怎么运行的,这样你可以可以使 ...
- Spring 4 + Reactor Integration Example--转
原文地址:http://www.concretepage.com/spring-4/spring-4-reactor-integration-example Reactor is a framewor ...
- SDL2.0教程翻译·目录
原文地址:SDL 2.0 Tutorial Index Welcome! 下面的教程旨在为你提供一个SDL2.0以及c++中游戏设计和相关概念的介绍.在本教程中,我们假定你对C++有一定程度上的知识, ...
- <<linux device driver,third edition>> Chapter 2: Building and Running Modules
Kernel Modules Versus Applications Kernel modules programming is similar to event driven programming ...
随机推荐
- 【Processing-日常3】等待动画1
之前在CSDN上发表过: https://blog.csdn.net/fddxsyf123/article/details/79755976
- Anaconda, conda, pyenv, virtualenv的区别
1.Python环境 Python解释器--Python.exe Python包集合--Lib,包括自带包和第三方包 2.Anaconda--一个科学计算环境,Python的发行版本 包括了Conda ...
- python3 连接数据库~
~目前记录的是针对python3写的数据库连接,不适用于pyhon2.python3如果想要与数据库进行连接,则需要先下载对应各数据库的插件包,然后导入包.python3的插件下载地址:https:/ ...
- Java知识系统回顾整理01基础04操作符05赋值操作符
一.赋值操作 赋值操作的操作顺序是从右到左 int i = 5+5; 首先进行5+5的运算,得到结果10,然后把10这个值,赋给i public class HelloWorld { public s ...
- 1个LED的亮度自动控制
控制任务和要求 通过程序控制LED的亮度按照要求变化 电路设计 程序设计 1 int bright_number = 0; //此变量用来表示LED的亮度 2 int bright_gap = 5; ...
- 9.23 T1 tree
题意描述: 给你一个长度为 \(n\) 的序列,让你从中选出 \(k\) 个数组成一个集合,定义这个集合的极限高度为\(a_i...a_k\) 的最大值. 让你求所有的集合极限高度 之和对 \(100 ...
- MinGW与Cygwin的关系与差别
PART1 共同点 Cygwin / GCC和MinGW都是gcc在WINDOWS下的实现. gcc:它是一款原来只能在Linux系统上使用的开源C语言编译器,后来移植到了Windows操作系统上(以 ...
- docker-命令帮助
1. 命令参考 http://www.runoob.com/docker/docker-command-manual.html2. docker-命令,可以使用docker --help查看或 ...
- DDOS、CC、sql注入,跨站攻击防御方法
web安全常见攻击解读--DDos.cc.sql注入.xss.CSRF 一,DDos https://www.cnblogs.com/sochishun/p/7081739.html#4111858 ...
- 安装mariadb/mysql 连接失败问题
在linux下安装mariadb会出现一系列问题 问题1->服务器端不需要用户名密码就可登陆数据库 问题2->php使用mysql不能连接数据库 访问受限 问题3->navicate ...