5131
clevermous
1
2
>
3
>
4
>SDL_Event
5
>
6
NAME="GENERATOR"
7
CONTENT="Modular DocBook HTML Stylesheet Version 1.64
8
">
9
REL="HOME"
10
TITLE="SDL Library Documentation"
11
HREF="index.html">
12
REL="UP"
13
TITLE="SDL Event Structures."
14
HREF="eventstructures.html">
15
REL="PREVIOUS"
16
TITLE="SDL Event Structures."
17
HREF="eventstructures.html">
18
REL="NEXT"
19
TITLE="SDL_ActiveEvent"
20
HREF="sdlactiveevent.html">
21
>
22
CLASS="REFENTRY"
23
BGCOLOR="#FFF8DC"
24
TEXT="#000000"
25
LINK="#0000ee"
26
VLINK="#551a8b"
27
ALINK="#ff0000"
28
>
29
CLASS="NAVHEADER"
30
>
31
WIDTH="100%"
32
BORDER="0"
33
CELLPADDING="0"
34
CELLSPACING="0"
35
>
36
>
37
COLSPAN="3"
38
ALIGN="center"
39
>SDL Library Documentation
40
>
41
>
42
>
43
WIDTH="10%"
44
ALIGN="left"
45
VALIGN="bottom"
46
>
47
HREF="eventstructures.html"
48
>Prev
49
>
50
>
51
WIDTH="80%"
52
ALIGN="center"
53
VALIGN="bottom"
54
>
55
>
56
WIDTH="10%"
57
ALIGN="right"
58
VALIGN="bottom"
59
>
60
HREF="sdlactiveevent.html"
61
>Next
62
>
63
>
64
>
65
>
66
ALIGN="LEFT"
67
WIDTH="100%">
68
>
69
>
70
NAME="SDLEVENT"
71
>SDL_Event
72
>
73
>
74
CLASS="REFNAMEDIV"
75
>
76
NAME="AEN3289"
77
>
78
>
79
>Name
80
>SDL_Event -- General event structure
81
>
82
CLASS="REFSECT1"
83
>
84
NAME="AEN3292"
85
>
86
>
87
>Structure Definition
88
>
89
CLASS="PROGRAMLISTING"
90
>typedef union{
91
Uint8 type;
92
SDL_ActiveEvent active;
93
SDL_KeyboardEvent key;
94
SDL_MouseMotionEvent motion;
95
SDL_MouseButtonEvent button;
96
SDL_JoyAxisEvent jaxis;
97
SDL_JoyBallEvent jball;
98
SDL_JoyHatEvent jhat;
99
SDL_JoyButtonEvent jbutton;
100
SDL_ResizeEvent resize;
101
SDL_QuitEvent quit;
102
SDL_UserEvent user;
103
SDL_SywWMEvent syswm;
104
} SDL_Event;
105
>
106
>
107
CLASS="REFSECT1"
108
>
109
NAME="AEN3295"
110
>
111
>
112
>Structure Data
113
>
114
CLASS="INFORMALTABLE"
115
>
116
NAME="AEN3297"
117
>
118
>
119
>
120
>
357
>
358
>
359
>
360
>
361
>
362
CLASS="REFSECT1"
363
>
364
NAME="AEN3364"
365
>
366
>
367
>Description
368
>
369
>The
370
CLASS="STRUCTNAME"
371
>SDL_Event
372
> union is the core to all event handling is SDL, its probably the most important structure after
373
CLASS="STRUCTNAME"
374
>SDL_Surface
375
>.
376
CLASS="STRUCTNAME"
377
>SDL_Event
378
> is a union of all event structures used in SDL, using it is a simple matter of knowing which union member relates to which event
379
CLASS="STRUCTFIELD"
380
>
381
>type
382
>
383
>.
384
>
385
>
386
CLASS="INFORMALTABLE"
387
>
388
NAME="AEN3372"
389
>
390
>
391
>
392
>
643
>
644
>
645
>
646
>
647
>
648
>
649
CLASS="REFSECT1"
650
>
651
NAME="AEN3452"
652
>
653
>
654
>Use
655
>
656
>The
657
CLASS="STRUCTNAME"
658
>SDL_Event
659
> structure has two uses
660
>
661
>
662
>
663
COMPACT="COMPACT"
664
>
665
>
666
>Reading events on the event queue
667
>
668
>
669
>
670
>Placing events on the event queue
671
>
672
>
673
>
674
>Reading events from the event queue is done with either
675
HREF="sdlpollevent.html"
676
>
677
CLASS="FUNCTION"
678
>SDL_PollEvent
679
>
680
> or
681
HREF="sdlpeepevents.html"
682
>
683
CLASS="FUNCTION"
684
>SDL_PeepEvents
685
>
686
>. We'll use
687
CLASS="FUNCTION"
688
>SDL_PollEvent
689
> and step through an example.
690
>
691
>First off, we create an empty
692
CLASS="STRUCTNAME"
693
>SDL_Event
694
> structure.
695
696
CLASS="PROGRAMLISTING"
697
>SDL_Event test_event;
698
>
699
700
CLASS="FUNCTION"
701
>SDL_PollEvent
702
> removes the next event from the event queue, if there are no events on the queue it returns
703
CLASS="RETURNVALUE"
704
>0
705
> otherwise it returns
706
CLASS="RETURNVALUE"
707
>1
708
>. We use a
709
CLASS="FUNCTION"
710
>while
711
> loop to process each event in turn.
712
713
CLASS="PROGRAMLISTING"
714
>while(SDL_PollEvent(&test_event)) {
715
>
716
The
717
CLASS="FUNCTION"
718
>SDL_PollEvent
719
> function take a pointer to an
720
CLASS="STRUCTNAME"
721
>SDL_Event
722
> structure that is to be filled with event information. We know that if
723
CLASS="FUNCTION"
724
>SDL_PollEvent
725
> removes an event from the queue then the event information will be placed in our
726
CLASS="STRUCTNAME"
727
>test_event
728
> structure, but we also know that the
729
CLASS="EMPHASIS"
730
>type
731
> of event will be placed in the
732
CLASS="STRUCTFIELD"
733
>
734
>type
735
>
736
> member of
737
CLASS="STRUCTNAME"
738
>test_event
739
>. So to handle each event
740
CLASS="STRUCTFIELD"
741
>
742
>type
743
>
744
> seperately we use a
745
CLASS="FUNCTION"
746
>switch
747
> statement.
748
749
CLASS="PROGRAMLISTING"
750
> switch(test_event.type) {
751
>
752
We need to know what kind of events we're looking for
753
CLASS="EMPHASIS"
754
>and
755
> the event
756
CLASS="STRUCTFIELD"
757
>
758
>type
759
>
760
>'s of those events. So lets assume we want to detect where the user is moving the mouse pointer within our application. We look through our event types and notice that
761
CLASS="LITERAL"
762
>SDL_MOUSEMOTION
763
> is, more than likely, the event we're looking for. A little
764
HREF="sdlmousemotionevent.html"
765
>more
766
> research tells use that
767
CLASS="LITERAL"
768
>SDL_MOUSEMOTION
769
> events are handled within the
770
HREF="sdlmousemotionevent.html"
771
>
772
CLASS="STRUCTNAME"
773
>SDL_MouseMotionEvent
774
>
775
> structure which is the
776
CLASS="STRUCTFIELD"
777
>
778
>motion
779
>
780
> member of
781
CLASS="STRUCTNAME"
782
>SDL_Event
783
>. We can check for the
784
CLASS="LITERAL"
785
>SDL_MOUSEMOTION
786
> event
787
CLASS="STRUCTFIELD"
788
>
789
>type
790
>
791
> within our
792
CLASS="FUNCTION"
793
>switch
794
> statement like so:
795
796
CLASS="PROGRAMLISTING"
797
> case SDL_MOUSEMOTION:
798
>
799
All we need do now is read the information out of the
800
CLASS="STRUCTFIELD"
801
>
802
>motion
803
>
804
> member of
805
CLASS="STRUCTNAME"
806
>test_event
807
>.
808
809
CLASS="PROGRAMLISTING"
810
> printf("We got a motion event.\n");
811
printf("Current mouse position is: (%d, %d)\n", test_event.motion.x, test_event.motion.y);
812
break;
813
default:
814
printf("Unhandled Event!\n");
815
break;
816
}
817
}
818
printf("Event queue empty.\n");
819
>
820
>
821
>It is also possible to push events onto the event queue and so use it as a two-way communication path. Both
822
HREF="sdlpushevent.html"
823
>
824
CLASS="FUNCTION"
825
>SDL_PushEvent
826
>
827
> and
828
HREF="sdlpeepevents.html"
829
>
830
CLASS="FUNCTION"
831
>SDL_PeepEvents
832
>
833
> allow you to place events onto the event queue. This is usually used to place a
834
CLASS="LITERAL"
835
>SDL_USEREVENT
836
> on the event queue, however you could use it to post fake input events if you wished. Creating your own events is a simple matter of choosing the event type you want, setting the
837
CLASS="STRUCTFIELD"
838
>
839
>type
840
>
841
> member and filling the appropriate member structure with information.
842
843
CLASS="PROGRAMLISTING"
844
>SDL_Event user_event;
845
846
user_event.type=SDL_USEREVENT;
847
user_event.user.code=2;
848
user_event.user.data1=NULL;
849
user_event.user.data2=NULL;
850
SDL_PushEvent(&user_event);
851
>
852
>
853
>
854
CLASS="REFSECT1"
855
>
856
NAME="AEN3509"
857
>
858
>
859
>See Also
860
>
861
>
862
HREF="sdlpollevent.html"
863
>
864
CLASS="FUNCTION"
865
>SDL_PollEvent
866
>
867
>,
868
869
HREF="sdlpushevent.html"
870
>
871
CLASS="FUNCTION"
872
>SDL_PushEvent
873
>
874
>,
875
876
HREF="sdlpeepevents.html"
877
>
878
CLASS="FUNCTION"
879
>SDL_PeepEvents
880
>
881
>
882
>
883
>
884
CLASS="NAVFOOTER"
885
>
886
ALIGN="LEFT"
887
WIDTH="100%">
939
>
940
>
941
>
942
>