SDL2 中使用多线程绘图

   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 constants

extern int SCREEN_WIDTH;
extern int SCREEN_HEIGHT;
//Starts up SDL and creates window


//Loads media
bool loadMedia();//加载图片

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
extern SDL_Window* g_screen_window;

//The surface contained by the window
extern SDL_Surface* g_screen_surface;

//The image we will load and show on the screen
SDL_Surface* gXOut = NULL;

bool loadMedia() {
	//Loading success flag
	bool 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 surface
	SDL_FreeSurface(gXOut);
	gXOut = NULL;

	//Destroy window
	SDL_DestroyWindow(g_screen_window);
	g_screen_window = NULL;

	//Quit SDL subsystems
	SDL_Quit();
}
void Thread1(void *pdata) {

	MainTask(0);//子线程中的绘画函数都在这里

	SDL_Log("Thread runing");
}
int SDL_main(int argc, char* args[]) {

	//Start up SDL and create window
	GUI_Init();//初始化windows 以及surface

	//Load media
	if (!loadMedia()) {
		printf("Failed to load media!\n");
	} else {
		//Main loop flag
		bool quit = false;

		//Event handler
		SDL_Event e;

		SDL_Thread *thread = SDL_CreateThread(Thread1, "TestThread",
				(void *) NULL);

		//While application is running

		while (!quit) {
			//Handle events on queue
			while (SDL_PollEvent(&e) != 0) {
				//User requests quit
				if (e.type == SDL_QUIT) {
					quit = true;
				}
				if (e.type == SDL_FINGERUP) {
					quit = true;
				}
			}

			//Apply the image

			//Update the surface
			SDL_UpdateWindowSurface(g_screen_window);
			SDL_Log("Invalidate");
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SDL2,你可以使用如下代码配置多显示器: 1. 首先,你需要使用`SDL_Init()`函数初始化SDL2库。 2. 然后,你需要通过`SDL_GetNumVideoDisplays()`函数获取当前系统上的显示器数量。 3. 接着,你需要使用`SDL_GetDisplayBounds()`函数获取每个显示器的分辨率和位置信息。 4. 最后,你可以使用`SDL_CreateWindow()`函数创建一个窗口,并将它放在你想要的显示器上。 下面是一个示例代码: ``` #include <SDL2/SDL.h> int main(int argc, char* argv[]) { // 初始化SDL2库 if (SDL_Init(SDL_INIT_VIDEO) != 0) { SDL_Log("Failed to initialize SDL: %s", SDL_GetError()); return 1; } // 获取系统上的显示器数量 int numDisplays = SDL_GetNumVideoDisplays(); if (numDisplays < 1) { SDL_Log("No displays found."); return 1; } // 获取每个显示器的分辨率和位置信息 for (int i = 0; i < numDisplays; ++i) { SDL_Rect displayBounds; SDL_GetDisplayBounds(i, &displayBounds); SDL_Log("Display %d: %d,%d %dx%d", i, displayBounds.x, displayBounds.y, displayBounds.w, displayBounds.h); } // 创建一个窗口,并将它放在第二个显示器上 SDL_Window* window = SDL_CreateWindow("SDL2 Multi-Display", SDL_WINDOWPOS_UNDEFINED_DISPLAY(1), SDL_WINDOWPOS_UNDEFINED_DISPLAY(1), 640, 480, SDL_WINDOW_SHOWN); if (window == NULL) { SDL_Log("Failed to create window: %s", SDL_GetError()); return 1; } // 等待窗口关闭 SDL_Event event; while (SDL_WaitEvent(&event)) { if (event.type == SDL_QUIT) { break; } } // 清理SDL2库 SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` 在上面的示例代码,我们使用`SDL_GetNumVideoDisplays()`函数获取系统上的显示器数量,并使用`SDL_GetDisplayBounds()`函数获取每个显示器的分辨率和位置信息。然后,我们使用`SDL_CreateWindow()`函数创建一个窗口,并将它放在第二个显示器上。最后,我们等待窗口关闭,并在程序结束前清理SDL2库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值