15_ Game Programming Mode EventQueue

Source: Internet
Author: User

# # # of two examples1. GuiEventLoop " " while(running) {//get an event from the event queueEventEvent=getnextevent (); //Handle Event ...}```2. CentralEventbus different system common Communication Center # # # # # # # # # # # # # problem code 'classaudio{ Public:  Static voidPlaySound (Soundid ID,intvolume);};classaudio{ Public:  Static voidPlaySound (Soundid ID,intvolume);};classmenu{ Public:  voidOnSelect (intindex)    {Audio::p laysound (Sound_bloop, Vol_max); //Other stuff ...}}; ' ' Question:1. API synchronous call, blocking to audio processing finished request2. Multiple requests cannot be combined processing3. Processing requests may run on the wrong thread (no Locks) # # # #模式定义: ' A series of notifications or requests are stored in the FIFO queue. Send notifications to the queue; The request processor obtains the request from the queues. ' ##### when to use '1If you just want to get the message from sender, it will be easier to use observer or command.2when you need to push something into another module, or pull something from another place, you need a buffer, and you need a queue now.3. The pull operation provided by the team, receiver can delay processing, merge requests, or discard. Pull requests are not open to sender, and when sender needs to get a response, the queue is a bit poor. (send then Pray) "# # # Notes"1The central event queue is a global variable2The World State will change, (queue processing is not timely)3Trapped in the feedback loop (a->b, ab ...).    If it's a sync queue, you'll quickly find a looping bug. General principle: Avoid sending events in functions that handle events. "# # # #structplaymessage{soundid ID; intvolume;};classaudio{ Public:  Static voidinit () {Numpending_=0; }  //Other stuff ...Private:  Static Const intMax_pending = -; StaticPlaymessage pending_[max_pending]; Static intnumpending_;};voidAudio::p laysound (soundid ID,intvolume) {Assert (Numpending_<max_pending); Pending_[numpending_].id=ID; Pending_[numpending_].volume=volume; Numpending_++;}classaudio{ Public:  Static voidUpdate () { for(inti =0; i < Numpending_; i++) {ResourceId resource=Loadsound (pending_[i].id); intChannel =Findopenchannel (); if(Channel = =-1)return;    Startsound (Resource, channel, Pending_[i].volume); } numpending_=0; }  //Other stuff ...}; ' ' ##### ring buffer cycle buffer 'classaudio{ Public:  Static voidinit () {Head_=0; Tail_=0; }  //Methods ...Private:  Static intHead_; Static intTail_; //Array ...};voidAudio::p laysound (soundid ID,intvolume) {Assert (Tail_+1)% max_pending! =Head_); //Add to the end of the list.Pending_[tail_].id =ID; Pending_[tail_].volume=volume; Tail_= (Tail_ +1) %max_pending;}voidaudio::update () {//If There is no pending requests, do nothing.  if(Head_ = = Tail_)return; ResourceId Resource=Loadsound (pending_[head_].id); intChannel =Findopenchannel (); if(Channel = =-1)return;  Startsound (Resource, channel, Pending_[head_].volume); Head_= (Head_ +1) %max_pending;} "' ##### merger request 'voidAudio::p laysound (soundid ID,intvolume) {  //Walk the pending requests.   for(inti = Head_; I! =Tail_; I= (i +1) %max_pending) {    if(Pending_[i].id = =ID) {//Use the larger of the volumes.Pending_[i].volume =Max (volume, pending_[i].volume); //Don ' t need to enqueue.      return; }  }  //Previous Code ...} ' ##### multithreaded push pull operation requires thread safety # # # # # What's in the queue to save 'Eventor message1 EventQueue (one-to-many) describes something that has happened, similar to the asynchronous observer pattern1Allow multiple listeners, and the queue holds events that have occurred *, the sender does not care who is going to accept it. 2the scope is broader. is used to broadcast a class of things. tends to be globally visible.2The Message Queue (many-to-one) tends to have only one listener. Multiple requests are sent from different places, and a processor handles ' # # # # who can read the queue '1Unicast queue:1The . Queue implementation reads. The sender just sends2the queue is encapsulated better3no read competition (decide whether to broadcast or distribute)2Broadcast queue:1If there is no listener, the event is discarded2. You may need an event filter3task queue: Similar to broadcast queues, such as worker pool1need to dispatch "# # # # who can write queue '1a writer (similar to synchronous observer)1. You know exactly who sent the incident.2. Often allows multiple readers2multiple written by1. Note Loops2need to have access to the sender (the event contains the sender's reference) "The life cycle of the object in the team "1. Transfer of ownership. There is a sender to the queue, and the queue is forwarded to the recipient2. Share ownership.3. Ownership is only given to the queue. (The queue requests memory, then the sender populates the data, the recipient gets a reference) ' # # #See also '1The queue is much like an asynchronous observer.2. Message Queue, Pub sub3. Finite state machines, state machines require input, and if you want to execute asynchronously, you can use the queue. If you need multiple state machines to send messages to each other, you need a queue to accept input (mail box), which is called the Actor model4the channel built into the go language is essentially an event or message queue. "

15_ Game Programming Mode EventQueue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.