• Advertisement
Sign in to follow this  

Map Editor

This topic is 4425 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

After successfuly solving my problem(with the help of u guys), with the maps, and tiles, and .... forget it... I'm working on a map editor... I'm using c/c++ and SDL. first i have a function that opens the map file:
bool Load_Map(char *filename){

    char c;
    int y=0, x=0;

    FILE *f = fopen(filename, "rb");

    cout << "Loading map .....";

    while((c = fgetc(f)) != EOF) {
        if(c=='\n') {
            y++;
            x=0;
            continue;
        }
        map[y][x++] = c;
    }
    cout << "ok" << endl;
    return true;
}
and then i have the function that draws the map its located right after input handling. The code:
for(int y=0;y<MAPHEIGHT;y++)
        {
            for(int x=0;x<MAPWIDTH;x++){

                int i = map[y][x];

                if(i==0){ Draw_BMP(x * TILESIZE, y * TILESIZE, image); }
                if(i==1){ Draw_BMP(x * TILESIZE, y * TILESIZE, image2); }
                if(i==2){
                    Draw_BMP(x * TILESIZE, y * TILESIZE, image);
                    Draw_BMP(x * TILESIZE, y * TILESIZE, image3);
                }

            }
        }
        SDL_Flip(screen);

It's the same method i use to draw the map on my game... but it doesn't seem to work right with the editor, i just get a black screen :/ my map is a char map[10][10] and i'm using 32x32 tiles. Thanks a lot for your attention Best regards

Share this post


Link to post
Share on other sites
Advertisement
I can't see any problems in the code samples you gave us, but you have valid graphics in image, image2, image3? And Draw_BMP(..) works fine?

Also, for this statement

if(i==0){ Draw_BMP(x * TILESIZE, y * TILESIZE, image); }
if(i==1){ Draw_BMP(x * TILESIZE, y * TILESIZE, image2); }
if(i==2){
Draw_BMP(x * TILESIZE, y * TILESIZE, image);
Draw_BMP(x * TILESIZE, y * TILESIZE, image3);
}

you could have "else if (i==1)" and "else if (i==2)" so the program doesn't have to check a couple more times if i is 0.

Share this post


