Added fileCommon, added freebie font, start listening to SDL Keyboard text input

This commit is contained in:
2026-09-26 21:52:09 -05:00
parent 0c4be0ca87
commit b8d2270996
11 changed files with 312 additions and 194 deletions
+2 -2
View File
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 4.2)
project(Text-Adventure) project(Text-Adventure)
set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp) set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ui/window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Common/fileCommon.cpp)
find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared) find_package(SDL3 REQUIRED CONFIG REQUIRED COMPONENTS SDL3-shared)
find_package(SDL3_ttf REQUIRED) find_package(SDL3_ttf REQUIRED)
@@ -16,4 +16,4 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Editor)
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/) #add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/)
target_link_libraries(Text-Adventure PUBLIC Engine SDL3::SDL3 SDL3_ttf::SDL3_ttf) target_link_libraries(Text-Adventure PUBLIC Engine SDL3::SDL3 SDL3_ttf::SDL3_ttf)
target_include_directories(Text-Adventure PUBLIC ${CMAKE_SOURCE_DIR}/Extern) target_include_directories(Text-Adventure PUBLIC ${CMAKE_SOURCE_DIR}/Extern ${CMAKE_SOURCE_DIR}/ui ${CMAKE_SOURCE_DIR}/Common)
+44
View File
@@ -0,0 +1,44 @@
#include "fileCommon.h"
#include "json.hpp"
bool OpenFile(fs::path path) {
path = fs::absolute(path);
if (!fs::exists(path)) {
printf("File Read error! Could not find file at: \n");
printf("%s\n", path.c_str());
return false;
}
stream.open(absolute(path));
if (!stream) {
printf("Error opening stream \n");
return false;
}
return true;
}
nlohmann::json GetData() {
if (!stream) {
printf("Open the file first! \n");
return nlohmann::json();
}
return nlohmann::json::parse(stream);
}
bool SaveJsonFile(fs::path path, nlohmann::json j) {
if (stream) {
printf("Stream open for reading! Please close stream before saving \n");
return false;
}
stream.open(absolute(path), std::fstream::in | std::fstream::out | std::fstream::app);
stream << std::setw(4) << j << std::endl;
stream.close();
}
+14
View File
@@ -0,0 +1,14 @@
#include <filesystem>
#include <fstream>
#include "json.hpp"
namespace fs = std::filesystem;
static std::fstream stream;
extern bool OpenFile(fs::path path);
extern nlohmann::json GetData();
/// Note we have a specific function for json for pretty printing purpose
bool SaveJsonFile(fs::path path, nlohmann::json j);
// bool SaveFile(fs::path path, void *dataToSave); // Leaving it commented out until later
inline void CloseFileStream() { stream.close(); }
+1 -1
View File
@@ -12,4 +12,4 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/room.cpp
add_library(Engine SHARED ${SOURCES}) add_library(Engine SHARED ${SOURCES})
target_include_directories(Engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/headers) target_include_directories(Engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/headers)
target_include_directories(Engine PUBLIC ${CMAKE_SOURCE_DIR}/Extern) target_include_directories(Engine PUBLIC ${CMAKE_SOURCE_DIR}/Extern ${CMAKE_SOURCE_DIR}/Common)
+3 -3
View File
@@ -50,10 +50,10 @@ int CommandParser::Parse() {
FUNCTION toRun; FUNCTION toRun;
/*
printf("Please Enter a command \n"); printf("Please Enter a command \n");
printf("> "); printf("> ");
getline(std::cin, cmd);
std::cin.clear();
cmd[0] = toupper(cmd[0]); cmd[0] = toupper(cmd[0]);
std::stringstream c(cmd); std::stringstream c(cmd);
@@ -82,7 +82,7 @@ int CommandParser::Parse() {
nouns.clear(); nouns.clear();
c.clear(); c.clear();
toRun = nullptr; toRun = nullptr;*/
return 0; return 0;
} }
+8 -16
View File
@@ -1,7 +1,7 @@
#include "game.h" #include "game.h"
#include <cstdio> #include <cstdio>
#include <filesystem> #include <filesystem>
#include <fstream> #include "fileCommon.h"
#include "player.h" #include "player.h"
std::vector<std::vector<Room>> rooms; std::vector<std::vector<Room>> rooms;
@@ -21,32 +21,24 @@ bool Game::Init() {
rooms[i].resize(MAP_WIDTH); rooms[i].resize(MAP_WIDTH);
} }
filePath = fs::absolute(filePath);
isActive = OpenFile(filePath);
if (!fs::exists(filePath)) { json = GetData();
printf("File Read error! Could not find Json at: \n"); isActive = (json != NULL);
printf("%s", filePath.c_str());
isActive = false;
return false;
}
// Read rooms into buffer
stream.open(absolute(filePath));
json = nlohmann::json::parse(stream);
rooms = json.get<std::vector<std::vector<Room>>>(); rooms = json.get<std::vector<std::vector<Room>>>();
player.Init(); player.Init();
parser = CommandParser();
CloseFileStream(); // We're done with it. Let's make sure it's closed!
parser = CommandParser();
parser.AddCommand("Exit", Quit); parser.AddCommand("Exit", Quit);
parser.AddCommand("Move", Move); parser.AddCommand("Move", Move);
parser.AddCommand("Look", Look); parser.AddCommand("Look", Look);
ChangeRoom(rooms[0][0]); ChangeRoom(rooms[0][0]);
return true; return isActive;
} }
Binary file not shown.
+53 -47
View File
@@ -1,16 +1,21 @@
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_keyboard.h>
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_log.h>
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_init.h> #include <SDL3/SDL_init.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
#include <SDL3/SDL_render.h> #include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
#include <SDL3_ttf/SDL_ttf.h> #include <SDL3_ttf/SDL_ttf.h>
#include "fileCommon.h"
#include "game.h" #include "game.h"
#include "window.h"
/* We will use this renderer to draw into this window every frame. */ Game game;
static SDL_Window *window = nullptr; Window window;
static SDL_Renderer *renderer = nullptr;
static int width = 800, height = 600;
static Game game;
void EditRooms() { void EditRooms() {
@@ -47,78 +52,79 @@ void EditRooms() {
{{3, "Waterfall", "A fine waterfall on a cliff", {"Waterfall"}, ObjectType::SCENERY, false, 3}}}}}; {{3, "Waterfall", "A fine waterfall on a cliff", {"Waterfall"}, ObjectType::SCENERY, false, 3}}}}};
nlohmann::json j(roomDat); nlohmann::json j(roomDat);
SaveJsonFile(path, j);
std::fstream outFile(absolute(path), std::fstream::in | std::fstream::out | std::fstream::app);
outFile << std::setw(4) << j << std::endl;
outFile.close();
} }
int Init() SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
{
if (SDL_Init(SDL_INIT_VIDEO) == false) { if (SDL_Init(SDL_INIT_VIDEO) == false) {
SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError()); SDL_Log("SDL could not initialize! SDL error: %s\n", SDL_GetError());
} }
TTF_Init(); SDL_AppResult result = window.CreateWindow(600, 800);
window = SDL_CreateWindow("Text Adventure", width, height, 0); if (result != SDL_APP_CONTINUE) {
return result;
if (window == nullptr) {
SDL_Log("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
return -1;
} }
renderer = SDL_CreateRenderer(window, nullptr); if (!TTF_Init()) {
if (renderer == nullptr) { SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
SDL_Log("SDL_CreateRenderer Error: %s\n", SDL_GetError()); return SDL_APP_FAILURE;
SDL_Quit();
return -1;
} }
if (!game) { if (!game) {
game.Init(); game.Init();
Game::SetGameInstance(&game); Game::SetGameInstance(&game);
} game.SetIsActive(true);
else { SDL_StartTextInput(window.GetWindow());
} else {
printf("Game Initalization failed! \n"); printf("Game Initalization failed! \n");
return -1; return SDL_APP_FAILURE;
} }
return 1; 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. */
}
int main(int argc, char *argv[]) { if (event->type == SDL_EVENT_KEY_DOWN) {
if (event->key.key == SDLK_ESCAPE) {
SDL_Event e; SDL_StopTextInput(window.GetWindow());
game.SetIsActive(Init());
while (game.GetIsActive()) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
game.SetIsActive(false); game.SetIsActive(false);
} }
if (event->key.key == SDLK_RETURN) {
SDL_StopTextInput(window.GetWindow());
}
} }
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set render draw color to black if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
SDL_RenderClear(renderer); // Clear the renderer window.Resize(event->window.data1, event->window.data2);
}
SDL_RenderPresent(renderer); // Render the screen 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
SDL_RenderPresent(window.GetRenderer()); // Render the screen
// game.Run(); // game.Run();
}
return 0;
} }
return SDL_APP_CONTINUE;
}
void Close() void SDL_AppQuit(void *appstate, SDL_AppResult result) {
{ if (result == SDL_APP_SUCCESS) {
SDL_DestroyRenderer(renderer); window.Close();
SDL_DestroyWindow(window);
SDL_Quit(); SDL_Quit();
} }
}
+40
View File
@@ -0,0 +1,40 @@
#include "window.h"
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
SDL_AppResult Window::CreateWindow(int height, int width) {
w = width;
h = height;
SDL_SetHint(SDL_HINT_MAIN_CALLBACK_RATE, "60");
window = SDL_CreateWindow("Text Adventure", width, height, SDL_WINDOW_RESIZABLE);
if (window == nullptr) {
SDL_Log("SDL_CreateWindow Error: %s\n", SDL_GetError());
SDL_Quit();
return SDL_APP_FAILURE;
}
renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr) {
SDL_Log("SDL_CreateRenderer Error: %s\n", SDL_GetError());
SDL_Quit();
return SDL_APP_FAILURE;
}
return SDL_APP_CONTINUE;
}
void Window::Resize(int width, int height) {
w = width;
h = height;
SDL_SetWindowSize(window, w, h);
}
void Window::Close() {
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
}
+22
View File
@@ -0,0 +1,22 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
class Window {
private:
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
int w, h;
public:
SDL_AppResult CreateWindow(int w, int h);
void Resize(int w, int h);
void Close();
SDL_Window *GetWindow() { return window; }
SDL_Renderer *GetRenderer() { return renderer; }
};
extern Window window;