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 - Jaenis

Pages: 1 [2] 3 4
16
System / Thread startet from a Thread
« on: August 25, 2009, 05:52:24 am »
"delete this" is okay if you never allocate that variable to stack.

Small example to illustrate this:
Code: [Select]
class Foo
{
void del(void)
{
delete this;
}
}

Not okay:
{
class Foo tmp;
tmp.del();        // Deletes the pointer
}                 // Variable tmp goes out of scope, destructor will be called again, undefined behavior

Okay:
{
class Foo *tmp= new Foo;
tmp.del();        // Deletes the pointer
}
But since this can lead into very interesting bugs, I would recommend you doing something else :)


So, you want to launch x number of threads and see that they will get freed when they are done?
Maybe just store them in a list? And wait them to finish
Code: [Select]

#include <list>

// List for allocated threads
std::list <sf::Thread *> CreatedThreads;


class TC1 : public sf::Thread{
private :
    virtual void Run()
    {
      std::cout << "I'm the main thread" << std::endl;
        // Print something...
        for (int i = 0; i < 10; ++i)
      {
         TC2 *tc = new TC2;
         CreatedThreads.push_back(tc); // Store the thread pointer
         tc->number = i;
         tc->Launch();
      }
    }

};


int main(int argc, char** argv)
{
TC1 tc;
tc.Launch();

system("PAUSE"); // Needed in this example for TC1 thread to run before next line

// Wait all threads to finish
while (!CreatedThreads.empty())
{
sf::Thread *Thread=CreatedThreads.front();
CreatedThreads.pop_front();

// Wait thread to finish and free memory when done
Thread->Wait();
delete Thread;
}

}

17
System / Thread startet from a Thread
« on: August 24, 2009, 05:35:30 pm »
This is quite simple actually.
Read this part from your first example:
Code: [Select]
{
TC2 tc;
tc.number = i;
tc.Launch();
} // What happens to variable tc at this point?

Answer is that class TC2 destructor will get called.
Since it is inherited from sf::Thread, also the thread will be closed.

When you allocate all threads will new in second example, their destructors will not get called at that point.
So if you have to do something like this, you need to allocate them dynamically and store pointers somewhere.

18
Graphics / Can't change sprite image
« on: August 15, 2009, 07:57:18 am »
Quote from: "Laurent"
I've implemented this new parameter in the sfml2 branch.

Enjoy :)

Whee, thanks!  :D

19
General discussions / SFML 2.0
« on: August 15, 2009, 07:33:40 am »
Quote from: "heishe"
now let's say you have a rock that has 7 corners. at the moment in sfml you can't really do stuff like that


Couldn't you use OpenGL to draw a 7 sided polygon for you?
Something like:
Code: [Select]
// Structure for coordinates
struct Coordinates
{
float x,y;
};

// Declare rock vertices and texture coordinates
struct Coordinates RockVert[7]={ {0,10}, {10,5}, ... };
struct Coordinates RockTex[7]={ {0,0}, {1,0.5}, ... };

// Load sf::Image
...
// Bind the image and set it to repeat, not mandatory
Image.Bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


// Inside main loop
{
...

// Draw polygon
Image.Bind();
glBegin(GL_POLYGON);
for (int i=0; i<7; i++)
{
glTexCoord2f(RockTex[i].x, RockTex[i].y);
glVertex2f(RockVert[i].x, RockVert[i].y);
}
glEnd();

...
}


If you want to set that texture repeating, it width and height has to be power of two (hardware requirement).
For example, texture size 128x128 or 32x64 will work with that repeat command.

20
General discussions / Is good enough?
« on: August 15, 2009, 07:11:48 am »
Quote from: "Core Xii"
The trick is that you can change easily what the colors are by swapping the palette.

This can be done with shaders, as l0calh05t already pointed out.
And since those shaders are supported by SFML, why couldn't you just use that approach?

21
Graphics / Can't change sprite image
« on: August 11, 2009, 03:44:22 pm »
Great, that sounds good :)
Thanks

22
Graphics / Can't change sprite image
« on: August 11, 2009, 03:35:29 pm »
I haven't even set the subrect for my sprite, so I wondered why it is displaying wrong after I change to another image.
It might be clear for you that this will be the case, but it will be confusing for users who do not know about this feature. Just mentioning that one should set the subrect after setting the image in documentation would clarify the issue.

23
Graphics / Can't change sprite image
« on: August 11, 2009, 03:20:48 pm »
Okay, could you also mention it on the documentation pages please :)
Couldn't find it there

24
Graphics / Can't change sprite image
« on: August 11, 2009, 03:02:35 pm »
Hi

Found that I can't change sprite source image in SFML2

Here's simple example:
Code: [Select]
// Load first image and create sprite
sf::Image Image;
Image.LoadFromFile("Logo.png"); // Size of 400x100
sf::Sprite Sprite(Image);

...time passes...

// Load second image and set it to sprite
sf::Image Img2;
Img2.LoadFromFile("Checkers.png"); // Size of 256x256
Sprite.SetImage(Img2);

After that last line the Sprite size is still 400x100 instead of 256x256 what I would expect.

