[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL_Texture works for SDL_RenderGeometry() in SDL2 but not for SDL3!? #11554

Closed
stalbordboat opened this issue Nov 29, 2024 · 1 comment
Closed

Comments

@stalbordboat
Copy link

I've been trying to migrate from SDL2 to SDL3 and ran into a rode block when using the SDL_RenderGeometry() function. I'm not sure if this is a bug or something that I'm just simply missing, so I thought I'd ask it here. When I try to render a triangle with a texture in SDL2 it works like a charm, but when I do the equivalent in SDL3 all I get is a white triangle. I'm using the same bmp image. The only discernible difference between the two, from what I can see is that SDL3's SDL_Vertex uses SDL_FColor instead of SDL_Color. Here is my working SDL2 code:

#include <SDL.h>

static SDL_Window   *window   = NULL;
static SDL_Renderer *renderer = NULL;

static SDL_bool is_running = SDL_TRUE;

static SDL_Event event = {0};

static SDL_Vertex vertex_1 = {{100.5, 100.5}, {255, 255, 255, 255}, {1, 1}};
static SDL_Vertex vertex_2 = {{200.5, 100.5}, {255, 255, 255, 255}, {1, 1}};
static SDL_Vertex vertex_3 = {{100.5, 200.5}, {255, 255, 255, 255}, {1, 1}};

static SDL_Vertex vertices[3] = {0};

static SDL_Surface *surface = NULL;
static SDL_Texture *texture = NULL;

int main(void) {
        int flags = SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC;
        vertices[0] = vertex_1;
        vertices[1] = vertex_2;
        vertices[2] = vertex_3;

        SDL_Init(SDL_INIT_VIDEO);

        window   = SDL_CreateWindow("", 0, 0, 1280, 720, SDL_WINDOW_SHOWN);
        renderer = SDL_CreateRenderer(window, -1, flags);

        surface = SDL_LoadBMP("Icon.bmp");
        texture = SDL_CreateTextureFromSurface(renderer, surface);

        while(is_running) {
                SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);

                SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

                SDL_RenderClear(renderer);

                while(SDL_PollEvent(&event)) {
                        switch(event.type) {
                                case SDL_QUIT:
                                        is_running = SDL_FALSE;
                                        break;
                        }
                }

                vertices[0].color.a -= 10;
                vertices[1].color.a -= 10;
                vertices[2].color.a -= 10;
                SDL_RenderGeometry(renderer, texture, vertices, 3, NULL, 0);

                SDL_RenderPresent(renderer);
        }

        SDL_FreeSurface(surface);
        SDL_DestroyTexture(texture);
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
}

This produces the texture mapped triangle and flashes it as I expected.
sdl2

and here's the equvelent SDL3 code:

#include <SDL3/SDL.h>

static SDL_Window   *window   = NULL;
static SDL_Renderer *renderer = NULL;

static bool is_running = true;

static SDL_Event event = {0};

static SDL_Vertex vertex_1 = {{100.5, 100.5}, {255, 255, 255, 255}, {1, 1}};
static SDL_Vertex vertex_2 = {{200.5, 100.5}, {255, 255, 255, 255}, {1, 1}};
static SDL_Vertex vertex_3 = {{100.5, 200.5}, {255, 255, 255, 255}, {1, 1}};

static SDL_Vertex vertices[3] = {0};

static SDL_Surface *surface = NULL;
static SDL_Texture *texture = NULL;

int main(void) {
        vertices[0] = vertex_1;
        vertices[1] = vertex_2;
        vertices[2] = vertex_3;

        SDL_Init(SDL_INIT_VIDEO);

        window   = SDL_CreateWindow("", 1280, 720, 0);
        renderer = SDL_CreateRenderer(window, NULL);
        SDL_SetRenderVSync(renderer, 1);

        surface = SDL_LoadBMP("Icon.bmp");
        texture = SDL_CreateTextureFromSurface(renderer, surface);

        while(is_running) {
                SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);

                SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);

                SDL_RenderClear(renderer);

                while(SDL_PollEvent(&event)) {
                        switch(event.type) {
                                case SDL_EVENT_QUIT:
                                        is_running = false;
                                        break;
                        }
                }

                vertices[0].color.a -= 10;
                vertices[1].color.a -= 10;
                vertices[2].color.a -= 10;
                SDL_RenderGeometry(renderer, texture, vertices, 3, NULL, 0);

                SDL_RenderPresent(renderer);
        }

        SDL_DestroySurface(surface);
        SDL_DestroyTexture(texture);
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
}

This is does not seem to produce the texture mapping at all, and when I log it to see if the texture is NULL, it is not NULL!
sdl3

Lastly, this is the texture that I'm loading in both example.

Icon

@madebr
Copy link
Contributor
madebr commented Nov 29, 2024

SDL3's SDL_Vertex.color has float members with a range 0.f-1.f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants