Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 18, 2024, 09:32:00 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsA game from scratch in C++ using SFML
Pages: 1 [2] 3
Print
Author Topic: A game from scratch in C++ using SFML  (Read 32409 times)
megamjau
Level 0
*


View Profile
« Reply #20 on: September 14, 2011, 01:17:25 PM »

Just finished going through part 5.. There seems to be a mixup with the GameObjectManager.cpp source code box as it is showing the VisibleGameObject.cpp source code. But the link to the GameObjectManager.cpp works.

I also get an error from the Remove function in the GameObjectManager when I try to compile.

GameObjectManager.cpp: In member function 'void GameObjectManager::Remove(std::string)':
GameObjectManager.cpp:24: error: no matching function for call to 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VisibleGameObject*, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VisibleGameObject*> > >::erase(std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VisibleGameObject*> >&)'
/usr/include/c++/4.2.1/bits/stl_map.h:453: note: candidates are: void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = VisibleGameObject*, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VisibleGameObject*> >]
/usr/include/c++/4.2.1/bits/stl_map.h:468: note:                 typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::size_type std::map<_Key, _Tp, _Compare, _Alloc>::erase(const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = VisibleGameObject*, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VisibleGameObject*> >]
/usr/include/c++/4.2.1/bits/stl_map.h:483: note:                 void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = VisibleGameObject*, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, VisibleGameObject*> >]


It might just be a missing import or a different implementation of map or something, since I'm not using visual studio and "StdAfx".
Logged
Serapth
Level 2
**


View Profile
« Reply #21 on: September 14, 2011, 01:56:55 PM »

Oops...  downside to last minute edits.  The right code is now there, thanks for the heads up.

What platform/compiler are you using?  It doesn't appear to be an include issue though ( although you did point out I forgot to add <map> to stdafx... oops again ), it found std::map just fine, it seems to be indicating that std::map does not have an erase() method that takes a single parameter.  That said, stl error messages have never been fun to parse.


Could you try one thing for me?  Change Remove from:

void GameObjectManager::Remove(std::string name)
{
   std::map<std::string, VisibleGameObject*>::const_iterator results = _gameObjects.find(name);
   if(results != _gameObjects.end() )
   {
      delete results->second;
      _gameObjects.erase(results);
   }
}

to

void GameObjectManager::Remove(std::string name)
{
   std::map<std::string, VisibleGameObject*>::iterator results = _gameObjects.find(name);
   if(results != _gameObjects.end() )
   {
      delete results->second;
      _gameObjects.erase(results);
   }
}


and tell me if that resolves your problem?
« Last Edit: September 14, 2011, 02:17:42 PM by Serapth » Logged
megamjau
Level 0
*


View Profile
« Reply #22 on: September 14, 2011, 02:23:47 PM »

Changed const_iterator to iterator as per your suggestion and it works like a charm. Smiley

I'm using gcc on mac os x.
Logged
Serapth
Level 2
**


View Profile
« Reply #23 on: September 14, 2011, 02:27:38 PM »

Alright, that makes complete sense actually and was an error on my part.  It should have been an iterator ( not a const_iterator ) all along, cut and paste coding bit me in the butt on this one.

What I am extremely confused about is why Visual Studio happily compiled and allowed me to pass a const_iterator to std::map.erase() in the first place?  I would think that would not work on any platform.

I am guessing I should code along in parallel on my Mac to catch these kinds of things.  Anyways, I have updated the source files to use a more cross platform friendly iterator. Smiley  Also snuck in a stealth edit to actually include <map> this time...   Who, Me?
« Last Edit: September 14, 2011, 02:35:45 PM by Serapth » Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #24 on: September 14, 2011, 02:40:03 PM »

What I am extremely confused about is why Visual Studio happily compiled and allowed me to pass a const_iterator to std::map.erase() in the first place?  I would think that would not work on any platform.

In my experience Visual C++ lets you get away with a lot of things you aren't supposed to be able to do.  GCC is a much more strict compiler.

Having ported relatively large VC++ projects to other platforms, this is a major pain in the ass.
Logged



What would John Carmack do?
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #25 on: September 15, 2011, 01:02:58 AM »

