SDL2-based event-driven programming

Source: Internet
Author: User

In fact, there is no need to say too complicated ... is to read user input.

Follow the previous code to join the event polling.

Environment: SDL2 + vc++2015

The following code will open Background.png and event.png and will background tile the background and render the event as a foreground.

1#include <stdexcept>2#include <string>3#include <iostream>4#include"SDL.h"5#include"sdl_image.h"6 7 //Screen Width8 Const intScreen_width = the;9 Const intScreen_height = the;Ten  One //Global window and renderer ASdl_window *window =nullptr; -Sdl_renderer *renderer =nullptr; -  the //Loading Pictures -sdl_texture* LoadImage (std::stringfile) - { -sdl_texture* Tex =nullptr; +Tex =img_loadtexture (Renderer, File.c_str ()); -     if(Tex = =nullptr) +         ThrowStd::runtime_error ("Failed to load Image:"+ file +img_geterror ()); A     returnTex; at } -  - //apply a surface to the renderer - voidApplysurface (intXintY, sdl_texture *tex, Sdl_renderer *rend) - { - Sdl_rect Pos; inPos.x =x; -Pos.y =y; toSdl_querytexture (tex, NULL, NULL, &AMP;POS.W, &pos.h); +Sdl_rendercopy (Rend, Tex, NULL, &POS); - } the  * intMainintargcChar**argv) $ {Panax Notoginseng     //Initialize SDL -     if(Sdl_init (sdl_init_everything) = =-1) the     { +Std::cout << sdl_geterror () <<Std::endl; A         return 1; the     } +  -     //Create Window $window = Sdl_createwindow ("Lesson 4-event driven programming", $ sdl_windowpos_centered, sdl_windowpos_centered, - screen_width, Screen_height, sdl_window_shown); -     if(Window = =nullptr) the     { -Std::cout << sdl_geterror () <<Std::endl;Wuyi         return 2; the     } -  Wu     //Create renderer -Renderer = sdl_createrenderer (window,-1, Aboutsdl_renderer_accelerated |sdl_renderer_presentvsync); $     if(Renderer = =nullptr) -     { -Std::cout << sdl_geterror () <<Std::endl; -         return 3; A     } +  the     //create background and foreground textures -Sdl_texture *background = nullptr, *image =nullptr; $     Try { theBackground = LoadImage ("Background.png"); theImage = LoadImage ("Event.png"); the     } the     Catch(ConstStd::runtime_error &e) { -Std::cout << e.what () <<Std::endl; in         return 4; the     } the  About     BOOLQuit =false; the sdl_event E; the     //main loop (high CPU consumption) the      while(!quit) +     { -         //Poll event Stack e the          while(Sdl_pollevent (&e))Bayi         { the             //User Close Window the             if(E.type = = sdl_quit) QUIT =true; -  -             //the user presses the keyboard the             if(E.type = = sdl_keydown) quit =true; the  the             //User Click Mouse the             if(E.type = = sdl_mousebuttondown) quit =true; -  the             //Empty renderer the sdl_renderclear (renderer); the 94             //internal flush the background in the renderer the             intBW, BH; theSdl_querytexture (background, NULL, NULL, &AMP;BW, &BH); the              for(inty =0; Y <= screen_height; Y + =BH)98                  for(intx =0; x <= screen_width; X + =BW) About applysurface (x, y, background, renderer); - 101 102             //placing the foreground in the center of the renderer103             intIW, IH;104Sdl_querytexture (image, NULL, NULL, &AMP;IW, &IH); the             intx = screen_width/2-IW/2;106             inty = screen_height/2-IH/2;107 applysurface (x, y, image, renderer);108 109             //Render Renderer the sdl_renderpresent (renderer);111         } the     }113  the     //Freeing Resources the sdl_destroytexture (background); the sdl_destroytexture (image);117 sdl_destroyrenderer (renderer);118 Sdl_destroywindow (window);119  - sdl_quit ();121 122     return 0;123}

SDL2-based event-driven programming

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.