Hi there.
I’m pretty new to SDL and to C++ in general so please don’t judge.
Here’s the code I’m confused about. Questions re blending logic are in comments.
Thanks.
#include <SDL3/SDL.h>
#include <cstdint>
#include <cstdlib>
#include <stdlib.h>
static inline constexpr int width{640};
static inline constexpr int height{480};
static inline constexpr uint8_t bytesPerPixel{sizeof(uint32_t)};
static inline constexpr int pitch{width * bytesPerPixel};
int main(int argc, char *args[])
{
SDL_Window *window{nullptr};
SDL_Renderer *renderer{nullptr};
SDL_Texture *texture1{nullptr};
SDL_Texture *texture2{nullptr};
uint32_t *pixelData{nullptr};
int nonConstPitchCopy = pitch;
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer("SDL3 Tutorial: Hello SDL3!!!", width, height, 0, &window, &renderer);
SDL_SetRenderVSync(renderer, 1);
texture1 = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, width, height);
texture2 = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, width, height);
// Commenting out SDL_SetRenderDrawColor and SDL_RenderClear is the same as set transparent black color. Is memory
// zeroed by default?
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_LockTexture(texture1, NULL, (void **)&pixelData, &nonConstPitchCopy);
for (int i = 0; i < width * height; ++i)
{
// If alpha is not opaque, then the texture1 color is blended with the color used for SDL_RenderClear
// unless SDL_SetTextureBlendMode(texture1, SDL_BLENDMODE_NONE) is called. Why?
pixelData[i] = 0xFF000080;
}
SDL_UnlockTexture(texture1);
pixelData = nullptr;
SDL_RenderTexture(renderer, texture1, NULL, NULL);
SDL_LockTexture(texture2, NULL, (void **)&pixelData, &nonConstPitchCopy);
for (int i = 0; i < width * height; ++i)
{
// If alpha is not opaque, then the texture2 color is blended with the color used for SDL_RenderClear
// but not with the texture1 color unless SDL_SetTextureAlphaMod is called with non-opaque aplha for the
// texture2. Why?
pixelData[i] = 0x00FF0080;
}
SDL_UnlockTexture(texture2);
// This call with non-opaque aplha is needed to actually blend texture1 and texture2 colors.
// Why is it even when texture2 pixels aplha is non-opaque?
SDL_SetTextureAlphaMod(texture2, 0x80);
pixelData = nullptr;
SDL_RenderTexture(renderer, texture2, NULL, NULL);
SDL_RenderPresent(renderer);
bool quit{false};
SDL_Event e;
SDL_zero(e);
while (quit == false)
{
SDL_WaitEvent(&e);
if (e.type == SDL_EVENT_QUIT)
{
quit = true;
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
renderer = nullptr;
window = nullptr;
texture1 = nullptr;
texture2 = nullptr;
SDL_Quit();
return 0;
}