SFML community forums
Help => System => Topic started by: acerookie1 on August 11, 2010, 02:28:24 am
-
how to i use a sfml string with fstream. it didnt work in my code. though i was converting the from stl string to sfml string.
this is the console class
console.hpp:
class Console
{
sf::Shape Rect;
sf::Font Text_font;
string buffer;
int x, y, h, w;
char get_typed(sf::Event &event);
void delete_letter();
void add_text(char l);
void add_letter(char text);
public:
sf::String Text;
string entered;
void set_text(string text);
void clear();
string get_text();
bool is_entering();
void set_entering(bool enter = true);
bool entering, Cancel;
void update(sf::Event &event);
void Draw(sf::RenderWindow &Window);
Console(int text_size,int o_x, int o_y, int o_h, int o_w, sf::Font &o_font);
sf::String GetText();
};
console.cpp
#include"headers.hpp"
#include"console.hpp"
void Console::set_text(string text)
{
Text.SetText(text);
}
void Console::clear()
{
//set_text("");
//cout<<" buffer :"<<buffer<<"."<<endl;
//cout<<" final :"<<entered<<"."<<endl;
buffer.clear();
}
void Console::set_entering(bool enter)
{
entering = enter;
}
bool Console::is_entering()
{
return entering;
}
string Console::get_text()
{
return entered;
}
void Console::delete_letter()
{
buffer.erase(buffer.length() - 1, 1);
}
char Console::get_typed(sf::Event &event)
{
return (char)event.Text.Unicode;
}
void Console::add_letter(char text)
{
buffer.insert(buffer.end(),text);
}
void Console::update(sf::Event &event)
{
if(entering)
{
entered = buffer;
Text.SetText(buffer);
if(event.Type == sf::Event::KeyReleased)
{
if(event.Key.Code == sf::Key::Delete || event.Key.Code == sf::Key::Back)
{
delete_letter();
}
else if(event.Key.Code == sf::Key::Return)
{
entering = false;
}
else if(event.Key.Code == sf::Key::Escape)
{
clear();
entering = false;
}
}
if(event.Type == sf::Event::TextEntered && event.Key.Code != sf::Key::Back && event.Key.Code != sf::Key::Delete)
{
add_letter(get_typed(event));
}
else if(event.Type == sf::Event::Closed)
{
entering = false;
}
}
}
void Console::Draw(sf::RenderWindow &Window)
{
if(entering)
{
Text.SetText(entered);
Window.Draw(Rect);
Window.Draw(Text);
}
}
sf::String Console::GetText()
{
return Text;
}
Console::Console(int text_size,int o_x, int o_y, int o_h, int o_w, sf::Font &o_font)
{
x = o_x; y = o_y; h = o_h; w = o_w;
Text_font = o_font;
Text.SetSize(text_size);
Text.SetFont(Text_font);
Text.SetPosition(x,y);
Text.SetColor(sf::Color(0,0,0,255));
entering = false;
Rect = sf::Shape::Rectangle(x,y,x + w,y + h,sf::Color(255,0,0),0.f,sf::Color(255,0,0));
}
-
What is the relation between your Console class and fstream? Can you show the relevant code (ie. the line which fails to compile), and the error that you get?
If all you want is the internal text of a sf::String as a std::string, you can do that:
sf::String Text;
...
std::string = Text.GetText();
-
What is the relation between your Console class and fstream? Can you show the relevant code (ie. the line which fails to compile), and the error that you get?
If all you want is the internal text of a sf::String as a std::string, you can do that:
sf::String Text;
...
std::string = Text.GetText();
it compiles but it wont create the file with the text. but if i use cin it will work fine! idk thats what i did and it wont open it with fstream and create my map file. the consoel doesnt have a relation with fstream the level class handles streamin.
Level.cpp: (in level::save and load)
#include"headers.hpp"
#include"level.hpp"
Config::Config(string file)
{
File = file;
string temp;
ifstream config;
config.open( file.c_str() );
while(config>>temp)
{
if(temp == "Map_x")
{
config>>x;
}
if(temp == "Map_y")
{
config>>y;
}
}
config.close();
}
void Level::Create_Tiles(int X, int Y, int Z)
{
x = X;
y = Y;
z = Z;
Clear_Tiles();
}
Level::~Level()
{
delete [] tile;
}
void Level::Clear_Tiles()
{
cout<<"Clearinng tiles"<<endl;
int counter = 1;
for(int a = 0; a < z; a++)
{
for(int b = 0; b < x; b++)
{
for(int c = 0; c < y; c++)
{
tile[a][b][c].x = b * 32;
tile[a][b][c].y = c * 32;
tile[a][b][c].s_x = 0;
tile[a][b][c].s_y = 0;
tile[a][b][c].h = 32;
tile[a][b][c].w = 32;
cout<<"Tile: "<< counter <<endl;
counter++;
}
}
}
}
void Level::Save_Map(sf::String &save, int H, int W)
{
string buffer = save.GetText();
cout<<"Is something wrong?"<<endl<<"converted sfml to string: "<<buffer<<endl;
#ifndef Debug
cout<<"buffer: "<< buffer <<endl;
ofstream Save( buffer.c_str() );
#endif
#ifdef Debug
cout<<"debug mode. Please Enter Filename: "<<endl;
cin>>buffer;
cin.ignore();
ofstream Save( buffer.c_str() );
#endif
cout<<"Saving to: "<< buffer <<" Length: "<<save.GetSize()<<endl;
if(Save.good())
{
cout<<"File being created..."<<endl;
Save<<"H "<< H <<" W "<< W <<"\n\n";
for(int a = 0; a < z; a++)
{
for(int b = 0; b < x; b++)
{
for(int c = 0; c < y; c++)
{
Save<<"Layer "<< a << " Tile "<< c;
Save<<" X "<< tile[a][b][c].x <<" Y " << tile[a][b][c].y;
Save<<" Strip_x "<< tile[a][b][c].s_x;
Save<<" Strip_y "<<tile[a][b][c].s_y;
Save<<" Hieght "<< H;
Save<<" Width "<< W;
Save<<" END \n\n";
}
Save<<"\n";
}
Save<<"\n \n \n";
}
cout<<"done."<<endl;
}
else
{
cout<<"File: "<< buffer<<" could not be created!"<<endl;
}
Save.close();
}
void Level::Load_Map(sf::String &load)
{
string temp;
string buffer = (string)load.GetText();
#ifndef Debug
ifstream Load( buffer.c_str() );
#endif
#ifdef Debug
cout<<"debug mode. Please Enter Filename: "<<endl;
cin>>buffer;
cin.ignore();
ifstream Load( buffer.c_str() );
#endif
if(Load.is_open())
{
cout<<"Found file"<<endl;
while(Load>>temp)
{
if(temp == "Layer")
{
Load>>layer;
}
if(temp == "Tile")
{
Load>>tile_count;
}
if(temp == "X")
{
Load>>x_coord;
}
if(temp == "Y")
{
Load>>y_coord;
}
if(temp == "Strip_x")
{
Load>>strip_x;
}
if(temp == "Strip_y")
{
Load>>strip_y;
}
if(temp == "H")
{
Load>>h;
}
if(temp == "W")
{
Load>>w;
}
if(temp == "END")
{
tile[layer][x_coord / w][y_coord / h].x = x_coord;
tile[layer][x_coord / w][y_coord / h].y = y_coord;
tile[layer][x_coord / w][y_coord / h].s_x = strip_x;
tile[layer][x_coord / w][y_coord / h].s_y = strip_y;
tile[layer][x_coord / w][y_coord / h].h = h;
tile[layer][x_coord / w][y_coord / h].w = w;
}
}
}
else
{
cout<<"Could not open or find file: "<< buffer <<endl;
}
Load.close();
}
void Level::Draw(sf::Image &image, sf::RenderWindow &Window)
{
draw.SetImage( image );
for(int a = 0; a < z; a++)
{
for(int b = 0; b < x; b++)
{
for(int c = 0; c < y; c++)
{
draw.SetSubRect( sf::IntRect(tile[a][b][c].s_x,tile[a][b][c].s_y, tile[a][b][c].s_x + tile[a][b][c].w, tile[a][b][c].s_y + tile[a][b][c].h) );
draw.SetPosition(tile[a][b][c].x + ofs_x, tile[a][b][c].y + ofs_y);
Window.Draw(draw);
}
}
}
}