diff --git a/config.ini b/config.ini index 50f80fc..0a20dcf 100644 --- a/config.ini +++ b/config.ini @@ -4,6 +4,6 @@ map_width = 240 map_height = 238 screen_width = 320 screen_height = 224 -zoom = 2 +zoom = 3 tiles = tiles.png map = map.map \ No newline at end of file diff --git a/main.cpp b/main.cpp index 01c4d16..16122a0 100644 --- a/main.cpp +++ b/main.cpp @@ -10,6 +10,10 @@ #define STATE_TILEPAL 1 #define STATE_MINIMAP 2 +#define MOUSE_BUTTON_LEFT 0 +#define MOUSE_BUTTON_MIDDLE 1 +#define MOUSE_BUTTON_RIGHT 2 + struct Mouse { int x, y; bool buttons[3]; @@ -31,7 +35,8 @@ bool anyKey = false; LoopFun loop; Mouse mouse; int error = 0; -int state = STATE_MINIMAP; +int dialog = 0; +int state = STATE_TILEMAP; int tile_width = 16; int tile_height = 16; @@ -82,13 +87,26 @@ void LoadConfig() { fclose(f); } +void UpdateMiniMap() { + const int tmtw = (tilemap_width / tile_width); + Uint32* pixels = (Uint32*)malloc(map_width * map_height*sizeof(Uint32)); + for (int y = 0; y < map_height; y++) { + for (int x = 0; x < map_width; x++) { + const Uint8 tile = map[x + y*map_width]; + pixels[x + y*map_width] = minitiles[(tile % tmtw) + (tile / tmtw)*tmtw]; + } + } + SDL_UpdateTexture(sdlMinimapTexture, NULL, pixels, map_width * sizeof(Uint32)); + free(pixels); +} + void Init() { LoadConfig(); SDL_Init(SDL_INIT_EVERYTHING); sdlWindow = SDL_CreateWindow("Mappy v0.1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width*zoom, screen_height*zoom, SDL_WINDOW_SHOWN); sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, SDL_RENDERER_PRESENTVSYNC); - //SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + SDL_SetRenderDrawBlendMode(sdlRenderer, SDL_BLENDMODE_BLEND); SDL_RenderSetLogicalSize(sdlRenderer, screen_width, screen_height); unsigned short w, h; @@ -136,23 +154,36 @@ void Init() { stbi_image_free(buffer); f = fopen(map_filename, "rb"); - if (!f) { error = 3; return; } - unsigned char size[2]; - fread(&map_width, 1, 1, f); - fread(&map_height, 1, 1, f); map = (unsigned char*)malloc(map_width*map_height); - fread(map, map_width*map_height, 1, f); - fclose(f); + 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); + } 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); + for (int y = 0; y < map_height; y++) { + for (int x = 0; x < map_width; x++) { + map[x + y*map_width] = old_map[x + y*old_map_width]; + } + } + dialog = 2; + } + fclose(f); + } else { + f = fopen(map_filename, "wb"); + 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); + fclose(f); + dialog = 1; + } sdlMinimapTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, map_width, map_height); - pixels = (Uint32*)malloc(map_width * map_height*sizeof(Uint32)); - for (int y = 0; y < map_height; y++) { - for (int x = 0; x < map_width; x++) { - const Uint8 tile = map[x + y*map_width]; - pixels[x + y*map_width] = minitiles[(tile % tmtw) + (tile / tmtw)*tmtw]; - } - } - SDL_UpdateTexture(sdlMinimapTexture, NULL, pixels, map_width * sizeof(Uint32)); + UpdateMiniMap(); keyboard = SDL_GetKeyboardState(nullptr); //SDL_Surface* bmp = SDL_LoadBMP("assets.bmp"); @@ -202,13 +233,13 @@ void Print(int x, int y, const char* text) { } bool Button(int x, int y, char* text) { - bool inside = (mouse.x >= x) && (mouse.y >= y) && (mouse.x < x + 51) && (mouse.y < y + 8); + bool inside = (mouse.x >= x) && (mouse.y >= y) && (mouse.x < x + 55) && (mouse.y < y + 12); if (inside) { - FillRect(x, y, 51, 9, 128, 64, 64, 255); + FillRect(x, y, 55, 13, 128, 64, 64, 255); } else { - FillRect(x, y, 51, 9, 255, 64, 64, 255); + FillRect(x, y, 55, 13, 255, 64, 64, 255); } - Print(x+2, y+2, text); + Print(x+4, y+4, text); return inside && mouse.buttons[0]; } @@ -246,9 +277,30 @@ void PrintFatalError(const char* errorMsg) { } void ShowError() { switch (error) { - case 1: PrintFatalError("No s'ha trobat l'arxiu config.ini"); break; - case 2: PrintFatalError("No s'ha trobat l'arxiu de tiles"); break; - case 3: PrintFatalError("No s'ha trobat l'arxiu de mapa"); break; + case 1: PrintFatalError("config.ini not found"); break; + case 2: PrintFatalError("tiles file not found"); break; + case 3: PrintFatalError("cannot create map file"); break; + } +} + +void ShowDialog() { + int size = 0; + switch (dialog) { + case 1: + size = strlen("Map file not found"); + Print((screen_width - (size * 6)) / 2, (screen_height - 6) / 2, "Map file not found"); + size = strlen("A new one has been created"); + Print((screen_width - (size * 6)) / 2, ((screen_height - 6) / 2)+6, "A new one has been created"); + if (Button((screen_width - 55) / 2, screen_height - 40, "Ok")) { dialog = 0; ignoreMouse = true; } + break; + case 2: + size = strlen("Map has been resized"); + Print((screen_width - (size * 6)) / 2, (screen_height - 6) / 2, "Map has been resized"); + size = strlen("using config.ini info"); + Print((screen_width - (size * 6)) / 2, ((screen_height - 6) / 2) + 6, "using config.ini info"); + if (Button((screen_width - 55) / 2, screen_height - 40, "Ok")) { dialog = 0; ignoreMouse = true; } + break; + //case 3: PrintFatalError("No s'ha trobat l'arxiu de mapa"); break; } } @@ -280,6 +332,8 @@ void DoTilePal() { } void DoTileMap() { + static bool menu = false; + const int width_in_tiles = screen_width / tile_width; const int height_in_tiles = screen_height / tile_height; const int pal_width_in_tiles = tilemap_width / tile_width; @@ -289,19 +343,70 @@ void DoTileMap() { 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); } } - const int cur_x = mouse.x / tile_width; - const int cur_y = mouse.y / tile_height; - DrawRect(cur_x*tile_width-1, cur_y*tile_height-1, tile_width + 2, tile_height + 2, 255, 255, 255, 128); - if (keyJustPressed == SDL_SCANCODE_Q) state = STATE_TILEPAL; - if (keyJustPressed == SDL_SCANCODE_E) state = STATE_MINIMAP; - if (keyJustPressed == SDL_SCANCODE_RIGHT || keyJustPressed == SDL_SCANCODE_D) if (map_x < map_width - width_in_tiles) map_x += width_in_tiles; - if (keyJustPressed == SDL_SCANCODE_LEFT || keyJustPressed == SDL_SCANCODE_A) if (map_x > 0) map_x -= width_in_tiles; - if (keyJustPressed == SDL_SCANCODE_DOWN || keyJustPressed == SDL_SCANCODE_S) if (map_y < map_height - height_in_tiles) map_y += height_in_tiles; - if (keyJustPressed == SDL_SCANCODE_UP || keyJustPressed == SDL_SCANCODE_W) if (map_y > 0) map_y -= height_in_tiles; - if (mouse.buttons[2]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_back; } - if (mouse.buttons[0]) { map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_sel; } - if (mouse.buttons[1]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; } + if (!menu) { + const int cur_x = mouse.x / tile_width; + const int cur_y = mouse.y / tile_height; + DrawRect(cur_x*tile_width - 1, cur_y*tile_height - 1, tile_width + 2, tile_height + 2, 255, 255, 255, 128); + + if (keyJustPressed == SDL_SCANCODE_TAB) { + menu = true; return; } + if (keyJustPressed == SDL_SCANCODE_Q) state = STATE_TILEPAL; + if (keyJustPressed == SDL_SCANCODE_E) { state = STATE_MINIMAP; UpdateMiniMap(); } + if (keyJustPressed == SDL_SCANCODE_X) { int temp = tile_back; tile_back = tile_sel; tile_sel = temp; } + if (keyJustPressed == SDL_SCANCODE_RIGHT || keyJustPressed == SDL_SCANCODE_D) if (map_x < map_width - width_in_tiles) map_x += width_in_tiles; + if (keyJustPressed == SDL_SCANCODE_LEFT || keyJustPressed == SDL_SCANCODE_A) if (map_x > 0) map_x -= width_in_tiles; + if (keyJustPressed == SDL_SCANCODE_DOWN || keyJustPressed == SDL_SCANCODE_S) if (map_y < map_height - height_in_tiles) map_y += height_in_tiles; + if (keyJustPressed == SDL_SCANCODE_UP || keyJustPressed == SDL_SCANCODE_W) if (map_y > 0) map_y -= height_in_tiles; + 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_MIDDLE]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; } + } else { + if (keyJustPressed == SDL_SCANCODE_TAB) { menu = false; return; } + FillRect(0, 0, screen_width, screen_height, 0, 0, 0, 192); + if (Button(6, 6, "Save")) { + FILE* f = fopen(map_filename, "wb"); + fwrite(&map_width, 1, 1, f); + fwrite(&map_height, 1, 1, f); + fwrite(map, map_width*map_height, 1, f); + fclose(f); + menu = false; ignoreMouse = true; + } + if (Button(6, 24, "Clear")) { + for (int y = 0; y < height_in_tiles; y++) { + for (int x = 0; x < width_in_tiles; x++) { + map[(map_x + x) + (map_y + y) * map_width] = tile_back; + } + } + menu = false; ignoreMouse = true; + } + if (Button(6, 42, "Border")) { + for (int y = 0; y < height_in_tiles; y++) { + map[(map_x) + (map_y + y) * map_width] = tile_sel; + map[(map_x + width_in_tiles - 1) + (map_y + y) * map_width] = tile_sel; + } + for (int x = 0; x < width_in_tiles; x++) { + map[(map_x + x) + (map_y) * map_width] = tile_sel; + map[(map_x + x) + (map_y + height_in_tiles - 1) * map_width] = tile_sel; + } + menu = false; ignoreMouse = true; + } + } +} + +#define mod(x, y) (((x % y)+y)%y) + +void ShiftMap(int sx, int sy) { + int test = mod(-1, 240); + sx *= (screen_width/tile_width); + sy *= (screen_height / tile_height); + unsigned char* new_map = (unsigned char*)malloc(map_width*map_height); + for (int y = 0; y < map_height; y++) { + for (int x = 0; x < map_width; x++) { + new_map[x + y*map_width] = map[x + y*map_width]; + } + } + } void DoMiniMap() { @@ -328,14 +433,19 @@ void DoMiniMap() { const int screen_height_in_tiles = screen_height / tile_height; DrawRect((ox + map_x) - 1, (oy + map_y) - 1, screen_width_in_tiles + 2, screen_height_in_tiles + 2, 255, 255, 255); //DrawRect(ox + (tile_sel % tiles_per_row)*tile_width - 1, oy + (tile_sel / tiles_per_row)*tile_height - 1, tile_width + 2, tile_height + 2, 255, 255, 255); - if (mouse.buttons[0]) { + if (mouse.buttons[MOUSE_BUTTON_LEFT]) { map_x = int((mouse.x-ox) / screen_width_in_tiles) * screen_width_in_tiles; map_y = int((mouse.y - oy) / screen_height_in_tiles) * screen_height_in_tiles; state = STATE_TILEMAP; ignoreMouse = true; } if (keyJustPressed == SDL_SCANCODE_E) state = STATE_TILEMAP; - + if (keyboard[SDL_SCANCODE_LCTRL]) { + if (keyJustPressed == SDL_SCANCODE_A) ShiftMap(-1, 0); + if (keyJustPressed == SDL_SCANCODE_D) ShiftMap(1, 0); + if (keyJustPressed == SDL_SCANCODE_W) ShiftMap(0, -1); + if (keyJustPressed == SDL_SCANCODE_S) ShiftMap(0, 1); + } } int main(int argc, char* argv[]) { @@ -345,33 +455,37 @@ int main(int argc, char* argv[]) { Clear(); if (error == 0) { - switch (state) { - case STATE_TILEMAP: DoTileMap(); break; - case STATE_TILEPAL: DoTilePal(); break; - case STATE_MINIMAP: DoMiniMap(); break; - }; + if (dialog == 0) { + switch (state) { + case STATE_TILEMAP: DoTileMap(); break; + case STATE_TILEPAL: DoTilePal(); break; + case STATE_MINIMAP: DoMiniMap(); break; + }; - /*char num[5]; - sprintf(num, "%d", mouse.x); - Print(100, 100, num); - sprintf(num, ",%d", mouse.y); - Print(118, 100, num); + /*char num[5]; + sprintf(num, "%d", mouse.x); + Print(100, 100, num); + sprintf(num, ",%d", mouse.y); + Print(118, 100, num); - if (Button(2, 2, "New")) { - strcpy(state, "New pressed"); - } - if (Button(2, 12, "Open")) { - strcpy(state, "open pressed"); - } - if (Button(2, 22, "Save")) { - strcpy(state, "save pressed"); - } - if (Button(2, 32, "Save As")) { - strcpy(state, "Save As pressed"); - } + if (Button(2, 2, "New")) { + strcpy(state, "New pressed"); + } + if (Button(2, 12, "Open")) { + strcpy(state, "open pressed"); + } + if (Button(2, 22, "Save")) { + strcpy(state, "save pressed"); + } + if (Button(2, 32, "Save As")) { + strcpy(state, "Save As pressed"); + } - FillRect(0, 183, 256, 9, 128, 128, 128, 255); - Print(2, 185, state);*/ + FillRect(0, 183, 256, 9, 128, 128, 128, 255); + Print(2, 185, state);*/ + } else { + ShowDialog(); + } } else { ShowError(); } diff --git a/map.map b/map.map index d321884..6a81ca5 100644 Binary files a/map.map and b/map.map differ diff --git a/mappy.vcxproj b/mappy.vcxproj index 5cd110e..66fbaed 100644 --- a/mappy.vcxproj +++ b/mappy.vcxproj @@ -1,5 +1,5 @@  - + Debug @@ -46,6 +46,8 @@ false + C:\dev\lib\sdl2\include;$(IncludePath) + C:\dev\lib\sdl2\lib\x86;$(LibraryPath) @@ -56,7 +58,7 @@ WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - Console + Windows true SDL2.lib;SDL2main.lib;%(AdditionalDependencies) @@ -72,10 +74,11 @@ WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - Console + Windows true true true + SDL2.lib;SDL2main.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)