added load und save function + map folder;

new Tile type for map

git-svn-id: svn://ammerhai.com/home/mike/pokemon_repo@8 24008968-59e6-ed4c-a10b-0b2c954b24ab
This commit is contained in:
mikeb 2021-02-28 11:27:29 +00:00
parent 5ccc22deae
commit f489f4e3ee
5 changed files with 80 additions and 6 deletions

BIN
assets/map/map.sv Normal file

Binary file not shown.

BIN
assets/map/map_save_file Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -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<uint32>(file);
map_height = read<uint32>(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<Tile>(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,