1
C / Re: [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 18, 2013, 09:53:11 pm »
Interesting.
I added
I added
sfRenderWindow_SetActive(mainWindow, sfFalse);
before launching the thread as you mensioned:#include <stdlib.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
sfRenderWindow* mainWindow;
sfSprite* sprite;
void rendering_thread_handler (void* arg);
int main(int argc, char* argv[])
{
sfWindowSettings windowSettings = {24, 8, 0};
sfVideoMode videoMode = {800, 600, 32};
sfImage* image;
sfEvent event;
mainWindow = sfRenderWindow_Create(videoMode, "SFML window", sfResize | sfClose, windowSettings);
if (!mainWindow)
return EXIT_FAILURE;
image = sfImage_CreateFromFile("metal-hull.png");
if (!image)
return EXIT_FAILURE;
sprite = sfSprite_Create();
sfSprite_SetImage(sprite, image);
sfThread* rendering_thread = sfThread_Create(rendering_thread_handler, NULL);
sfRenderWindow_SetActive(mainWindow, sfFalse);
sfThread_Launch(rendering_thread);
while (sfRenderWindow_IsOpened(mainWindow))
{
while (sfRenderWindow_GetEvent(mainWindow, &event))
{
if (event.Type == sfEvtClosed)
{
sfRenderWindow_Close(mainWindow);
break;
}
}
}
sfThread_Wait(rendering_thread);
sfSprite_Destroy(sprite);
sfImage_Destroy(image);
sfRenderWindow_Destroy(mainWindow);
return EXIT_SUCCESS;
}
void rendering_thread_handler (void* arg)
{
while (sfRenderWindow_IsOpened(mainWindow))
{
sfRenderWindow_Clear(mainWindow, sfBlack);
sfRenderWindow_DrawSprite(mainWindow, sprite);
sfRenderWindow_Display(mainWindow);
}
}
The image is now correctly rendered to the window... but, as you said, I get an error when I close the program:#include <SFML/Graphics.h>
#include <SFML/Window.h>
sfRenderWindow* mainWindow;
sfSprite* sprite;
void rendering_thread_handler (void* arg);
int main(int argc, char* argv[])
{
sfWindowSettings windowSettings = {24, 8, 0};
sfVideoMode videoMode = {800, 600, 32};
sfImage* image;
sfEvent event;
mainWindow = sfRenderWindow_Create(videoMode, "SFML window", sfResize | sfClose, windowSettings);
if (!mainWindow)
return EXIT_FAILURE;
image = sfImage_CreateFromFile("metal-hull.png");
if (!image)
return EXIT_FAILURE;
sprite = sfSprite_Create();
sfSprite_SetImage(sprite, image);
sfThread* rendering_thread = sfThread_Create(rendering_thread_handler, NULL);
sfRenderWindow_SetActive(mainWindow, sfFalse);
sfThread_Launch(rendering_thread);
while (sfRenderWindow_IsOpened(mainWindow))
{
while (sfRenderWindow_GetEvent(mainWindow, &event))
{
if (event.Type == sfEvtClosed)
{
sfRenderWindow_Close(mainWindow);
break;
}
}
}
sfThread_Wait(rendering_thread);
sfSprite_Destroy(sprite);
sfImage_Destroy(image);
sfRenderWindow_Destroy(mainWindow);
return EXIT_SUCCESS;
}
void rendering_thread_handler (void* arg)
{
while (sfRenderWindow_IsOpened(mainWindow))
{
sfRenderWindow_Clear(mainWindow, sfBlack);
sfRenderWindow_DrawSprite(mainWindow, sprite);
sfRenderWindow_Display(mainWindow);
}
}
pure virtual method called
terminate called without an active exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Process returned 3 (0x3) execution time : 9.661 s
Press any key to continue.
terminate called without an active exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Process returned 3 (0x3) execution time : 9.661 s
Press any key to continue.