Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - M4X

Pages: [1]
1
C / Re: [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 18, 2013, 09:53:11 pm »
Interesting.
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:
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.

2
C / Re: [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 18, 2013, 04:48:14 pm »
No problem, I was just a little bit busy ;)
So I left the things that I think caused the problem (i.e. the rendering from a separate thread), the event handling (for closing the window) is left in the main()
Notice that, now, it doesn't matter whether I comment the sfContext lines or not, no rendering is made in the window.
#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);
        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)
{
        //sfContext* p_context = sfContext_Create();
        //sfContext_SetActive(p_context , sfTrue);
       
        while (sfRenderWindow_IsOpened(mainWindow))
        {
                sfRenderWindow_Clear(mainWindow, sfBlack);
                sfRenderWindow_DrawSprite(mainWindow, sprite);
                sfRenderWindow_Display(mainWindow);
        }
       
        //sfContext_Destroy(p_context);
}

Quote
You can't link CSFML statically, it is written in C++ and would thus produce errors with your C linker.
Is that also the reason why -static-libgcc and/or -static have no effect on the linker ? (the program can't run without libgcc_s_dw2-1.dll )

3
C / Re: [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 18, 2013, 01:57:58 pm »
It's actually what I would call a minimal program with csfml...
I'll try to remove the unnecessary function calls later tonight and post the result.

Conserning the static linking, I tried linking (the Debug build) with these options
-lcsfml-window-s-d
-lcsfml-graphics-s-d
-lcsfml-system-s-d
but the build failed:
X:\programming\c\HelloCSFML\main.c||In function 'gui_init':|
X:\programming\c\HelloCSFML\main.c|74|warning: implicit declaration of function 'sfFont_CreateFromFile' [-Wimplicit-function-declaration]|
X:\programming\c\HelloCSFML\main.c|74|warning: assignment makes pointer from integer without a cast [enabled by default]|
X:\programming\c\HelloCSFML\main.c||In function 'gui_cleanup':|
X:\programming\c\HelloCSFML\main.c|90|warning: implicit declaration of function 'sfFont_Destroy' [-Wimplicit-function-declaration]|
C:\mingw\lib\SFML\libcsfml-window-s-d.a(Context.o):Context.cpp|| undefined reference to `sf::Context::SetActive(bool)'|
C:\mingw\lib\SFML\libcsfml-window-s-d.a(Context.o):Context.cpp:(.text$_ZN9sfContextC1Ev[__ZN9sfContextC1Ev]+0xd)||undefined reference to `sf::Context::Context()'|
||=== Build finished: 2 errors, 3 warnings (0 minutes, 0 seconds) ===|
 

4
C / Re: [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 18, 2013, 01:38:24 pm »
You'll find the complete Code::Blocks project here https://docs.google.com/file/d/0Bxj_y2ql85uAZS1MZzVsUnZ2bDA/edit?usp=sharing (notice that I've put the dlls/resources within the Debug/Release folders on purpose)

I just copied the program you provided in the CFML 1.6 chm doc file and separated the init/rendering/enventHandling/cleaning in different functions (only the rendering is done in a separate thread, I would have liked to put the event handling in another thread, but the program didn't work)
I "tried" to keep everything relatively organized ;)

PS: also, if you know, I'd like to know how I could launch the program without the console window appearing. I didn't exaclty get how to do this with CSFML:
Quote
Under Windows operating systems, you may have created a "Windows Application" project, especially if don't want the console to show up. In such case, to avoid replacing main by WinMain, you can link with SFML_Main static library and keep a standard and portable main entry point.
# http://www.sfml-dev.org/tutorials/1.6/window-window.php

5
C / Re: [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 18, 2013, 01:01:05 pm »
The app doesn't freeze if I remove them, but nothing is rendered, all I get is a black window.

6
C / Re: [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 17, 2013, 11:38:33 pm »
lol
When I split the example program, I forgot to add the outer loop in the event handler ^^"
Thank you for your help.
Now the everything runs as expected :)
I'm posting the working code (some cleaning is needed though ;) ), in case someone else got the same problem (notice the necessity of the sfContext creation for both the thread and the function called from the main, that's the only way I got the program to work without errors. I don't know why, but I'll try to figure it out once I get deeper into the library (that's still my first program :p ))
#ifdef __linux__
        #include <unistd.h>
#elif _WIN32
        #include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// CSFML headers
#include <SFML/Graphics.h>
#include <SFML/Window.h>

// csfml
sfWindowSettings windowSettings = {24, 8, 0};
sfVideoMode videoMode = {800, 600, 32};
sfRenderWindow* mainWindow;
sfImage* image;
sfSprite* sprite;
sfFont* font;
sfString* text;
sfEvent event;


int gui_init ();
void gui_cleanup ();

void rendering_thread_handler (void* arg);
void event_handler_thread_handler (void* arg);

long mx_get_uptime ();
void mx_msleep ();
void mx_sync_loop (long* t1, long fps);



int main(int argc, char* argv[])
{
        if (gui_init() != EXIT_SUCCESS)
                return EXIT_FAILURE;
       
       
        sfThread* rendering_thread = sfThread_Create(rendering_thread_handler, NULL);
        //sfThread* event_handling_thread = sfThread_Create(event_handler_thread_handler, NULL);
       
        sfThread_Launch(rendering_thread);
        //sfThread_Launch(event_handling_thread);
       
        event_handler_thread_handler (NULL);
       
        //sfThread_Wait(event_handling_thread);
        sfThread_Wait(rendering_thread);
       
       
        gui_cleanup();

        return EXIT_SUCCESS;

}

int gui_init ()
{
        /* Create the main window */
        mainWindow = sfRenderWindow_Create(videoMode, "SFML window", sfResize | sfClose, windowSettings);
        if (!mainWindow)
                return EXIT_FAILURE;

        /* Load a sprite to display */
        image = sfImage_CreateFromFile("metal-hull.png");
        if (!image)
                return EXIT_FAILURE;
       
        sprite = sfSprite_Create();
        sfSprite_SetImage(sprite, image);

        /* Create a graphical string to display */
        font = sfFont_CreateFromFile("UbuntuMono-R.ttf", 64, NULL);
        if (!font)
                return EXIT_FAILURE;
       
        text = sfString_Create();
        sfString_SetText(text, "Hello CSFML");
        sfString_SetFont(text, font);
        sfString_SetSize(text, 32);
       
        return EXIT_SUCCESS;
}

void gui_cleanup ()
{
        /* Cleanup resources */
        sfString_Destroy(text);
        sfFont_Destroy(font);
        sfSprite_Destroy(sprite);
        sfImage_Destroy(image);
        sfRenderWindow_Destroy(mainWindow);
}

void rendering_thread_handler (void* arg)
{
        sfContext* p_context = sfContext_Create();
        sfContext_SetActive(p_context , sfTrue);
       
        long t1 = mx_get_uptime();
        while (sfRenderWindow_IsOpened(mainWindow))
        {
                /* Clear the screen */
                sfRenderWindow_Clear(mainWindow, sfBlack);

                /* Draw the sprite */
                sfRenderWindow_DrawSprite(mainWindow, sprite);

                /* Draw the string */
                sfRenderWindow_DrawString(mainWindow, text);

                /* Update the window */
                sfRenderWindow_Display(mainWindow);
               
                mx_sync_loop(&t1, 30);
        }
       
        sfContext_Destroy(p_context);
}

void event_handler_thread_handler (void* arg)
{
        sfContext* p_context = sfContext_Create();
        sfContext_SetActive(p_context , sfTrue);
       
        /* Process events */
        long t1 = mx_get_uptime();
        while (sfRenderWindow_IsOpened(mainWindow))
        {
                while (sfRenderWindow_GetEvent(mainWindow, &event))
                {

                        /* Close window : exit */
                        if (event.Type == sfEvtClosed)
                        {
                                sfRenderWindow_Close(mainWindow);
                                break;
                        }


                }
               
                mx_sync_loop(&t1, 30);
        }
        sfContext_Destroy(p_context);
}

/** \brief Returns the amount of micro-seconds since the program was started.
 * \return The time (in milli-seconds) since the program was started.
 *
 */

long mx_get_uptime ()
{
        return (long) ((double) clock() / (CLOCKS_PER_SEC / 1000.0));   // ... until I think of a better implementation...
}

/** \brief Causes the program to wait for ms_time milliseconds.
 *
 */

void mx_msleep (long ms_time)
{
#ifdef __linux__
        usleep(ms_time * 1000);
#elif _WIN32
        Sleep(ms_time);
#endif
}

/** \brief Synchronizes a loop to be executed at certain rate.
 * \param t1 a pointer to the last syncing time in milliseconds. Must be initialized before entering the loop.
 * \param fps the frame rate to maintain
 *
 */

void mx_sync_loop (long* t1, long fps)
{
        long t2 = mx_get_uptime();
        if ((t2-*t1) > (1000/fps))
        {
                *t1 = t2;
        }
        else
        {
                mx_msleep((1000/fps) - (t2-*t1));
                *t1 = mx_get_uptime();
        }
}
 

7
C / Re: [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 17, 2013, 11:07:31 pm »
Thank you for replying :) . I actually have to do this project entirely in C ;)
I moved the event handling in the main:
int main(int argc, char* argv[])
{
        if (gui_init() != EXIT_SUCCESS)
                return EXIT_FAILURE;
       
       
        sfThread* rendering_thread = sfThread_Create(rendering_thread_handler, NULL);
        sfThread_Launch(rendering_thread);
       
        event_handler_thread_handler(NULL);
       
        sfThread_Wait(rendering_thread);
       
       
        gui_cleanup();

        return EXIT_SUCCESS;

}
and I also removed the sfContext lines from the functions, but the problem is still the same.

8
C / [Solved] [CSFML 1.6] Using threads, window freezes, no rendering
« on: February 17, 2013, 10:42:44 pm »
Hello  :)

I was trying the example program provided in the CSFML 1.6 doc and everything went as expected.
But when I tried to use separate threads (one for rendering, another for handling events) the window freezes, and all I get is a black display  :o  the only way to close the program is through the task manager.

Here is my code:
#ifdef __linux__
        #include <unistd.h>
#elif _WIN32
        #include <windows.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// CSFML headers
#include <SFML/Graphics.h>
#include <SFML/Window.h>

// csfml
sfWindowSettings windowSettings = {24, 8, 0};
sfVideoMode videoMode = {800, 600, 32};
sfRenderWindow* mainWindow;
sfImage* image;
sfSprite* sprite;
sfFont* font;
sfString* text;
sfEvent event;


int gui_init ();
void gui_cleanup ();

void rendering_thread_handler (void* arg);
void event_handler_thread_handler (void* arg);

long mx_get_uptime ();
void mx_msleep ();
void mx_sync_loop (long* t1, long fps);



int main(int argc, char* argv[])
{
        if (gui_init() != EXIT_SUCCESS)
                return EXIT_FAILURE;
       
       
        sfThread* rendering_thread = sfThread_Create(rendering_thread_handler, NULL);
        sfThread* event_handling_thread = sfThread_Create(event_handler_thread_handler, NULL);
       
        sfThread_Launch(rendering_thread);
        sfThread_Launch(event_handling_thread);
       
        sfThread_Wait(event_handling_thread);
        sfThread_Wait(rendering_thread);
       
       
        gui_cleanup();

        return EXIT_SUCCESS;

}

int gui_init ()
{
        /* Create the main window */
        mainWindow = sfRenderWindow_Create(videoMode, "SFML window", sfResize | sfClose, windowSettings);
        if (!mainWindow)
                return EXIT_FAILURE;

        /* Load a sprite to display */
        image = sfImage_CreateFromFile("metal-hull.png");
        if (!image)
                return EXIT_FAILURE;
       
        sprite = sfSprite_Create();
        sfSprite_SetImage(sprite, image);

        /* Create a graphical string to display */
        font = sfFont_CreateFromFile("UbuntuMono-R.ttf", 64, NULL);
        if (!font)
                return EXIT_FAILURE;
       
        text = sfString_Create();
        sfString_SetText(text, "Hello CSFML");
        sfString_SetFont(text, font);
        sfString_SetSize(text, 32);
       
        return EXIT_SUCCESS;
}

void gui_cleanup ()
{
        /* Cleanup resources */
        sfString_Destroy(text);
        sfFont_Destroy(font);
        sfSprite_Destroy(sprite);
        sfImage_Destroy(image);
        sfRenderWindow_Destroy(mainWindow);
}

void rendering_thread_handler (void* arg)
{
        sfContext* p_context = sfContext_Create();
        sfContext_SetActive(p_context , sfTrue);
       
        long t1 = mx_get_uptime();
        while (sfRenderWindow_IsOpened(mainWindow))
        {
                /* Clear the screen */
                sfRenderWindow_Clear(mainWindow, sfBlack);

                /* Draw the sprite */
                sfRenderWindow_DrawSprite(mainWindow, sprite);

                /* Draw the string */
                sfRenderWindow_DrawString(mainWindow, text);

                /* Update the window */
                sfRenderWindow_Display(mainWindow);
               
                mx_sync_loop(&t1, 30);
        }
       
        sfContext_Destroy(p_context);
}

void event_handler_thread_handler (void* arg)
{
        sfContext* p_context = sfContext_Create();
        sfContext_SetActive(p_context , sfTrue);
       
        /* Process events */
        long t1 = mx_get_uptime();
        while (sfRenderWindow_GetEvent(mainWindow, &event))
        {
               
                /* Close window : exit */
                if (event.Type == sfEvtClosed)
                {
                        sfRenderWindow_Close(mainWindow);
                        break;
                }
               
                mx_sync_loop(&t1, 30);
        }
       
        sfContext_Destroy(p_context);
}

/** \brief Returns the amount of micro-seconds since the program was started.
 * \return The time (in milli-seconds) since the program was started.
 *
 */

long mx_get_uptime ()
{
        return (long) ((double) clock() / (CLOCKS_PER_SEC / 1000.0));   // ... until I think of a better implementation...
}

/** \brief Causes the program to wait for ms_time milliseconds.
 *
 */

void mx_msleep (long ms_time)
{
#ifdef __linux__
        usleep(ms_time * 1000);
#elif _WIN32
        Sleep(ms_time);
#endif
}

/** \brief Synchronizes a loop to be executed at certain rate.
 * \param t1 a pointer to the last syncing time in milliseconds. Must be initialized before entering the loop.
 * \param fps the frame rate to maintain
 *
 */

void mx_sync_loop (long* t1, long fps)
{
        long t2 = mx_get_uptime();
        if ((t2-*t1) > (1000/fps))
        {
                *t1 = t2;
        }
        else
        {
                mx_msleep((1000/fps) - (t2-*t1));
                *t1 = mx_get_uptime();
        }
}
 

Can someone help me, please ?
I'm using Code::Blocks 12.11, MinGW with gcc 4.7.2 and my OS is a 64-bit Windows 7.

PS: I don't know why, but when I run the Debug build, the program prints a lot of
An internal OpenGL call failed in SOURCE_FILE.cpp (LINE_NUMBER) : GL_INVALID_OPERATION, the specified operation is not allowed in the current state
messages to the console (where SOURCE_FILE.cpp (LINE_NUMBER) are various, for example Image.cpp (482)).
Also, I tried with and without the
    sfContext* p_context = sfContext_Create();
    sfContext_SetActive(p_context , sfTrue);
    ...
    sfContext_Destroy(p_context);
 
lines, but the problem is the same.

Pages: [1]
anything