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
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).
Related
So in my project I make a Event thread to catch the sdl event and may be pass it to the main thread to rander. But sometimes I get the Segementfault.
this is my test code.
#include "SDL2/SDL.h"
#include <SDL2/SDL_timer.h>
#include <iostream>
#include <thread>
using namespace std;
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
bool running = true;
/* this is my event thread */
auto run = [&] {
for (; running; ) {
SDL_Event event; SDL_PollEvent(&event);
// ...
SDL_Delay(10);
}
};
thread t(run);
/* and the main is the randering thread */
for (int i = 0; i < 10; ++i) {
SDL_Window *window = SDL_CreateWindow("cycle. ",
100,
100,
600,
600,
SDL_WINDOW_SHOWN);
SDL_Renderer *screen = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
// ... do some rander ...
// may be i will get some task to rander use the blocking queue
SDL_Delay(500);
SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window);
}
running = false;
t.join();
SDL_Quit();
}
I want to know why I get the segement fault.
Should I make the Event listening and the rander in the same thead? (This will cause another problem: If placed in the same thread, when I set the loop to only 60 times per second, it also means that I can only render 60 times per second. This will cause my program to become stuck.)
Fundamentally, you should try to call SDL from a single thread. Even if you decide you need to multithread your program, you should do work in other threads and then synchronize that work to the main thread, which should use SDL to render / event-handle / etc.
Your program may be segfaulting because you're attempting to join() a thread that already join()-ed, so you want to at least check joinable() before you do so.
You also need to call PollEvent in a loop, since multiple events may be queued here.
But, especially since you're using Delays, it seems like you won't need the possible performance gained by multithreading your events. I would therefore suggest something like this:
#include <SDL2/SDL.h>
#include <iostream>
#include <thread>
using namespace std;
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
bool running = true;
SDL_Window *window =
SDL_CreateWindow("cycle. ", 100, 100, 600, 600, SDL_WINDOW_SHOWN);
SDL_Renderer *screen =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
/* and the main is the randering thread */
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
// TODO: handle events
// for example, set `running` to false when the window is closed or
// escape is pressed
// ...
}
// TODO: render here
// ...
}
SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window);
SDL_Quit();
}
I'm not going to assume what you do and don't need, so here's a possible solution where you multithread the events (but you really, really, really should only use this if you absolutely need it).
This does the same as your code, except it calls PollEvent in the same thread as the rendering (the main thread), but handles the events somewhere else.
For this we use a queue of events, and a mutex to make sure it's thread-safe. I'm just putting this here for completeness' sake, and you probably (definitely) don't need this.
#include <SDL2/SDL.h>
#include <iostream>
#include <mutex>
#include <queue>
#include <thread>
using namespace std;
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
bool running = true;
std::mutex eventQueueMutex;
std::queue<SDL_Event> eventQueue;
auto handleEvents = [&running, &eventQueue, &eventQueueMutex] {
while (running) {
{ // scope for unique_lock
std::unique_lock<std::mutex> lock(eventQueueMutex);
if (!eventQueue.empty()) {
auto event = eventQueue.front();
eventQueue.pop();
lock.unlock();
// TODO: handle the event here
}
} // end of scope for unique_lock
std::this_thread::sleep_for(
std::chrono::milliseconds(16)); // adjust this to your needs
}
};
std::thread eventHandlerThread = std::thread(handleEvents);
SDL_Window *window =
SDL_CreateWindow("cycle. ", 100, 100, 600, 600, SDL_WINDOW_SHOWN);
SDL_Renderer *screen =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
/* and the main is the randering thread */
while (running) {
SDL_Event event;
eventQueueMutex.lock();
while (SDL_PollEvent(&event)) {
eventQueue.push(event); // copy event into queue
}
eventQueueMutex.unlock();
// TODO: render here
// ...
}
if (eventHandlerThread.joinable()) {
eventHandlerThread.join();
}
SDL_DestroyRenderer(screen);
SDL_DestroyWindow(window);
SDL_Quit();
}
In both of these examples, check for my TODO comments to see where you write your code.
This is my code:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, const char * argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *_window;
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 500, SDL_WINDOW_RESIZABLE);
SDL_Delay(20000);
SDL_DestroyWindow(_window);
SDL_Quit();
return 0;
}
Im working in Xcode. I've downloaded SDL2 and imported the library to the projects build phases. I've tested that the SDL2 works correctly.
The problem is that window never shows up. I just get a "spinning-mac-wheel" and then the program quits after the delay. I've made sure that the window is not hidden behind somewhere.
Ideas?
You have to give the system a chance to have it's event loop run.
easiest is to poll for events yourself:
SDL_Event e;
bool quit = false;
while (!quit){
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT){
quit = true;
}
if (e.type == SDL_KEYDOWN){
quit = true;
}
if (e.type == SDL_MOUSEBUTTONDOWN){
quit = true;
}
}
}
instead of the wait loop
--- Addendum
Since this answer is still helping people maybe it's nice if I also add a bit more info on why this works instead of just posting the solution.
When on the Mac (same for Windows actually) a program starts, it starts with just the 'main thread'. This is the thread which is used to set up UI stuff. The 'main thead' differs from other threads in that it comes with an event handling system. This system catches events like mouse moves, key presses, button clicks and then queues these and lets your code respond to it. All the UI things on Mac (and Windows) rely on this event pump being there and running. This is the reason why if you do anything UI related in your code you need to make sure you are not on a different thread.
Now, in your code you initialise the window and the UI, but then you do a SDL_Delay. This just blocks the thread and halts it for 20 seconds so nothing is done. And since you do that on the main thread, even the handling of the queue with the events is blocked. So on the Mac that shows as the spinning macwheel.
So the solution I posted actually keeps on polling for events and handles them. This way you are effectively also 'idling', but the moment events are posted (like mouse clicks and keys) the thread will wake up again and stuff will be processed.
You have to load a bitmap image, or display something on the window, for Xcode to start displaying the window.
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main() {
SDL_Window * window = nullptr;
SDL_Surface * window_surface = nullptr;
SDL_Surface * image_surface = nullptr;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
window_surface = SDL_GetWindowSurface(window);
image_surface = SDL_LoadBMP("image.bmp");
SDL_BlitSurface(image_surface, NULL, window_surface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
SDL_DestroyWindow(window);
SDL_FreeSurface(image_surface);
SDL_Quit();
}
You need to initialize SDL with SDL_Init(SDL_INIT_VIDEO) before creating the window.
Please remove the sdl_delay() and replace it with the below mentioned code. I don't have any reason for it but I tried on my own and it works
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}
Consider a simple program: (require -std=c++11 -lpthread -lreadline -lSDL2 when compile with g++ with pthread)
#include <iostream>
#include <thread>
#include <SDL2/SDL.h>
#include <cstdlib>
extern "C" {
#include <readline/readline.h>
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
auto window = SDL_CreateWindow("title", 0, 0, 200, 200, SDL_WINDOW_SHOWN);
std::thread console([](){
while (true) {
char *console_input_c_str = readline("> ");
if (console_input_c_str == NULL)
break;
std::cout << "line: " << console_input_c_str << '\n';
std::free(console_input_c_str);
}
});
while (true) {
SDL_Event event;
SDL_WaitEvent(&event);
std::cerr << "received event type "<<event.type<<'\n';
if(event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE)
break;
}
SDL_DestroyWindow(window);
SDL_Quit();
console.join();
}
The main thread creates a window window with SDL2, enter a SDL event loop, while the thread console repeatedly read from the console using readline. When the window is exited, it waits for the console thread to finish then exit.
The program works fine; however, how can I make the console thread to stop when the window is exited?
It's fine if the program only use a single thread. It's also fine if exiting the console readline quits the program.
Exit readline is easy, but there is a problem with all the answers there - pthread_kill or reset -Q is not portable.
Terminating a thread is easy (using std::terminate or .detach()), but the console is left in a bad state because readline don't terminate normally. To reset the console it's possible to use reset -Q, but that's not portable.
Using readline's alternative (async) interface doesn't work because SDL don't listen for events on the console. Using select is also not portable.
Actually, readline is not completely blocking, it calls rl_event_hook every 1/10 seconds (by default). It's possible to check whether the program is stopped inside the event hook, and if so do a longjmp or throw to get out of the function (in this example int is used, although it's possible to define your own exception class):
std::atomic<bool> stopped (false);
rl_event_hook = [](){
if (stopped)
throw 0;
return 0;
};
try {
/* something calls readline() */
} catch (int) {
rl_cleanup_after_signal();
}
return 0;
or by setting rl_done = 1: (this won't work when rl_event_hook is nullptr. Also note that rl_event_hook is not called when a key is held, it's necessary to override rl_getc_function to change the behavior. It's not recommended to modify rl_done from the other thread)
std::atomic<bool> stopped (false);
rl_event_hook = [](){
if (stopped)
rl_done = true;
return 0;
};
/* something calls readline() */
if (stopped) return 0;
/* process */
return 0;
and in the main thread use:
stopped = true;
console.join();
This is my code:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, const char * argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *_window;
_window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 700, 500, SDL_WINDOW_RESIZABLE);
SDL_Delay(20000);
SDL_DestroyWindow(_window);
SDL_Quit();
return 0;
}
Im working in Xcode. I've downloaded SDL2 and imported the library to the projects build phases. I've tested that the SDL2 works correctly.
The problem is that window never shows up. I just get a "spinning-mac-wheel" and then the program quits after the delay. I've made sure that the window is not hidden behind somewhere.
Ideas?
You have to give the system a chance to have it's event loop run.
easiest is to poll for events yourself:
SDL_Event e;
bool quit = false;
while (!quit){
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT){
quit = true;
}
if (e.type == SDL_KEYDOWN){
quit = true;
}
if (e.type == SDL_MOUSEBUTTONDOWN){
quit = true;
}
}
}
instead of the wait loop
--- Addendum
Since this answer is still helping people maybe it's nice if I also add a bit more info on why this works instead of just posting the solution.
When on the Mac (same for Windows actually) a program starts, it starts with just the 'main thread'. This is the thread which is used to set up UI stuff. The 'main thead' differs from other threads in that it comes with an event handling system. This system catches events like mouse moves, key presses, button clicks and then queues these and lets your code respond to it. All the UI things on Mac (and Windows) rely on this event pump being there and running. This is the reason why if you do anything UI related in your code you need to make sure you are not on a different thread.
Now, in your code you initialise the window and the UI, but then you do a SDL_Delay. This just blocks the thread and halts it for 20 seconds so nothing is done. And since you do that on the main thread, even the handling of the queue with the events is blocked. So on the Mac that shows as the spinning macwheel.
So the solution I posted actually keeps on polling for events and handles them. This way you are effectively also 'idling', but the moment events are posted (like mouse clicks and keys) the thread will wake up again and stuff will be processed.
You have to load a bitmap image, or display something on the window, for Xcode to start displaying the window.
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
int main() {
SDL_Window * window = nullptr;
SDL_Surface * window_surface = nullptr;
SDL_Surface * image_surface = nullptr;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
window_surface = SDL_GetWindowSurface(window);
image_surface = SDL_LoadBMP("image.bmp");
SDL_BlitSurface(image_surface, NULL, window_surface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
SDL_DestroyWindow(window);
SDL_FreeSurface(image_surface);
SDL_Quit();
}
You need to initialize SDL with SDL_Init(SDL_INIT_VIDEO) before creating the window.
Please remove the sdl_delay() and replace it with the below mentioned code. I don't have any reason for it but I tried on my own and it works
bool isquit = false;
SDL_Event event;
while (!isquit) {
if (SDL_PollEvent( & event)) {
if (event.type == SDL_QUIT) {
isquit = true;
}
}
}
I am trying to follow a SFML tutorial from this link, however, for some reason my application seems to be caling the close event as soon as my program enters into the event loop.
Here is my code.
#include <SFML/Graphics.hpp>
#include <iostream>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main()
{
sf::RenderWindow Window(sf::VideoMode(500,400),"SFML tutorial");
/* Or we can do this
* sf::RenderWindow Window
* Window.create (sf::VideoMode(800,600),"SFML tutorial");
*/
//Game loop
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event));
{
if(Event.type == sf::Event::Closed)
{
Window.close();
std::cout << "Close" << std::endl;
}
}
}
return 0;
}
For some reason as soon as my program enters the event loop it receives a sf::Event::Closed event. I have done some research and this is not because the window is too big for my screen.
To fix my issue I have had to ad a int variable canClose to 0 then modify the event loop to this.
if(Event.type == sf::Event::Closed)
{
if(canClose == 1)
{
Window.close();
} else {
canClose = 1;
}
}
After I do this the window now opens and runs as expected.
P.S this is made using xcode and the SFML template.
Edit:
Just made a new xcode SFML project and removed some code in it to make a empty window and it seems to whork. Code looks exactly the same. This seams strange because it is exactly the same code.
From what i have seen, do not use an if statement for checking events, use a switch statement. Also Use a while(window.isOpen()) when trying to poll events
Have you read through the tutorials on http://www.sfml-dev.org ? Because i can guarantee that it is the best source for learning sfml. Use it before using another source.
Your problem is that you declared the variable Event Event type, you have to change the name of the variable to lowercase.
in this way
#include <SFML/Graphics.hpp>
#include <iostream>
// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
int main()
{
sf::RenderWindow Window(sf::VideoMode(500,400),"SFML tutorial");
/* Or we can do this
* sf::RenderWindow Window
* Window.create (sf::VideoMode(800,600),"SFML tutorial");
*/
//Game loop
while(Window.isOpen())
{
sf::Event event;
while(Window.pollEvent(event));
{
if(event.type == sf::Event::Closed)
{
Window.close();
std::cout << "Close" << std::endl;
}
}
}
return 0;
that way you will not have any problems