How to continuously load spliced maps from map files in 2D games

Source: Internet
Author: User
Tags bmp image

The map of many 2D games is not a whole piece, but a small BMP image (such as 32x32) is spliced by arrangement, and these small images are numbered 0, 1, 2, 3, and so on. In this way, you can write a file to store a map. For example, if a map file is opened, it is like this:

 

111111111111111111111

110000000000000000011

112222222222222222211

112222222220000000011

113333333333333333311

110000000000000000011

112222222222222222211

 

Then we can use ifstream to read the data from the file to the array map [] []. however, the map path parameter is a constant. You can read a map and it will not work if you pass through the map, but it does not matter. We can create another array to store the map path.

The following code is written to me:

 

# Include <iostream>
# Include <fstream>
# Include <string>
# Include <stdio. h>
Using namespace STD;

Char s [10];
Int mapindex = 2;

Int main ()
{
Sprintf (S, "map // % d. Map", mapindex );
Const char * mappath = s;
Ifstream infile (mappath );

Char map [34] [42]; // row (Y axis) = 34, column (X axis) = 42
String STR;
For (INT y = 0; y <34; ++ y)
{
If (Getline (infile, STR )){
Memcpy (Map [Y], str. c_str (), 42); // The second parameter must be a constant pointer. c_str () can change the string variable to a constant of char.
}
}

For (INT y = 0; y <34; ++ y)
{
For (INT x = 0; x <42; ++ X ){
Cout <map [y] [x];
}
Cout <Endl;
}

Cout <s;
Return 0;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.