Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

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
...