1

I'm working on Windows (MinGW64), though I suspect the problem will exist on MacOS and Linux too. Not sure though.

Given the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>

#define WIDTH 800
#define HEIGHT 600

#define LOGICAL_WIDTH 320
#define LOGICAL_HEIGHT 240

int main(int argc, char *argv[]) {
  (void)argc; (void)argv; // Prevent unused parameters warnings

  SDL_Init(SDL_INIT_EVERYTHING);

  SDL_Window *window = SDL_CreateWindow("Problem", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE);
  
  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
  SDL_RenderSetLogicalSize(renderer, LOGICAL_WIDTH, LOGICAL_HEIGHT);
  SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, LOGICAL_WIDTH, LOGICAL_HEIGHT);
  // Create pixels array and fill it in white
  uint8_t *pixels = malloc(LOGICAL_WIDTH * LOGICAL_HEIGHT * 4);
  memset(pixels, 255, LOGICAL_WIDTH * LOGICAL_HEIGHT * 4);
  
  SDL_Event event;
  while (1) {
    if (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        break;
      }
    }

    SDL_UpdateTexture(texture, NULL, pixels, LOGICAL_WIDTH * 4);

    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);
  }

  free(pixels);
  SDL_DestroyTexture(texture);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);

  SDL_Quit();
  return 0;
}

If I resize the window, the white background is centered correctly. However, if I drag the caption of the window and resize it, the background is not centered while dragging, only after freeing.

Example

Related questions: Getting Contiunous Window Resize Event in SDL 2

Is there a way to work around that?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
  • "If I resize the window, the white background is centered correctly." I disagree. In the video the recentering does not occur while resizing either. At least that is how is seems to me. I focus on the only part in which recentering during resizing would be vosoble as far as I cam tell, i.e. while you increase the window width to the right. – Yunnosch Dec 23 '20 at 06:46
  • @Yunnosch Not _while_ resizing, but _after_ resizing. I meant for example when double-click the caption to maximize – Chayim Friedman Dec 23 '20 at 15:52

0 Answers0