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)
+27 -27
View File
@@ -50,39 +50,39 @@ int CommandParser::Parse() {
FUNCTION toRun; FUNCTION toRun;
printf("Please Enter a command \n"); /*
printf("> "); printf("Please Enter a command \n");
getline(std::cin, cmd); printf("> ");
std::cin.clear();
cmd[0] = toupper(cmd[0]);
std::stringstream c(cmd);
verbs = Verb(c); cmd[0] = toupper(cmd[0]);
nouns = Noun(c); std::stringstream c(cmd);
for (const auto &verb: verbs) { verbs = Verb(c);
if (!commands[verb]) { nouns = Noun(c);
CommandUnrecognized();
break; for (const auto &verb: verbs) {
if (!commands[verb]) {
CommandUnrecognized();
break;
}
for (const auto &n: nouns) {
noun = n;
}
toRun = commands[verb];
} }
for (const auto &n: nouns) { if (toRun) {
noun = n; toRun(noun);
} }
cmd.clear();
toRun = commands[verb]; word.clear();
} verbs.clear();
nouns.clear();
c.clear();
if (toRun) { toRun = nullptr;*/
toRun(noun);
}
cmd.clear();
word.clear();
verbs.clear();
nouns.clear();
c.clear();
toRun = nullptr;
return 0; return 0;
} }
+10 -18
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,38 +21,30 @@ 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;
} }
void Game::Run() { void Game::Run() {
parser.Parse(); parser.Parse();
// Process game logic in here // Process game logic in here
} }
bool Game::Save() { return false; } bool Game::Save() { return false; }
Binary file not shown.
+93 -93
View File
@@ -1,101 +1,101 @@
[ [
[ [
{
"description": "First Test Room",
"exits": [
{ {
"dir": 1, "description": "First Test Room",
"exitID": 0, "exits": [
"isLocked": false {
"dir": 1,
"exitID": 0,
"isLocked": false
},
{
"dir": 2,
"exitID": 1,
"isLocked": false
}
],
"id": 0,
"name": "Test Room 1",
"scenery": [
{
"canTake": false,
"description": "A Grey Rock",
"id": 0,
"keywords": [
"Rock"
],
"name": "Rock",
"roomIdItBelongsTo": 0,
"type": 1
}
]
}, },
{ {
"dir": 2, "description": "A court yard",
"exitID": 1, "exits": [
"isLocked": false {
"dir": 3,
"exitID": 2,
"isLocked": false
},
{
"dir": 2,
"exitID": 3,
"isLocked": false
},
{
"dir": 1,
"exitID": 4,
"isLocked": false
}
],
"id": 1,
"name": "Test Room 2",
"scenery": [
{
"canTake": false,
"description": "A fine looking Tree stands in the courtyard",
"id": 1,
"keywords": [
"Tree"
],
"name": "Tree",
"roomIdItBelongsTo": 1,
"type": 1
}
]
} }
], ],
"id": 0, [
"name": "Test Room 1",
"scenery": [
{ {
"canTake": false, "description": "You can hear the sound of a waterfall nearby",
"description": "A Grey Rock", "exits": [
"id": 0, {
"keywords": [ "dir": 0,
"Rock" "exitID": 5,
], "isLocked": false
"name": "Rock", },
"roomIdItBelongsTo": 0, {
"type": 1 "dir": 2,
"exitID": 6,
"isLocked": false
}
],
"id": 3,
"name": "Test Room 3",
"scenery": [
{
"canTake": false,
"description": "A fine waterfall on a cliff",
"id": 3,
"keywords": [
"Waterfall"
],
"name": "Waterfall",
"roomIdItBelongsTo": 3,
"type": 1
}
]
} }
] ]
},
{
"description": "A court yard",
"exits": [
{
"dir": 3,
"exitID": 2,
"isLocked": false
},
{
"dir": 2,
"exitID": 3,
"isLocked": false
},
{
"dir": 1,
"exitID": 4,
"isLocked": false
}
],
"id": 1,
"name": "Test Room 2",
"scenery": [
{
"canTake": false,
"description": "A fine looking Tree stands in the courtyard",
"id": 1,
"keywords": [
"Tree"
],
"name": "Tree",
"roomIdItBelongsTo": 1,
"type": 1
}
]
}
],
[
{
"description": "You can hear the sound of a waterfall nearby",
"exits": [
{
"dir": 0,
"exitID": 5,
"isLocked": false
},
{
"dir": 2,
"exitID": 6,
"isLocked": false
}
],
"id": 3,
"name": "Test Room 3",
"scenery": [
{
"canTake": false,
"description": "A fine waterfall on a cliff",
"id": 3,
"keywords": [
"Waterfall"
],
"name": "Waterfall",
"roomIdItBelongsTo": 3,
"type": 1
}
]
}
]
] ]
+59 -53
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(false);
game.SetIsActive(Init());
while (game.GetIsActive()) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
game.SetIsActive(false);
}
} }
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set render draw color to black if (event->key.key == SDLK_RETURN) {
SDL_RenderClear(renderer); // Clear the renderer SDL_StopTextInput(window.GetWindow());
}
SDL_RenderPresent(renderer); // Render the screen
//game.Run();
} }
return 0;
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()) {
void Close() SDL_SetRenderDrawColor(window.GetRenderer(), 0, 0, 0, 255); // Set render draw color to black
{ SDL_RenderClear(window.GetRenderer()); // Clear the renderer
SDL_DestroyRenderer(renderer); SDL_RenderPresent(window.GetRenderer()); // Render the screen
SDL_DestroyWindow(window);
SDL_Quit(); // game.Run();
} }
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
if (result == SDL_APP_SUCCESS) {
window.Close();
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;