5.5 C
New York
Saturday, March 15, 2025

sdl2 – Is there extra environment friendly technique of terrain rendering in 2D airplane than redrawing tens of millions of SDL_FRect every body


I need to render terrain efficently utilizing perlin nosie and a numerous rects. Ought to I take advantage of rects or is there one other approach. Following is my implementation.

I take advantage of rect dimension = 1 which isn’t actually sensible however might be used as a benchmark of my code.

The whole variety of rects utilizing rect dimension = 1 is 964,004 and this provides round 10-15 fps.

NOTE: No rect is Out Of FOV for this take a look at

I’ve a struct:

typedef struct Obj {
    SDL_FRect rect;
    uint8_t r, g, b;
} Obj;

that holds information about every object to be rendered on the display.

I’m utilizing them to create my base terrain layer with perlin noise era.

Obj **initTerrainMap(int seed) {
    Obj **terrain_map = malloc(ROW_COUNT * sizeof(Obj *));
    uint8_t r, g, b;

    if (!terrain_map) {
        fprintf(stderr, "ERROR: Didn't allotted reminiscence for the terrain map init.n");
        return NULL;
    }
    for (int y = -1; y < ROW_COUNT - 1; y++) {
        terrain_map[y + 1] = malloc(COL_COUNT * sizeof(Obj));
        if (!terrain_map[y + 1]) {
            fprintf(stderr, "ERROR: Didn't allocate reminiscence for terrain map's coloums.n");
            freeTerrainMap(terrain_map);
            return NULL;
        }
        for (int x = -1; x < COL_COUNT - 1; x++) {
            FBM(PERLIN_TERRAIN_FREQ, x, y, seed, &r, &g, &b);
            terrain_map[y + 1][x + 1] = createObj(x * TILE_SQUARE_SIDE, y * TILE_SQUARE_SIDE,
                                        TILE_SQUARE_SIDE, TILE_SQUARE_SIDE, r, g, b);
        }
    }
    return terrain_map;
}

And rendering them every body utilizing:

static void renderObj(SDL_Renderer *renderer, Obj *pTo_move) {
    SDL_SetRenderDrawColor(renderer, pTo_move->r, pTo_move->g, pTo_move->b, 255);
    SDL_RenderFillRectF(renderer, &pTo_move->rect);
}

void render(SDL_Renderer *renderer, Participant *pPlayer, Obj **terrain_map,
            float accumulated_x_offset, float accumulated_y_offset) {
    Obj temp;

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    for (int y = 0; y < ROW_COUNT; y++) {
        for (int x = 0; x < COL_COUNT; x++) {
            temp = terrain_map[y][x];
            // It is because the participant continues to be and the terrain strikes.
            setObjPos(&temp, terrain_map[y][x].rect.x - accumulated_x_offset, terrain_map[y][x].rect.y - accumulated_y_offset);
            renderObj(renderer, &temp);
        }
    }
    renderObj(renderer, &pPlayer->obj);
    SDL_RenderPresent(renderer);
}

Necessary: The participant can transfer just a little bit every body, we have to replace what’s being rendered consistently

Is there any higher approach to do that rendering.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles