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

Pages: 1 [2] 3
16
General discussions / SFML and C++0x; "error: use of deleted function"
« on: October 30, 2011, 03:25:11 am »
That seems to be not working. Here is a minimal example.

Code: [Select]
#include <functional>
#include <vector>

#include <SFML/Graphics.hpp>

class entity
{
public:
entity (int x)
{
sf::FloatRect rect(x, 50, 50, 50);
Shape = sf::Shape::Rectangle(rect, sf::Color::Red);
}

void update ()
{
}

void draw (sf::RenderTarget& target)
{
target.Draw(Shape);
}

private:
sf::Shape Shape;

};

int main ()
{
std::vector<entity> ents;

sf::RenderWindow win(sf::VideoMode(640, 480, 32), "test", sf::Style::Close);

entity en1(0);
entity en2(50);
entity en3(150);

ents.push_back(en1);
ents.push_back(en2);
ents.push_back(en3);

while (win.IsOpened())
{
sf::Event event;

while (win.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
win.Close();
}

win.Clear(sf::Color::Black);

for_each(ents.begin(), ents.end(), std::mem_fun_ref(&entity::update));

for_each(ents.begin(), ents.end(), std::bind(std::mem_fun_ref(&entity::draw), std::ref(win)));

win.Display();
}

return 0;
}


It still won't compile. I get a different error, which is....cryptic to me :)

17
General discussions / SFML and C++0x; "error: use of deleted function"
« on: October 26, 2011, 08:49:52 pm »
Hi there!

As some of you may know I'm still a beginner with C++, so sometimes I got newbie questions.
Let's suppose, I have this object:

Code: [Select]
class aClass
{
public:
   aClass () { // initialize stuff }

    void Update () { // update stuff, not relevant }

    void Draw (const sf::RenderTarget& target)
    {
         Sprite.Draw(target);
    }

private:
    sf::Sprite Sprite;
};


I have a few of this object in a vector:
Code: [Select]
std::vector<aClass> vec;

So, I was experimenting with for_each and std::mem_fun_ref, and I came up with this:

Code: [Select]

// win is a sf::RenderWindow object
for_each(vec.begin(), vec.end(), std::mem_fun_ref(&aClass::Update));

for_each(vec.begin(), vec.end(), std::bind(std::mem_fun_ref(&aClass::Draw), win));


I compile it with -std=c++0x using g++ (4.6, so it supports C++0x), under Linux, and I got a nice error message, "error: use of deleted function" at the second line. After a bit of Google, I really didn't get a good answer. I think, it is because the sf::RenderWindow is a non-copyable class, which is the base class of sf::RenderTarget. Is this correct ? Or I'm doing it in a wrong way ?
Because, if I use a "classic" for loop, it compiles just fine. (note: the first line compiles without any error, and does what it should do in both cases)
Code: [Select]

std::vector<aClass>::iterator iter;
for (iter = vec.begin(); iter != vec.end(); ++iter)
{
   iter->Draw(win);
}

18
Window / Mouse Click without isButtonPressed
« on: October 26, 2011, 12:06:17 am »
Maybe I got it wrong, what you want to achieve, but check out the ConvertCoords function.
Possible usage might be something like this:

Code: [Select]

sf::RenderWindow win(...);
//...
sf::FloatRect SomeRect; // you can get a rect from every sf::Text and sf::Sprite object, check the documentation, for more about this

sf::Vector2i mouse_pos = sf::Mouse::GetPosition(win);

if (sf::Mouse::IsButtonPressed(sf::Mouse::Left) && (SomeRect.Contains(win.ConvertCoords(mouse_pos.x, mouse_pos.y))))
{
    // do something
}
//...

19
General / SFML2 installation on Linux?
« on: September 17, 2011, 12:17:42 pm »
Quote from: "Laurent"
Don't forget that SFML 2 hasn't been released yet.


Sorry! That is true. But on my Arch there is a package for SFML 2 (sfml-git), so I was assuming that most of the distro's have a package for it aswell.

Well, this should work...

1.) Get the SFML git sources.

2.)
Code: [Select]
cmake -DCMAKE_INSTALL_PREFIX=/usr/ .. -DBUILD_DOC=true -DBUILD_EXAMPLES=true


