SDL: SDL2 opens no window without giving any errors on VMWare

I’m trying to test a simple SDL2 application. The app runs without any errors. However, no window shows up. I’m using Ubuntu18.04 on a VMWare machine. I also tried to compile SDL from the source but got the same result. The problem might be related to using the virtual machine. Here is a simple application from docs (I just added SDL_GetNumDisplayModes to make sure the display mode is OK- returns 1) The version is 2.28, and I’ve already enabled 3D graphics on my VM.

// Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>

// Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char *args[])
{

    // The window we'll be rendering to
    SDL_Window *window = NULL;

    // The surface contained by the window
    SDL_Surface *screenSurface = NULL;

    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }
    else
    {
        int res = SDL_GetNumDisplayModes(0);
        printf("SDL_GetNumDisplayModes: %d\r\n", res);
        // Create window
        window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
        if (window == NULL)
        {
            printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        }
        else
        {
            // Get window surface
            screenSurface = SDL_GetWindowSurface(window);

            // Fill the surface white
            SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));

            // Update the surface
            SDL_UpdateWindowSurface(window);

            // Hack to get window to stay up
            SDL_Event e;
            bool quit = false;
            while (quit == false)
            {
                while (SDL_PollEvent(&e))
                {
                    if (e.type == SDL_QUIT)
                    {
                        quit = true;
                    }
                }
            }
        }
    }

    // Destroy window
    SDL_DestroyWindow(window);

    // Quit SDL subsystems
    SDL_Quit();

    return 0;
}

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Ah, I see now. Ubuntu 18.04 ships libwayland 1.16, while SDL wants 1.18 at minimum, so that’s why it’s failing.

Wayland has other dependencies as well: libxkbcommon-dev and libegl1-mesa-dev. I see EGL support in that log, so the latter is there, is xkbcommon installed?

This is probably the issue then as @icculus mentioned:

checking for Wayland support... no

Ubuntu 18.04 should default to Wayland, you can confirm with echo $XDG_SESSION_TYPE.

I think it’s the package libwayland-dev you need. You can check if it’s installed with dpkg -l | grep libwayland-dev and install it with sudo apt install libwayland-dev

Edit: As a clarification as to why you didn’t get any errors I’m guessing is because SDL chose the offscreen renderer as your default. So your window is rendering but inside a buffer and never actually to a real display. I’m not very knowledgeable with this particular renderer.

Oh. I didn’t realize this was locally built. I thought it was the lib that ships in apt.

Can you attach the output of configure or cmake so we can see what happened during configuration?

I’m no expert in VMWare. Since nothing is obviously breaking you’ll have to investigate further and see if you can figure out what the issue could be I guess. Perhaps your window doesn’t get any dimensions etc.

If you run this code you should get some more info:

#include "SDL_video.h"
#include <stdio.h>
#include <SDL.h>
#include <stdbool.h>

int main(void)
{
	if (SDL_InitSubSystem(SDL_INIT_EVERYTHING) < 0)
		SDL_Log("SDL fails to initialize! %s\n", SDL_GetError());

	int num_drivers = SDL_GetNumVideoDrivers();
	printf("Number of drivers: %d\n", num_drivers);
	for (int i = 0; i < num_drivers; i++)
		printf("Driver name: %s\n", SDL_GetVideoDriver(i));

	int num_displays = SDL_GetNumVideoDisplays();
	printf("Number of displays: %d\n", num_displays);
	for (int d = 0; d < num_displays; d++) {
		int num_modes = SDL_GetNumDisplayModes(d);
		printf("Display %d, modes: %d", d, num_modes);
		for (int m = 0; m < num_modes; m++) {
			SDL_DisplayMode mode;
			SDL_GetDisplayMode(d, m, &mode);
			printf("\tMode(%d) format: %d, %dx%d %dHz\n", m, mode.format, mode.w, mode.h, mode.refresh_rate);
		}
	}

	SDL_Window *window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
	if (!window) {
		printf("Failed to create window: %s\n", SDL_GetError());
	}

	int w, h;
	SDL_GetWindowSize(window, &w, &h);
	printf("Window dimensions: %dx%d\n", w, h);

	bool quit = false;
	SDL_Event e;
	while (!quit) {
		while (SDL_PollEvent(&e)) {
			if (e.type == SDL_QUIT) {
				quit = true;
			}
		}
	}

	SDL_DestroyWindow(window);

	return 0;
}