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

Pages: [1]
1
Network / [Solved] Sending binary files over TCP
« on: September 23, 2011, 08:08:03 pm »
Once again, you've saved the day Laurent. Thanks for the additional tips also. Merci!

2
Network / [Solved] Sending binary files over TCP
« on: September 23, 2011, 06:14:59 pm »
Hi there,

My question might not be directly related to SFML, however, I use SFML for all my networking needs.

I'm currently trying to implement an update function that would require sending binary files over TCP. The problem is, with binary files, they always get corrupted. I can transfer text files correctly.

Here is an example of the code used in the server:
Code: [Select]

std::ifstream File(Filename, std::ios::in | std::ios::binary | std::ios::ate);

// Here I got some code to send the filename and size before sending packets

File.seekg(0, std::ios::beg);
EncryptedPacket ToSend; // EncryptedPacket is the same as the one used in the tutorials, for now
Buffer = new char[4096];
while(File.read(Buffer, sizeof(Buffer)))
{
    ToSend.Clear(); // Clear the previous packet
    ToSend << Buffer; // Add the 4096 bytes we just read to the packet
    Socket.Send(ToSend); // Send the packet
    std::fill(Buffer, Buffer + sizeof Buffer, 0); // Clear the buffer
}
File.close();
delete[] Buffer;


And here is the code used in the client to write the packets into a file:
Code: [Select]

OutputFile.open(Filename, std::ofstream::binary); // Open a new file for writing, filename comes from the server

while(true)
{
    EncryptedPacket Received;
    Client.Receive(Received);
    std::string Buffer;
    Received >> Buffer; // Read the buffer from the packet
    OutputFile.write(Buffer.c_str(), sizeof(Buffer)); // Write the buffer to the file
}


I've tried sending PNG, MP4 and TAR files, without success.

Here is an example of the original PNG file header:
http://www.zimagez.com/zimage/screenshot-230911-120853pm.php

And here is an example of the transfered PNG file header:
http://www.zimagez.com/zimage/screenshot-230911-121142pm.php

If you need a complete working example, I could write one.

Thanks for your help, I've tried a lot of different things...[/img]

3
System / [SOLVED] Thread acting differently than in SDL
« on: September 08, 2011, 03:12:14 pm »
Thanks, definitely gives me an insight on how thread works. I did change everything to a delta time like technique. All good now, no more SDL :)

4
System / [SOLVED] Thread acting differently than in SDL
« on: September 07, 2011, 08:35:29 pm »
I don't use the window in the other thread, just modify a variable so that in my main rendering loop, it changes the opacity.

I must be the one that didn't understood something somewhere... but I just create a quick sample of the problem:

main.cpp
Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>

 
int Opacity = 255;
bool Running = true;

void FadeInThread(void* Data)
{
 
    float Delay = 0.007;
   
    while(Opacity <= 255)
    {
        std::cout << "Fade is at " << Opacity << std::endl;
        Opacity++;
        sf::Sleep(Delay);
    }
   
    std::cout << "Thread terminated" << std::endl;
   
}

void FadeIn()
{

    Opacity = 0;
    sf::Thread Thread(FadeInThread);
    Thread.Launch();

}

int main()
{
   
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

    // Usually this is a video, or image, or text that I fade in and out
    sf::Image Image(200, 200, sf::Color(0, 255, 0, Opacity));
    sf::Sprite Sprite;
    Sprite.SetImage(Image);
   
    FadeIn();
   
    while(Running)
    {
       
        // Start - Non related event handling stuff
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed || (Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            {
                Running = false;
            }
        }
        // End - Non related event handling stuff
       
        App.Clear();

        // Here I apply the opacity to the image, so that it "fades in"
        Sprite.SetColor(sf::Color(0, 255, 0, Opacity));
        App.Draw(Sprite);
       
        App.Display();
       
    }
   
    return 0;
   
}

5
System / [SOLVED] Thread acting differently than in SDL
« on: September 07, 2011, 08:15:28 pm »
Hello from Canada! (French Canadian, but nope not a "Quebecois")

I'm new to SFML, as of today. I'm porting my SDL application to SFML 1.6 for performance reasons. A simple 1080p video is rendering about 4x faster in SFML.

So, I got a problem with my threads... I'm not really an expert with C++ nor an expert with multi-threading.

The problem is that I create and launch a thread in the constructor of one of my classes, but my main loop (in which the object is created, hence the constructor called), doesn't display the RenderWindow until the thread is finished. Which is the reason I want to have threads.

So here is an example of my situation (not the exact code, but you'll understand):

main.cpp
Code: [Select]

new Scene();

while(App->IsRunning())
{
    ... my render stuff, that only starts after the thread is terminated (so after the FadeInThread method is done) ...    
}


Scene.cpp
Code: [Select]

Scene::Scene(sf::RenderWindow* Window, float Length)
{
   
    ... some code ...

    this->fadeIn();
   
}

void Scene::FadeIn()
{

    this->Opacity = 0;
    sf::Thread Thread(FadeInWrap, this);
    Thread.Launch();
    // SDL : SDL_CreateThread(FadeInWrap, this);

}

// This is a static method.
void FadeInWrap(void* Scn)
{
    ((Scene*) Scn)->FadeInThread();
}

void Scene::FadeInThread()
{
 
    float Delay = 0.007;
   
    while(this->Opacity < 255)
    {
        this->Opacity++;
        sf::Sleep(Delay);
        // SDL: SDL_Delay(Delay); (Delay was an int calculated differently)
    }
   
}


I know the static to non-static method is kinda weird, but only way I found out in SDL.

There's not much of a difference between a thread in SDL and in SFML, as far as I can see. Just don't understand why it doesn't continue the main execution while performing an operation in a thread.

Basically, if I launch my application, there's a 1.785 second delay because of the FadeInThread.

Could anyone help me out? I'm currently using the SDL_Thread, it works along SFML, but why import and use a huge library when SFML could do that for me.

Pages: [1]