Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

C++ SFML


Ertzel
 Share

Recommended Posts

I have a question to do with C++ and SFML loading images.

For a map system I currently have, it saves chunks of a square with different colored pixels. It then takes these pixels and get their ID's from the png image and sends them to the client.

I was wondering, would anyone be able to guide me on how I could take out the need for the png file and just save the pixel's colors to a binary file and load those to match this current system and send to the client?

The current code is the following:

```

void Map::loadChunk(int chunkX, int chunkY)

{

std::stringstream ss;

ss << "rsc/map/" << chunkX << '-' << chunkY << ".png";

sf::Image img;

img.loadFromFile(ss.str());

srand(time(NULL));

for(int x = 0; x < gl::chunkSize; x++)

{

for(int y = 0; y < gl::chunkSize; y++)

{

if(map[chunkX * gl::chunkSize + x][chunkY * gl::chunkSize + y] == NULL)

{

// Tile spawning

Tile* tile = new Tile;

tile->x = chunkX * gl::chunkSize + x;

tile->y = chunkY * gl::chunkSize + y;

tile->ID = getID(img.getPixel(x, y));

mutex.lock();

map[chunkX * gl::chunkSize + x][chunkY * gl::chunkSize + y] = tile;

mutex.unlock();

// Animal spawning

if((rand() % 1000 == 1) && (tile->ID == 1) && (!ent::objectHandler.isObjectOnTile(chunkX * gl::chunkSize + x, chunkY * gl::chunkSize + y)))

{

ent::animalHandler.spawnAnimal(tile->x, tile->y, rand() % 3);

}

}

}

}

for(int x = 0; x < gl::chunkSize; x++)

{

for(int y = 0; y < gl::chunkSize; y++)

{

updateTransID(chunkX * gl::chunkSize + x, chunkY * gl::chunkSize + y);

}

}

}

```

And an example of a chunk image is this: ![](http://www.freemmorpgmaker.com/files/imagehost/pics/02eec98c0c2cebf777779ae06f35ed2d.png) and that image is called 4-27.png
Link to comment
Share on other sites

Okay, I setup a mock-map XML file now. It uses the following format:

```

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

```
and Y goes up to 2000 and X goes up to 2000\. So once Y gets to 2000 it goes to the next X and recounts Y grabbing the ID that matches those the X and Y number's. That ID is then send to the client so they know which tile to draw. For default I just set it as 1 to draw grass for every tile.

I made a generic map-generator that sets up this 2000x2000 xml file with all ID's equaling to one. Now I need to know, how would I read this xml file server-side and get the ID value to send to the client in place of the original post's way of doing it by images?
Link to comment
Share on other sites

See that part where it says "tile->id" and then assigns that to an id? Use the code in the ObjectHandler.cpp to load it like a normal XML file.

The server doesn't send any images over to the client. It simply opens an image file (say you want chunk 8, 10), and runs through each pixel in that file. If the pixel of 0,0 is equal to an RBG value of [0, 255, 0] (grass), then it sets the ID to the ID of 1.

Here is a list of conversion colors:

```

----------------------------------

-- --

-- TileID Color Codes --

-- --

----------------------------------

0 | Water (0, 0, 255)

1 | Grassland (0, 255, 0)

2 | Sand (255, 255, 0)

3 | Stone (100, 100, 100)

4 | Forest (100, 255, 100)

5 | Bog (0, 255, 100)

6 | Fertileland (255, 100, 100)

7 | Smoothstone (200, 200, 200)

```

You'll want to delete this color coding and just load the values from the XML file instead. Delete the getColor() and getID() functions because those will be worthless (all those functions do is convert pixel RGB values to an assigned int, like shown above).
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...