diff --git a/assets/map/map.sv b/assets/map/map.sv index 174f27f..1076f61 100644 Binary files a/assets/map/map.sv and b/assets/map/map.sv differ diff --git a/assets/tile_empty.tga b/assets/tile_empty.tga new file mode 100644 index 0000000..6990404 Binary files /dev/null and b/assets/tile_empty.tga differ diff --git a/assets/tile_error.tga b/assets/tile_error.tga new file mode 100644 index 0000000..36d77ce Binary files /dev/null and b/assets/tile_error.tga differ diff --git a/bin/pokemon.exe b/bin/pokemon.exe index 21e4ed2..e692e3a 100644 Binary files a/bin/pokemon.exe and b/bin/pokemon.exe differ diff --git a/bin/pokemon.pdb b/bin/pokemon.pdb index caf7bb1..985840b 100644 Binary files a/bin/pokemon.pdb and b/bin/pokemon.pdb differ diff --git a/src/main.cpp b/src/main.cpp index 65ac3fd..4e03a23 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,6 +99,16 @@ uint32 map_height = view_height; Tile* map_tiles; +struct Player { + int64 pos_x; + int64 pos_y; +}; + +Player player = { + .pos_x = 8, + .pos_y = 6, +}; + bool LoadShaders() { ID3DBlob* error_msgs = 0; @@ -188,6 +198,82 @@ void load_map() { log("Loading map was successful."); } +void change_map_size(char direction, int amount) { + + Tile* old_map = map_tiles; + auto old_map_width = map_width; + auto old_map_height = map_height; + + switch (direction) { + case 'W': { + map_width += 1; + map_tiles = (Tile*)malloc(map_width * map_height * sizeof(Tile)); + for (int y = 0; y < old_map_height; y++) { + for (int x = 0; x < old_map_width; x++) { + map_tiles[y * map_width + x + 1] = old_map[y * old_map_width + x]; + } + } + + for (int y = 0; y < map_height; y++) { + map_tiles[y * map_width + 0].type = 0; + } + + log("Increased map size: WEST"); + } break; + + case 'E': { + map_width += 1; + map_tiles = (Tile*)malloc(map_width * map_height * sizeof(Tile)); + for (int y = 0; y < old_map_height; y++) { + for (int x = 0; x < old_map_width; x++) { + map_tiles[y * map_width + x] = old_map[y * old_map_width + x]; + } + } + + for (int y = 0; y < map_height; y++) { + map_tiles[y * map_width + old_map_width].type = 0; + } + + log("Increased map size: EAST"); + } break; + + case 'N': { + map_height += 1; + map_tiles = (Tile*)malloc(map_width * map_height * sizeof(Tile)); + for (int y = 0; y < old_map_height; y++) { + for (int x = 0; x < old_map_width; x++) { + map_tiles[(y + 1) * map_width + x] = old_map[y * old_map_width + x]; + } + } + + for (int x = 0; x < map_width; x++) { + map_tiles[0 * map_width + x].type = 0; + } + + log("Increased map size: NORTH"); + } break; + + case 'S': { + map_height += 1; + map_tiles = (Tile*)malloc(map_width * map_height * sizeof(Tile)); + for (int y = 0; y < old_map_height; y++) { + for (int x = 0; x < old_map_width; x++) { + map_tiles[y * map_width + x] = old_map[y * old_map_width + x]; + } + } + + for (int x = 0; x < map_width; x++) { + map_tiles[old_map_height * map_width + x].type = 0; + } + + log("Increased map size: SOUTH"); + }break; + } + + free(old_map); +} + + LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { LRESULT Result = 0; @@ -219,10 +305,39 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { int16 mouse_y = (int16)((lParam & 0xffff0000) >> 16); float tile_width = window_width / (float) view_width; float tile_height = window_height / (float) view_height; - int tile_x = mouse_x / tile_width; - int tile_y = mouse_y / tile_height; - map_tiles[tile_x + map_width * tile_y].type = (map_tiles[tile_x + map_width * tile_y].type + 1) % 3; + int tile_x = (int)(mouse_x / tile_width) + player.pos_x - (view_width / 2); + int tile_y = (int)(mouse_y / tile_height) + player.pos_y - (view_height / 2); + if(0 <= tile_x && tile_x < map_width && + 0 <= tile_y && tile_y < map_height) + map_tiles[tile_x + map_width * tile_y].type = (map_tiles[tile_x + map_width * tile_y].type + 1) % 4; + if (wParam & MK_SHIFT && tile_x == -1) { + if(wParam & MK_CONTROL) + change_map_size('W', -1); + else + change_map_size('W', 1); + } + + if (wParam & MK_SHIFT && tile_x == map_width) { + if (wParam & MK_CONTROL) + change_map_size('E', -1); + else + change_map_size('E', 1); + } + + if (wParam & MK_SHIFT && tile_y == -1) { + if (wParam & MK_CONTROL) + change_map_size('N', -1); + else + change_map_size('N', 1); + } + + if (wParam & MK_SHIFT && tile_y == map_height) { + if (wParam & MK_CONTROL) + change_map_size('S', -1); + else + change_map_size('S', 1); + } } break; case WM_KEYDOWN: { @@ -230,6 +345,19 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { //key_down[wParam] = true; } + if (wParam == VK_UP || wParam == 'W') { + player.pos_y = clamp(0, player.pos_y - 1, map_height - 1); + } + if (wParam == VK_LEFT || wParam == 'A') { + player.pos_x = clamp(0, player.pos_x - 1, map_width - 1); + } + if (wParam == VK_DOWN || wParam == 'S') { + player.pos_y = clamp(0, player.pos_y + 1, map_height - 1); + } + if (wParam == VK_RIGHT || wParam == 'D') { + player.pos_x = clamp(0, player.pos_x + 1, map_width - 1); + } + if (wParam == VK_F1) { save_map(); } @@ -584,8 +712,8 @@ int main() { } //mehrere Texturen erzeugen - char* tile_texture_array[] = { "../assets/tile_grass_2.tga" , "../assets/tile_dirt.tga" , "../assets/tile_water.tga" }; - if (!(tile_texture = create_shader_texture_array(3, tile_texture_array, "tile_textures"))) { + char* tile_texture_array[] = { "../assets/tile_empty.tga", "../assets/tile_grass_2.tga" , "../assets/tile_dirt.tga" , "../assets/tile_water.tga", "../assets/tile_error.tga" }; + if (!(tile_texture = create_shader_texture_array(sizeof(tile_texture_array) / sizeof(*tile_texture_array), tile_texture_array, "tile_textures"))) { log_error("CreateShaderTextrueArray has failed."); return 1; } @@ -603,9 +731,15 @@ int main() { DispatchMessage(&Message); } - for (int y = 0 ; y < view_height ; y++) { + //Tiles updaten + 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; + if (x + player.pos_x - view_width / 2 >= 0 && x + player.pos_x - view_width / 2 < map_width && + y + player.pos_y - view_height / 2 >= 0 && y + player.pos_y - view_height / 2 < map_height) { + tiles_instances[x + y * view_width].tile_type = map_tiles[(x + player.pos_x - view_width / 2) + (y + player.pos_y - view_height / 2) * map_width].type; + } else { + tiles_instances[x + y * view_width].tile_type = 4; + } } } diff --git a/src/math_graphics.h b/src/math_graphics.h index daa1534..bb5e358 100644 --- a/src/math_graphics.h +++ b/src/math_graphics.h @@ -26,6 +26,15 @@ constexpr inline float clamp01(float a) { return clamp(0, a, 1); } +//clamp für Integer +constexpr inline int64_t clamp(int64_t min, int64_t a, int64_t max) { + int64_t result = a; + if (a < min) + result = min; + if (a > max) + result = max; + return result; +} //wurzelberechnung inline float square_root(float a) {