diff --git a/assets/map/map.sv b/assets/map/map.sv new file mode 100644 index 0000000..174f27f Binary files /dev/null and b/assets/map/map.sv differ diff --git a/assets/map/map_save_file b/assets/map/map_save_file new file mode 100644 index 0000000..eb3bac5 Binary files /dev/null and b/assets/map/map_save_file differ diff --git a/bin/pokemon.exe b/bin/pokemon.exe index b0fefca..21e4ed2 100644 Binary files a/bin/pokemon.exe and b/bin/pokemon.exe differ diff --git a/bin/pokemon.pdb b/bin/pokemon.pdb index c78d54c..caf7bb1 100644 Binary files a/bin/pokemon.pdb and b/bin/pokemon.pdb differ diff --git a/src/main.cpp b/src/main.cpp index c22b759..65ac3fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,7 @@ #include "m_string.h" #include "math_graphics.h" #include "load_tga_file.h" +#include "load_entire_file.h" typedef uint8_t uint8; typedef uint16_t uint16; @@ -60,6 +61,7 @@ bool Running = true; #define view_width 17 #define view_height 13 + struct Vertex { V4 pos; V4 uvst; @@ -88,8 +90,14 @@ Instance tiles_instances[view_width * view_height] = { Instance player_instance = { {0.5f, 0.5f, 1.0f / view_width, 1.0f / view_height } }; -//TODO +struct Tile { + uint32 type; +}; +uint32 map_width = view_width; +uint32 map_height = view_height; + +Tile* map_tiles; bool LoadShaders() { @@ -123,6 +131,63 @@ bool LoadShaders() { return 0; } +void save_map() { + log("Save file is under construction."); + FILE* file = fopen("../assets/map/map.sv", "wb"); + + if (!file) { + log_error("Save file creation has failed."); + return; + } + defer(fclose(file)); + + if (fwrite(&map_width, sizeof(uint32), 1, file) != 1) { + log_error("fwrite for map_width has failed."); + return; + } + + if (fwrite(&map_height, sizeof(uint32), 1, file) != 1) { + log_error("fwrite for map_height has failed."); + return; + } + + for (int i = 0; i < map_width * map_height; i++) { + if (fwrite(&map_tiles[i], sizeof(Tile), 1, file) != 1) { + log_error("fwrite for tile_type at tile number %d has failed.", i); + return; + } + } + + log("Saving map was successful."); +} + +void load_map() { + log("Load save file."); + String file = load_entire_file("../assets/map/map.sv"); + if (!file.length) { + log_error("Loading save file has failed."); + return; + } + + map_width = read(file); + map_height = read(file); + if (file.length != map_width * map_height * sizeof(uint32)) { + log_error("Incorrect file.length."); + return; + } + + if (map_tiles) + free(map_tiles); + + map_tiles = (Tile*)malloc(map_width * map_height * sizeof(Tile)); + + for (int i = 0; i < map_width * map_height; i++) { + map_tiles[i] = read(file); + } + + log("Loading map was successful."); +} + LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { LRESULT Result = 0; @@ -156,7 +221,7 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { float tile_height = window_height / (float) view_height; int tile_x = mouse_x / tile_width; int tile_y = mouse_y / tile_height; - tiles_instances[tile_x + view_width * tile_y].tile_type = (tiles_instances[tile_x + view_width * tile_y].tile_type + 1) % 3; + map_tiles[tile_x + map_width * tile_y].type = (map_tiles[tile_x + map_width * tile_y].type + 1) % 3; } break; @@ -165,8 +230,11 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { //key_down[wParam] = true; } - if (wParam == VK_F5) { - //LoadShaders(); + if (wParam == VK_F1) { + save_map(); + } + if (wParam == VK_F4) { + load_map(); } } break; @@ -522,7 +590,7 @@ int main() { return 1; } - tiles_instances[17].tile_type = 1; + load_map(); MSG Message; while (Running) { @@ -534,6 +602,13 @@ int main() { TranslateMessage(&Message); DispatchMessage(&Message); } + + for (int y = 0 ; y < view_height ; y++) { + for (int x = 0; x < view_width; x++) { + tiles_instances[x + y * view_width].tile_type = map_tiles[x + y * map_width].type; + } + } + //Grafikkarte updaten D3D11_MAPPED_SUBRESOURCE mapped_subressource; devicecontext->Map(tiles_instance_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subressource); @@ -576,7 +651,6 @@ int main() { devicecontext->PSSetShaderResources(0, 1, &tile_texture); devicecontext->DrawIndexedInstanced(6, view_width * view_height, 0, 0, 0); - //Spieler Feld ID3D11Buffer* player_input_buffers[] = { vertex_buffer,