That is certainly true. Some but not all problems can be avoided by going to the project settings and turn off "Microsoft Language Extensions", which defaults to on, iirc.
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Serapth
Level 2
**


View Profile
« Reply #26 on: September 15, 2011, 09:51:20 AM »

So, if I just finished recompiling and getting everything to work with XCode, and the fight was more with getting XCode working but there were a couple code changes required. First off, gcc does not allow you to specify a nested enums type, which I view as a shame as it is less readable. It also failed on _tchar which is included just for Microsofts _tmain.

Starting with Pang5 I will make sure all releases work on Mac and Windows, even though the tutorial is specifically about Visual C+ +, Express. Simply put writing non portable code would just be silly.

Oh and this is my first experience with XCode 4.2 but frankly it seems... horrible. Nothing is intuitive, it's slow, but debugging is cumbersome and every time I google anything it seems to be aa rant about how broken things are since 3.x. Being new to XCode I am in no position to judge but it seems like a dud.
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #27 on: September 15, 2011, 10:12:30 AM »

First off, gcc does not allow you to specify a nested enums type, which I view as a shame as it is less readable.

I do nested enums in GCC all the time.  Post the code, you must be doing something wrong.
Logged



What would John Carmack do?
Serapth
Level 2
**


View Profile
« Reply #28 on: September 15, 2011, 10:28:13 AM »

First off, gcc does not allow you to specify a nested enums type, which I view as a shame as it is less readable.

I do nested enums in GCC all the time.  Post the code, you must be doing something wrong.

No, I you misunderstand. My punctuation wasn't clear, it should have read "specify a nested enum's type".

For example:

class example
{
public:
enum Colours { Red, Blue} ;
} ;


Colours red = example::Colours::Red

« Last Edit: September 15, 2011, 11:54:15 AM by Serapth » Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #29 on: September 15, 2011, 10:36:47 AM »

First off, gcc does not allow you to specify a nested enums type, which I view as a shame as it is less readable.

I do nested enums in GCC all the time.  Post the code, you must be doing something wrong.

No, I you misunderstand. My punctuation wasn't clear, it should have read "specify a nested enums type".

For example:

class example
{
public:
enum Colours { Red, Blue} ;
} ;


Colours red = example::Colours::Red



That's actually a new feature in C++2011.  GCC has it in version 4,4 and higher, but I don't think Apple ever went past GCC 4.2.  The MS compiler must have already implemented it and enabled it by default.  In C++98, that is indeed illegal.
Logged



What would John Carmack do?
Serapth
Level 2
**


View Profile
« Reply #30 on: September 24, 2011, 03:46:59 PM »

Sorry I took way too long with this update ( my bad ).


Anyways Part 6 is now live.

In this section, we covered up player keyboard input and got the player's paddle moving, as well as added the game ball.  Of course we covered a couple more C++ concepts.


Again, if you encounter any problems or have any questions, let me know!  All your comments and questions I believe make the tutorial better and I very much appreciate them.  Plus, I don't want to disseminate wrong information or make people more confused then when they started!


We aren't quite to full game status, but we are getting close.  Pang! to this point:

Logged
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #31 on: September 25, 2011, 12:47:45 PM »

Hard to learn much from it, but you helped me anyway  Evil

Why is it hard?

Because you use enums, define static variables. It's just really hard to remember what each one does later, if you haven't made it yourself  Tiger. But thanks!
Logged

Serapth
Level 2
**


View Profile
« Reply #32 on: October 27, 2011, 08:00:08 AM »

Ok, I admit I took a really really really long time getting the next update out, but on the brightside, it is out! Wink

In this chapter we get the ball moving, literally, and add collision detection and ricochets to the game.  On the C++ front, it introduces and explains the various types of type casts available in C++.  As always, the chapter ends with the full project sources.


Click here for part 7.

If you haven't started the tutorial yet, start here at the table of contents.



I promise that the next chapter in the ongoing saga of the worlds most over-engineered Pong clone will be much quicker in coming to be!


Pang is it stands today:
Logged
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #33 on: October 28, 2011, 11:13:04 AM »

Quote
and explains the various types of type casts available in C++.

We have a great book in Poland called "C++'s symfony standard" or however to translate it! But it explains these and i wouldn't be capable to learn casts from internet.

I don't recommend learning from the internet at general (The basics).

