Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ with SDL, help on how to create a 2D map


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 theseven   Members   -  Reputation: 143

Like
0Likes
Like

Posted 17 August 2013 - 12:10 PM

Hey guys,

 

I'll begin by telling you exactly what I'm doing and then telling what kind of help I need.

 

What I'm doing:

I've been learning C++ and I've been learning how to do a text-based RPG game. It's something simple, you have a storyline to follow, classes, NPC's to talk to, combat system, menu system, equipping items system, etc etc.

 

- I have a fair knowledge of C++;

- I don't know how to, like, do checkpoints through I/O files (i think that's the correct term, forgive me if not).

- I have yet to start learning SDL.

 

What I want help with:

In that game, I want to have a simple 2D map that will help the player to know where he/she is to make the game much less confusing. This map will appear above the text and I want it to change as the player advances in the game, like:

 

|___|

|  o  |

|___|       (Player Location is "o")

 

You wake up in the middle of a forest bla bla bla a path goes North.

1: Go north

 

(player presses 1)

 

|      |

| __ |

|  o  |

|___|

|      |

|___|

 

bla bla bla bla the path continues to the north.

1: Go north

 

 

Get it?

Now I want to do this with SDL to make it easier so i don't have to build the map in C++ everytime I write a new area.

 

How can you help me:

 

I need a good tutorial (text or video, it's indifferent) that explains me how to build this kind of map for this purpose in SDL.

 

Does anyone know such a thing?

Can I learn how to do this by simply learning SDL?

Is there any other (preferrably simpler) way of doing this?

 

Thank you so much in advance.



Sponsor:

#2 AllEightUp   Moderators   -  Reputation: 4274

Like
2Likes
Like

Posted 17 August 2013 - 02:07 PM

"You are likely to be eaten by a grue."  Couldn't resist. :)

 

Anyway, the details will depend on further information.  But in general it doesn't really matter if it is SDL or not, it will be more important to find a method of describing the map in a data description method.  In the most simplistic case you could start with:

 

6 6  # width and height
X X X O X X
O O O O X X
X X O X X X
... etc

 

Now a simple text reader can read in the file, allocate an array of 6*6 elements and then fill them in by reading the lines.  X's would be "solid" areas and the O's would be open areas (rooms).  Obviously this is exceptionally simple and you will want more information in most cases.  So, you may need to think in terms of graphs instead of a rectangular region of simple open/closed areas.  You also probably want to put things "in" the rooms so you want more detail.  You could do something like the following (lua based text style):

 

Rooms =
{
  "Room1" =
  {
    "Connections" = {{"North", "Room2"}, {"South", "RoomXXX"}},
    "Objects" = {{"Chest", GoldChest}}
     .... etc ...
  },
  "Room2" = .. etc ..
}

 

Reading in the more complex style description takes a bit more work of course and perhaps others would know of tutorials and such which may cover this.  In general though, SDL wouldn't help, SDL would be appropriate for the rendering side which is another subject all together.



#3 ViX3LG0N   Members   -  Reputation: 376

Like
2Likes
Like

Posted 17 August 2013 - 03:54 PM

1. The feature you are trying to implement is called "Automapping".

2. SDL is used for opening a window with a context to display stuff on it and handles also the input. You have to think about if you want to use the SDL integrated 2D draw library or if you want to use OpenGL to draw your stuff. I can recommend you used OpenGL as it is easy to begin with, but also has endless level of complexity if you want to. Also there are a lot of tutorials for SDL+OpenGL 2D

3. The next problem would be you need to draw text in an SDL context, with OpenGL it could be hard for you to do text rendering. If you pick the SDL drawing library just use SDL_ttf.

4. I would do automapping with std::vector, so you have "dynamic arrays" and your level can grow

struct Location
{
	int x;
	int y;
};

vector<Location> automap;

void moveTo(int xPos, int yPos)
{	
	//Check if you already exploared this location
	bool alreadyAdded = false;
	for (unsigned int i = 0; i < automap.size; i++)
	{
			if (automap[i].x == xPos)
			{
				if (automap[i].y == yPos)
				{
					alreadyAdded = true;
				}
			}
	}
	if (!alreadyAdded)
	{
		Location tmpPos;
		tmpPos.x = xPos;
		tmpPos.y = yPos;
		automap.push_back(tmpPos);
	}
}

Lets say the location "middle in the forrest" is on position x:0 y:0 and if you start the game you call "moveTo(0,0);"

Your Player moves to the north then call "moveTo(0,1);".

Also its better if you make variables for the player-position and just call "moveTo(player.x,player.y)" every time you move to another location and also change the postion so if you go to the north just do "player.y++;" and call "moveTo(player.x,player.y);".

The only thing you need to do now is draw the actual map(which should be correctly automapped with my theorie of automapping).

But this is your decision on how to do that, if you choose OpenGL, i can help you again.

 

Hope this helps you a bit biggrin.png



#4 theseven   Members   -  Reputation: 143

Like
0Likes
Like

Posted 17 August 2013 - 04:13 PM

@AllEightUp the problem is that I want something drawed like in my example, not just with symbols like x's and o's, if I understood correctly what you were trying to say.

 

@ViX3LG0N that helped a bit, I would like to do it just with the SDL drawing library because I'm trying to learn SDL (and by trying I mean I nearly started) and learning even another "language" would be even more confusing for me. But that example you gave me left me a bit lost there ;D



#5 ryutenchi   Members   -  Reputation: 353

Like
1Likes
Like

Posted 17 August 2013 - 08:59 PM

@theseven I believe @AllEightUp was trying to show you have to how he would "serialize" your data. Serialization is when you turn your in-memory data into file on the HD or SSD or Cloud Drive or whatever. SDL and OpenGL are both librarys and neither deal with data or data structures. I would probably either make a structure  called a graph (the boost library has a good one if you want to use that) and store the contents of the room in a struct/class in the graph and then you can setup your rendering from that.



#6 yckx   Prime Members   -  Reputation: 1291

Like
3Likes
Like

Posted 19 August 2013 - 12:56 PM

Since you're using C++ you may want to consider using SFML rather than SDL. Very similar in concept, but it's a C++ library instead of a C library.



#7 theseven   Members   -  Reputation: 143

Like
0Likes
Like

Posted 19 August 2013 - 05:09 PM

never heard of it, what is it?



#8 yckx   Prime Members   -  Reputation: 1291

Like
2Likes
Like

Posted 19 August 2013 - 05:46 PM

Google is your friend :D

 

http://www.sfml-dev.org



#9 ryutenchi   Members   -  Reputation: 353

Like
1Likes
Like

Posted 22 August 2013 - 02:32 AM

Google is your friend biggrin.png

 

http://www.sfml-dev.org

I use this in my engine as well. It's a nice little framework^^






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS