Jump to content
  • Advertisement
Sign in to follow this  
Samsonite

Creating maps in another way than this(SDL)

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I was wondering if there are a better way of creating a pre-defined map(no random map generator) than this method here:
//First row of drawing map
                      DrawSprite(roadN, screen, NULL, NULL, 70, 70, 0, 0, 70, 70);
                      DrawSprite(sign, screen, NULL, NULL, 70, 70, 70, 0, 70, 70);
                      DrawSprite(bush, screen, NULL, NULL, 70, 70, 140, 0, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 210, 0, 70, 70); 
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 280, 0, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 350, 0, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 420, 0, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 490, 0, 70, 70);
                      DrawSprite(grass, screen, NULL, NULL, 70, 70, 560, 0, 70, 70);
                      DrawSprite(grass, screen, NULL, NULL, 70, 70, 630, 0, 70, 70);
                      DrawSprite(grass, screen, NULL, NULL, 70, 70, 700, 0, 70, 70);
                      //Second row of drawing map
                      DrawSprite(roadN, screen, NULL, NULL, 70, 70, 0, 70, 70, 70);
                      DrawSprite(bush, screen, NULL, NULL, 70, 70, 70, 70, 70, 70); 
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 140, 70, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 210, 70, 70, 70);
                      DrawSprite(house, screen, NULL, NULL, 70, 70, 280, 70, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 350, 70, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 420, 70, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 490, 70, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 560, 70, 70, 70);
                      DrawSprite(bush, screen, NULL, NULL, 70, 70, 630, 70, 70, 70);
                      //Third row of drawing map
                      DrawSprite(bridgeN1, screen, NULL, NULL, 70, 70, 0, 140, 70, 70);
                      DrawSprite(water, screen, NULL, NULL, 70, 70, 70, 140, 70, 70);
                      DrawSprite(water, screen, NULL, NULL, 70, 70, 140, 140, 70, 70);
                      DrawSprite(water, screen, NULL, NULL, 70, 70, 210, 140, 70, 70); 
                      DrawSprite(bridgeN1, screen, NULL, NULL, 70, 70, 280, 140, 70, 70);
                      DrawSprite(watertS, screen, NULL, NULL, 70, 70, 350, 140, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 420, 140, 70, 70);
                      DrawSprite(bush, screen, NULL, NULL, 70, 70, 490, 140, 70 ,70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 560, 140, 70, 70);
                      DrawSprite(tree, screen, NULL, NULL, 70, 70, 630, 140, 70, 70);
                      //Fourth row of drawing map



And if it is. How do i do it? Thanks for any help!

Share this post


Link to post
Share on other sites
Advertisement
You mean, something like:

const int map_width = 11;
const int map_height = 3;

SOME_TYPE* map_data[] =
{
{ roadN, sign, bush, tree, tree, tree, tree, tree, grass, grass, grass, },
{ roadN, bush, tree, tree, house, tree, tree, tree, tree, bush, bush },
{ bridgeN1, water, water, water, bridgeN1, watertS, tree, bush, tree, tree, },
};


for(int row = 0; row<map_height; ++row)
for(int col = 0; col<map_width; ++col)
DrawSprite(map_data[col+row*map_width], screen, NULL, NULL,
70, 70, row*70, col*70, 70, 70);

Share this post


Link to post
Share on other sites
Alternatively, something similar to:

map.txt

11 3
|SBTTTTTGGG
|BTTHTTTTBB
bWWWbWTBTTB



#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
#include <fstream>

struct MapData
{
int width;
int height;
std::vector<SOME_TYPE*> data;
};

SOME_TYPE* TranslateMapFile(char ch)
{
switch(ch)
{
default : return dummy_sprite;
case '|': return roadN;
case 'S': return sign;
case 'G': return grass;
case 'B': return bush;
case 'T': return tree;
case 'W': return water;
case 'b': return bridgeN1;
case 'H': return house;
};
}

void LoadMap(const std::string& filename, MapData& map)
{
std::ifstream ifs(filename.c_str());

MapData tmp;
ifs >> tmp.width >> tmp.height;

while(!ifs.eof())
{
ifs >> std::ws;

std::string line;
std::getline(ifs, line);

for(int i=0; i<line.size(); ++i)
tmp.data.push_back( TranslateMapFile(line) );
}

std::swap( tmp.width, map.width );
std::swap( tmp.height, map.height );
std::swap( tmp.data, map.data );
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Samsonite
Yeah something like that. But what would you recommend for the type? int's doesn't work. Neither do chars.


I used SOME_TYPE* because I don't know what the types of tree, house and so on are. I suspect they're pointers to some type of sprite data structure, but since I don't know what it's called in your program, I made something up. That much should have been obvious [wink]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!