3.)
Code: [Select]
make
make doc

Then as root:
Code: [Select]
make install

Here is the package for Arch (from AUR): link (check the PKGBUILD)

20
General / SFML2 installation on Linux?
« on: September 16, 2011, 04:23:18 pm »
I think using your ditribution's package manager is the simpliest way...

21
General / deleting sf::Texture* - I must be doing it really wrong
« on: August 19, 2011, 02:47:38 pm »
Maybe I am wrong, but your "temp" sf::Texture goes out of scope, doesn't it ? When that happens the sf::Texture* objects in your map are pointing to non-existent objects. And then you iterate through the map and try to delete an invalid pointer.

Code: [Select]

std::map<std::string, sf::Texture> images;

//the load image function is like this:
sf::Texture temp;
if( temp.LoadFromFile(fileName) == false )
   return false;
images[imageKey] = temp;

This should work without any problem. Or if you insist on using pointers, then use a smart pointer instead of raw pointers.

22
General discussions / Design flaw in my game ?
« on: August 16, 2011, 05:22:31 pm »
Quote from: "Disch"

I think you're making this more complicated than it needs to be.


That's what I think, as well :)

Quote from: "Disch"

I would just do this:

Code: [Select]

struct GameEnvironment
{
  // .. various "game wide" data that can be used in every state

  // throw the Save function in here
  void Save();
};


Create one instance of that class in main and pass a pointer to it to each game state when they're created.


Something like this ?
Code: [Select]

// this is from the OP
GameEnvironment* ge = GameEnvironment::GetInstance();

/*...*/

// declare game states
PtrToGameState Title(new TitleState(TITLE_STATE, ge));
PtrToGameState Map(new MapState(MAP_STATE, ge));


Quote from: "Nexus"
You should carefully read the official documentation of Boost.Function and Boost.Bind, everything is explained there.

That's what I'll do, when I got the time :)

After all of these replies, I think something is moving in my head. The GameState and all of its derivatives are good as they are, and iterating through a vector is fine, I just need to pass each state an "all-purpose" object. (and I need to expand my knowledge with the function objects, just in case :) )

23
General discussions / Design flaw in my game ?
« on: August 16, 2011, 03:56:10 pm »
So, I can't avoid using function objects :) They seem powerful to me, but I've not yet grasped the idea behind them. I recently found a nice article about Boost.Function and Bind on gpwiki. But there is something I don't really get. Can function objects have different parameters with the same name (the return-type don't really matters) ? Like: func (), funct (int a), func (std::string), func (int a, std::string abc)

By the way, I checked Thor::Event yesterday before I opened this thread, and was thinking maybe I can use it for my states, it provides a very similar functionality.

So, polymorphism is not the best way to write such a state manager, is it ?

24
General discussions / Design flaw in my game ?
« on: August 16, 2011, 04:45:39 am »
Quote from: "Nexus"
Maybe a virtual GameState::Save() function that is implemented in each concrete state class? If you want to save the game, then this function is called for all elements of your states vector...


Yes, you are right :) It seems I was thinking too much, so I forgot about the easiest solution. But is this the best way ? Or should I not worry about it, just implement it ?

Quote from: "MorleyDev"
Perhaps some kind of centralised event mapping system?

If you want to save, simply fire off an event state and anything that wants to listen to it handles it. And if not, then that state simply ignores it.

I'm really sorry, but I'm not quite sure what you mean. Do you mean some system like the "observer pattern" ?

25
General discussions / Design flaw in my game ?
« on: August 15, 2011, 06:11:12 pm »
Hi there

I hope, this is the right place to ask this.

I got really stuck with my project. I think I have a nice flaw in my design, or maybe I simply lack of proper knowledge of C++.

Right, so I have a game engine. It handles different game states. Something like this:

Code: [Select]

// main.cpp

// include things

int main ()
{
  Engine* MyEngine = Engine::GetInstance();
 
  // declare game states
  PtrToGameState Title(new TitleState(TITLE_STATE));
  PtrToGameState Map(new MapState(MAP_STATE));
 
  MyEngine->AddState(Title);
  MyEngine->AddState(Map);
 
  MyEngine->Init(...);
  MyEngine->Run(...);
 
  return 0;
}


