Nimbin[12]?SDL & Graphics / mapcreator / src/map/map.hpp

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gz
src/map/map.hpp 2.8 KB · 100 lines raw
#pragma once

#include <SDL3/SDL.h>

#include <unordered_map>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>

#include "defaults/defaults.hpp"
#include "map/tile.hpp"
#include "defaults/input.hpp"
#include "character/character.hpp"
#include "character/enemy.hpp"

class Game;


typedef struct {
    Tile_s background;
    std::vector<Tile_s> overlays;
    std::vector<TileObject> objects;
    SDL_Point position;
} TileMap;

namespace Map {
    extern std::string save_path;
    extern std::string save_collission_path;

    extern bool setting_offset;
    extern SDL_Point offset_to_set;
    extern SDL_Point offset;
    extern SDL_Point size;
    extern SDL_Point boundaries; // JEee fucked up the size, 
                                 // to much work to resize (i have an outer cage which fucks the path finding)

    extern int   frame_water;
    extern float frame_water_time;
    extern float frame_water_time_max;
    extern int   frame_meadow;
    extern float frame_meadow_time;
    extern float frame_meadow_time_max;

    extern std::vector<TileMap> tile_map;
    extern std::vector<bool> tile_collission_map;

    void Init();
    void init_Collider();
    void set_CollissionFromTileset();

    void save_MapTiles(const std::vector<TileMap>& tile_map, const std::string& path);
    std::vector<TileMap> load_MapTiles(const std::string& path);

    void draw_MapTiles(Game*);
    void draw_SelectionBorder(SDL_Renderer* renderer, Input& input);
    void draw_ObjectTiles(SDL_Renderer* renderer, Character& player, SDL_Rect win_size);

    void Reset(); // <- thats not doing what it should..

    void save_TileCollision(const std::string& path);
    void load_TileCollision(const std::string& path);



    void set_Offset(Input& input);
    void set_Collission(Input& input);
    void set_CollissionForTile(SDL_Point tile);
    void set_ObjectCollisionOnTilemap(size_t object_id, SDL_Point map_tile);
    void set_TileCollissionForObjects(SDL_Point map_tile);

    std::vector<TileObject>&
    get_ObjectsFromMapTile(SDL_Point tile_point);

    inline SDL_Point
    get_TileFromXy(SDL_FPoint point) {
        return {
            static_cast<int>((point.x) / Tile::tile_size),
            static_cast<int>((point.y) / Tile::tile_size)
        };
    }
    inline size_t
    get_IdFromTilePoint(SDL_Point point) {
        if (point.x < 0) point.x = 0;
        if (point.y < 0) point.y = 0;
        if (point.x >= Map::size.x) point.x = Map::size.x-1;
        if (point.y >= Map::size.y) point.y = Map::size.y-1;
        size_t id =  static_cast<size_t>(point.y * static_cast<int>(Map::size.x) + point.x);
        return id > static_cast<size_t>(Map::size.x*Map::size.y-1) ? SIZE_MAX : id;
    }

    void
    place_ConstructionSide();

    bool
    is_FreePosition(SDL_FRect candidate_rect);

    SDL_FPoint
    get_ClosestPointWithoutCollision(const SDL_FRect &rect);
};