Using multi-threaded drawing in SDL2

Source: Internet
Author: User

SDL's Chinese information is relatively small, recommend an English website inside the very detailed click Open link to solve my many doubts.

Recently in porting Ucgui to Android, Ucgui's Windows demo can be used to draw in one thread and refresh in another thread. Using Ucgui in Android is also possible with SDL as a canvas, painting in sub-threads, and refreshing in the main thread. The code is as follows:

/*this source code copyrighted by Lazy Foo ' Productions (2004-2013) and is not being redistributed without written Permissio N.*///using SDL and Standard io#include <SDL.h> #include <stdio.h> #include <stdbool.h> #include "GUI.h "//screen dimension constantsextern int Screen_width;extern int screen_height;//starts up SDL and creates window//loads me Diabool Loadmedia ();//Load Picture//frees media and shuts down sdlvoid close ();//the window we'll be rendering Toextern Sdl_window * G_screen_window;//the surface contained by the Windowextern sdl_surface* g_screen_surface;//the image We'll load and s How on the screensdl_surface* gxout = Null;bool Loadmedia () {//loading success Flagbool success = True;//load Splash image Gxout = Sdl_loadbmp ("X.bmp"), if (gxout = = NULL) {sdl_logerror (sdl_log_category_application, "Unable to load image%s! SDL Error:%s\n "," 03_event_driven_programming/x.bmp ", Sdl_geterror ()); success = false;} return success;} void Close () {//deallocate surfacesdl_freesurface (GXout); gxout = Null;//destroy Windowsdl_destroywindow (g_screen_window); G_screen_window = Null;//quit SDL subsystemsSDL_ Quit ();} void Thread1 (void *pdata) {maintask (0);//The Paint function in the sub-thread is here Sdl_log ("Thread runing");} int sdl_main (int argc, char* args[]) {//start up SDL and create windowgui_init ();//Initialize Windows and Surface//load mediaif (!lo Admedia ()) {printf ("Failed to load media!\n");} else {//main loop flagbool quit = False;//event handlersdl_event e; Sdl_thread *thread = Sdl_createthread (Thread1, "Testthread", (void *) NULL);//while application is Runningwhile (!quit) {/ /handle events on Queuewhile (Sdl_pollevent (&e)! = 0) {//user requests quitif (E.type = = sdl_quit) {QUIT = true;} if (E.type = = Sdl_fingerup) {quit = true;}} Apply the Image//update the Surfacesdl_updatewindowsurface (G_screen_window); Sdl_log ("Invalidate");}} Free resources and close sdlclose (); return 0;}



Using multi-threaded drawing in SDL2

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.