This behavior comes from SFML/Graphics/Sprite.cpp:
Code: [Select]
void Sprite::SetImage(const Image& image)
{
    // If there was no source image before and the new image is valid, adjust the source rectangle
    if (!myImage && (image.GetWidth() > 0) && (image.GetHeight() > 0))
    {
        SetSubRect(IntRect(0, 0, image.GetWidth(), image.GetHeight()));

So, if image did already exist, then sprite size will not get changed.

This gets fixed by making this change to SFML:
Code: [Select]
   // If new image is valid, adjust the source rectangle
    if (image.GetWidth() > 0) && (image.GetHeight() > 0)

Or calling Sprite.SetSubRect( sf::IntRect(0,0, Img2.GetWidth(), Img2.GetHeight()) ) after I change the image.

Is there some reason for this behavior or is it just a small bug?

25
Window / Where are the event callbacks?
« on: August 10, 2009, 10:15:29 am »
Quote from: "Laurent"
Have you seen on this forum the number of people who retrieve the key code after a mouse move event (for example)? ;)

Ahaa, you are thinking this from general usage perspective.
I instead thought this from my usage perspective, where when I have "KeyHandler" function, I'am supposed to access the key data and ignore other union members.

To get rid of this thing you pointed above, change that event structure from unions to inheritance. Then when user gets keypress event, he has to downcast it to proper class.
Though this would be more complicated to use because of dynamic_casts :lol:


But anyhow, just adding that "event count" would allow both ways ;)

Quote
Hey, why don't you use a void* parameter? This way you won't have to update your code, even if SFML switches to something else than sf::Event :wink:
 :shock:
Scary  8)

26
Window / Where are the event callbacks?
« on: August 10, 2009, 09:24:02 am »
Quote from: "Laurent"
Quote
Laurent, could we get "sf::Event::Count" variable to get number of event enums?

Well, I think you shouldn't need this ;)
You code is simple and short, but it doesn't have the best design in my opinion. If you're using a separate callback fore every event then you should pass it the proper parameters, not the full sf::Event.

Why shouldn't I use the full sf::Event? It is union anyhow, which is great for this kind of thing.
If I explode that code to handle all events separately, it gets quite long. And I don't see any point in it ;)

Also, if/when SFML will add more events I'll have to modify that code if I am passing the actual parameters. But if I am just passing that sf::Event, then I don't need to do anything to get it work.

27
Window / Where are the event callbacks?
« on: August 10, 2009, 06:36:55 am »
Actually decoupling is very simple, with boost signals.


First declare event signals global (or better yet, have a event handler class that does this)
Code: [Select]
boost::signals2::signal<void (const sf::Event &)> EventSignals[ 16 ]; (See note on bottom)


Then hook your functions into signals:
Code: [Select]

// Connect sf::Event::Close event to CloseEvent function
EventSignals[ sf::Event::Closed ].connect( CloseEvent );

// Callback function
void CloseEvent(const sf::Event &Event)
{
// Do something
}



And in mainloop have this event processing code:
Code: [Select]
// Process events
sf::Event Event;
while (App.GetEvent(Event))
// Call all connected functions
EventSignals[ Event.Type ]( Event );



Or with classes:
Code: [Select]

class Foo
{
// Constructor
Foo();

// Key event handler, has to be static
// (one should be able to use boost::function too, haven't checked that yet)
static void KeyEvent(const sf::Event &Event);
};

// Constructor
void Foo::Foo()
{
// Connect KeyPressed event to Foo::KeyEvent function
EventSignals[ sf::Event::KeyPressed ].connect( Foo::KeyEvent );
}

// This function will get called when key is pressed
void Foo::KeyEvent(const sf::Event &Event)
{
if (Event.Key.Code==sf::Key::Escape)
EscPressed=true;
}




Note:
Laurent, could we get "sf::Event::Count" variable to get number of event enums?
Pretty please?  :roll:

28
General / Keylogger warning from Comodo
« on: August 10, 2009, 05:48:24 am »
Hi

I lately installed Comodo firewall/virus scanner and noticed that I'll get following alert when running something that uses SFML:


It seems to think that my app could be a keylogger since it is using dinput.dll.
Wonder would there be any way around this, this kind of windows will scare people :D

29
I got inspired of your system and made similar test.

What you are doing when you call renderTask->pause(true) ?
I do this instead:
Code: [Select]

void Renderer::DetachWindow(void)
{
// Stop the thread
Stop();              // Send async stop command
Thread.join();       // Wait thread to finish
Clear();             // Clear function queue

// We do not have window anymore
Window=NULL;
}


And where you unpause your thread I do following:
Code: [Select]

void Renderer::AttachWindow(sf::Window &NewWindow)
{
// Store pointer and set caller window inactive
Window=&NewWindow;
Window->SetActive(false);

// Launch thread
Thread=boost::thread( boost::bind(&Renderer::ThreadMain,this) );
}


So when I want to change videomode I just wait the renderer thread to exit, then I can be sure that it does not require access to SFML window. And afterwards I restart the renderer thread.
Might be overkill, but I don't care if fullscreen<->windowed change takes few more milliseconds ;)

30
Interesting app...
Have you noticed that you can drag that SMFL window around and it's contents are still being updated while dragging? (On Windows OS)

Normally Windows blocks this that your screen would freeze while you are dragging the window.
Hmm.. You seem to have actual drawing on another thread, so it gets around this. Nice trick :)

Pages: 1 [2] 3 4
anything