The engine stores the game state objects in a vector, and when it's running it iterates through this vector and calls some function:
Code: [Select]

// hopefully this piece of code is easy to understand...
while (running && lState != EXIT_STATE)
{
  for (iter = states.begin(); iter != states.end(); ++iter)
  {
    if ((*iter)->GetState() == lState)
    {
      (*iter)->Init();
      lState = (*iter)->Mainloop();
    }
  }
}


So, with this I have a nice state manager-like thing, which handles various states I create, as long as I inherit from an abstract base class called, GameState.
However, the state objects can't communicate with eachother. I try to explain what I mean, and want to achieve.
I start a new game, and I'm playing it for 2 hours, then decide to call it a day. So I want to save my progress. But with this code I can't do that!
The TitleState don't know where I am in the MapState, so even though TitleState has a Save Game function, it doesn't know what I want to save. (And the engine only knows the GameState base class.)

I hope you understand my problem, and give me some advice on what should I modify in my design. Because I am out of ideas.

26
Graphics / Strange window resize behaviour
« on: August 08, 2011, 05:47:40 pm »
Quote from: "Laurent"
When the window is resized, mouse coordinates no longer matches scene coordinates (because the view is zoomed). You must use RenderWindow::ConvertCoords to convert the mouse position accordingly.

Thanks a lot, it does the trick :) I promise, next time I will read the documentation properly!
And I was wondering that it has to do something with the default sf::View or maybe I have to catch resize event in the event loop. But no :) As expected from SFML, a simple function call and everything is solved.
Thanks, again.

27
Graphics / Strange window resize behaviour
« on: August 07, 2011, 10:36:15 pm »
Okay. Maybe I am really doing something wrong.

Here is a little example. It's stupid and messy, but compiles and runs.

Code: [Select]

#include <SFML/Graphics.hpp>

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Selecting with mouse - test", sf::Style::Close | sf::Style::Resize);

sf::Text text;
text.SetPosition(50, 50);
text.SetColor(sf::Color::White);
text.SetString("Selected color:");

sf::Text text2 = text;
text2.SetPosition(600, 50);
text2.SetString("Avaliable colors:");

sf::Text text3 = text;
text3.SetPosition(50, 350);
text3.SetString("Test area");

sf::FloatRect rect1(650, 100, 50, 50);
sf::Shape shape1 = sf::Shape::Rectangle(rect1, sf::Color::Red);

sf::FloatRect rect2(650, 150, 50, 50);
sf::Shape shape2 = sf::Shape::Rectangle(rect2, sf::Color::Green);

sf::FloatRect sel(50, 100, 50, 50);
sf::Shape selected = sf::Shape::Rectangle(sel, sf::Color::White);

sf::Color selectedColor = sf::Color::White;

sf::FloatRect test1(50, 400, 50, 50);
sf::FloatRect test2(50, 450, 50, 50);

sf::Shape s1 = sf::Shape::Rectangle(test1, selectedColor);
sf::Shape s2 = sf::Shape::Rectangle(test2, selectedColor);


while (App.IsOpened())
{
sf::Event event;

while (App.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
App.Close();
if (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape)
App.Close();
}

if (sf::Mouse::IsButtonPressed(sf::Mouse::Left))
{

if (rect1.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
selectedColor = sf::Color::Red;
}
else if (rect2.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
selectedColor = sf::Color::Green;
}

if (test1.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
s1.SetColor(selectedColor);
}
if (test2.Contains((sf::Vector2f)sf::Mouse::GetPosition(App)))
{
s2.SetColor(selectedColor);
}
}

selected.SetColor(selectedColor);

App.Clear(sf::Color::Black);

App.Draw(text);
App.Draw(text2);
App.Draw(text3);
App.Draw(selected);
App.Draw(shape1);
App.Draw(shape2);
App.Draw(s1);
App.Draw(s2);

App.Display();
}

return 0;
}


If I resize the window, everything is scaled accordingly, and I can't select any of the colors anymore. Is this how it should be ? Or is it a bug ?

