Dungeon.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <string>
  2. #include <physfs.h>
  3. #include <tinyxml2.h>
  4. #define SDL_MAIN_USE_CALLBACKS 1
  5. #include <SDL3/SDL.h>
  6. #include <SDL3/SDL_main.h>
  7. #include "AppState.hpp"
  8. #include "components/Position.hpp"
  9. #include "components/Sprite.hpp"
  10. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
  11. Dungeon::AppState *state = new Dungeon::AppState;
  12. PHYSFS_init(argv[0]);
  13. PHYSFS_mount("assets", "/assets", 1);
  14. bool success = SDL_CreateWindowAndRenderer(
  15. "Penis Dungeon", 1280, 720, SDL_WINDOW_HIGH_PIXEL_DENSITY, &state->window, &state->renderer
  16. );
  17. if (!success) {
  18. SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
  19. return SDL_APP_FAILURE;
  20. }
  21. try {
  22. const auto boar = state->registry.create();
  23. const char *boar_path = "/assets/boar.xml";
  24. if (!PHYSFS_exists(boar_path)) throw std::string("boar.xml not found!");
  25. PHYSFS_file *file = PHYSFS_openRead(boar_path);
  26. PHYSFS_sint64 length = PHYSFS_fileLength(file);
  27. char *buffer = new char[length];
  28. PHYSFS_readBytes(file, buffer, length);
  29. PHYSFS_close(file);
  30. tinyxml2::XMLDocument doc;
  31. tinyxml2::XMLError error = doc.Parse(buffer);
  32. if (error != tinyxml2::XML_SUCCESS) {
  33. delete[] buffer;
  34. throw std::string("Failed to parse XML");
  35. }
  36. tinyxml2::XMLElement *root = doc.RootElement();
  37. tinyxml2::XMLElement *component = root->FirstChildElement();
  38. for (int i = 0; i < root->ChildElementCount(); i++) {
  39. if (std::string(component->Name()) == "Position") {
  40. Dungeon::Position pos = Dungeon::Position::FromXML(component);
  41. state->registry.emplace<Dungeon::Position>(boar, pos);
  42. } else if (std::string(component->Name()) == "Sprite") {
  43. Dungeon::Sprite sprite = Dungeon::Sprite::FromXML(state->renderer, component);
  44. state->registry.emplace<Dungeon::Sprite>(boar, sprite);
  45. }
  46. tinyxml2::XMLNode *next = component->NextSibling();
  47. if (next) component = next->ToElement();
  48. };
  49. delete[] buffer;
  50. } catch (std::string ex) {
  51. SDL_Log("Failed to construct 'boar' entity: %s", ex.c_str());
  52. }
  53. *appstate = state;
  54. return SDL_APP_CONTINUE;
  55. }
  56. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
  57. Dungeon::AppState *state = static_cast<Dungeon::AppState *>(appstate);
  58. if (event->type == SDL_EVENT_QUIT) {
  59. return SDL_APP_SUCCESS;
  60. } else if (event->type == SDL_EVENT_KEY_DOWN) {
  61. switch (event->key.scancode) {
  62. case SDL_SCANCODE_ESCAPE:
  63. return SDL_APP_SUCCESS;
  64. break;
  65. default:
  66. break;
  67. }
  68. }
  69. return SDL_APP_CONTINUE;
  70. }
  71. SDL_AppResult SDL_AppIterate(void *appstate) {
  72. Dungeon::AppState *state = static_cast<Dungeon::AppState *>(appstate);
  73. int w = 0, h = 0;
  74. SDL_GetRenderOutputSize(state->renderer, &w, &h);
  75. const float scale = h / 180.0f;
  76. SDL_SetRenderScale(state->renderer, scale, scale);
  77. SDL_SetRenderDrawColor(state->renderer, 50, 200, 255, 255);
  78. SDL_RenderClear(state->renderer);
  79. state->renderingSystem.Render(state->renderer, state->registry);
  80. SDL_RenderPresent(state->renderer);
  81. return SDL_APP_CONTINUE;
  82. }
  83. void SDL_AppQuit(void *appstate, SDL_AppResult result) {
  84. Dungeon::AppState *state = static_cast<Dungeon::AppState *>(appstate);
  85. delete state;
  86. PHYSFS_deinit();
  87. }