159 lines
4.6 KiB
C++
159 lines
4.6 KiB
C++
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
|
|
|
|
#include <SDL3/SDL.h>
|
|
#include <SDL3/SDL_events.h>
|
|
#include <SDL3/SDL_init.h>
|
|
#include <SDL3/SDL_keyboard.h>
|
|
#include <SDL3/SDL_keycode.h>
|
|
#include <SDL3/SDL_log.h>
|
|
#include <SDL3/SDL_main.h>
|
|
#include <SDL3/SDL_render.h>
|
|
#include <SDL3/SDL_video.h>
|
|
#include <SDL3_ttf/SDL_ttf.h>
|
|
#include "fileCommon.h"
|
|
#include "game.h"
|
|
#include "window.h"
|
|
|
|
Game game;
|
|
Window window;
|
|
TTF_Font *font = nullptr;
|
|
SDL_Texture *font_texture = nullptr;
|
|
|
|
fs::path fontPath("Resources/Pixelzone-E4MV4.ttf");
|
|
|
|
|
|
void EditRooms() {
|
|
|
|
fs::path path("Resources/rooms.json");
|
|
|
|
std::vector<std::vector<Room>> roomDat = {
|
|
{
|
|
{
|
|
0,
|
|
"Test Room 1",
|
|
"First Test Room",
|
|
{{0, S, false}, {1, E, false}},
|
|
{
|
|
{0, "Rock", "A Grey Rock", {"Rock"}, ObjectType::SCENERY, false, 0},
|
|
},
|
|
},
|
|
{1,
|
|
"Test Room 2",
|
|
"A court yard",
|
|
{{2, W, false}, {3, E, false}, {4, S, false}},
|
|
{{1,
|
|
"Tree",
|
|
"A fine looking Tree stands in the courtyard",
|
|
{"Tree"},
|
|
ObjectType::SCENERY,
|
|
false,
|
|
1}}},
|
|
|
|
},
|
|
{{3,
|
|
"Test Room 3",
|
|
"You can hear the sound of a waterfall nearby",
|
|
{{5, N, false}, {6, E, false}},
|
|
{{3, "Waterfall", "A fine waterfall on a cliff", {"Waterfall"}, ObjectType::SCENERY, false, 3}}}}};
|
|
|
|
nlohmann::json j(roomDat);
|
|
SaveJsonFile(path, j);
|
|
}
|
|
|
|
|
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
|
SDL_Surface *text;
|
|
SDL_Color color = {255, 255, 255, SDL_ALPHA_OPAQUE};
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) == false) {
|
|
SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError());
|
|
}
|
|
|
|
SDL_AppResult result = window.CreateWindow(600, 800);
|
|
|
|
if (result != SDL_APP_CONTINUE) {
|
|
return result;
|
|
}
|
|
|
|
if (!TTF_Init()) {
|
|
SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
font = TTF_OpenFont(fs::absolute(fontPath).c_str(), 12.0f);
|
|
text = TTF_RenderText_Blended(font, "Hello World!", 0, color);
|
|
if (text) {
|
|
font_texture = SDL_CreateTextureFromSurface(window.GetRenderer(), text);
|
|
SDL_DestroySurface(text);
|
|
}
|
|
if (!font_texture) {
|
|
SDL_Log("Couldn't create text: %s\n", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
if (!game) {
|
|
game.Init();
|
|
Game::SetGameInstance(&game);
|
|
game.SetIsActive(true);
|
|
SDL_StartTextInput(window.GetWindow());
|
|
} else {
|
|
printf("Game Initalization failed! \n");
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) {
|
|
if (event->type == SDL_EVENT_QUIT || !game.GetIsActive()) {
|
|
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
|
}
|
|
|
|
if (event->type == SDL_EVENT_KEY_DOWN) {
|
|
if (event->key.key == SDLK_ESCAPE) {
|
|
SDL_StopTextInput(window.GetWindow());
|
|
game.SetIsActive(false);
|
|
}
|
|
|
|
if (event->key.key == SDLK_RETURN) {
|
|
SDL_StopTextInput(window.GetWindow());
|
|
}
|
|
}
|
|
|
|
if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
|
|
window.Resize(event->window.data1, event->window.data2);
|
|
}
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
SDL_AppResult SDL_AppIterate(void *appstate) {
|
|
if (game.GetIsActive()) {
|
|
|
|
SDL_SetRenderDrawColor(window.GetRenderer(), 0, 0, 0, 255); // Set render draw color to black
|
|
SDL_RenderClear(window.GetRenderer()); // Clear the renderer
|
|
int w = 0, h = 0;
|
|
SDL_FRect dst;
|
|
const float scale = 4.0f;
|
|
|
|
/* Center the text and scale it up */
|
|
SDL_GetRenderOutputSize(window.GetRenderer(), &w, &h);
|
|
SDL_SetRenderScale(window.GetRenderer(), scale, scale);
|
|
SDL_GetTextureSize(font_texture, &dst.w, &dst.h);
|
|
SDL_RenderTexture(window.GetRenderer(), font_texture, NULL, &dst);
|
|
SDL_RenderPresent(window.GetRenderer()); // Render the screen
|
|
|
|
// game.Run();
|
|
}
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
|
if (result == SDL_APP_SUCCESS) {
|
|
window.Close();
|
|
TTF_CloseFont(font);
|
|
SDL_Quit();
|
|
}
|
|
}
|