Added fileCommon, added freebie font, start listening to SDL Keyboard text input
This commit is contained in:
+2
-2
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 4.2)
|
||||
|
||||
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_ttf REQUIRED)
|
||||
@@ -16,4 +16,4 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/Editor)
|
||||
#add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/)
|
||||
|
||||
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)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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(); }
|
||||
@@ -12,4 +12,4 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/room.cpp
|
||||
|
||||
add_library(Engine SHARED ${SOURCES})
|
||||
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)
|
||||
|
||||
@@ -50,10 +50,10 @@ int CommandParser::Parse() {
|
||||
FUNCTION toRun;
|
||||
|
||||
|
||||
/*
|
||||
printf("Please Enter a command \n");
|
||||
printf("> ");
|
||||
getline(std::cin, cmd);
|
||||
std::cin.clear();
|
||||
|
||||
cmd[0] = toupper(cmd[0]);
|
||||
std::stringstream c(cmd);
|
||||
|
||||
@@ -82,7 +82,7 @@ int CommandParser::Parse() {
|
||||
nouns.clear();
|
||||
c.clear();
|
||||
|
||||
toRun = nullptr;
|
||||
toRun = nullptr;*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+8
-16
@@ -1,7 +1,7 @@
|
||||
#include "game.h"
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include "fileCommon.h"
|
||||
#include "player.h"
|
||||
|
||||
std::vector<std::vector<Room>> rooms;
|
||||
@@ -21,32 +21,24 @@ bool Game::Init() {
|
||||
rooms[i].resize(MAP_WIDTH);
|
||||
}
|
||||
|
||||
filePath = fs::absolute(filePath);
|
||||
|
||||
|
||||
if (!fs::exists(filePath)) {
|
||||
printf("File Read error! Could not find Json at: \n");
|
||||
printf("%s", filePath.c_str());
|
||||
isActive = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Read rooms into buffer
|
||||
stream.open(absolute(filePath));
|
||||
json = nlohmann::json::parse(stream);
|
||||
isActive = OpenFile(filePath);
|
||||
json = GetData();
|
||||
isActive = (json != NULL);
|
||||
|
||||
rooms = json.get<std::vector<std::vector<Room>>>();
|
||||
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("Move", Move);
|
||||
parser.AddCommand("Look", Look);
|
||||
|
||||
ChangeRoom(rooms[0][0]);
|
||||
|
||||
return true;
|
||||
return isActive;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
@@ -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_init.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"
|
||||
|
||||
/* We will use this renderer to draw into this window every frame. */
|
||||
static SDL_Window *window = nullptr;
|
||||
static SDL_Renderer *renderer = nullptr;
|
||||
static int width = 800, height = 600;
|
||||
static Game game;
|
||||
Game game;
|
||||
Window window;
|
||||
|
||||
void EditRooms() {
|
||||
|
||||
@@ -47,78 +52,79 @@ void EditRooms() {
|
||||
{{3, "Waterfall", "A fine waterfall on a cliff", {"Waterfall"}, ObjectType::SCENERY, false, 3}}}}};
|
||||
|
||||
nlohmann::json j(roomDat);
|
||||
|
||||
std::fstream outFile(absolute(path), std::fstream::in | std::fstream::out | std::fstream::app);
|
||||
outFile << std::setw(4) << j << std::endl;
|
||||
|
||||
outFile.close();
|
||||
SaveJsonFile(path, j);
|
||||
}
|
||||
|
||||
|
||||
int Init()
|
||||
{
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) == false) {
|
||||
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 (window == nullptr) {
|
||||
SDL_Log("SDL_CreateWindow Error: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
if (result != SDL_APP_CONTINUE) {
|
||||
return result;
|
||||
}
|
||||
|
||||
renderer = SDL_CreateRenderer(window, nullptr);
|
||||
if (renderer == nullptr) {
|
||||
SDL_Log("SDL_CreateRenderer Error: %s\n", SDL_GetError());
|
||||
SDL_Quit();
|
||||
return -1;
|
||||
if (!TTF_Init()) {
|
||||
SDL_Log("Couldn't initialize SDL_ttf: %s\n", SDL_GetError());
|
||||
return SDL_APP_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
if (!game) {
|
||||
game.Init();
|
||||
Game::SetGameInstance(&game);
|
||||
}
|
||||
else {
|
||||
game.SetIsActive(true);
|
||||
SDL_StartTextInput(window.GetWindow());
|
||||
} else {
|
||||
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[]) {
|
||||
|
||||
SDL_Event e;
|
||||
|
||||
game.SetIsActive(Init());
|
||||
|
||||
while (game.GetIsActive()) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set render draw color to black
|
||||
SDL_RenderClear(renderer); // Clear the renderer
|
||||
if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {
|
||||
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();
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void Close()
|
||||
{
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
if (result == SDL_APP_SUCCESS) {
|
||||
window.Close();
|
||||
SDL_Quit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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;
|
||||
Reference in New Issue
Block a user