28
Graphics / Strange window resize behaviour
« on: August 07, 2011, 08:52:10 pm »
Hi there!

I created a nice main menu class last week, and today I expanded it with mouse events. It works nicely. But only in my test program. If I try to
use it in my game, it works, but sf::Text.SetPosition() doesn't set the position how I want it. Interestingly the GetRect() returns the correct
position.

I try to show how I use it.

test program
Code: [Select]

// include things

MenuSystem menu;

void init ()
{
// get font file from disk
menu.SetFont(font);
menu.SetPos(50, 100);
}

int main ()
{
sf::RenderWindow App(sf::VideoMode(800, 600m 32), "Menu test 2", sf::Style::Close);

init();

menu.AddEntry("New", NEW);
menu.AddEntry("Load Game", LOAD);
// etc...

while (App.IsOpened())
{
MenuLabel choice = NONE;
// process sf::Event::Closed here

choice = menu.OnEvent(App);

menu.UpdateEntries(); // this just sets the color of the text

App.Clear(sf::Color::Black);

menu.DrawEntries(App); // draw them

App.Display();
}

return 0;
}

This works perfectly like I said before.

And my game...
Code: [Select]

void MenuState::Init ()
{
// load font
mMenu.SetPos(50, 100);
mMenu.SetFont(font);

mMenu.AddEntry("New Game", NEW);
// .... like above
}

State MenuState::Mainloop (sf::RenderWindow& win)
{
while (mRunning)
{
mRunning = EventHandler(win);

Update();

Render(win);
}

return mNextState;
}

bool MenuState::EventHandler (sf::RenderWindow& win)
{
switch(mMenu.OnEvent(win))
{
// ... not relevant
// but if the player selects a menu it returns false
}

return true;  // if no menu was clicked
}

void MenuState::Update ()
{
mMenu.UpdateEntries();
}

void MenuState::Render (sf::RenderWindow& win)
{
// clear screen and draw stuff, not relevant
}


Basically, I use it in the same way in both cases, but the outcome is different. In my test program the text is in the right place, in my game it is not. Even if I tell it the exact position, the text is rendered somewhere else, but GetRect() gives back the right position.

EDIT:
Okay, I just tested it with sf::Style::Fullscreen, and it works like I intended.

EDIT2:
:) I think I figured out what the problem is. Apparently if I set sf::Style::Resize, then position is not correct. So, the problem is with my system, to be exact how it handles the windows. (I use XFCE)
Sorry about this silly topic  :oops:

29
Graphics / Simulating Window-Border Collision!
« on: August 07, 2011, 01:34:18 am »
Think about this.
If x is 0 for example, the function returns false, and you can never change x again. Or x is 800, the function returns again false, for all eternity :)

I would go with something like this:

Code: [Select]

// not real code
void moveBlob (sf::Sprite& sprite, int direction_x)
{
   if (sprite.position_x >= 800 || sprite.position_x <= 0)
       sprite.position_x = sprite.position_x;
   else
       sprite.position += 1 * direction_x;
}

// usage, is something like this
sf::Sprite blob;
if (left_key_ispressed)
   moveBlob(blob, -1);
if (right_key_ispressed)
   moveBlob(blob, 1);

30
General / Theory behind a simple GUI
« on: August 01, 2011, 03:48:02 pm »
Quote from: "Nexus"
The better approach is not to use manual memory management (new and delete) but instead RAII classes like STL containers or smart pointers, that automatically copy and destroy objects when necessary. Then, the compiler-generated copy constructor, copy assignment operator and destructor do the right thing.

The need to define the Big Three is often a hint that you don't abstract your design enough from low-level features (such as new/delete). If you look at Thor, most of the classes don't implement the Big Three, since the compiler-generated functions work correctly. Classes without meaningful copy semantics are made noncopyable (copy constructor and assignment operator are private), I achieve this by deriving from a NonCopyable base class.


This is exactly what I did in the last few hours. I changed the raw pointers to std::tr1::shared_ptr (it suits my needs), and where it was needed I derived my classes from a NonCopyable class, like you said.
Well, reading the documents about smart pointers took more time than modify my code, to be honest :)
Again, thanks for the advice :)

Pages: 1 [2] 3
anything