Render background image

This commit is contained in:
2025-05-24 15:01:08 +02:00
parent 6a81b1989f
commit 266b86fb30
4 changed files with 28 additions and 6 deletions

View File

@@ -1,13 +1,19 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_iostream.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <SDL3_image/SDL_image.h>
#include <flecs.h>
#include <spdlog/spdlog.h>
const uint8_t image_data[] = {
#embed "main.cpp"
#embed "jungle.jpg"
};
static constexpr int WINDOW_WIDTH = 400;
static constexpr int WINDOW_HEIGHT = 280;
auto main() -> int {
spdlog::info("Initialize SDL...");
bool sdl_success = SDL_Init(SDL_INIT_VIDEO);
@@ -16,7 +22,8 @@ auto main() -> int {
std::terminate();
}
auto *sdl_window = SDL_CreateWindow("HansTheGatherer", 400, 280, 0);
auto *sdl_window =
SDL_CreateWindow("HansTheGatherer", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (sdl_window == nullptr) {
spdlog::critical("Failed to create SDL window!\nCause: {}", SDL_GetError());
}
@@ -29,6 +36,12 @@ auto main() -> int {
flecs::world world;
auto *image_iostream = SDL_IOFromConstMem(image_data, sizeof(image_data));
SDL_Texture *img = IMG_LoadTexture_IO(sdl_renderer, image_iostream, false);
if (img == nullptr) {
spdlog::error("Failed to load SDL texture!\nCause: {}", SDL_GetError());
}
bool exit_gameloop = false;
while (!exit_gameloop) {
SDL_Event event;
@@ -44,6 +57,10 @@ auto main() -> int {
break;
}
}
SDL_RenderClear(sdl_renderer);
SDL_RenderTexture(sdl_renderer, img, nullptr, nullptr);
SDL_RenderPresent(sdl_renderer);
}
SDL_DestroyRenderer(sdl_renderer);