# How can I keep my texture center during resizing the window

**URL:** <https://discourse.libsdl.org/t/how-can-i-keep-my-texture-center-during-resizing-the-window/58965>\
**Category:** Game Development\
**Created:** [April 2, 2025, 9:50am UTC](https://discourse.libsdl.org/t/how-can-i-keep-my-texture-center-during-resizing-the-window/58965 "2025-04-02T09:50:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![distaling](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/c77e96/32.png) [@distaling](https://discourse.libsdl.org/u/distaling)\
**Post date:** [April 2, 2025, 9:50am UTC](https://discourse.libsdl.org/t/how-can-i-keep-my-texture-center-during-resizing-the-window/58965/1 "2025-04-02T09:50:18Z")

</div>

I wrote a snippet. Its main logic just like this:

```auto
int main() {
    // create a window and a texture and other initializations

    // always render the texture to the center of the window
    bool quit = false;
    while (!quit) {
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) {
                quit = true;
            }
            if (e.type == SDL_WINDOWEVENT) {
                switch (e.window.event) {
                    // when window is resized, repaint the texture to the center
                    case SDL_WINDOWEVENT_SIZE_CHANGED: {
                        windowWidth = e.window.data1;
                        windowHeight = e.window.data2;
                        RenderMyTextureToCenter(windowWidth, windowHeight);
                    }
                }
            }
        }
    }
}

```

It partly meeted my requirement.I found that texture only repainted when resizing was totally done.But if I keep dragging my mouse and zoom the window, the texture will not repaint.I want my texture always keep in the center even during the resizing.How can I realize that?

---

<div class="post-metadata">

**Author:** ![elefantinho](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/e/e0b2c6/32.png) [@elefantinho](https://discourse.libsdl.org/u/elefantinho)\
**Post date:** [April 2, 2025, 12:37pm UTC](https://discourse.libsdl.org/t/how-can-i-keep-my-texture-center-during-resizing-the-window/58965/2 "2025-04-02T12:37:22Z")

</div>

are you on Windows?

1. write an event filter function following this signature: [SDL2/SDL\_EventFilter - SDL2 Wiki](https://wiki.libsdl.org/SDL2/SDL_EventFilter)
  - you must return 1 from this event filter. if you return zero, you break SDL.
  - for `SDL_WINDOWEVENT_SIZE_CHANGED` and `SDL_WINDOWEVENT_RESIZED`, execute code strictly related to resizing, NOT rendering
  - catch `SDL_WINDOWEVENT_EXPOSED`, then execute render-related code
  - these three events are guaranteed to be watched/filtered in main thread, despite documentation saying they can be called from any thread. you can write assertions to ensure this, if you feel paranoid

2. during initialization, activate the callback with an event watcher: [SDL2/SDL\_AddEventWatch - SDL2 Wiki](https://wiki.libsdl.org/SDL2/SDL_AddEventWatch)

see this: [Is it thread-safe to render inside event watcher?](https://discourse.libsdl.org/t/is-it-thread-safe-to-render-inside-event-watcher/49409)  
and this: [Live-resize of an SDL window](https://discourse.libsdl.org/t/live-resize-of-an-sdl-window/56045)

---

<div class="post-metadata">

**Author:** ![flowCRANE](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/flowcrane/32/5726_2.png) [@flowCRANE](https://discourse.libsdl.org/u/flowCRANE)\
**Post date:** [April 2, 2025, 1:20pm UTC](https://discourse.libsdl.org/t/how-can-i-keep-my-texture-center-during-resizing-the-window/58965/3 "2025-04-02T13:20:46Z")

</div>

There is an easier solution. Just register an event watcher callback using the [SDL\_AddEventWatch()](https://wiki.libsdl.org/SDL3/SDL_AddEventWatch) function, detect the `SDL_EVENT_WINDOW_RESIZED` or `SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED` event in it, update your window data there and repaint the entire window. It is safe to repaint the window in the watcher. The new window size is delivered in the event, but you can also get it using the [SDL\_GetWindowSize()](https://wiki.libsdl.org/SDL3/SDL_GetWindowSize) function.

This works perfectly fine, and even smoother performance can be achieved by disabling VSync while the window is repainted (and turning it back on after repainting).
