【转】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 ...
随机推荐
- 黑菜菌的JAVA学习笔记
简介 本文是笔者对<JAVA编程思想>的学习笔记.以自己的思维理解来写下这篇文章,尽可能地简练,易懂.本文将随本人学习进度实时更新 对象导论 抽象过程 汇编语言是对底层机器码的抽象,而面向 ...
- spring Boot面试题(2020最新版)
概述 什么是 Spring Boot? Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提 ...
- 跟我一起学.NetCore之Swagger让前后端不再烦恼及界面自定义
前言 随着前后端分离开发模式的流行,接口对接.联调成为常事,前端同事会经常问:我需要调哪个接口?这个接口数据格式是啥?条件都传啥? 对于一些紧急接口可能会采取沟通对接,然后补文档,其他的都会回一句:看 ...
- C++练习案例1.计算机类(利用多态实现)
c++简单计算机类 简介 大家好,这里是天天like的博客,这是我发的第一篇随笔,用来记录我的学习日程,大家可以相互学习,多多交流,感谢 今天我要记录的随笔是在学习c++多态的知识点练习改进的一个案例 ...
- ViewBinding的简单使用
Android自家的,又可以省去findviewbyid(),而且Butterknife上大神都已经推荐使用的,还有什么理由不去改写呢 build.gradle 开启viewBinding功能 and ...
- Centos-创建目录-mkdir
mkdir 创建目录 相关选项 -m 对新建目录设置存取权限,数字表现形式 -p 递归创建目录
- 山寨一个Spring的@Component注解
1. 前言 我们在上一篇对Mybatis如何将Mapper接口注入Spring IoC进行了分析,有同学问胖哥这个有什么用,这个作用其实挺大的,比如让你实现一个类似@Controller的注解(或者继 ...
- 【题解】[USACO09NOV]A Coin Game S
Link \(\text{Solution:}\) 菜鸡自己想出来了状态设计,但是没有实现出来--菜死了 设\(dp[i][j]\)表示该选第\(i\)个,最多选\(j\)个的最优解.注意这里的定义仅 ...
- 【题解】[JSOI2007]字符加密
Link \(\text{Solution:}\) 后缀数组第一题祭-- 观察一下,这个是让求一个环形的原字符串的后缀,我们可以考虑一下断环为链. 对于\(aba\)我们扩展成\(abaaba,\)则 ...
- Tensorflow学习笔记No.4.1
使用CNN卷积神经网络(1) 简单介绍CNN卷积神经网络的概念和原理. 已经了解的小伙伴可以跳转到Tensorflow学习笔记No.4.2学习如和用Tensorflow实现简单的卷积神经网络. 1.C ...