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

Pages: [1]
1
General / Re: General question about references, pointers, and unique_ptr
« on: December 21, 2013, 10:33:50 pm »
Hi Nexus!

Thank you for the detailed answer. I understood a few things, but not everything I guess.

In your POV, I should always use plain objects, when I can't then unique_ptr, and if I still can't then pointers. References only for methods parameters.

I think I really need to be pointed to a tutorial that shows the mechanics, because it's still confusing.

Say I have these typical (??) game engine classes:
- A Game instance, that manages a bit of everything, the heart of the game in some way
- A few State instances, that represent the different game states (splash screen, menu, in-game, ...)

I'm still unsure how to define the member variables (or methods parameters), regarding RenderWindow, AssetManager, etc.

1) Plain objects, public visibility, in the Game instance, and a static instance() accessor so that we can retrieve the game easily?

2) Do as I were once told: never use public members. Therefore, RenderWindow and AssetManager instances are protected and will be passed by reference to the StateManager, so that it can also pass them by reference to the States

3) Any other solution...

For now, I went with solution 1) in my code, because I fought so much with solution 2) earlier, that I decided to go with the easiest possible solution...

Is there a preferred way to do it? But then, when do the unique_ptr come into play? (I only use them in the AssetManager, thanks to the SFML book advices)

I'm sorry for my question. But C++ is really a pain in my back! :) I'm used to Java, Javascript, and PHP, which are way easier to use... No question to ask yourself: in Java everything is a pointer. Piece of cake :) In PHP objects are passed by reference and primitive types variables are passed by value. Piece of cake :)

I really need to dive into a (recent) game engine tutorial. I found the one of GQE, which is interesting but incomplete.

Best regards,

2
General / General question about references, pointers, and unique_ptr
« on: December 21, 2013, 11:37:15 am »
Hi there!

I bought the SFML book and started to read it. It is very interesting! It also quickly speaks of the unique_ptr feature of C++ 11, and advises to use it for asset management (which I did).

But then it came to me that when I started to study C++ , it was like 10 years ago, and at the time I was advised to use pointers and to not forget to delete them when not needed anymore.

Then I stopped using C++ after only a few months after learning it, to go web (PHP et al)

But today, I'm getting back to C++ with a friend, and I saw many people telling "use pointers only if you do arithmetics, and use references everywhere else".

What is unclear to me is how everything should be stocked (as member variables) in a game engine:
- plain objects (I think only in the object that creates them, like Game for WindowRenderer, AssetManager, ...)
- references (need to be set exclusively in the ctor, unless I'm mistaken?)
- pointers
- or even std::unique_ptr like it is advised for asset management in the book

The book doesn't talk about that, maybe because it is not the main topic.
Does anyone know of a game engine tutorial that emphasis on this particular problem? I would really like to use "best practices" and do things the right way. And honestly, references and pointers (and not unique_ptr) confuse me a lot for the moment!

Thank you :)

3
Graphics / Re: Sprite not displaying when using it in another class
« on: December 21, 2013, 11:26:21 am »
Ok, I was so tired that I changed really everything, and now it works.

I think I was doing something wrong, like using statics, and also instead of using a reference, I copied the reference I was given (not sure if I'm clear). Adding "&" to the variable type helped. I also had this error with my new way of doing things but I could spot it and fix it.

I will post a new topic regarding this matter (using references, or pointers, or std::unique_ptr) :)

Thank you!

4
Graphics / Re: Sprite not displaying when using it in another class
« on: December 20, 2013, 05:56:52 pm »
Hi! Thanks for your answer.

Yes, the sprite is a pointer. It is created here:
Code: [Select]
sf::Sprite* AssetManager::factorySprite(std::string texName)
{
return new sf::Sprite(*AssetManager::loadTexture("images/" + texName));
}

Therefore, I get a pointer to a sprite object. When I dereference this pointer in the SplashScreenState::draw() function, it does not display anything.

But what is strange, is that if I dereference it in the main game loop function in the exact same way, it works fine.

Code: [Select]
// In SplashScreenState::draw()
m_game->m_window.draw(*m_splashImg); // Doesn't work

// In main game loop
sf::Sprite* sprite = currentState->m_splashImg;
m_window.draw(*sprite); // Works nice

It seems I can't pass a pointer to the WindowRenderer::draw() function.

I read a SFML tutorial (written for a game engine named GQE), and it uses references. But I can't use references member variables in a singleton class.

What should I do to convert my pointer to something that works? Although it still works in the main game loop using a pointer...

I also read in the forum a post by the Thor developer that says that SFML has some known issues with static variables or something. Is it the case here? Or is it a C++ error on my side?

Thank you in advance!

PS : I just bought the SFML book! I'll look into it, but if it is a C++ error that I made, I don't think it'll help

5
Graphics / [FIXED] Sprite not displaying when using it in another class
« on: December 20, 2013, 03:59:06 pm »
Hi there!

I'm new to SFML, and I experience my first troubles...

I try to do things well: I created a StateManager, and a simple SplashScreenState. I also have an AssetManager that is a factory and creates me sf::Sprites (or other things) when I need one. It takes care of the textures and memory management.

But I have a problem...

My SplashScreenState creates a simple Sprite with an image inside. Then my main loop calls "currentState->draw()"... But nothing shows on the screen! However, if I get a pointer to that sprite, and call "renderWindow.draw()" directly from my main loop, it works like a charm.

What on earth is happening? I tried to debug references and pointers, but they are all valid and identical. AssetManager works well, because I can display the sprite properly in one case, therefore the sprite is ok.

I don't understand what is going on...

Code: [Select]
// Main loop :
void Game::loop()
{
JLE::StateManager stateManager = JLE::StateManager(*this);
stateManager.push(SplashScreenState::instance());

sf::Sprite* sprite;

while (m_window.isOpen())
{
JLE::AState* currentState = stateManager.current();
if (!currentState)
break;

sprite = currentState->m_splashImg; // I added splashImg in AState for debug purposes
std::cout << sprite << " / " << this << " / " << &this->m_window << std::endl;

m_window.clear();

// I switch between these two options to test...
//currentState->draw(); // This one does not work
m_window.draw(*sprite); // This one works

m_window.display();
}
}

// Init splash screen
void SplashScreenState::init(JLE::AGame& game)
{
m_game = &game;
m_active = true;
m_splashImg = JLE::AssetManager::factorySprite("splash.jpg");
LOG_S() << "splash : " << m_splashImg << std::endl;
}

// Draw splash screen
void SplashScreenState::draw()
{
LOG_S() << "draw..." << m_splashImg << " / " << m_game << " / " << &m_game->m_window << std::endl;
m_game->m_window.draw(*m_splashImg);
}


Please, help me!  :-\

PS : also, do not hesitate to tell me if I do things that are not best practices. I try to do my best, but I haven't touched C++ for like 10 years :) and I was just some beginner at the time

Thanks in advance!

Pages: [1]
anything