| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include <string>
- #include <physfs.h>
- #include <tinyxml2.h>
- #define SDL_MAIN_USE_CALLBACKS 1
- #include <SDL3/SDL.h>
- #include <SDL3/SDL_main.h>
- #include "AppState.hpp"
- #include "components/Position.hpp"
- #include "components/Sprite.hpp"
- SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
- Dungeon::AppState *state = new Dungeon::AppState;
- PHYSFS_init(argv[0]);
- PHYSFS_mount("assets", "/assets", 1);
- bool success = SDL_CreateWindowAndRenderer(
- "Penis Dungeon", 1280, 720, SDL_WINDOW_HIGH_PIXEL_DENSITY, &state->window, &state->renderer
- );
- if (!success) {
- SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
- return SDL_APP_FAILURE;
- }
- try {
- const auto boar = state->registry.create();
- const char *boar_path = "/assets/boar.xml";
- if (!PHYSFS_exists(boar_path)) throw std::string("boar.xml not found!");
- PHYSFS_file *file = PHYSFS_openRead(boar_path);
- PHYSFS_sint64 length = PHYSFS_fileLength(file);
- char *buffer = new char[length];
- PHYSFS_readBytes(file, buffer, length);
- PHYSFS_close(file);
- tinyxml2::XMLDocument doc;
- tinyxml2::XMLError error = doc.Parse(buffer);
- if (error != tinyxml2::XML_SUCCESS) {
- delete[] buffer;
- throw std::string("Failed to parse XML");
- }
- tinyxml2::XMLElement *root = doc.RootElement();
- tinyxml2::XMLElement *component = root->FirstChildElement();
- for (int i = 0; i < root->ChildElementCount(); i++) {
- if (std::string(component->Name()) == "Position") {
- Dungeon::Position pos = Dungeon::Position::FromXML(component);
- state->registry.emplace<Dungeon::Position>(boar, pos);
- } else if (std::string(component->Name()) == "Sprite") {
- Dungeon::Sprite sprite = Dungeon::Sprite::FromXML(state->renderer, component);
- state->registry.emplace<Dungeon::Sprite>(boar, sprite);
- }
- tinyxml2::XMLNode *next = component->NextSibling();
- if (next) component = next->ToElement();
- };
- delete[] buffer;
- } catch (std::string ex) {
- SDL_Log("Failed to construct 'boar' entity: %s", ex.c_str());
- }
- *appstate = state;
- return SDL_APP_CONTINUE;
- }
- SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
- Dungeon::AppState *state = static_cast<Dungeon::AppState *>(appstate);
- if (event->type == SDL_EVENT_QUIT) {
- return SDL_APP_SUCCESS;
- } else if (event->type == SDL_EVENT_KEY_DOWN) {
- switch (event->key.scancode) {
- case SDL_SCANCODE_ESCAPE:
- return SDL_APP_SUCCESS;
- break;
- default:
- break;
- }
- }
- return SDL_APP_CONTINUE;
- }
- SDL_AppResult SDL_AppIterate(void *appstate) {
- Dungeon::AppState *state = static_cast<Dungeon::AppState *>(appstate);
- int w = 0, h = 0;
- SDL_GetRenderOutputSize(state->renderer, &w, &h);
- const float scale = h / 180.0f;
- SDL_SetRenderScale(state->renderer, scale, scale);
- SDL_SetRenderDrawColor(state->renderer, 50, 200, 255, 255);
- SDL_RenderClear(state->renderer);
- state->renderingSystem.Render(state->renderer, state->registry);
- SDL_RenderPresent(state->renderer);
- return SDL_APP_CONTINUE;
- }
- void SDL_AppQuit(void *appstate, SDL_AppResult result) {
- Dungeon::AppState *state = static_cast<Dungeon::AppState *>(appstate);
- delete state;
- PHYSFS_deinit();
- }
|