Link to post
Share on other sites
yeah everything is fine, both functions the one that draw_bmp and the loading map are from the game and the're working fine...
:( don't know what to do

Share this post


Link to post
Share on other sites
im not the must uptodate in c++ code, but your reading a char, and saving it as a char or mabe an int(you never show the map decleration, prob an int tho) and then handeling it like an int...that is probably the source of your confusion, the char form of 1 is ascii code 49....

if map is an int, and the map file reads

1111111111
1000022001

the array, i think, is going to look like

49 49 49 49 49 49 49 49 49 49 49
49 48 48 48 48 48 50 50 48 48 49

and so on, since 0=48 1=49 and 2=50
make sense?(sorry not the best at explaining.)

Share this post


Link to post
Share on other sites
Quote:
Original post by davidjustus
im not the must uptodate in c++ code, but your reading a char, and saving it as a char or mabe an int(you never show the map decleration, prob an int tho) and then handeling it like an int...that is probably the source of your confusion, the char form of 1 is ascii code 49....

if map is an int, and the map file reads

1111111111
1000022001

the array, i think, is going to look like

49 49 49 49 49 49 49 49 49 49 49
49 48 48 48 48 48 50 50 48 48 49

and so on, since 0=48 1=49 and 2=50
make sense?(sorry not the best at explaining.)


If he outputted in binary of a char, and read in a char, then he would be reading 0 or 1 rather than "0" or "1".

But he should output the 'c' variable in his load_map function to check and make sure it is correct.
cout << (int)c << ' ';
rather than cout << c; so that you it won't output as non-displayable characters.

-edit-
BrasiLokau, any reason why you are using C functions for file handling rather than C++'s fstream?


#include <fstream>

using namespace std; // you probably have this already.

bool Load_Map(char *filename){

char c;
int y=0, x=0;

ifstream fin(filename, ifstream::binary);

cout << "Loading map .....";

while(fin.good()) {
c = fin.get();
if(c=='\n') {
y++;
x=0;
continue;
}
map[y][x++] = c;
}
fin.close();
cout << "ok" << endl;
return true;
}

bool Save_Map(char *filename){

int ysize=10, xsize=10;

ofstream fout(filename, ofstream::binary);

cout << "Saving map .....";

for(int y = 0; y < ysize; ++y)
{
for(int x = 0; x < xsize; ++x)
{
fout.put(map[y][x]);

}
fout.put('\n');
}
fout.close();
cout << "ok" << endl;
return true;
}




Share this post


Link to post
Share on other sites
well, he never showed a load function..so i was just stating that would be a problem.

but yeah i agree, output the array to the screen to see whats going on with the vars.

Share this post


Link to post
Share on other sites
The reason why i'm not using c++?
Because i could not make it work... soh i got it to work with this function, because it prints the map correctly when i use cout << c;
But i don't really know the problem... because the drawing functions seems ok...

Here's is the full source code...


#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"

#define TILESIZE 32

#define SCREENWIDTH 320
#define SCREENHEIGHT 320
#define SCREENBPP 16

#define MAPWIDTH 10
#define MAPHEIGHT 10

using namespace std;

// the screen
SDL_Surface* screen;
bool end = false;
SDL_Event event;

char map[MAPHEIGHT][MAPWIDTH];

// function that draw an image to the screen
int Draw_BMP(int x, int y, SDL_Surface *sprite)
{
SDL_Rect dest;
SDL_Rect src;

src.x = 0;
src.y = 0;
src.h = sprite->h;
src.w = sprite->w;

dest.x = x;
dest.y = y;
dest.h = src.h;
dest.w = src.w;

SDL_BlitSurface(sprite, &src, screen, &dest);

return 0;
}

int main ( int argc, char** argv )
{

if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("Could not start sdl: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

screen = SDL_SetVideoMode(SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_SWSURFACE|SDL_ANYFORMAT);
if(screen==NULL){
printf("Could not set video mode: %s\n", SDL_GetError());
exit(1);
}

SDL_WM_SetCaption("Amazon Engine v0.0.2", NULL);

cout << "Loading resources .....";

SDL_Surface *image;
SDL_Surface *image2;
SDL_Surface *image3;

image = SDL_LoadBMP("tile001.bmp");
image2 = SDL_LoadBMP("tile002.bmp");
image3 = SDL_LoadBMP("bau.bmp");

SDL_SetColorKey(image3, SDL_SRCCOLORKEY, SDL_MapRGB(image3->format, 0, 255, 0));

if(image!=NULL){
if(image2!=NULL){
if(image3!=NULL){
cout << "ok" << endl;
}
}
}

char c;
int y=0, x=0;

FILE *f = fopen("map01.map", "rb");

cout << "Loading map .....";

while((c = fgetc(f)) != EOF) {
if(c=='\n') {
y++;
x=0;
continue;
}
map[y][x++] = c;
cout << c;
}
fclose(f);
cout << "ok" << endl;

cout << "\nEntering main game loop ....." << endl;

while(end == false){
while(SDL_PollEvent(&event)){
if(event.type==SDL_KEYDOWN){
switch(event.key.keysym.sym){
case SDLK_ESCAPE:
end = true;
}
}
if(event.type==SDL_QUIT){
end = true;
}
}

for(int y=0;y<MAPHEIGHT;y++)
{
for(int x=0;x<MAPWIDTH;x++){

int i = map[y][x];

if(i==0){ Draw_BMP(x * TILESIZE, y * TILESIZE, image); }
if(i==1){ Draw_BMP(x * TILESIZE, y * TILESIZE, image2); }
if(i==2){
Draw_BMP(x * TILESIZE, y * TILESIZE, image);
Draw_BMP(x * TILESIZE, y * TILESIZE, image3);
}
}
}

SDL_Flip(screen);
}

}

Share this post


Link to post
Share on other sites
I'm figuring that if it prints out the 0,1,2 as numbers when you cout << c; then you should change your if statment in your loop.

I put single quotes around the numbers and change 'i' to be a char, so you aren't casting from char to int. If that doesn't fix it, I'm pretty stumped.


for(int y=0;y<MAPHEIGHT;y++)
{
for(int x=0;x<MAPWIDTH;x++){

char i = map[y][x];

if(i=='0'){ Draw_BMP(x * TILESIZE, y * TILESIZE, image); }
if(i=='1'){ Draw_BMP(x * TILESIZE, y * TILESIZE, image2); }
if(i=='2'){
Draw_BMP(x * TILESIZE, y * TILESIZE, image);
Draw_BMP(x * TILESIZE, y * TILESIZE, image3);
}
}
}


Share this post


Link to post
Share on other sites
It worked...
i was declared as char before, i don't know how i changed it. And i didn't even thing about doing if i =='0' the correct way.

Thanks a lot

Share this post


Link to post
Share on other sites
Hi, I don't know if this helps, but I was a bit confused with symbolic constants myself while I was first working on my own game engine. I now have a function that I use to convert the input from the map file to ints (various reasons I needed to):

int Convert(char duhr)
{
switch (duhr)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
//etc etc etc
}
}


My map file contained alphabetic characters as well, but this suits you well enough to figure it out. Hope it helps :)

Share this post


Link to post
Share on other sites
Or he could make it simplier with stringstream. Of course I don't think passing 'a' would give a valid result.

#include <sstream>

int CharToInt(char c)
{
int result;
stringstream ss;
ss << c;
ss >> result;
return result;
}

int StringToInt(string s)
{
int result;
stringstream ss;
ss << s;
ss >> result;
return result;
}

Share this post


Link to post
Share on other sites
If the map doesn't change sizes he could just write the binary of map out to a file and read it in. This probably wouldn't make it easy to edit in a texteditor though.


bool Load_Map(char *filename){

char c;
int y=0, x=0;

ifstream fin(filename, ifstream::binary);

cout << "Loading map .....";

fin.read((char*)&map, sizeof(map));

fin.close();
cout << "ok" << endl;
return true;
}

bool Save_Map(char *filename){

int ysize=10, xsize=10;

ofstream fout(filename, ofstream::binary);

cout << "Saving map .....";

fout.write((char*)&map, sizeof(map));

fout.close();
cout << "ok" << endl;
return true;
}

Share this post


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

  • Advertisement