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

Author Topic: sf::NonCopyable, can you tell me what in my class causes it.  (Read 3202 times)

0 Members and 1 Guest are viewing this topic.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
sf::NonCopyable, can you tell me what in my class causes it.
« on: August 25, 2014, 01:16:41 pm »
I suppose something like sf::RenderTexture is NoNCopyable. I will just make it a pointer and transfer ownership when operator= called, but i got no clue what is NoNCopyable. Can you help me and point it out?

The error:
Code: [Select]
1>------ Build started: Project: TheRPG, Configuration: Release Win32 ------
1>  Chat.cpp
1>Chat.cpp(27): warning C4244: 'argument' : conversion from 'float' to 'unsigned int', possible loss of data
1>Chat.cpp(65): warning C4018: '>=' : signed/unsigned mismatch
1>C:\C++\Libs\SFML VS2013\include\SFML/Graphics/RenderTarget.hpp(419): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
1>          C:\C++\Libs\SFML VS2013\include\SFML/System/NonCopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
1>          C:\C++\Libs\SFML VS2013\include\SFML/System/NonCopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::RenderTarget::RenderTarget(const sf::RenderTarget &)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

//Removing this line of code makes it go away, and compile as it should
void DialogHolder::AddDialog(Dialog & d)
{
listDialog.push_back(d);//This line here
}
Code: [Select]
//HPP
class Dialog
{
public:
Dialog(int linkedObjectVecID = -1, int posX = -1, int posY = -1);
Dialog(std::string & str, sf::Font & font, int linkedObjectVecID = -1, int posX = -1, int posY = -1);
void MakeText(std::string & str, sf::Font & font);
void Loop();

sf::Text txt;
sf::RenderTexture renTex;
sf::Sprite spr;

int linkedObjectVecID;
int posX, posY;
};

class DialogHolder
{
public:
DialogHolder(Font & ObjFont);

void AddDialog(Dialog & d);
void RemoveDialog(int vecID);
void Loop();
void Draw(sf::RenderWindow & renWin);

std::vector<Dialog> listDialog;
private:
Font & objFont;
};

//CPP
Dialog::Dialog(int LinkedObjectVecID, int PosX, int PosY)
{
linkedObjectVecID = LinkedObjectVecID;
posX = PosX;
posY = PosY;
}

Dialog::Dialog(std::string & str, sf::Font & font,int LinkedObjectVecID, int PosX, int PosY)
{
linkedObjectVecID = LinkedObjectVecID;
posX = PosX;
posY = PosY;
MakeText(str, font);
}
void Dialog::MakeText(std::string & str, sf::Font & font)
{
txt.setCharacterSize(32);
txt.setColor(sf::Color::White);
txt.setFont(font);
txt.setString(str);
txt.setPosition(-txt.getLocalBounds().left, -txt.getLocalBounds().top);

renTex.create(txt.getLocalBounds().width + txt.getLocalBounds().left * 2, txt.getLocalBounds().height + txt.getLocalBounds().top * 2);
renTex.draw(txt);
renTex.display();

spr.setTexture(renTex.getTexture(), true);
if (linkedObjectVecID != -1)
{//Set position on unit
Object & o = glob::objFunc.objObjectH->listObject[linkedObjectVecID];
spr.setPosition(o.pos.x + (o.size.x/2) - (txt.getLocalBounds().width / 2), o.pos.y - txt.getLocalBounds().height - (txt.getLocalBounds().top*2));
}
else
{//Set position on position
spr.setPosition(posX - (txt.getLocalBounds().width / 2), posY - txt.getLocalBounds().height - (txt.getLocalBounds().top * 2));
}
}

void Dialog::Loop()
{
if (linkedObjectVecID != -1)
{//Set position on unit
Object & o = glob::objFunc.objObjectH->listObject[linkedObjectVecID];
spr.setPosition(o.pos.x + (o.size.x / 2) - (txt.getLocalBounds().width / 2), o.pos.y - txt.getLocalBounds().height - (txt.getLocalBounds().top * 2));
}
}


DialogHolder::DialogHolder(Font & ObjFont)
: objFont(ObjFont)
{

}

void DialogHolder::AddDialog(Dialog & d)
{
listDialog.push_back(d);
}
void DialogHolder::RemoveDialog(int vecID)
{
if (vecID < 0 || vecID >= listDialog.size())
return;

//listDialog.erase(listDialog.begin() + vecID);
}


void DialogHolder::Loop()
{
for (Dialog & d : listDialog)
d.Loop();
}

void DialogHolder::Draw(sf::RenderWindow & renWin)
{
for (Dialog & d : listDialog)
renWin.draw(d.spr);
}

BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: sf::NonCopyable, can you tell me what in my class causes it.
« Reply #1 on: August 25, 2014, 01:29:01 pm »
It's this: http://sfml-dev.org/documentation/2.1/classsf_1_1NonCopyable.php

Basically, this is a class with a private copy constructor and copy assignment operator, so nobody can copy it.

All the SFML classes for which copying is logically and/or technically impossible inherit from this class to prevent any accidental or misguided attempts to copy them.  Like most STL insertion methods, push_back() does make a copy of the element you give it, so that's where Dialog's default compiler-generated copy constructor got invoked and failed.  Consider using a list or map with emplace() if you have C++11, since that lets you construct the Dialog directly inside the container without any copying.

Quote
I will just make it a pointer and transfer ownership when operator= called
Whatever you mean by this is probably possible if done right, but considering you aren't familiar with the idiom of non-copyability through inheritance, and "just making it a (raw?) pointer" is a bad idea, I'm worried you might not know about things like RAII and when it's actually a good idea to use owning pointers and why they should almost always be smart pointers and how to ensure exception safety when doing this stuff and so on.  If you aren't, please ask (well, google first, then ask if it still doesn't make sense).
« Last Edit: August 25, 2014, 01:40:39 pm by Ixrec »

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: sf::NonCopyable, can you tell me what in my class causes it.
« Reply #2 on: August 25, 2014, 01:57:14 pm »
Okay, redesign....
Code: [Select]
//In class tex=sf::Texture, spr=sf::Sprite
sf::RenderTexture renTex;
renTex.create(txt.getLocalBounds().width + txt.getLocalBounds().left * 2, txt.getLocalBounds().height + txt.getLocalBounds().top * 2);
renTex.draw(txt);
renTex.display();
tex.create(renTex.getSize().x, renTex.getSize().y);
tex = renTex.getTexture();
//This line here
tex = renTex.getTexture();
For some reason the "tex" texture is fully white when i use it later down the line, but when i extracted this code in minimal example, and sf::RenderTexture renTex; didn't deconstruct the tex texture is correct.

Can i copy the renderTexture texture intro the "tex" Texture somehow? Or just make the renderTexture's texture the "tex" Texture? so i directly draw intro it, and don't have to copy.
Because
Code: [Select]
tex = renTex.getTexture();
Doesn't really make a new copy of the texture as far as i see.
« Last Edit: August 25, 2014, 02:01:12 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::NonCopyable, can you tell me what in my class causes it.
« Reply #3 on: August 25, 2014, 02:01:00 pm »
You can directly use or reference (not modify, it's const) the RenderTexture's texture, or you can copy it to a separate sf::Texture instance if you need to.
Laurent Gomila - SFML developer

 

anything