How to get window position on the screen with SDL2 in C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>
#include <SDL.h>

void init() {
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        printf("SDL_Init Error: %s\n", SDL_GetError());
        exit(0);
    }
}

int main(int argc, char* argv[]) {
    init();

    SDL_Window* window = SDL_CreateWindow("SDL2 Display Image",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        800, 600, SDL_WINDOW_RESIZABLE); 

    if (window == NULL) {
        printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
        exit(0);
    }

    int x, y;
 
    // event loop
    bool close = false;
    while (!close) {
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {

            case SDL_QUIT:
                close = true;
                break;
            }
        }

        SDL_GetWindowPosition(window, &x, &y);

        printf("x = %d, y = %d\n", x, y);
    }

    // free 
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}



/*
run:

x = 524, y = 427
x = 524, y = 427
x = 524, y = 427
x = 524, y = 427
x = 524, y = 427
.
.
.
x = 1686, y = 807
x = 1686, y = 807
x = 1686, y = 807
x = 1686, y = 807
x = 1686, y = 807

*/



 



answered Jan 6, 2022 by avibootz
...