diff --git a/src/main.cpp b/src/main.cpp index e95e39a..7a91d0c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,20 +2,19 @@ #include #include "world.h" -const int SCREEN_HEIGHT = 320; -const int SCREEN_WIDTH = 320; - -int main() { - InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Test game"); - - SetTargetFPS(60); - +int main() { create_world_with(1./10.); World& world = get_world(); + Vector2i window_size = get_map_size("assets/map"); + + InitWindow(window_size.x, window_size.y, "Zoomba"); + world.set_size(window_size); load_world(world,"assets/map"); + + SetTargetFPS(60); world.setup(); while (WindowShouldClose() == false) { diff --git a/src/maploader.cpp b/src/maploader.cpp index 582a901..8a96778 100644 --- a/src/maploader.cpp +++ b/src/maploader.cpp @@ -1,5 +1,4 @@ #include -#include #include "components.h" #include "world.h" @@ -10,19 +9,10 @@ void load_world(World& world,const char* path) { int x = 0; int y = 0; - int max_x = 0; - int max_y = 0; - while(in.eof() == false) { char c; - in.get(c); - - - if (max_x < x) - max_x = x; - if (max_y < y) - max_y = y; + in.get(c); switch (c) { @@ -57,7 +47,37 @@ void load_world(World& world,const char* path) { } - world.height = CELL_SIZE * (max_y-1); - world.width = CELL_SIZE * (max_x-1); } +Vector2i get_map_size(const char* path){ + std::ifstream in; + in.open(path); + + int x = 0; + int y = 0; + + int max_x = 0; + int max_y = 0; + + while(in.eof() == false) + { + char c; + in.get(c); + + + if (max_x < x) + max_x = x; + if (max_y < y) + max_y = y; + + if(c == '\n'){ + x = 0; + y++; + } + else { + x++; + } + } + + return {max_x * CELL_SIZE,max_y * CELL_SIZE}; +} diff --git a/src/world.cpp b/src/world.cpp index 22e6eaf..a362629 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -91,7 +91,10 @@ void World::update_grid() { } } - +void World::set_size(Vector2i size){ + this->width = size.x; + this->height = size.y; +} void create_world_with(float seconds_per_tick){ get_world().seconds_per_tick = seconds_per_tick; diff --git a/src/world.h b/src/world.h index da7529f..3504dba 100644 --- a/src/world.h +++ b/src/world.h @@ -11,6 +11,11 @@ const int GRID_COLUMNS = 20; const int GRID_CAPACITY = GRID_COLUMNS*GRID_ROWS; const int CELL_SIZE = 16; +struct Vector2i{ + int x; + int y; +}; + /// Class that holds information about game world class World { public: @@ -32,6 +37,7 @@ class World { void draw() const; /// Should be called at the end of frame. /// Calls draw() on every entity + void set_size(Vector2i); private: float clock; void update_grid(); @@ -40,6 +46,7 @@ class World { void create_world_with(float seconds_per_tick); World& get_world(); // Thanks, 2ndbeam, helps a lot int indexify_position(Vector2 vector); +Vector2i get_map_size(const char* path); void load_world(World& world, const char* path); #endif