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

Author Topic: SFML Game Development by Example - 4th SFML book  (Read 179286 times)

0 Members and 1 Guest are viewing this topic.

_Imperial_

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #210 on: May 09, 2017, 04:49:00 am »
void StateManager::update(const sf::Time& time) {
   if (statesContainer.empty()) { return; }
   if (statesContainer.back().second->isTranscendent() && statesContainer.size() > 1) {
      bool reversing = true;
      for (auto statesItr = statesContainer.end() - 1; statesItr != statesContainer.end(); (reversing ? --statesItr : ++statesItr)) {
         if (!statesItr->second->isTranscendent() || statesItr == statesContainer.begin()) {
            reversing = false;
            if (!statesItr->second->isTranscendent()) { continue; }
         }
         if (!reversing) {
            statesItr->second->update(time);
         }

      }
   }
   else { statesContainer.back().second->update(time); }
}

void StateManager::draw() {
   if (statesContainer.empty()) { return; }
   if (statesContainer.back().second->isTransparent() && statesContainer.size() > 1) {
      bool reversing = true;
      for (auto statesItr = statesContainer.end() - 1; statesItr != statesContainer.end(); (reversing ? --statesItr : ++statesItr)) {
         if (!statesItr->second->isTransparent() || statesItr == statesContainer.begin()) {
            reversing = false;
            if (!statesItr->second->isTransparent()) { continue; }
         }
         if (!reversing) {
            locator->window->getRenderWindow()->setView(statesItr->second->getView());
            statesItr->second->draw();
         }

      }
   }
   else {
      locator->window->getRenderWindow()->setView(statesContainer.back().second->getView());
      statesContainer.back().second->draw();
   }
}

Yes OrderNexus, you are right. The previous approach wasn't doing any work inside the first loop. Sorry for the delay to reply. I wasn't able to dive into the engine code for a couple days, too busy. Anyway, after returning to it, I took my time to understand what needed to be accomplished there and I believe I got it right this time. Actually it uses only one for loop now. 8)

OrderNexus

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #211 on: May 09, 2017, 06:00:54 pm »
Nice job! From looking at it quickly, it seems like it should work as intended.

drewbarbs

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #212 on: July 03, 2017, 09:19:44 pm »
Just started going through this book and enjoying it so far. Chapter 3, after introducing the basic code for the snake game, has a section on finding bugs. At the end of this it says:

Quote
There's one more fault with the game that didn't get addressed here on purpose. Try to find it and fix it in order to practise resolving problems like this in the future.

Hint: It has to do with how many segments the snake has when the game starts.

I'm stumped as to what this could be. I thought this could be referring the case where our world is smaller than the space required to accommodate our hardcoded initial position, but that seems overly paranoid. Also, I've added some code to ensure the apple spawn isnt in a position occupied by the snake. I've looked at the finished code online, and fail to see what is fixed there that wasn't described in the text. Anyone know what this is referring to?

Eyfenna

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #213 on: August 20, 2017, 11:23:59 pm »
its my second sfml book and have to say thanks for the many stimula though i started to rebuild the examples with smart pointers i'm able to get nearly everything  to run.

OrderNexus

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #214 on: August 23, 2017, 10:16:28 pm »
its my second sfml book and have to say thanks for the many stimula though i started to rebuild the examples with smart pointers i'm able to get nearly everything  to run.
I'm happy to hear that! Let me know if you have any problems.

Eyfenna

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #215 on: August 25, 2017, 06:52:16 pm »
so far what does not run is the register state template that captures this in the lambda. even enabling shared from this and creating a shared ptr inside the class with self and vapturing self by ref gets me a bad weak ptr error.
the other thing is the utility get working directory macro the char array is not accepted by the winapi function in vs 2015.

Grundkurs

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #216 on: August 27, 2017, 12:50:36 pm »
so far what does not run is the register state template that captures this in the lambda. even enabling shared from this and creating a shared ptr inside the class with self and vapturing self by ref gets me a bad weak ptr error.
since the States stored in the StateContainer and StateFactory are not shared among other entities, i recommend using std::unique_ptr<> instead of std::shared_ptr.
using StateContainer = std::vector<std::pair<StateType, std::unique_ptr<State>>>;
using StateFactory = std::unordered_map<StateType, std::function<std::unique_ptr<State>()>>;
...
template<class T>
void StateManager::registerState(StateType stateType){
        mFactory[stateType] = [this]()->std::unique_ptr<State>{
                return std::make_unique<T>(this);
        };
}

Eyfenna

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #217 on: August 30, 2017, 02:03:27 pm »
so far what does not run is the register state template that captures this in the lambda. even enabling shared from this and creating a shared ptr inside the class with self and vapturing self by ref gets me a bad weak ptr error.
since the States stored in the StateContainer and StateFactory are not shared among other entities, i recommend using std::unique_ptr<> instead of std::shared_ptr.
using StateContainer = std::vector<std::pair<StateType, std::unique_ptr<State>>>;
using StateFactory = std::unordered_map<StateType, std::function<std::unique_ptr<State>()>>;
...
template<class T>
void StateManager::registerState(StateType stateType){
        mFactory[stateType] = [this]()->std::unique_ptr<State>{
                return std::make_unique<T>(this);
        };
}

thanks that helped and header include juggling, it does work properly now.

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #218 on: September 21, 2017, 09:59:47 am »
is free on September 21 2017

go here

https://www.packtpub.com/packt/offers/free-learning

make an account

download free pdf of book

Grundkurs

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #219 on: October 17, 2017, 03:21:13 pm »
i may have found a bug in the code-base of "SFML Game Development by Example". Please correct me if i'm wrong:
AnimBase.h
using Frame = unsigned int;
class AnimBase{
...
    Frame m_frameCurrent;
    Frame m_frameStart;
    Frame m_frameEnd;
    Frame m_frameRow;
};
 
AnimDirectional.cpp
void Anim_Directional::FrameStep(){
    if (m_frameStart < m_frameEnd){ ++m_frameCurrent; }
    else { --m_frameCurrent; }

    if ((m_frameStart < m_frameEnd && m_frameCurrent > m_frameEnd) ||
        (m_frameStart > m_frameEnd && m_frameCurrent < m_frameEnd)){
        if (m_loop){ m_frameCurrent = m_frameStart; return; }
        m_frameCurrent = m_frameEnd;
        Pause();
    }
}
 

In the if-Statement in Anim_Directional::FrameStep() a check is performed if
m_frameCurrent is smaller than m_frameEnd. This is problematic in the case that the Animation runs backwards (like m_frameStart = 4; m_frameEnd = 0), because given m_frameEnd is set to 0, m_frameCurrent will never be smaller than m_frameEnd because both variables are of type unsigned int. So when m_frameCurrent is set to smaller then 0 with "--m_frameCurrent" it will automatically "roll over" to the highest possible unsigned int-value since its unsigned and therefore the if-statement (m_frameCurrent < m_frameEnd) will never trigger.

A workaround by shifting m_frameStart and m_frameEnd one to the right (m_frameStart = 5; m_frameEnd = 1) is not solving the problem because it produces other issues when cropping the sprite from the spritesheet.

Setting "Frame m_frameCurrent" to "int m_frameCurrent" to allow m_frameCurrent to be assigned to negative values evokes other problems because then you are comparing an int (m_frameCurrent) to an unsigned int (m_frameEnd) in the mentioned if-statement, which can lead to further problems (see https://stackoverflow.com/questions/8233161/compare-int-and-unsigned-int/8233184#8233184). In my case the if-statement didnt trigger even though m_frameCurrent was smaller than m_frameEnd (which was set to 0). So i ended up using int instead of "Frame" aka unsigned int in AnimBase.h. That solved all my problems.
   
« Last Edit: October 17, 2017, 03:56:41 pm by Grundkurs »

OrderNexus

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #220 on: November 03, 2017, 07:03:28 am »
First of all, sorry about the late response. I should've tackled this sooner.

You're right, this is something that got overlooked by me during the writing process. Funny thing is, I had that corner case pinned in my notes and I completely "goofed" it when making final alterations before submitting it. My bad on that one.

Setting the type of m_frameCurrent to a regular integer would indeed fix the issue, or you could simply add >= and <= checks in the right half of the lowest if-statement. I haven't tested this yet, but upon first glance after a long time of not looking at this code, it seems like that might be a good way of dealing with the issue as well. Having negative frames was never really intended behavior, so it's important to avoid that as much as possible, since it could produce unexpected bugs later on. The code would then look like this:

void Anim_Directional::FrameStep(){
    ...
    if ((m_frameStart < m_frameEnd && m_frameCurrent >= m_frameEnd) ||
        (m_frameStart > m_frameEnd && m_frameCurrent <= m_frameEnd)){
        if (m_loop){ m_frameCurrent = m_frameStart; return; }
        m_frameCurrent = m_frameEnd;
        Pause();
    }
}
The m_frameCurrent = m_frameEnd; line is still kept, simply to ensure we didn't blow through the intended range somehow. This should allow you to switch back to the 'Frame' typedef for continuity.

BetokHaney

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #221 on: November 30, 2017, 07:47:07 pm »
I just bought the book, and have a question regarding items in Chapter 4:

The keys.cfg file uses key numerical values for key bindings, like:

Fullscreen_toggle 5:89

...where KeyDown is represented by 5, and the F5 key by the number 89.

Where do the key number values come from?  I can't for the life of me find how the various keys on the keyboard are tied to a numerical value in SFML.

Thanks for your help.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #222 on: November 30, 2017, 08:29:56 pm »
I haven't bought or read through the book, but it seems likely that those numbers represent the position of those values within their corresponding enum. KeyPressed is equal to 5 in enum sf::Event::EventType (start counting from 0), and F5 is likely equal to 89 in
enum sf::Keyboard::Key


BetokHaney

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #223 on: December 01, 2017, 05:31:18 pm »
I haven't bought or read through the book, but it seems likely that those numbers represent the position of those values within their corresponding enum. KeyPressed is equal to 5 in enum sf::Event::EventType (start counting from 0), and F5 is likely equal to 89 in
enum sf::Keyboard::Key


This is it!  Thank you so much.

Devo

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #224 on: December 20, 2017, 02:32:57 am »
I've been working through the book with a Mac and enjoying it quite a bit. There are a few roadblocks for Mac users,  and so I thought I'd share the ones I've encountered so far to possibly spare others some pain.

- Make sure to use the ResourcePath helper function that comes with the OS X SFML installation whenever you need to load something from the file system (e.g. graphics, fonts, etc.)

- Earlier in this thread there is a discussion about errors caused by unordered_map and hashing custom objects. This applies to Xcode as well, and you will need to create and use the CustomHash header file as described.

- If you try to read in text files that come with the source code download, your code will throw a runtime error due to a null pointer (this first happens with keys.cfg in the EventManager and StateManager chapters). It took me forever to figure out that this appears to be due to the different end-of-line characters between Windows and OS X. A solution is to recreate the files on the Mac, I just typed them in using nano in a terminal window.

- More to come I'm sure!

Devo