SDL study note 4 (event processing)

Source: Internet
Author: User

Games are primarily interactive games. What is the difference between games without interaction and movies...

So we have to deal with many related events, press or open the keyboard, move the mouse, and click ....

Events are used to drive the game. SDL provides convenient functions to process events in the program. Events are defined as the combined sdl_event for multiple events, the following function retrieves the first event in the event queue:

Int sdl_pollevent (sdl_event * event );

This function obviously gives him the address of the sdl_event variable, which will return the event to the corresponding structure. This function will not wait for the event to arrive, or will not block, whether there is any event, return directly. The following function will wait for the event to arrive.

Int sdl_waitevent (sdl_event * event );

Both functions extract events from the queue, and

Int sdl_peepevent (sdl_event * event );

Just peek at the content of the first event...

It is enough to have these three functions to obtain the event. The following describes how to handle the event.

We can easily determine the Event Type

Sdl_event event;

Determine the event. the value of type is used to differentiate the event type, and then the corresponding type. The event is a different struct. Corresponding Members can determine what event occurred and where it has changed. For example: which button is pressed?

Of course, sometimes it is annoying to judge one by one. We can use the following functions to get the status of events we care about:

Uint8 * sdl_getkeystate (int * numkeys );

Uint8 sdl_getmousestate (int * X, int * y );

There are also some similar functions that return an array to save the status of some events. By accessing the array, you can know the status of the corresponding events.

Finally, based on what we learned, write a simple program that obtains keyboard and mouse events and outputs events in Chinese.

# Include <SDL/SDL. h> # include <SDL/sdl_image.h> # include <SDL/sdl_ttf.h> # include <stdio. h> # define wm_width 860 # define wm_height 460 # define wm_bits 32 # define wm_flags sdl_swsurfaceconst char * fontname = "jdfyuanyi. TTF "; const char * backname =" back.bmp "; sdl_surface * screen = NULL; sdl_surface * Background = NULL; ttf_font * font = NULL; void Init (void ); void clean (void); void event_handle (void); sdl_surface * load_image (const char * INAME); void draw_surface (int x, int y, const char * MSG ); void error_quit () {printf ("some errors: % s \ n", sdl_geterror (); exit (exit_failure);} int main (INT argc, char ** argv) {Init (); atexit (clean); draw_surface (screen-> clip_rect.w-12)/2, (screen-> clip_rect.h-16)/2, "Welcome to SDL "); event_handle (); exit (exit_success);} void Init (void) {If (sdl_init (handle) =-1) error_quit (); screen = sdl_setvideomode (wm_width, wm_height, wm_bits, wm_flags); If (screen = NULL) error_quit (); sdl_wm_setcaption ("SDL events", null); If (ttf_init () =-1) error_quit (); font = ttf_openfont (fontname, 16); If (font = NULL) error_quit (); background = load_image (backname); If (font = NULL) error_quit ();} void clean (void) {sdl_freesurface (background); ttf_closefont (font); ttf_quit (); sdl_quit ();} void event_handle (void) {int gameover = 0; while (gameover = 0) {sdl_event gevent; If (sdl_waitevent (& gevent) {Switch (gevent. type) {Case sdl_quit: gameover = 1; break; Case sdl_keydown: draw_surface (screen-> clip_rect.w-12)/2, (screen-> clip_rect.h-16)/2, "press the button"); break; Case sdl_keyup: draw_surface (screen-> clip_rect.w-12)/2, (screen-> clip_rect.h-16)/2, "button released "); break; Case sdl_mousemotion: draw_surface (screen-> clip_rect.w-12)/2, (screen-> clip_rect.h-16)/2, "move the mouse"); break; Case sdl_mousebuttonup: draw_surface (screen-> clip_rect.w-12)/2, (screen-> clip_rect.h-16)/2, "Mouse removed"); break; Case sdl_mousebuttondown: draw_surface (screen-> clip_rect.w-12) /2, (screen-> clip_rect.h-16)/2, "Mouse pressed"); break; default: draw_surface (screen-> clip_rect.w-12)/2, (screen-> clip_rect.h-16) /2, "Unrecognized events"); break ;}}} void draw_surface (int x, int y, const char * MSG) {sdl_surface * message = NULL; sdl_color wordcolor = {255,255, 0}; message = ttf_renderutf8_solid (font, MSG, wordcolor); If (Message = NULL) error_quit (); If (sdl_blitsurface (background, null, screen, null) =-1) error_quit (); sdl_rect offset; offset. X = x; offset. y = y; If (sdl_blitsurface (message, null, screen, & offset) =-1) error_quit (); sdl_freesurface (Message); If (sdl_flip (screen) =-1) error_quit ();} sdl_surface * load_image (const char * INAME) {sdl_surface * image = NULL; sdl_surface * format = NULL; image = img_load (INAME ); if (image = NULL) error_quit (); format = sdl_displayformat (image); sdl_freesurface (image); If (format = NULL) error_quit (); Return format ;}

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.