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,884 questions

51,810 answers

573 users

How to get window size (width and height) with SDL2 in C++

1 Answer

0 votes
#include <iostream>

#include "SDL.h"

#define WIDTH 1200
#define HEIGHT 800

int main(int argc, char* argv[]) {
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        std::cout << "SDL_Init Error: " << SDL_GetError();
        return -1;
    }

    SDL_Window* window = SDL_CreateWindow("SDL",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        WIDTH, HEIGHT, SDL_WINDOW_SHOWN);

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 0, 0, 255, 0); // Blue
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    int width, height;
    SDL_GetWindowSize(window, &width, &height);
    std::cout << "width  = " << width << " height  = " << height;

    int close = 0;
    while (!close) {

        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {

            case SDL_QUIT:
                close = 1;
                break;
            }
        }
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}




/*
run:

width  = 1200 height  = 800

*/

 



answered Feb 15, 2022 by avibootz
edited Mar 6, 2022 by avibootz
...