Context
In my SDL2 app, I am trying to load a png image(transparent). I want to give a background color to this image with SDL2 because I want to reuse the image somewhere else in the program and I do not want to have multiple version of the same image.
Question
How to give a background color to a SDL_Surface* or a SDL_Texture* with SDL2?
Important Details
OS : Windows
Text Editor : VScode
Building Mehtod : Makefile
My code
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
int main(int argc, char* argv[]){
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_CreateWindowAndRenderer(900, 500, SDL_WINDOW_RESIZABLE, &window, &renderer);
SDL_SetWindowTitle(window, "StackOverflowApp");
SDL_Surface* surface = IMG_Load("./yourImage.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Event event;
bool run = true;
while (run) {
if(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
run = false;
}
}
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_FreeSurface(surface);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
Makefile
all:
@cls
g++ -I src/include -L src/lib -std=c++23 -o main main.cpp -lmingw32 -lSDL2main -lSDL2 -lSDL2_image
./main