Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to load and center a .bmp file texture using SDL3 in Win32 C++

1 Answer

0 votes
#include <SDL3/SDL.h>

#if _WIN32 // PLATFORM_WINDOWS

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>

static int texture_width = 0;
static int texture_height = 0;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR lpCmdLine,
    _In_ int nCmdShow) {

    if (!SDL_Init(SDL_INIT_VIDEO)) {
        // SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
        MessageBoxA(0, SDL_GetError(), "Couldn't initialize SDL", MB_OK);
        return 3;
    }

    SDL_Window* window;
    SDL_Renderer* renderer;
    if (!SDL_CreateWindowAndRenderer("SDL3 Load and move a texture", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
        // SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
        MessageBoxA(0, SDL_GetError(), "Couldn't create window and renderer", MB_OK);
        return 3;
    }

    // Textures are pixel data (image/sprite) that are uploaded to GPU memory for fast drawing
    // Upload once, draw many times with pixel data from a bitmap file

    // SDL_Surface is pixel data the CPU can access
    // SDL_Texture is pixel data the GPU can access

    // Load a .bmp into a surface and move it to a texture

    SDL_Surface* surface = SDL_LoadBMP("abc.bmp");
    if (!surface) {
        // SDL_Log("Couldn't load bitmap: %s", SDL_GetError());
        MessageBoxA(0, SDL_GetError(), "Couldn't load bitmap", MB_OK);
        return SDL_APP_FAILURE;
    }

    texture_width = surface->w;
    texture_height = surface->h;

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    if (!texture) {
        // SDL_Log("Couldn't create texture: %s", SDL_GetError());
        MessageBoxA(0, SDL_GetError(), "Couldn't create texture", MB_OK);
        return SDL_APP_FAILURE;
    }

    SDL_DestroySurface(surface);  // the texture has a copy of the pixels now

    SDL_Event event;

    while (1) {
        SDL_PollEvent(&event);
        if (event.type == SDL_EVENT_QUIT) {
            break;
        }

        // clear the windows with a black color
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(renderer); // start with a blank canvas

        SDL_FRect dst_rect;

        // Center the texture
        dst_rect.x = ((float)(WINDOW_WIDTH - texture_width)) / 2.0f;
        dst_rect.y = ((float)(WINDOW_HEIGHT - texture_height)) / 2.0f;
        dst_rect.w = (float)texture_width;
        dst_rect.h = (float)texture_height;
        SDL_RenderTexture(renderer, texture, NULL, &dst_rect);

        SDL_RenderPresent(renderer); // put it all on the screen
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    SDL_Quit();

    return 0;
}

#endif

 



answered Jan 4, 2025 by avibootz
...