From a0223f8e4ece4cbc210eaa03a4048ca4f4bdd6c6 Mon Sep 17 00:00:00 2001 From: Rodrigo Verdiani Date: Fri, 26 Dec 2025 23:48:59 -0300 Subject: [PATCH] initial commit --- .gitignore | 2 ++ CMakeLists.txt | 20 ++++++++++++++++++++ app.c | 26 ++++++++++++++++++++++++++ app.h | 19 +++++++++++++++++++ config.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ config.h | 18 ++++++++++++++++++ input.c | 21 +++++++++++++++++++++ input.h | 12 ++++++++++++ main.c | 11 +++++++++++ render.c | 14 ++++++++++++++ render.h | 10 ++++++++++ system.c | 24 ++++++++++++++++++++++++ system.h | 15 +++++++++++++++ window.c | 24 ++++++++++++++++++++++++ window.h | 11 +++++++++++ 15 files changed, 274 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 app.c create mode 100644 app.h create mode 100644 config.c create mode 100644 config.h create mode 100644 input.c create mode 100644 input.h create mode 100644 main.c create mode 100644 render.c create mode 100644 render.h create mode 100644 system.c create mode 100644 system.h create mode 100644 window.c create mode 100644 window.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8b1aab2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +cmake-build-debug \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..51b7a14 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 4.1) +project(tibia C) + +set(CMAKE_C_STANDARD 11) + +find_package(SDL3 CONFIG REQUIRED) + +add_executable(tibia main.c + app.c + window.c + system.c + system.h + render.c + render.h + input.c + input.h + config.c + config.h) + +target_link_libraries(tibia SDL3::SDL3) diff --git a/app.c b/app.c new file mode 100644 index 0000000..9dd7eaa --- /dev/null +++ b/app.c @@ -0,0 +1,26 @@ +#include "app.h" +#include "window.h" +#include "render.h" +#include "input.h" + +void App_Init(App_t *app) { + Window_Init_SDL(); + System_QueryMetrics(&app->metrics); + + Window_Init(app->metrics.screenWidth, app->metrics.screenHeight); + + Config_Load_Settings(&app->configParams); + + app->isRunning = true; +} + +void App_Run(App_t *app) { + while (app->isRunning) { + Input_Poll(app); + Render_Frame(); + } +} + +void App_Shutdown(App_t *app) { + Window_Shutdown(); +} diff --git a/app.h b/app.h new file mode 100644 index 0000000..e21fda0 --- /dev/null +++ b/app.h @@ -0,0 +1,19 @@ +#ifndef APP_H +#define APP_H + +#include + +#include "config.h" +#include "system.h" + +typedef struct App { + bool isRunning; + SystemMetrics_t metrics; + ConfigParams_t configParams; +} App_t; + +void App_Init(App_t* app); +void App_Run(App_t* app); +void App_Shutdown(App_t* app); + +#endif \ No newline at end of file diff --git a/config.c b/config.c new file mode 100644 index 0000000..d7e6142 --- /dev/null +++ b/config.c @@ -0,0 +1,47 @@ +// +// Created by rov on 12/26/25. +// + +#include "config.h" +#include +#include +#include + +void Config_Load_Settings(ConfigParams_t* configParams) { + FILE* configFile = fopen("CLIENT.CFG", "rb"); + + if (configFile != NULL) { + SDL_LogInfo(SDL_LOG_CATEGORY_CUSTOM, "Config_Load_Settings: loaded CLIENT.CFG"); + + fread(configParams->serverAddress, 1, 100, configFile); + fread(&configParams->serverPort, 1, 4, configFile); + fread(configParams->lastAccount, 1, 30, configFile); + fread(configParams->lastPassword, 1, 30, configFile); + + fclose(configFile); + return; + } + + SDL_LogInfo(SDL_LOG_CATEGORY_CUSTOM, "Config_Load_Settings: couldn't load CLIENT.CFG. Using default values."); + + strcpy(configParams->serverAddress, "rrws27.uni-regensburg.de"); + configParams->serverPort = 7171; + configParams->lastAccount[0] = '\0'; + configParams->lastPassword[0] = '\0'; +} + +void Config_Save_Settings(ConfigParams_t* configParams) { + FILE* configFile = fopen("CLIENT.CFG", "wb"); + + if (configFile == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Config_Save_Settings: couldn't open config file."); + return; + } + + fwrite(configParams->serverAddress, 1, 100, configFile); + fwrite(&configParams->serverPort, 1, 4, configFile); + fwrite(configParams->lastAccount, 1, 30, configFile); + fwrite(configParams->lastPassword, 1, 30, configFile); + + fclose(configFile); +} \ No newline at end of file diff --git a/config.h b/config.h new file mode 100644 index 0000000..d52624b --- /dev/null +++ b/config.h @@ -0,0 +1,18 @@ +// +// Created by rov on 12/26/25. +// + +#ifndef TIBIA_CONFIG_H +#define TIBIA_CONFIG_H + +typedef struct ConfigParams { + char serverAddress[100]; + long serverPort; + char lastAccount[30]; + char lastPassword[30]; +} ConfigParams_t; + +void Config_Load_Settings(ConfigParams_t* configParams); +void Config_Save_Settings(ConfigParams_t* configParams); + +#endif //TIBIA_CONFIG_H \ No newline at end of file diff --git a/input.c b/input.c new file mode 100644 index 0000000..20d835f --- /dev/null +++ b/input.c @@ -0,0 +1,21 @@ +// +// Created by rov on 12/26/25. +// + +#include "input.h" + +#include + +void Input_Poll(App_t *app) { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_EVENT_QUIT: + app->isRunning = false; + break; + default: + break; + } + } +} diff --git a/input.h b/input.h new file mode 100644 index 0000000..87a8977 --- /dev/null +++ b/input.h @@ -0,0 +1,12 @@ +// +// Created by rov on 12/26/25. +// + +#ifndef TIBIA_INPUT_H +#define TIBIA_INPUT_H + +#include "app.h" + +void Input_Poll(App_t* app); + +#endif //TIBIA_INPUT_H \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..5bc06ef --- /dev/null +++ b/main.c @@ -0,0 +1,11 @@ +#include "app.h" + +int main(int argc, char* argv[]) { + App_t app; + + App_Init(&app); + App_Run(&app); + App_Shutdown(&app); + + return 0; +} \ No newline at end of file diff --git a/render.c b/render.c new file mode 100644 index 0000000..6dcd658 --- /dev/null +++ b/render.c @@ -0,0 +1,14 @@ +// +// Created by rov on 12/26/25. +// + +#include "render.h" +#include "window.h" + +void Render_Frame() { + SDL_Renderer* renderer = Window_GetRenderer(); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_RenderPresent(renderer); +} diff --git a/render.h b/render.h new file mode 100644 index 0000000..71a3614 --- /dev/null +++ b/render.h @@ -0,0 +1,10 @@ +// +// Created by rov on 12/26/25. +// + +#ifndef TIBIA_RENDER_H +#define TIBIA_RENDER_H + +void Render_Frame(); + +#endif //TIBIA_RENDER_H \ No newline at end of file diff --git a/system.c b/system.c new file mode 100644 index 0000000..63a12c3 --- /dev/null +++ b/system.c @@ -0,0 +1,24 @@ +// +// Created by rov on 12/26/25. +// + +#include "system.h" +#include + +void System_QueryMetrics(SystemMetrics_t *systemMetrics) { + SDL_Rect bounds; + + const SDL_DisplayID displayId = SDL_GetPrimaryDisplay(); + if (displayId && SDL_GetDisplayBounds(displayId, &bounds)) { + systemMetrics->screenWidth = bounds.w; + systemMetrics->screenHeight = bounds.h; + return; + } + + // Fallback + + SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "System_QueryMetrics: couldn't get primary display bounds. Using fallback values."); + + systemMetrics->screenWidth = 800; + systemMetrics->screenHeight = 600; +} diff --git a/system.h b/system.h new file mode 100644 index 0000000..ffce769 --- /dev/null +++ b/system.h @@ -0,0 +1,15 @@ +// +// Created by rov on 12/26/25. +// + +#ifndef TIBIA_SYSTEM_H +#define TIBIA_SYSTEM_H + +typedef struct SystemMetrics { + int screenWidth; + int screenHeight; +} SystemMetrics_t; + +void System_QueryMetrics(SystemMetrics_t* systemMetrics); + +#endif //TIBIA_SYSTEM_H \ No newline at end of file diff --git a/window.c b/window.c new file mode 100644 index 0000000..eb4aed6 --- /dev/null +++ b/window.c @@ -0,0 +1,24 @@ +#include "window.h" + +static SDL_Window *window; +static SDL_Renderer *renderer; + +void Window_Init_SDL() { + SDL_Init(SDL_INIT_VIDEO); + SDL_SetHint(SDL_HINT_LOGGING, "*=info"); +} + +void Window_Init(int width, int height) { + SDL_CreateWindowAndRenderer( + "Tibia", width, height, 0, &window, &renderer); +} + +void Window_Shutdown() { + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} + +SDL_Renderer *Window_GetRenderer() { + return renderer; +} diff --git a/window.h b/window.h new file mode 100644 index 0000000..6b26c50 --- /dev/null +++ b/window.h @@ -0,0 +1,11 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include + +void Window_Init_SDL(); +void Window_Init(int width, int height); +void Window_Shutdown(); +SDL_Renderer* Window_GetRenderer(); + +#endif \ No newline at end of file