I’m trying to create a spritesheet that generates two-triangle textured rects based on the sprite test in SDL. I decided not to use SDL_Vertex or SDL_RenderGeometry and just use SDL_RenderGeometryRaw with a single array of floats interleaving x,y and u,v values. I’m currently just trying to render a single rect but despite everything looking correct, nothing at all renders. I know the texture is loaded because it renders with SDL_RenderCopy.
I’m posting asking if someone more familiar with this can tell me what I’ve gotten wrong here. Unfortunately, Google doesn’t really help. I’ve deleted irrelevant stuff like allocation/reallocation etc.
typedef struct Spritesheet {
int num_vertices, topindex;
float* vertices; // interleaved x,u,y,v
} Spritesheet;
static void PushSprite(Spritesheet* s, SDL_FRect* dstrect)
{
/*
* 0--1
* |\ |
* | \|
* 3--2
*/
//0
s->vertices[s->topindex++] = dstrect->x;
s->vertices[s->topindex++] = dstrect->y;
s->vertices[s->topindex++] = 0.0f;
s->vertices[s->topindex++] = 0.0f;
//1
s->vertices[s->topindex++] = dstrect->x + dstrect->w;
s->vertices[s->topindex++] = dstrect->y;
s->vertices[s->topindex++] = 1.0f;
s->vertices[s->topindex++] = 0.0f;
//2
s->vertices[s->topindex++] = dstrect->x + dstrect->w;
s->vertices[s->topindex++] = dstrect->y + dstrect->h;
s->vertices[s->topindex++] = 1.0f;
s->vertices[s->topindex++] = 1.0f;
//0
s->vertices[s->topindex++] = dstrect->x;
s->vertices[s->topindex++] = dstrect->y;
s->vertices[s->topindex++] = 0.0f;
s->vertices[s->topindex++] = 0.0f;
//2
s->vertices[s->topindex++] = dstrect->x + dstrect->w;
s->vertices[s->topindex++] = dstrect->y + dstrect->h;
s->vertices[s->topindex++] = 1.0f;
s->vertices[s->topindex++] = 1.0f;
//3
s->vertices[s->topindex++] = dstrect->x;
s->vertices[s->topindex++] = dstrect->y + dstrect->h;
s->vertices[s->topindex++] = 0.0f;
s->vertices[s->topindex++] = 1.0f;
}
static void RenderSprites(SDL_Renderer* ren, Spritesheet* s, SDL_Texture* tex)
{
SDL_Color color;
int stride = sizeof(float) * 4;
SDL_GetRenderDrawColor(ren, &color.r, &color.g, &color.b, &color.a);
SDL_RenderGeometryRaw(ren, tex, &s->vertices, stride, &color, 0, &s->vertices[2], stride, s->topindex, NULL, 0, 0);
}
edit: I kept getting uv errors so I looked at the source for SDL_RenderGeometryRaw, and it looks like it expects those values to be adjacent, so I rewrote the interleaving and the updated code is replaced above.
However, now I get the following: Values of ‘uv’ out of bounds 0.000000 0.000000 at 9/24, but that shouldn’t be out of bounds?