Making an SDL loop

As a follow-up to the C/SDL issue,

	while( !quit )
	{
		//Handle events on queue
		while( SDL_PollEvent( &e ) != 0 )
		{
			//printf("%d,%d\n",e,&e);
			//User requests quit
			if( e.type == SDL_QUIT )
			{
				quit = true;
			}
		}
		t1=SDL_GetTicks();
		dt=t1-t0; t0=t1;
		total+=dt;
		if(total>timer) {
			printf("update;%d,%d\n",timer,total);
			total=0;
			if(!(PL_cons_functor(goal, PL_new_functor(PL_new_atom("update"), 2), a1,a2)&&PL_call(goal,m))) {
				printf("error: 'update' predicate not found\n");
				PL_raise_exception(exception);
				PL_fail;
			}
			a1=a2;
			a2=PL_new_term_ref();
		}
		SDL_Delay(1);
	}

The full code is on a repository: GitHub - cosmos-lang/sdl_pl: SDL bindings for Prolog.


update([Y,I],[Y,I2]) :- ...
	setRGBA(255,255,255,255),clear,
	I2 is I+1,setRGBA(0,0,0,255),drawImage(Y,I,120),
	rect(I,1,50,50),refresh.

The idea is that an update/2 predicate continually passes the state around. That would be the SDL main loop. This actually works well and moves a rectangle. However, drawImage sometimes does and sometimes doesn’t display the image. Is it possible to make something like this?

Hard to say. Could be drawImage/3 does something wrong, the redisplay loop doesn’t work, etc.

I do see another problem.

This eventually runs out of stack as the old term references are not reclaimed. You can use

    PL_put_term(a1, a2);
    PL_put_variable(a2);

Overall though, I’d make e.g. SDL_Delay(1) available from Prolog and write the loop in Prolog. Calling Prolog from Prolog is faster and simpler than from C :slight_smile:

1 Like

There were a few issues with loadimage. It was resolved and SDL for Prolog works now!


A Prolog-only loop oddly crashes which is why I assumed C was better. It’s there at any rate.

loop :- sdl_delay(1),loop.

I didn’t mind since a lot of UIs use callbacks or whatelse anyway.