Added fileCommon, added freebie font, start listening to SDL Keyboard text input
This commit is contained in:
@@ -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) {
|
||||
game.SetIsActive(false);
|
||||
}
|
||||
if (event->type == SDL_EVENT_KEY_DOWN) {
|
||||
if (event->key.key == SDLK_ESCAPE) {
|
||||
SDL_StopTextInput(window.GetWindow());
|
||||
game.SetIsActive(false);
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Set render draw color to black
|
||||
SDL_RenderClear(renderer); // Clear the renderer
|
||||
|
||||
SDL_RenderPresent(renderer); // Render the screen
|
||||
|
||||
//game.Run();
|
||||
|
||||
if (event->key.key == SDLK_RETURN) {
|
||||
SDL_StopTextInput(window.GetWindow());
|
||||
}
|
||||
}
|
||||
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_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
}
|
||||
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 SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result) {
|
||||
if (result == SDL_APP_SUCCESS) {
|
||||
window.Close();
|
||||
SDL_Quit();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user