C SDL2- Is it possible to exclude some layers of abstraction?

I was trying to learn how to use the SDL library with C (not C ++), and recently discovered that the reason for all the errors I received was that I was looking at the SDL1 tutorial, not the SDL2 tutorial. Since then, I have discovered that SDL2 has implemented several layers of abstraction that are absent (as far as I know) from SDL1, which apparently cannot be circumvented.

In particular, while in SDL1, drawing a set of pixels on the screen was done using:

define surface - find rgb values ​​for pixels - draw pixels on the surface - draw surface on the screen

in SDL2 instead it turns out:

define a window - create a render for a window - create a surface - find the rgb values ​​for pixels - draw pixels to the surface - convert the surface to a texture - send the texture to render - redraw window

Although I understand how textures can be an easier way to do things if there are things other than raw pixel data, especially if there are several data sources, in the specific case I want to use it for (a single window program in which raw pixel data is the only thing that will ever be painted). The difference between a window, a visualizer, and a texture is simply a meaningless abstraction. In addition, it seems that in most cases the data from the images will be loaded into the SDL_Surface class, which then needs to be converted to a texture, adding an even more unnecessary abstraction.

Is there any way around this, other than reverting to an older version of SDL?

+4
source share
1 answer

, SDL2.

, , SDL2, OGL/DX, 2D .


, SDL_RenderCopyEx , . SDL1 rotozoom .

, . , SDL_RenderDrawLine, SDL1 , , .

, , , , , .


, , SDL1 "", SDL2 . , SDL2. , (SDL_Surface - ), , . .


, " ", , . , - , :

struct SurfaceElement {
   SDL_Surface* s;
   SDL_Rect clip;
   SDL_Rect position;
   struct SurfaceElement * next;
};

typedef struct SurfaceElement SurfaceElement;

void ProcessingLoop() {
    SurfaceElement *surfacesList = (SurfaceElement *)malloc(sizeof(SurfaceElement));

    // Create surfaces, work on surfaces
    // When you're done with a surface, add it to the list:
    surfacesList =  AddToBlitList(yourSurface, yourClipPos, yourBlitPos, surfacesList);

    // When everything is okay and you're ready to blit:
    TextureBlittingProcess(surfacesList, renderer);
}

SurfaceElement* AddToBlitList(SDL_Surface *s, SDL_Rect clip, SDL_Rect position, SurfaceElement *head)   {
    SurfaceElement *current = (SurfaceElement*)malloc(sizeof(SurfaceElement));
    current->s = s;
    current->clip = clip;
    current->position = position;
    current->next = head;
    return current;
}

void TextureBlittingProcess(SurfaceElement *surfacesList, SDL_Renderer *renderer)   {
    while (surfaceList) {
        SDL_Texture *t = SDL_CreateTextureFromSurface(renderer, surfaceList->s)
        SDL_RenderCopy(renderer, t, &(surfaceList->clip), &(surfaceList->position));
        surfaceList = surfaceList->next;
    }
}

, , , , , SDL . Matlab , , Scripting, Python Caml.

+2

Source: https://habr.com/ru/post/1614009/


All Articles