1

I'm trying to create a pong game with sdl2 in c. I'm able to make it work in a laptop running UBUNTU 20.04 (gtx 1650 mobile) with nvidia driver 470. But running it on the same setup in ubuntu 21.10 with nvidia 470 on a different laptop (rtx 3060 mobile) gives me this error.

Here's the code (stripped off any game logic to only part where it crashes).

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "SDL2/SDL.h"


const int SCREEN_WIDTH = 1200;
const int SCREEN_HEIGHT = 800;


int main(int argc, char** argv)
{

    int sdl_init_status = SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER);

    if (sdl_init_status != 0)
    {
        printf("Initialization failed. Error: %s \n", SDL_GetError());
        return 1;
    }

    SDL_Window* window = SDL_CreateWindow("PONG", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);

    if (!window)
    {
        printf("Window initialization error: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    puts("Window created");

    // // create renderer
    Uint32 rflags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, rflags);
    if (!renderer)
    {
        printf("renderer initialization error: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
    puts("Renderer created");

    SDL_Surface* screen_surface = SDL_GetWindowSurface(window);
    if (!screen_surface)
    {
        printf("surface initialization error: %s\n", SDL_GetError());
        SDL_DestroyWindow(window);
        SDL_Quit();
        return 1;
    }
    puts("Surface created");

    SDL_Delay(1000);

    // cleanup and quit
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

This is the output I get:

gcc -std=c99 -g3 -Wall -o1 -o main main.c `sdl2-config --libs --cflags` -lm -lSDL2_image
./main
Window created
Renderer created
surface initialization error: No hardware accelerated renderers available
make: *** [Makefile:10: test] Error 1
genpfault
  • 51,148
  • 11
  • 85
  • 139
yuser099881232
  • 311
  • 1
  • 11
  • does it happen even if `SDL_RENDERER_PRESENTVSYNC` flag is removed? – tstanisl Nov 22 '21 at 15:38
  • SDL version? Is SDL self-compiled from source or are you using the Ubuntu binaries? Are you running this under X? Wayland? Linux console? What does [this test program](https://stackoverflow.com/a/57684805/44729) output? – genpfault Nov 22 '21 at 16:30
  • 3
    https://wiki.libsdl.org/SDL_GetWindowSurface **You may not combine this with 3D or the rendering API on this window.**. You either use renderer or window surface, not both. – keltar Nov 22 '21 at 16:32
  • @keltar calling SDL_CreateWindow - SDL_CreateRenderer - SDL_GetWindowSurface works fine for me on two VMs (Ubuntu Fossa and Windows 10), but is failing on an Android device, so it's not related to the renderer. SDL 1.2 works fine on all 3. – vesperto Jan 11 '23 at 09:28
  • @vesperto sdl 1.2 don't have renderer API. If documentation says it is invalid to do something but you insist on doing it on premise of "it sometimes accidentally works" - well, you do you, just don't be surprised when it fails. – keltar Jan 11 '23 at 10:23
  • @keltar I'm aware that SDL 1.2 has no renderer, i've been porting a C app to SDL 2.0. See my answer. – vesperto Jan 11 '23 at 10:25

1 Answers1

0

You have

Uint32 rflags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, rflags);

Your error states No hardware accelerated renderers available.

The documentation for SDL_CreateRenderer refers 4 flags you can use.

I was having this issue on Android devices and solved with replacing the flags in SDL_CreateRenderer with only SDL_RENDERER_SOFTWARE. So use

Uint32 rflags = SDL_RENDERER_SOFTWARE;

instead.

vesperto
  • 804
  • 1
  • 6
  • 26