Also, I don’t see where you’re setting this color palette in any way that affects the texture. You set it on a surface that has no connection to the texture, and you aren’t doing it until after the texture is created (so even if the underlying API and GPU support it, and even if the texture was created from the surface, it’s probably too late to change it).
metal → Supported: NO
opengl → Supported: NO
opengles2 → Supported: NO
software → Supported: NO
I must have misunderstood how SDL2 works. I assumed I would be able to create an array of byte containing indexes and SDL2 would take care of converting this to Color via a palette and then upload that to the GPU.
Can you confirm, I need to convert my indexes bitmap to a supported format of the renderer like RGBA and then update the texture manually?
If you have an SDL_surface that uses paletted color, SDL may be able to convert it to something the hardware supports when the texture is created (assuming you’ve already set the surface’s palette and create the texture with SDL_CreateTextureFromSurface()).
If the texture creation itself is successful (check for this!), in your above code you can query the texture format you actually got with something like:
// after you create the texture
uint32_t format = SDL_PIXELFORMAT_UNKNOWN;
SDL_QueryTexture(texture, &format, NULL, NULL, NULL);
printf("Texture format: %s\n", SDL_GetPixelFormatName(format);
But basically paletted texture formats are a thing of the past as far as modern GPUs and APIs are concerned.
You don’t have to use SDL_Render though.
If you do unaccelerated rendering, indexed surfaces are supported.
See https://wiki.libsdl.org/SDL_GetWindowSurface (it has a code example for how to blit a surface into the window surface) and https://wiki.libsdl.org/SDL_Surface (has minimal example on updating the pixels of a surface)
Or you still do the operations on a surface but create a new SDL_Texture from it each frame and render that (which would allow stretching the whole thing to a bigger window size and making the single pixels more visible)
Here is the code sample with Texture. Notice that I only had to create the window twice as big and the renderer did everything automatically. This API is awesome !
Code for future references:
#include <SDL.h>
int main() {
int w = 320; int h = 200;
Note that in your second example you’re leaking textures - you’re calling SDL_CreateTextureFromSurface() each frame but never call SDL_DestroyTexture()
I don’t think it matters.
I think it’s just supposed to demonstrate how palettes work and doesn’t need super high performance?
Also, creating just one texture per frame shouldn’t be a problem anyway.
(For example, Quake2 already generated one OpenGL texture per frame when displaying videos, and that was back in 1997)
To improve performance (if it really turns out performance sucks) you’d have to convert the paletted image to RGBX (whatever format the SDL_Texture supports and uses) each frame and update the SDL_Texture with that, like you originally tried
That Q2 example is just uploading new texture data, not creating a new texture every frame. A small nitpick, I guess, but there’s often (usually?) an optimized path for replacing the contents of an existing texture, especially if that texture was created with a flag that it’s going to be changed often.
edit: in any case, if it works and is fast enough then great!