1

I tried running this program that is supposed to display simple black window on Ubuntu 22.04:

#include <iostream>
#include <SDL2/SDL.h>

using namespace std;

int main(int argc, char *argv[]) {
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        cout << "Failed to initialize SDL2: " << SDL_GetError() << endl;
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("SDL2 Window",
                                           SDL_WINDOWPOS_CENTERED,
                                           SDL_WINDOWPOS_CENTERED, 
                                           800, 600, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        cout << "Failed to create window: " << SDL_GetError() << endl;
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
    if (renderer == NULL) {
        cout << "Failed to create renderer: " << SDL_GetError() << endl;
        return 1;
    }

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

    SDL_Delay(5000);

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

    return 0;
}

However this does not show window, yet no warnings or errors is displayed when compiling. When I run executable (from terminal) it just holds for couple seconds before exiting (again, no core dumped or anything).

genpfault
  • 51,148
  • 11
  • 85
  • 139
user1234
  • 19
  • 3
  • 1
    Why are you blocking for 5 seconds instead of processing the event queue via `SDL_WaitEvent()`/`SDL_PollEvent()`/`SDL_PumpEvents()`? Are you running this under X11? Wayland? Something else? – genpfault May 03 '23 at 22:32
  • Did you try putting a `std::cout << SDL_GetError();` outside any `== NULL` check? Sometimes there might be no problem with the window/renderer itself but there could still be an error in general, that happened to me once. – vitopigno May 03 '23 at 22:35
  • Which version of SDL are you using? Distro-supplied or self-built? – genpfault May 04 '23 at 12:43
  • @genpfault I tried both distro and self-built on standard Ubuntu 22 GNOME. – user1234 May 05 '23 at 18:37

0 Answers0