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

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

0 Members and 3 Guests are viewing this topic.

Sanction

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #225 on: January 15, 2018, 03:31:47 am »
I'm having some problems with Chapter 4 covering the Event Manager.

At the end you should be able to move a simple texture around with Mouse Left Click and also the LShift key.

However when I load the keys.cfg I found a weird problem.
(click to show/hide)


I do have my code on github:https://github.com/SanctionMan/SFML_Project/
I have went back though my code again and copy and pasted the code from the example code and I still have the same problem. I've also walked thought this form post hoping to find the same problem. Maybe I'm missing something here.

I am using SFML 2.4.2 ATM and also have changed the following that you showed in a post before here.
if (bind->m_details.m_keyCode == -1) {
// changed != to ==
 

I'v created a function that prints out all the bindings and events in each binding and its reading them all.
(click to show/hide)



Knowledge comes, but wisdom lingers. It may not be difficult to store up in the mind a vast quantity of facts within a comparatively short time, but the ability to form judgments requires the severe discipline of hard work and the tempering heat of experience and maturity.

yreaction

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #226 on: February 09, 2018, 09:25:53 am »
Mac usere here aswell, figured out the first issues but the endline for keys.cfg saved me a lot of headcheds. Thanks mate.

theo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #227 on: March 02, 2018, 04:03:57 pm »
First of all: I like the book a lot. Thanks for it.

At the moment I'm going through the event manager chapter. I have one question and one remark.

The question:
You store the Binding objects in an unordered_map keyed on name. The only reason for this seems to be to enforce only one Binding per callback, since you never access the bindings map on name. Why is that important? I would drop that restriction, since there are reasons to want multiple bindings to the same callback. For example, if you want to bind <ctrl>-c to some event, you need two bindings: one for left control and one for right control. Differentiating between left and right shift, alt and control seems artificial to me. The user shouldn't be bothered with the technical fact that left and right key have different keycodes. So I would use a std::vector<Binding*> (or better still: std::vector<std::unique_ptr<Binding>> and even silently include double bindings when the user wants ctrl/alt/shift combinations.

The remark:
You do a great job of abstracting the event side of the event manager, but the callbacks are just wrapped function pointers. Why not put a Command pattern on the callback side? See http://gameprogrammingpatterns.com/command.html for a nice explanation. Of course that adds some code and complexity, but it enables some cool features:
1- if the AI also puts Command objects on the queue, enemies (and a demo mode, if applicable) can use the exact same control mechanism as the user-controlled character.
2- you can control all kinds of actors. So the player character is an actor, but the Window class can also be an actor (to handle focus gained/lost, toggle full screen). You could make anything that receives input an actor, using the same mechanism without messy exceptions to the rule.
3- it makes undo easy. In certain situations (think a strategy game where the player has to plan actions for many units before committing the turn) this could be a really nice touch.
Abstracting the callback side as well as the event side makes the event manager even more versatile.

And a minor point: you have many functions return a bool to indicate succes. It seems cleaner and more natural to me to use exceptions for that kind of behaviour. But that could just be me, I do java for a living.

theo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #228 on: March 08, 2018, 11:22:23 am »
I got to the statemanager chapter. A few constructive remarks:

1- You tend to pass very small data-items bij const ref: e.g. void f (const bool&). I don't see what's the merit in that. A bool or an int is probably even smaller than a pointer. Just pass these things by value and use const refs where a copy is (potentially) expensive.
2- You have a tendency of declaring empty destructors. Probably power of habit, but in C++11 this prevents the generation of move constructor and assignment. This will of course not be a problem with classes like eventmanager and statemanager because they will not be moved around anyway, but this is not a good habit.
3- In the draw and update functions of the statemanager, you traverse back and forth to find the last non-transparent state. In the past I used to be a C programmer, so traversing pointer structures is nothing new for me, but the C++ Committee gave us the algorithms library to be used. This code can be rewritten to be a lot more obvious:


#include <algorithm>

using State_elem = std::pair<StateType, BaseState*>>;  // convenience

void StateManager::Draw()
{
  if (m_states.empty() {return;}
  if (m_states.back().second->IsTransparent() && m_states.size() > 1)
  {
    // find last non-transparent state. find_if normally finds the first element that satisfies its predicate,
    // but if we feed it reverse iterators, it searches backwards.
    // I use find_if_not to avoid confusing negative logic.
    auto r_itr = std::find_if_not (m_states.rbegin(), m_states.rend(), [](State_elem e) {return e.second->isTransparent();});
    // we gave find_if_not reverse iterators, it also gives us a reverse iterator. So we have to trick it:
    for (; r_itr != m_states.rbegin(); --r_itr)
    {
      r_itr -> second -> Draw();
    }
  }
  else
  {
    m_states.back().second->Draw();
  }
}



IMHO this code is self-explanatory, except for the little trick with the reverse iterators.

It might be possible to just say

auto r_itr = std::find_if_not (m_states.rbegin(), m_states.rend(), [](auto e) {return e.second -> isTransparent();});


and let the compiler figure out the type of the argument of the predicate.
The algorithm library works brilliantly with lambdas. Every new C++ programmer should learn about that. This also goes for the HasState function: it can be simplified a lot by using find_if. Use find_if to find the state in m_states and use find to find it in m_toRemove. If the state is found in m_states and not found in m_toRemove, the result is true. Otherwise the result is false.


bool StateManager::HasState(const StateType type)  // type is an int, just pass by value
{
  auto state = std::find_if (m_states.begin(), m_states.end(), [type](State_elem e) {return e.first == type;});
  auto remove = std::find(m_toRemove.begin(), m_toRemove.end(), type);
  if (state != m_states.end() && remove == m_toRemove.end())  // found in m_states, not found in m_toRemove
  {
    return true;
  }
  else
  {
    return false;
  }
}


4- The draw and update functions are almost identical. Duplicate code is Not Very Nice. The only differences are the functions calles (IsTransparent vs IsTranscendent, Draw vs Update). It should be possible to make a private helper function that does the searching and that accepts the functions that are used. I haven't worked this out yet. Probably it isn't worth the trouble in this simple case.

Stormik

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #229 on: April 17, 2018, 11:31:46 am »
Hello,
first of all congratulations for the book, that is really well written.
Reading through chapter 5 I have encountered a problem, while the code works I cannot go past the intro state because the program doesn't recognize the input, and this problem is encountered both in my code and in the sample code and I don't understand why since the code in chapter 4 worked.
I'm using SFML 2.4.2.

Best regards

Michele

OrderNexus

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #230 on: April 19, 2018, 03:24:53 am »
Hello,
first of all congratulations for the book, that is really well written.
Reading through chapter 5 I have encountered a problem, while the code works I cannot go past the intro state because the program doesn't recognize the input, and this problem is encountered both in my code and in the sample code and I don't understand why since the code in chapter 4 worked.
I'm using SFML 2.4.2.

Best regards

Michele
I've sent you a PM back. I'll keep this thread updated on the issue and solution once more specifics come into play.

Dazedi

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #231 on: April 24, 2018, 02:46:20 am »
Hello, I'm really enjoying the book. It's been such a long time since I've used C++ so this has worked nicely as a reminder as well!
Anyways, I got up to chapter 7 until I encountered a problem I just haven't been able to solve. Any help would be appreciated!

The problem is that when the map file is being read and the entityManager's Add is being called with EntityType::Player, it throws an exception at the first find in the function.
Exception is "Exception thrown: read access violation. **this** was 0x50"
I did a lot of googling and even tried directly copying some of the code from the provided files with no success.

Thanks! I look forward to being able to proceed with this project.

kaim3lis

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #232 on: March 28, 2019, 03:16:58 pm »
Hello,
first of all congratulations for the book, that is really well written.
Reading through chapter 5 I have encountered a problem, while the code works I cannot go past the intro state because the program doesn't recognize the input, and this problem is encountered both in my code and in the sample code and I don't understand why since the code in chapter 4 worked.
I'm using SFML 2.4.2.

Best regards

Michele

Hi guys. I also have the same problem. I hope someone could tell me how to fix it.

OrderNexus

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #233 on: April 14, 2019, 01:29:54 am »
It sounds like that's a fairly common issue. Between different SFML version releases, the enums being used end up changing in value. The default keys.cfg file that comes with the projects uses "old" values. You'll have to look up the values you need in the enumeration and use those in your file instead.

Another common issue people have with loading files in general is the extensions. If you have your extensions hidden, even if you save a file as something like "keys.cfg" from your notepad, it will save as keys.cfg.txt, and since you have file extensions hidden, you won't see the difference. I'd suggest enabling the file extension visibility or using a more robust piece of software that doesn't assume a .txt file type.

Hello, I'm really enjoying the book. It's been such a long time since I've used C++ so this has worked nicely as a reminder as well!
Anyways, I got up to chapter 7 until I encountered a problem I just haven't been able to solve. Any help would be appreciated!

The problem is that when the map file is being read and the entityManager's Add is being called with EntityType::Player, it throws an exception at the first find in the function.
Exception is "Exception thrown: read access violation. **this** was 0x50"
I did a lot of googling and even tried directly copying some of the code from the provided files with no success.

Thanks! I look forward to being able to proceed with this project.

Is this happening with the unaltered code that was included? Can you include a snippet of the code that throws this error?

mequint

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #234 on: May 24, 2019, 05:39:49 am »
Hey OrderNexus,

Finally finishing up the Networking Portion of the book and I've run into a snag with the Chapter 14 code.  When I compile the code for the Client, I get the following errors everywhere an instance of the Shared Context is referenced.

Output (VS2017)
1>c:\src\sfml-2.4.2\include\sfml\network\sockethandle.hpp(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\src\sfml-2.4.2\include\sfml\network\sockethandle.hpp(46): error C2146: syntax error: missing ';' before identifier 'SocketHandle'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(140): error C3646: 'getHandle': unknown override specifier
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(140): error C2059: syntax error: '('
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(140): error C2238: unexpected token(s) preceding ';'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(159): error C2061: syntax error: identifier 'SocketHandle'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(159): error C2535: 'void sf::Socket::create(void)': member function already defined or declared
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(148): note: see declaration of 'sf::Socket::create'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(177): error C3646: 'm_socket': unknown override specifier
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(177): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

The culprit seems to be in the SocketHandle.hpp (SFML/Networking) with the following line of code:

#if defined(SFML_SYSTEM_WINDOWS)

    typedef UINT_PTR SocketHandle;

#else
 

I've determined that the problem is coming from the Client somewhere, and yet when I run the same code in Chapter 13, it works as intended.  Odd.

I was hoping to finish this chapter before moving onto your next book (Mastering SFML).

Q1: Any ideas how I can fix this?  Are there any changes you are aware of in SFML since 2.4 in Networking?
Q2: Is the Networking module for SFML needed in Mastering SFML?

I'd like to get the code working as intended

Thank you!

mequint

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #235 on: December 01, 2019, 01:30:53 am »
This is still a problem...it doesn't have anything to do with the Shared Context.  Something is wrong with the S_Network...the strange thing is that the Chapter 13 project works fine.

Furthermore, I'm seeing some other problems...there seems to be another error with Rect.inl in the API and this project.  Fortunately, it isn't necessary for the next book but it would be nice to get it implemented successfully.

OrderNexus

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #236 on: December 01, 2019, 06:46:52 am »
Hey OrderNexus,

Finally finishing up the Networking Portion of the book and I've run into a snag with the Chapter 14 code.  When I compile the code for the Client, I get the following errors everywhere an instance of the Shared Context is referenced.

Output (VS2017)
1>c:\src\sfml-2.4.2\include\sfml\network\sockethandle.hpp(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\src\sfml-2.4.2\include\sfml\network\sockethandle.hpp(46): error C2146: syntax error: missing ';' before identifier 'SocketHandle'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(140): error C3646: 'getHandle': unknown override specifier
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(140): error C2059: syntax error: '('
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(140): error C2238: unexpected token(s) preceding ';'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(159): error C2061: syntax error: identifier 'SocketHandle'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(159): error C2535: 'void sf::Socket::create(void)': member function already defined or declared
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(148): note: see declaration of 'sf::Socket::create'
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(177): error C3646: 'm_socket': unknown override specifier
1>c:\src\sfml-2.4.2\include\sfml\network\socket.hpp(177): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

The culprit seems to be in the SocketHandle.hpp (SFML/Networking) with the following line of code:

#if defined(SFML_SYSTEM_WINDOWS)

    typedef UINT_PTR SocketHandle;

#else
 

I've determined that the problem is coming from the Client somewhere, and yet when I run the same code in Chapter 13, it works as intended.  Odd.

I was hoping to finish this chapter before moving onto your next book (Mastering SFML).

Q1: Any ideas how I can fix this?  Are there any changes you are aware of in SFML since 2.4 in Networking?
Q2: Is the Networking module for SFML needed in Mastering SFML?

I'd like to get the code working as intended

Thank you!

Q2: networking isn't covered at all in the "Mastering" title.


This is still a problem...it doesn't have anything to do with the Shared Context.  Something is wrong with the S_Network...the strange thing is that the Chapter 13 project works fine.

Furthermore, I'm seeing some other problems...there seems to be another error with Rect.inl in the API and this project.  Fortunately, it isn't necessary for the next book but it would be nice to get it implemented successfully.

If I had to guess at the moment (I don't have the source code available), I'd say this is probably some kind of include order issue. I vaguely recall running into something like this in conjunction with some windows-specific includes, specifically in "Utilities.h". I might be completely wrong, but I can't verify this at the moment. Does anyone else have memory of / experience with this type of issue?

Grundkurs

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #237 on: October 27, 2020, 11:47:21 pm »
Hi OrderNexus,
i may have discovered a subtle bug in the EventManager-Class.

EventManager.h
struct EventInfo{
        EventInfo(){ m_code = 0; }
        EventInfo(int l_event){ m_code = l_event; }
        EventInfo(const GUI_Event& l_guiEvent){ m_gui = l_guiEvent; }
        union {
                int m_code;
                GUI_Event m_gui;
        };
};
If no argument is passed to an EventInfo Constructor during Object-Creation, the EventInfo's default constructor is invoked which initializes the union member int m_code to 0. So when creating an EventInfo object with a default constructor you are not allowed to use the other union member (here m_gui), this would lead to undefined behaviour.

EventManager.cpp
void EventManager::LoadBindings(){
    ...
        EventInfo eventInfo;
        if (type == EventType::GUI_Click || type == EventType::GUI_Release ||
            type == EventType::GUI_Hover || type == EventType::GUI_Leave)
        {
               ...
        eventInfo.m_gui.m_interface = w;
        eventInfo.m_gui.m_element = e;
        }
 ....

in EventManager::LoadBindings() an EventInfo-object is created with a default constructor (EventInfo eventInfo). However further down below, if the EventType is some GUI-Type, the eventInfo.m_gui-variable is getting assigned to the data that is read from the config-file (here w & e). I consider this assignmeht as undefined since the created EventInfo-Object can only use the "int"-variable from its union-members. Or am i missing here something?

« Last Edit: October 28, 2020, 07:30:48 am by Grundkurs »

Jack Lam

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: SFML Game Development by Example - 4th SFML book
« Reply #238 on: February 26, 2022, 02:49:05 pm »
Really appreciate your work, thank you!

jamesnozyz

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: SFML Game Development by Example - 4th SFML book
« Reply #239 on: January 08, 2024, 01:11:34 pm »
I'm at the beginning of Chapter 7 and just finished testing the Tile/Mapping classes. The background texture loads, but when I hit the close button or click the "exit" button on the main menu, I receive an exception. I believe the error is occurring because the Game object's deconstructor is called when the window closes, causing my SharedContext object to lose scope.