naval_swarm/src/init.cpp
2025-02-18 15:23:34 +01:00

51 lines
1.4 KiB
C++

#include "init.hpp"
#include <cstdlib>
#include <fstream>
#include <nlohmann/json.hpp>
#include <ostream>
#include <string>
#include <csignal>
#include "main.hpp"
#include <iostream>
#include <cstdlib>
#include "color.h"
using json = nlohmann::json;
void init_entity(Entity &entity, SDL_Renderer *renderer,std::string type, std::string name) {
json entities;
try {
std::ifstream entitiesF("./assets/entities.json");
entities = json::parse(entitiesF);
} catch (const json::parse_error &e) {
std::cerr << "Parse error: " << e.what() << "\n";
exit(1);
} catch (const std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
exit(2);
}
loaded_entities.push_back(&entity);
std::clog << entities[type];
entity.texture = IMG_LoadTexture(renderer, entities[type][name]["texture"].get<std::string>().c_str());
if(entity.texture == NULL) {
std::cerr << "\n" << RED"[ERROR]" << RESET" failed to load texture\n";
exit(ENOENT);
}
entity.width = entities[type][name]["width"].get<unsigned int>();
entity.height = entities[type][name]["height"].get<unsigned int>();
entity.srcRect = {0, 0, static_cast<int>(entity.width),
static_cast<int>(entity.height)};
entity.set_central_offset(static_cast<float>(entity.width) / 2, static_cast<float>(entity.height) / 2);
entity.speed = entities[type][name]["speed"].get<float>();
}