-1

so I got the following code(piece):

_Bool create_new_window(rectanglestruct *rectangle, colorstruct *colorfill, char *winname)
{

    ....

    log_printf("creating main renderer for window ( window : %s )\n", ptr->winname);
    // Setup renderer
    SDL_Renderer *renderer = SDL_CreateRenderer( ptr->window, -1, SDL_RENDERER_ACCELERATED);
    ptr->renderer = renderer;

    if (colorfill != NULL)
    {
        log_printf("\n - background color set r=%d g=%d b=%d with opacity of %d\n", colorfill->r,colorfill->g,colorfill->b, colorfill->opacity);

        // Set render color to red ( background will be rendered in this color )
        SDL_SetRenderDrawColor( ptr->renderer, colorfill->r,colorfill->g,colorfill->b, colorfill->opacity );

        log_printf("background rendered\n");
    }

    // Clear window
    SDL_RenderClear( ptr->renderer );

    SDL_ShowWindow(ptr->window);
    SDL_RenderPresent( ptr->renderer );

    getchar();

with

typedef struct SDL_Window SDL_Window;
typedef struct windowstruct {
    char *winname;
    SDL_Window *window;
    SDL_Renderer *renderer;
    struct windowstruct *next;
    struct windowstruct *previous;
} windowstruct;

static windowstruct *root = NULL;

and

typedef struct colorstruct {
    uint8_t r;
    uint8_t g;
    uint8_t b;
    uint8_t opacity;
} colorstruct;

With in main:

_Bool start_SDL(void)

// scope this
{
    //draw background
    colorstruct *colorfill = malloc(sizeof(rectanglestruct));
    colorfill->r = 0xFF;
    colorfill->g = 0xFF;
    colorfill->b = 0xFF;
    colorfill->opacity = 0xFF;

    rectanglestruct *winplace = malloc(sizeof(rectanglestruct));
    winplace->x = 0;
    winplace->y = 0;
    winplace->w = 300;
    winplace->h = 300;

    create_new_window(winplace, colorfill, "appscreen"); 

    free(colorfill);
    free(winplace);
}

and

_Bool start_SDL(void)
{
    //Initialization flag
    _Bool success = true;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
         log_printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
    success = false;
    }
}

and I got the following output (after a couple of times):

output screen

The point is I thought that the renderer was just a copy of the screen like a buffer in which you can write and refresh on the screen. But I gues not?

MVT
  • 137
  • 8

1 Answers1

2

No, SDL_Renderer implements SDL2 drawing, usually with hardware-accelerated backend. Your image is corrupt because you didn't issue redraw at appropriate time. If your window needs redraw (resized, overshadowed by other window or sceen borders) - you have to draw again and present result (this is in fact the same for every windowing library; even in GUI toolkits like Qt or GTK if your callback didn't return quickly, you can experience the same corruption). You can render to texture and then display it again if your image remains unchanged and calculations are heavy.

To do what you've said would require accumulating all data sent to renderer (may be high memory usage) and either calling update on regular intervals or on events, or taking away main loop from calling side (like most GUI toolkits do), which is against SDL design. Also, since SDL's main target are video games, scenes there are rarely static.

Before SDL2 there was no renderer and SDL only provided display surface to which you draw, but it is quite the same basic concept, it wouldn't update all by itself.

It doesn't mean it cannot be done with SDL, however - it gives you much more control. If you want to redraw only when it is really required - watch for SDL_WindowEvent.

keltar
  • 17,711
  • 2
  • 37
  • 42