Reputation:
I have two thread which Displays the color changing SDL window.It compiles and runs fine, But I cant close them by clicking the close button of the window.
here is my code:-
#include <iostream>
#include <SDL2/SDL.h>
#include <thread>
using namespace std;
void th(string name,int x,int y,int w,int h,Uint32 flags)
{
int i=0,o=0,p=0;
SDL_Window *win;
SDL_Event e;
win = SDL_CreateWindow(name.c_str(),x,y,w,h,flags);
SDL_ShowWindow(win);
SDL_Surface *win_sur = SDL_GetWindowSurface(win);
SDL_FillRect(win_sur,0,SDL_MapRGB(win_sur->format,i,o,p));
SDL_UpdateWindowSurface(win);
while(1)
{
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT)
goto end;
}
SDL_FillRect(win_sur,0,SDL_MapRGB(win_sur->format,i,o,p));
SDL_UpdateWindowSurface(win);
i++;o+=2;p+=3;
SDL_Delay(10);
}
end:
SDL_DestroyWindow(win);
}
int main()
{
int lk =0;
SDL_Init(SDL_INIT_VIDEO);
thread t1(th,"win1",90,100,500,500,SDL_WINDOW_OPENGL);
thread t2(th,"win2",10,400,500,500,SDL_WINDOW_OPENGL);
t1.join();
t2.join();
SDL_Quit();
}
I am using gcc 7.3 and Linux
Upvotes: 0
Views: 283
Reputation: 13269
From the docs:
you can only call this function in the thread that set the video mode
In your case, since you call SDL_Init(SDL_INIT_VIDEO)
in the main thread, you can only call SDL_PollEvent
there as well. It probably never returns anything in your other threads.
Also, SDL_QUIT
is generated when the user clicks on the close button of the last existing window. Check out SDL_WINDOWEVENT_CLOSE
when you have multiple windows (or even by default, it's probably the better choice anyway).
Upvotes: 2