Just to ensure, did you mean, for example:
Code:
static_cast<double>(myinteger)
reinterpreter_cast<double>(myinteger)
I forgot the one that will just do what you want without thinking of what is he doing  Lips Sealed.
Logged

Serapth
Level 2
**


View Profile
« Reply #34 on: October 28, 2011, 11:44:12 AM »

Quote
and explains the various types of type casts available in C++.

We have a great book in Poland called "C++'s symfony standard" or however to translate it! But it explains these and i wouldn't be capable to learn casts from internet.

I don't recommend learning from the internet at general (The basics).

Just to ensure, did you mean, for example:
Code:
static_cast<double>(myinteger)
reinterpreter_cast<double>(myinteger)
I forgot the one that will just do what you want without thinking of what is he doing  Lips Sealed.


Essentially yes.

There are four styles of casts in C++

Object myObject = (Object)someonesObject; // Standard C++ style
Object myObject = static_cast<Object> someStaticVar;
Object myObject = dynamic_cast<Object> someSimilarObject;
Banana myBanana = reinterpret_cast<Banana>someApple;

It runs through which to use, when, although truth of the matter is, you should really only be using static and dynamic casts 99.999% of casts; although true is, I still use C style casts all the time, just lazy like that.
Logged
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #35 on: October 28, 2011, 12:40:30 PM »

Quote
Object myObject = (Object)someonesObject; // Standard C++ style

From what i know this isn't Standard C++ style, but old C style, which isn't recommended anymore  Durr...?
Logged

Serapth
Level 2
**


View Profile
« Reply #36 on: November 14, 2011, 01:01:23 PM »

Chapter 8 is now live.


This chapter is... odd.

First off, almost all of the code is awful.  Second, anything implementation specific you learn is more or less terrible.

Got your interest?

This chapter covers implementing an underlying audio system to play sounds and music in Pang!  That said, it actually uses this premise to go off on a tangent on design patterns, most specifically on the Service Provider pattern.

To be honest, this was my favorite chapter to write, and I think it will potentially the most useful to people who are fairly new to OO programming.  When completed, you will have learned a clean mechanism for handling globally available services that are easy to maintain and extend.  In fact, at the end, you will be able to completely substitute the audio subsystem between SFML and FMod using a single line of code.

So if you ever wondered how games could provide multiple renderers, or a clean way to create loosely coupled classes that need to be available globally ( like graphics, audio, logging, etc... ), check this chapter out.


Please let me know if you like the format of this post, or if you would prefer I stay a bit more... focused in future chapters.

Coincidentally, the next chapter is almost complete and it covers how to "actually" implement SFML audio correctly.
Logged
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #37 on: November 14, 2011, 01:52:26 PM »

Quote
and explains the various types of type casts available in C++.

We have a great book in Poland called "C++'s symfony standard" or however to translate it! But it explains these and i wouldn't be capable to learn casts from internet.

I don't recommend learning from the internet at general (The basics).

Just to ensure, did you mean, for example:
Code:
static_cast<double>(myinteger)
reinterpreter_cast<double>(myinteger)
I forgot the one that will just do what you want without thinking of what is he doing  Lips Sealed.


Essentially yes.

There are four styles of casts in C++

Object myObject = (Object)someonesObject; // Standard C++ style
Object myObject = static_cast<Object> someStaticVar;
Object myObject = dynamic_cast<Object> someSimilarObject;
Banana myBanana = reinterpret_cast<Banana>someApple;

It runs through which to use, when, although truth of the matter is, you should really only be using static and dynamic casts 99.999% of casts; although true is, I still use C style casts all the time, just lazy like that.

You forgot const_cast.
Logged



What would John Carmack do?
Serapth
Level 2
**


View Profile
« Reply #38 on: November 14, 2011, 01:57:50 PM »

True, I didnt in the tutorial though. Smiley
Logged
Hima
Level 4
****


OM NOM NOM


View Profile WWW
« Reply #39 on: November 20, 2011, 10:46:01 PM »

Thank you for writing this tutorial! I'm following your tutorial using SFML2.0 and XCode 4.2 at the moment. Will let you know how things go  Coffee
Logged

Pages: 1 [2] 3
Print
Jump to:  

Theme orange-lt created by panic