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

Pages: [1]
1
General / Re: Problems with void pointer and sf::Transformable
« on: May 29, 2017, 09:04:11 pm »
Not only a pracmatic, but the correct view.
Better Google usage would have eased my search: https://stackoverflow.com/questions/2379427/multiple-inheritance-unexpected-result-after-cast-from-void-to-2nd-base-class

Thanks and sorry for my bad research.
I guess I have to scrap the void pointer and come up with a better (typesafe) storage.
Most likely the member variable approach.

Always knew, I couldn't trust thoose darn void pointers.
Seemed too easy...

Sidenote: I really like the multi inheritance idea (no really I think it's rather clever), but also curse it in many situations.

2
General / Re: Problems with void pointer and sf::Transformable
« on: May 29, 2017, 08:49:22 pm »
Thanks for the answer.
Sadly dynamic_cast only works for polymorphic types.

I wanted to prodcue a minimal example, without the memory overhead.
Also I would love to use unique_pointers, but the multi-inheritance of sf::Sprite makes this really hard.
I'm still using shared_ptr and templates.

I use the void pointer for storage only and I hide them behind an interface.
I do this, because I don't want to introduce an empty clas for the component.


I'm not sure if I understood you correctly, but the right function should be called:
Code: [Select]
#include <iostream>

class Base {
public:
virtual void foo() {
std::cout << "Foo base" << std::endl;
}
};

class Derived : Base {
public:
void foo() {
std::cout << "Foo derived" << std::endl;
}
};


int main(){


auto ptr = new Derived();
void* vp = ptr;
static_cast<Base*>(vp)->foo();

delete ptr;

std::getchar(); // Windows user

return 0;
}

Will output: "Foo derived"

3
General / Problems with void pointer and sf::Transformable [Solved]
« on: May 29, 2017, 08:27:18 pm »
Hello,

I'm programming an ECS in C++ and wanted to integrate SFML into it.

I save my components invoid pointer and save the corrisponding class information with it.
When I cast the void pointer backt to sf::Transformable, my calls will be mostly ignored(?) or simpy have no impact on the object.

I have created an example outside my ECS to illustrate the issue:
Code: [Select]
#include <SFML/Graphics.hpp>

int main() {
{
sf::CircleShape* cs = new sf::CircleShape(50);
cs->setFillColor(sf::Color::Green);

void* t = cs;
void* d = cs;


sf::RenderWindow window(sf::VideoMode(640, 640), "SFML Test");

// game loop
while (window.isOpen()) {
// events
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;

default:
break;
}
}
static_cast<sf::Transformable*>(t)->move(0.1, 0.1);

window.clear();
window.draw(*static_cast<sf::Drawable*>(d));
window.display();

}
delete cs;
}
return 0;
}

When this code is executed, the circle will only move once.

When I replace the void* t = cs; with sf::Transformable* t = cs;, everything works just fine and as expected (The circle moves offscreen.).
I'm curious if I'm missing something specific inside SFML, because the same approach works with other classes and examples.
So why won't the circle move in each iteration?

Normally I would ask this in a Cpp only forum, but I can't replicate the problem in other libraries or construct a similar example.

I'm using Windows 10 and Visual Studio 2015.

Pages: [1]
anything