diff --git a/CMakeLists.txt b/CMakeLists.txt index 795224c..3b2e067 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ add_executable(tibia main.c map.h bitmap.c bitmap.h - Objects.c - Objects.h) + objects.c + objects.h) target_link_libraries(tibia SDL3::SDL3) diff --git a/app.c b/app.c index 2df4b3f..ac18dd0 100644 --- a/app.c +++ b/app.c @@ -35,7 +35,7 @@ void App_Init(App_t *app) { void App_Run(App_t *app) { while (app->isRunning) { Input_Poll(app); - Render_Frame(); + Render_Frame(app); } } diff --git a/app.h b/app.h index dd9ae68..979a933 100644 --- a/app.h +++ b/app.h @@ -5,7 +5,7 @@ #include "bitmap.h" #include "config.h" -#include "Objects.h" +#include "objects.h" #include "map.h" #include "system.h" diff --git a/bitmap.c b/bitmap.c index a14cce9..076604a 100644 --- a/bitmap.c +++ b/bitmap.c @@ -4,31 +4,48 @@ #include "bitmap.h" -#include -#include -#include +#include +#include "window.h" bool Bitmap_Init(Bitmap_t* bitmap) { - bitmap->tibiaSurface = Bitmap_LoadFromFile("TIBIA.BMP"); - if (!bitmap->tibiaSurface) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Bitmap_Init: couldn't load tibia.bmp", NULL); + SDL_Renderer* renderer = Window_GetRenderer(); + + SDL_Surface* tibiaSurface = Bitmap_LoadFromFile("TIBIA.BMP"); + if (!tibiaSurface) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Bitmap_Init: couldn't load TIBIA.BMP", NULL); return false; } - bitmap->marbleSurface = Bitmap_LoadFromFile("MARBLE.BMP"); - if (!bitmap->marbleSurface) { - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Bitmap_Init: couldn't load marble.bmp", NULL); + bitmap->tibiaTexture = SDL_CreateTextureFromSurface(renderer, tibiaSurface); + if (bitmap->tibiaTexture == NULL) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Bitmap_Init: couldn't create tibia texture", NULL); return false; } + SDL_DestroySurface(tibiaSurface); + + SDL_Surface* marbleSurface = Bitmap_LoadFromFile("MARBLE.BMP"); + if (!marbleSurface) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Bitmap_Init: couldn't load MARBLE.BMP", NULL); + return false; + } + + bitmap->marbleTexture = SDL_CreateTextureFromSurface(renderer, marbleSurface); + if (bitmap->marbleTexture == NULL) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Bitmap_Init: couldn't create marble texture", NULL); + return false; + } + + SDL_DestroySurface(marbleSurface); + // TODO: Note: here, on the original client, it created a offscreen buffer of 36x36 pixels. return true; } void Bitmap_Destroy(const Bitmap_t* bitmap) { - SDL_DestroySurface(bitmap->tibiaSurface); - SDL_DestroySurface(bitmap->marbleSurface); + SDL_DestroyTexture(bitmap->tibiaTexture); + SDL_DestroyTexture(bitmap->marbleTexture); SDL_LogInfo(SDL_LOG_CATEGORY_CUSTOM, "Bitmap_Destroy: surfaces destroyed."); } diff --git a/bitmap.h b/bitmap.h index ab2854f..fda7087 100644 --- a/bitmap.h +++ b/bitmap.h @@ -4,11 +4,11 @@ #ifndef TIBIA_BITMAP_H #define TIBIA_BITMAP_H -#include +#include typedef struct Bitmap { - SDL_Surface* tibiaSurface; - SDL_Surface* marbleSurface; + SDL_Texture* tibiaTexture; + SDL_Texture* marbleTexture; } Bitmap_t ; bool Bitmap_Init(Bitmap_t* bitmap); diff --git a/Objects.c b/objects.c similarity index 99% rename from Objects.c rename to objects.c index b40faa6..861b722 100644 --- a/Objects.c +++ b/objects.c @@ -2,7 +2,7 @@ // Created by rov on 12/27/25. // -#include "Objects.h" +#include "objects.h" #include #include diff --git a/Objects.h b/objects.h similarity index 100% rename from Objects.h rename to objects.h diff --git a/render.c b/render.c index 6dcd658..9fae657 100644 --- a/render.c +++ b/render.c @@ -5,10 +5,41 @@ #include "render.h" #include "window.h" -void Render_Frame() { - SDL_Renderer* renderer = Window_GetRenderer(); +void Render_Frame(const App_t *app) { + SDL_Renderer *renderer = Window_GetRenderer(); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); + Render_MainWindowBackground(app); + SDL_RenderPresent(renderer); } + +void Render_MainWindowBackground(const App_t *app) { + SDL_Renderer *renderer = Window_GetRenderer(); + + if (app->bitmap.marbleTexture == NULL) { + SDL_SetRenderDrawColor(renderer, 64, 64, 64, 255); + SDL_RenderClear(renderer); + return; + } + + const int TILE_SIZE = 128; + + // Calculate how many tiles we need to cover the screen + const int tilesX = app->metrics.screenWidth / TILE_SIZE + 1; + const int tilesY = app->metrics.screenHeight / TILE_SIZE + 1; + + for (int y = 0; y < tilesY; y++) { + for (int x = 0; x < tilesX; x++) { + SDL_FRect destRect = { + (float) (x * TILE_SIZE), + (float) (y * TILE_SIZE), + (float) TILE_SIZE, + (float) TILE_SIZE + }; + + SDL_RenderTexture(renderer, app->bitmap.marbleTexture, NULL, &destRect); + } + } +} diff --git a/render.h b/render.h index 71a3614..002aa6e 100644 --- a/render.h +++ b/render.h @@ -4,7 +4,13 @@ #ifndef TIBIA_RENDER_H #define TIBIA_RENDER_H +#include -void Render_Frame(); +#include "app.h" + +void Render_Frame(const App_t* app); +void Render_MainWindowBackground(const App_t* app); + +void Render_DrawWindowFrame(SDL_Renderer* renderer, int x, int y, int w, int h); #endif //TIBIA_RENDER_H \ No newline at end of file