Map editor

I’m trying to make a simple map editor for my game and im having problem
putting the new tiles… my map is a bidimensional array char map[10][10]
and im using 32x32 tiles. the map is like this:
1111111111
1000111001
1000000001
and goes on until it reaches 10 lines. I’m using the following code:

[CODE]
case SDL_MOUSEBUTTONDOWN:

                if(event.button.button == SDL_BUTTON_LEFT){

                    if(map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 0){

                        map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] = 1;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 1){

                        map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] = 2;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 2){

                        map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 0;

                    }

            case SDL_MOUSEMOTION:

                //ActiveTile = SDL_CreateRGBSurface(SDL_SWSURFACE | 

SDL_SRCALPHA, 32, 32, format->BitsPerPixel, format->Rmask, format->Gmask,
format->Bmask, format->Amask );

                //if(ActiveTile == NULL) {

                //    cout << "An unexpected error occurred.\n";

                //}

                SDL_Rect dest;

                dest.x = event.button.x;
                dest.y = event.button.y;
                dest.h = 32;
                dest.w = 32;

                SDL_BlitSurface(image4, NULL, screen, &dest);


                if(event.motion.state == SDL_BUTTON(SDL_BUTTON_LEFT)){

                    if(map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 0){

                        map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] = 1;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 1){

                        map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] = 2;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 2){

                        map[event.button.y / TILESIZE][event.button.x / 

TILESIZE] == 0;

                    }

                }

[/CODE]

Also im trying to make like a cursor… when the mouse stays above a certain
title, it gets a little transparency(opacity), i tryied to use an red square
e set its opacity but no success. If anybody can help-me that would be
great…

Thanks a lot
Best Regards

Hi Thiago,

Start by structuring the damn code into functions and stuff. Also, if you’re
using gcc, feed it with the -Wall flag and fix all warnings. You’re making
logic tests instead of assignments.

Here is a better structured code:

#define MAP_WIDTH 10
#define MAP_HEIGHT 10
char map[MAP_WIDTH][MAP_HEIGHT];

/* It might be a good idea to make wrapper access functions to your array to
avoid crashes resulting of reading/writting outside of its boundries, and to
make thins more readable. As a bonus, it makes it easily extandable to
support more than one map. */

char get_tile(int x, int y)
{
if(x < 0 || x >= MAP_WIDTH)
return 0; // printf
return map[x][y];
}

void put_tile(int x, int y, char t)
{
if(x < 0 || x >= MAP_WIDTH)
return; // printf
map[x][y] = c;
}

/* Toggling of the tiles. */
#define MAX_TILES 3
void toggle_tile(int x, int y)
{
char t = get_tile(x,y) + 1;
if(t >= MAX_TILES)
t = 0;
put_tile(x,y,t);
}

(…)

/* A value to the tile that the mouse cursor is pointing to. Just used so we
know in mouse motion if we’re hover a new tile or still the same. */
int mouse_hover_tile = -1;

(…)

case SDL_MOUSEBUTTONDOWN:
{
int x = event.button.x / MAP_WIDTH,
y = event.button.y / MAP_HEIGHT;
mouse_hover_tile = x + (y * MAP_WIDTH);
toggle_tile(x,y);
break;
}
case SDL_MOUSEMOTION:
{
if(!(event.motion.state & SDL_BUTTON(1)))
break;

int x = event.motion.x / MAP_WIDTH,
y = event.motion.y / MAP_HEIGHT;

int hover_tile = x + (y * MAP_WIDTH);
if(hover_tile != mouse_hover_tile)
{
mouse_hover_tile = hover_tile;
toggle_tile(x,y);
}
break;
}

(…)

Espero que tenha ajudado. Mas esta lista n?o deve ser usada para estes
problemas… Se quiseres, envia-me o c?digo do teu programa que eu tenho todo
o prazer em te corrigir os erros.

Cheers,
RicardoEm Quinta, 19 de Janeiro de 2006 15:39, o Thiago Nunes Leite escreveu:

I’m trying to make a simple map editor for my game and im having problem
putting the new tiles… my map is a bidimensional array char map[10][10]
and im using 32x32 tiles. the map is like this:
1111111111
1000111001
1000000001
and goes on until it reaches 10 lines. I’m using the following code:

[CODE]
case SDL_MOUSEBUTTONDOWN:

                if(event.button.button == SDL_BUTTON_LEFT){

                    if(map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 0){

                        map[event.button.y / TILESIZE][event.button.x /

TILESIZE] = 1;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 1){

                        map[event.button.y / TILESIZE][event.button.x /

TILESIZE] = 2;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 2){

                        map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 0;

                    }

            case SDL_MOUSEMOTION:

                //ActiveTile = SDL_CreateRGBSurface(SDL_SWSURFACE |

SDL_SRCALPHA, 32, 32, format->BitsPerPixel, format->Rmask, format->Gmask,
format->Bmask, format->Amask );

                //if(ActiveTile == NULL) {

                //    cout << "An unexpected error occurred.\n";

                //}

                SDL_Rect dest;

                dest.x = event.button.x;
                dest.y = event.button.y;
                dest.h = 32;
                dest.w = 32;

                SDL_BlitSurface(image4, NULL, screen, &dest);


                if(event.motion.state == SDL_BUTTON(SDL_BUTTON_LEFT)){

                    if(map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 0){

                        map[event.button.y / TILESIZE][event.button.x /

TILESIZE] = 1;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 1){

                        map[event.button.y / TILESIZE][event.button.x /

TILESIZE] = 2;

                    }

                    if(map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 2){

                        map[event.button.y / TILESIZE][event.button.x /

TILESIZE] == 0;

                    }

                }

[/CODE]

Also im trying to make like a cursor… when the mouse stays above a
certain title, it gets a little transparency(opacity), i tryied to use an
red square e set its opacity but no success. If anybody can help-me that
would be great…

Thanks a lot
Best Regards


It’s always darkest just before it gets pitch black.