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

Author Topic: fstream & SFML - uploading and downlading data  (Read 3707 times)

0 Members and 1 Guest are viewing this topic.

Filosof

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
fstream & SFML - uploading and downlading data
« on: June 09, 2016, 04:34:11 pm »
Hey friends, I faced with the problem of data input and output.
All variables write in a file and read in binary form, but the trouble with sprites. There no mistake on reading and writing.
Programm compilation passes, but when I try to draw a sprite downloaded from a file this application crashes during execution.

Either I'm somewhere made a mistake or something I do not know.
Nobody faced such problem?

Hyden

  • Guest
Re: fstream & SFML - uploading and downlading data
« Reply #1 on: June 09, 2016, 05:07:55 pm »
I'm not exactly sure what your issue is?

You should post a sample of your code that loads the texture, sets the sprite's texture to the texture you loaded and the code where you draw the sprite.

If you still do not get an answer then you should post your question on Stackoverflow (http://stackoverflow.com/questions/tagged/sfml) whilst remembering to follow the Stackoverflow question guidelines.
« Last Edit: June 10, 2016, 08:47:13 am by Laurent »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: fstream & SFML - uploading and downlading data
« Reply #2 on: June 10, 2016, 10:20:15 am »
Sprites cannot be saved in files, because they contain pointers, and pointers are stuff that you can't predict. Why? because pointer is in real an int value holding an address from Ram and these addresses can (almost)never be the same :P
« Last Edit: June 10, 2016, 10:22:17 am by Mr_Blame »

Hyden

  • Guest
Re: fstream & SFML - uploading and downlading data
« Reply #3 on: June 10, 2016, 05:08:37 pm »
Alright, Filosof, you're trying to download a texture from the the internet and then load it into your program and draw it in the program?

Firstly, make sure the file is a type that is compatible with SFML. Here's a list: bmp, png, tga, jpg, gif, psd, hdr and pic. I don't understand fully how you're actually downloading the file but you will need to check that the file is actually being downloaded. Perhaps you can create a test program where you just download a file and save it into a folder?

Next, you're going to want to make sure you're loading the texture correctly into the program. By default SFML should return any error messages, so make sure you look in the console for errors.

Finally, make sure you're actually setting the sprite's texture correctly. You might of forgotten to actually set the sprite's texture to the texture you loaded.

Further more, you need to upload your code to something like www.hastebin.com so we can see and test for ourselves where the issue is. We can't do anything if you just tell us the problem, we need to actually see where the problem is in the code.

Filosof

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: fstream & SFML - uploading and downlading data
« Reply #4 on: June 10, 2016, 09:31:33 pm »
Alright... here it is...
First run the programm and everything is ok. But in second time do comment funcions "save_profile" in main and you get the error...
Even though you after "read_saves" function write that:
temp[0].avatar_spr_ = spr.sprite_;
temp[1].avatar_spr_ = spr2.sprite_;
You still get the error...

#include <iostream>
#include <SFML/Graphics.hpp>
#include <fstream>
#include <io.h>
using namespace std;
using namespace sf;

class SpriteManager{
public:
        String file_;
        Image image_;
        Texture texture_;
        Sprite sprite_;
        SpriteManager() {}
        SpriteManager(String File, int X = 0, int Y = 0){
                file_ = File;
                image_.loadFromFile("images/" + file_);
                texture_.loadFromImage(image_);
                sprite_.setTexture(texture_);
                sprite_.setPosition(X, Y);
        }
};

class profile
{
public:
        string name_;
        Sprite avatar_spr_;
        profile() {};
        profile(string name) { name_ = name; }
};

void save_profile(profile & player)
{
        ofstream file(player.name_ + ".save", ios_base::out | ios_base::binary);
        if (!file.is_open()){
                cerr << "Can't open file for output:\n";
                exit(EXIT_FAILURE);
        }
        file.write((char *)&player, sizeof player);
        file.close();
}

void read_saves(profile * temp)
{
        int hFile, count = 0;
        _finddata_t file[2];
        if ((hFile = _findfirst("*.save", &file[count])) == -1){
                cerr << "Can't open file for output:\n" << endl;
        }
        else while (_findnext(hFile, &file[++count]) == 0);

        ifstream save;
        for (int i = 0; i < count; i++){
                string temp_str(file[i].name);
                save.open(temp_str, ios_base::in | ios_base::binary);
                if (!save.is_open()){
                        cerr << "Can't read a file" << endl;
                }
                else{
                        save.read((char *)&temp[i], sizeof temp[i]);
                        save.close();
                }
        }
        _findclose(hFile);
}

int main()
{
        RenderWindow window(VideoMode(900, 450), "Lesson 1. kychka-pc.ru");
        View view;
        Vector2f pos;
        Event event;
        SpriteManager spr("image1.png"), spr2("image2.png");
        profile player1("First"), player2("Second"), temp[2];
        player1.avatar_spr_ = spr.sprite_;
        player2.avatar_spr_ = spr2.sprite_;
       
        save_profile(player1);
        save_profile(player2);
        read_saves(temp);

        while (window.isOpen())
        {
               
                while (window.pollEvent(event)){
                        if (event.type == Event::Closed)
                                window.close();
                }
                window.clear();
                temp[0].avatar_spr_.setPosition(385, 90);
                window.draw(temp[0].avatar_spr_);
                temp[1].avatar_spr_.setPosition(185, 120);
                window.draw(temp[1].avatar_spr_);
                window.display();
        }

        return 0;
}

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: fstream & SFML - uploading and downlading data
« Reply #5 on: June 11, 2016, 12:36:44 am »
the natural way to serialize class objects in C++, classes only a type like any other fundamental types int, float. only different with classes is that its size which is varying depends on its member data. for example:
class Foo{};
Foo has size of 1 byte only. this size can grow if you added member data. you don't know what stores in those size of space in memory, it could be address to somewhere in memory locations if it has pointers. so what you are trying to do is not only invalid but dangerous if it runs. please avoid it. don't run this code.

however, if you cared to store raw data for any image format like .png in file. you have to decode it first. most known decoder is base 64. and use it thing like this:

// data is encoded for raw data in .png
std::string decoded = base64_decode(data);

sf::Texture texture;
if (!texture.loadFromMemory(decoded.c_str(), decoded.length()))
{
    // handle error
}


i have post thread in here, when i was working in encode/decode for base 64. here link to it
« Last Edit: June 11, 2016, 01:00:07 am by MORTAL »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: fstream & SFML - uploading and downlading data
« Reply #6 on: June 11, 2016, 12:11:02 pm »
Which error message you get on crash?