diff --git a/main.cpp b/main.cpp index d8a510e..ed858e3 100644 --- a/main.cpp +++ b/main.cpp @@ -97,17 +97,23 @@ int StrToInt(const char* str) { #define UndoInc(var) (var = (var+1)%UNDO_MAX) #define UndoDec(var) (var = (var-1+UNDO_MAX)%UNDO_MAX) -void Undo_Create() { - unsigned char* dest = undo + (map_width*map_height*undo_pointer); - memcpy(dest, map, map_width*map_height); +void Undo_Create(const bool actor) { + unsigned char* dest = undo + ((map_width*map_height+1)*undo_pointer); + *dest = actor ? 1 : 0; + dest++; + unsigned char* src = actor ? map + (map_width*map_height) : map; + memcpy(dest, src, map_width*map_height); } void Undo_Restore() { - unsigned char* src = undo + (map_width*map_height*undo_pointer); - memcpy(map, src, map_width*map_height); + unsigned char* src = undo + ((map_width*map_height+1)*undo_pointer); + const bool actor = (*src == 1); + src++; + unsigned char* dest = actor ? map + (map_width*map_height) : map; + memcpy(dest, src, map_width*map_height); } -void Undo_Op() { +void Undo_Op(const bool actor = false) { UndoInc(undo_pointer); - Undo_Create(); + Undo_Create(actor); undo_max = undo_pointer; if (undo_pointer == undo_min) UndoInc(undo_min); } @@ -211,22 +217,24 @@ void Init() { stbi_image_free(buffer); f = fopen(GetPath(map_filename), "rb"); - map = (unsigned char*)malloc(map_width*map_height); - undo = (unsigned char*)malloc(map_width*map_height*UNDO_MAX); + map = (unsigned char*)calloc(map_width*map_height*2, 1); + undo = (unsigned char*)malloc((map_width*map_height*UNDO_MAX)+UNDO_MAX); if (f) { int old_map_width = 0, old_map_height = 0; fread(&old_map_width, 1, 1, f); fread(&old_map_height, 1, 1, f); if (old_map_width == map_width && old_map_height == map_height) { - fread(map, map_width*map_height, 1, f); + fread(map, map_width*map_height*2, 1, f); } else { unsigned char* old_map = (unsigned char*)malloc(old_map_width*old_map_height); - fread(old_map, old_map_width*old_map_height, 1, f); + fread(old_map, old_map_width*old_map_height*2, 1, f); for (int y = 0; y < map_height; y++) { for (int x = 0; x < map_width; x++) { map[x + y*map_width] = 0; + map[(x + y*map_width)+(map_width*map_height)] = 0; if (old_map_width > x && old_map_height > y && (x + y*map_width) < (old_map_width*old_map_height)) { - map[x + y*map_width] = old_map[x + y*(map_width)]; + map[x + y*map_width] = old_map[x + y*(old_map_width)]; + map[(x + y*map_width)+(map_width*map_height)] = old_map[(x + y*(old_map_width))+(old_map_width*old_map_height)]; } } } @@ -238,7 +246,7 @@ void Init() { if (!f) { error = 3; return; } fwrite(&map_width, 1, 1, f); fwrite(&map_height, 1, 1, f); - fwrite(map, map_width*map_height, 1, f); + fwrite(map, map_width*map_height*2, 1, f); fclose(f); dialog = 1; } @@ -448,6 +456,8 @@ void DoTileMap() { for (int x = 0; x < width_in_tiles; x++) { unsigned char tile = map[(map_x + x) + (map_y + y) * map_width]; Draw(sdlTilesTexture, x*tile_width, y*tile_height, (tile % pal_width_in_tiles)*tile_width, (tile / pal_width_in_tiles)*tile_height, tile_width, tile_height); + unsigned char sprite = map[((map_x + x) + (map_y + y) * map_width)+(map_width*map_height)]; + if (sprite != 0 ) Draw(sdlTilesTexture, x*tile_width, y*tile_height, (sprite % pal_width_in_tiles)*tile_width, (sprite / pal_width_in_tiles)*tile_height, tile_width, tile_height); } } @@ -471,7 +481,19 @@ void DoTileMap() { if (mouse.mouse_down[MOUSE_BUTTON_LEFT] || mouse.mouse_down[MOUSE_BUTTON_RIGHT]) { Undo_Op(); } if (mouse.buttons[MOUSE_BUTTON_RIGHT]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_back; } - if (mouse.buttons[MOUSE_BUTTON_LEFT]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_sel; } + if (mouse.buttons[MOUSE_BUTTON_LEFT]) { + const int pos = (map_x + cur_x) + (map_y + cur_y)*map_width; + if (keyboard[CTRL]) { + ignoreMouse = true; + if (map[pos + (map_width*map_height)] == 0) { + map[pos + (map_width*map_height)] = tile_sel; + } else { + map[pos + (map_width*map_height)] = 0; + } + } else { + map[pos] = tile_sel; + } + } if (mouse.buttons[MOUSE_BUTTON_MIDDLE]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; } if (keyJustPressed == SDL_SCANCODE_Z && keyboard[CTRL]) { Undo_Undo(); } @@ -485,7 +507,7 @@ void DoTileMap() { FILE* f = fopen(GetPath(map_filename), "wb"); fwrite(&map_width, 1, 1, f); fwrite(&map_height, 1, 1, f); - fwrite(map, map_width*map_height, 1, f); + fwrite(map, map_width*map_height*2, 1, f); fclose(f); menu = false; ignoreMouse = true; } diff --git a/mappy.vcxproj b/mappy.vcxproj index 47def7b..c9d9925 100644 --- a/mappy.vcxproj +++ b/mappy.vcxproj @@ -94,7 +94,7 @@ - + diff --git a/mappy.vcxproj.filters b/mappy.vcxproj.filters index d5b66a7..435d6f2 100644 --- a/mappy.vcxproj.filters +++ b/mappy.vcxproj.filters @@ -39,7 +39,7 @@ - + Resource Files diff --git a/res.rc b/res.rc index d6c565c..9453edc 100644 Binary files a/res.rc and b/res.rc differ