SDL2 中使用多线程绘图

来源:互联网 发布:淘宝带图评论福利 编辑:程序博客网 时间:2024/04/29 18:19

   SDL的中文资料比较少,推荐一个英文的网站里面讲的非常详细  点击打开链接解决了我不少的疑惑。

  最近在移植ucGUI到Android上,ucGUI的windows Demo中可以实现在一个线程中绘图,在另一个线程中刷新。在Android中使用ucGUI也是可以实现的用SDL 当画布,在子线程中绘画 ,在主线程中刷新。代码如下:

/*This source code copyrighted by Lazy Foo' Productions (2004-2013) and may not be redistributed without written permission.*///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 mediabool loadMedia();//加载图片//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 will load and show on the screenSDL_Surface* gXOut = NULL;bool loadMedia() {//Loading success flagbool success = true;//Load splash imagegXOut = 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);//子线程中的绘画函数都在这里SDL_Log("Thread runing");}int SDL_main(int argc, char* args[]) {//Start up SDL and create windowGUI_Init();//初始化windows 以及surface//Load mediaif (!loadMedia()) {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;}



2 0