Untitled

 avatar
unknown
plain_text
9 months ago
5.1 kB
12
Indexable
#pragma once
#include <iostream>
#include "engine/data_types/IscActor.h"
#include <string>
#include "ButtonProperties.h"
#include <tuple>
#include <SDL3/SDL.h>
#include <filesystem>
#include <SDL3_image/SDL_image.h>
#include <nlohmann/json.hpp>
#include <SDL3_ttf/SDL_ttf.h>
#include "StoryCard.h"
#include <imgui.h>
#include <sol/sol.hpp>
#include "engine/data_types/IscDialogue.h"
#include <thread>
#include "engine/data_types/IscEvent.h"
#include <variant>
#include <list>
#include <vector>
#include "engine/data_types/IscAudio.h"
#include "engine/data_types/IscVar.h"

using json = nlohmann::json;

struct IscActor;

struct StoryMainMenu {
    std::tuple<SDL_Texture*, SDL_FRect, std::string> storyName{};
    ButtonProperties buttonProperties{};
    SDL_Texture* background{};
    bool initialized = false;
    bool runScripts = false;

    StoryMainMenu(ButtonProperties buttonProperties_, std::string storyName_, std::string backgroundPath, SDL_Renderer* renderer):buttonProperties(buttonProperties_) {
        char* cwd = SDL_GetCurrentDirectory();
        std::filesystem::path fontPath = std::filesystem::path(cwd)/"Isc/fonts/PatrickHand-VMAl.ttf";
        TTF_Font* font = TTF_OpenFont(fontPath.string().c_str(), 35.0f);
        SDL_free(cwd);

        std::get<2>(storyName) = storyName_;

        SDL_Surface* storyNameSurf = TTF_RenderText_Solid(font, std::get<2>(storyName).c_str(), 0, SDL_Color{255, 255, 255, 255});
        std::get<0>(storyName) = SDL_CreateTextureFromSurface(renderer, storyNameSurf);
        SDL_DestroySurface(storyNameSurf);
        std::get<1>(storyName) = SDL_FRect{0.0f, 20.0f, static_cast<float>(std::get<0>(storyName)->w), static_cast<float>(std::get<0>(storyName)->h)};

        if (std::filesystem::exists(backgroundPath)) {
            SDL_Surface* img = IMG_Load(backgroundPath.c_str());
            background = SDL_CreateTextureFromSurface(renderer, img);
            SDL_DestroySurface(img);
        }

        TTF_CloseFont(font);
    };

    StoryMainMenu() = default;

    void Draw(SDL_Renderer* renderer, SDL_Window* window, SDL_Color& screenColor) {
        std::get<1>(storyName).x = (static_cast<float>(SDL_GetWindowSurface(window)->w)-std::get<1>(storyName).w)/2;

        if (background != NULL) {
            SDL_FRect backgroundRect = SDL_FRect{0.0f, 0.0f, static_cast<float>(SDL_GetWindowSurface(window)->w), static_cast<float>(SDL_GetWindowSurface(window)->h)};
            SDL_RenderTexture(renderer, background, NULL, &backgroundRect);
        }

        SDL_RenderTexture(renderer, std::get<0>(storyName), NULL, &std::get<1>(storyName));

        ImGui::SetCursorPos(ImVec2(
            (static_cast<float>(SDL_GetWindowSurface(window)->w)-120.0f)/2,
            (static_cast<float>(SDL_GetWindowSurface(window)->h)-40.0f)-20
        ));
        ImGui::PushStyleColor(ImGuiCol_Button, IM_COL32(
            buttonProperties.color.r,
            buttonProperties.color.g,
            buttonProperties.color.b,
            buttonProperties.color.a
        ));
        ImGui::PushStyleColor(ImGuiCol_ButtonActive, IM_COL32(
            buttonProperties.color.r,
            buttonProperties.color.g,
            buttonProperties.color.b,
            buttonProperties.color.a
        ));
        ImGui::PushStyleColor(ImGuiCol_ButtonHovered, IM_COL32(
            buttonProperties.color.r,
            buttonProperties.color.g,
            buttonProperties.color.b,
            buttonProperties.color.a
        ));
        if (ImGui::Button(buttonProperties.text.c_str(), ImVec2(120.0f, 40.0f))) {
            Destroy();
            screenColor = SDL_Color{0, 0, 0, 255};
        }
        ImGui::PopStyleColor(3);
    }

    void Destroy() {
        SDL_DestroyTexture(std::get<0>(storyName));
        initialized = false;
        runScripts = true;
        if (background != NULL) SDL_DestroyTexture(background);
    }
};

class Story {
    public:
        std::string m_filename;
        StoryMainMenu m_storyMainMenu;
        SDL_FRect m_dialogueBoxRect = SDL_FRect{0.0f, 0.0f, 600.0f, 140.0f};
        SDL_Renderer* m_renderer;
        SDL_Window* m_window;
        sol::state m_lua;

        //Story vars to contain user events, images, sound, etc
        json m_storyProperties = {
            {"showDialogueBox", true}
        };

        IscDialogue m_dialogueHandler;
        std::list<IscEvent> m_events;
        std::list<IscActor> m_actors;
        SDL_Texture* m_background;
        std::list<IscAudio> m_audio;
        std::list<IscVar> m_vars;


        //store lua functions here to call later
        std::vector<sol::function> m_eventFunctions;

    public:
        Story(std::string filename, json data, SDL_Renderer* renderer, SDL_Window* window);
        Story() = default;
        void Event(SDL_Event& event);
        void RunScripts();
        void MakeFuncAvailableToLua();
        void Draw(SDL_Window* window, SDL_Color& screenColor);
        void Destroy();
};
Editor is loading...
Leave a Comment