SDL1 to SDL2 migration, getting errors

Hi, I have an old project with SDL1.2 and I want to migrate to SDL2. I read the migration guide and I got so confused. I will post the code here with marker which lines should be updated as they show in red error in visual studio. They are a total of four errors when working with SDL2. They work with SDL1. All errors are of the form “X is undefined”

void AffGrid_open_window(AffGrille * AG) {
	int taille_pixel;
	Uint32 flags = SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF; // this one doesn't work with SDL2

	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		fprintf(stderr, // this one doesn't work with SDL2
			"Erreur: ne peut pas initialiser le système graphique (\"%s\").\n",
			SDL_GetError());
		return;
	}
	atexit(SDL_Quit);
	const SDL_VideoInfo *info = SDL_GetVideoInfo();// this one doesn't work with SDL2
	taille_pixel = info->current_h / 2;

	if ((taille_pixel * 1.0) / AG->G->n > 1) {
		AG->cas_aff = 0;
		AG->taille_case = taille_pixel / AG->G->n;
	}
	else {
		AG->cas_aff = 1;
		AG->modulo = AG->G->n / taille_pixel;
	}

	SDL_SetVideoMode(AG->taille_case * AG->G->n, AG->taille_case * AG->G->m, 0,
		flags);

	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_INTERVAL / 4,// this one doesn't work with SDL2
		SDL_DEFAULT_REPEAT_DELAY / 12);

	SDL_WM_SetCaption("Sorting Robot", 0);
}

I would really appreciate your help.

  • SDL_SetVideoMode: Replace this with SDL_CreateWindow. Note that 1.2 destroys an existing window with each call to SDL_SetVideoMode, but SDL_CreateWindow() will create a new one, since SDL2 allows you to manage multiple windows. You can get the 1.2 behavior by calling SDL_DestroyWindow on the previous window before creating a new one, or you can use SDL_SetWindowSize and SDL_SetWindowFullscreen, etc, on the existing window, if you don’t intend to replace the window completely.

  • SDL_EnableKeyRepeat: just remove it, you’ll get multiple SDL_KEYDOWN events, and the event will have a “repeat” field that says whether this is a repeat event, which the app can ignore if they don’t care about key repeats. These fire at whatever is appropriate for the system; you can’t control the repeat rate in SDL2, but you can calculate this yourself by calculating time since the last SDL_KEYDOWN event each frame, or using an SDL timer event.

  • SDL_GetVideoInfo: You want SDL_GetDesktopDisplayMode in SDL2.

  • SDL_WM_SetCaption: You can specify the window title when creating it with the first parameter of SDL_CreateWindow, or change it later with SDL_SetWindowTitle.

Hi, thanks. Though, I am kinda lost, it’s not easy for the first time. Can you please help by rewriting the function in SDL2, because I find it daunting. For 2nd point, do I just remove it and that’s it? because you explain it in a way as if I am quite knowledgeable about SDL2, but I am not. For 3rd, Ok, but I don’t know how to keep this functionality: const SDL_VideoInfo *info = SDL_GetVideoInfo(); taille_pixel = info->current_h / 2;. It will be really nice if you can explain in explicit terms what are the exact equivalences of SDL1 in SDL2. Like this line xxx is equivalent to yyy. Thank you very much

I tried to rewrite the function according to your notes, and I think it s full of mistakes. Please if you can correct any:

void AffGrille_ouvre_fenetre(AffGrille * AG) {
	int taille_pixel;
	//Uint32 flags = SDL_HWSURFACE | SDL_HWACCEL | SDL_DOUBLEBUF;
	Uint32 flags = SDL_MESSAGEBOX_ERROR | SDL_MESSAGEBOX_WARNING | SDL_MESSAGEBOX_INFORMATION;
	// Initialize SDL 2D display component.

	

	// Make sure that SDL_Quit is executed upon exit (especially
	// important if running in fullscreen display).

	atexit(SDL_Quit);

	// Find the screen height to decide the window size
	const SDL_DisplayMode *info = SDL_GetDesktopDisplayMode(0, info);
	//taille_pixel = info->current_h / 2;


	// Decide si la fenetre permet ou non un affiche des cases
	// Dans le cas contraire, une case sera representee par un seul pixel

	if ((taille_pixel * 1.0) / AG->G->n > 1) {
		AG->cas_aff = 0;
		AG->taille_case = taille_pixel / AG->G->n;
	}
	else {
		AG->cas_aff = 1;
		AG->modulo = AG->G->n / taille_pixel;
	}


	// Initialized windowed mode with given size and flags.

	SDL_SetVideoMode(AG->taille_case * AG->G->n, AG->taille_case * AG->G->m, 0,
		flags);
	SDL_Window *window = SDL_CreateWindow("Sorting Robot", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, AG->taille_case * AG->G->n,
		AG->taille_case * AG->G->m, flags);
	// Enable key repeat (i.e.: when a key is kept down, the keyboard
	// event is repeteadly triggered).

	/*SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_INTERVAL / 4,
		SDL_DEFAULT_REPEAT_DELAY / 12);*/

	/*SDL_WM_SetCaption("Sorting Robot", 0);*/
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		SDL_ShowSimpleMessageBox(flags, "error", "no initialization", window);
		//fprintf(stderr,
		//	"Erreur: ne peut pas initialiser le système graphique (\"%s\").\n",
		//	SDL_GetError());
		return;
	}
}

You need to initialize SDL first, then create a window. Don’t call SDL_SetVideoMode(), that function doesn’t exist in SDL 2.