SDL2-多窗口

SDL不仅是用来写游戏的,其实他的官方网站给他的定义是游戏和GUI界面,也就是说它其实就是一个实现图形用户界面的库。那么肯定可以有多窗口啦。 其实多窗口实现的方式很简单:你只要定义多个窗口,然后每个窗口再给一个Renderer就可以了。然后在循环的时候再调用各自的Renderer函数就可以了。

这里有一个例子给你看一下:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <SDL2/SDL.h>
#include <thread>
#include <iostream>
using namespace std;

void runWindow(int argc,char** argv){
	SDL_Window* win = SDL_CreateWindow("MultiWindow", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 700, SDL_WINDOW_SHOWN);
	SDL_Renderer* render = SDL_CreateRenderer(win, -1, 0);
	SDL_Event event;
	bool isQuit = false;
	SDL_SetRenderDrawColor(render ,255,255,255,255);
	while(!isQuit){
		SDL_RenderClear(render);
		while(SDL_PollEvent(&event)){
			if(event.type == SDL_QUIT)
					isQuit = true;
		}
		SDL_RenderPresent(render);
	}
}

int main(int argc, char** args){
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Window* win1 = SDL_CreateWindow("MultiWindow1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 700, SDL_WINDOW_SHOWN);
	SDL_Window* win2 = SDL_CreateWindow("MultiWindow2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 700, SDL_WINDOW_SHOWN);
	SDL_Renderer* render1 = SDL_CreateRenderer(win1, -1, 0);
	SDL_Renderer* render2 = SDL_CreateRenderer(win2, -1, 0);
	SDL_Event event;
	bool isQuit = false;
	SDL_SetRenderDrawColor(render2 ,255,255,255,255);
	SDL_SetRenderDrawColor(render1 ,0,255,0,255);
	while(!isQuit){
		SDL_RenderClear(render1);
		SDL_RenderClear(render2);
		while(SDL_PollEvent(&event)){
			if(event.window.event == SDL_WINDOWEVENT_CLOSE)
					isQuit = true;
       }
		SDL_RenderPresent(render1);
		SDL_RenderPresent(render2);
		SDL_Delay(30);
	}
	SDL_Quit();
	return 0;
}

最后的结果是这样: c6ecf40f2dec41aa58acbf18133ce778.png 需要注意的是这里的事件处理,我们使用的是event.window。

updatedupdated2023-06-082023-06-08