Fun demo

Ok, here’s that ODE simulator I knocked up last night - it’s not much, but
I am only after functionality…

p.s. when I run it I need to ‘wind it up’ like a clockwork thing - it
looks like SDL_PullEvent is blocking - is it sposed to do this?

njh
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/octet-stream
Size: 1078 bytes
Desc:
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/19980505/cca90d29/attachment.obj

Ok, here’s that ODE simulator I knocked up last night - it’s not much, but
I am only after functionality…

p.s. when I run it I need to ‘wind it up’ like a clockwork thing - it
looks like SDL_PullEvent is blocking - is it sposed to do this?

Yup. The new event model changes the functions though.

SDL_NumEvents() and SDL_PullEvent() are replaced by SDL_PollEvent()
and SDL_WaitEvent().

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Ok, here’s that ODE simulator I knocked up last night - it’s not much, but
I am only after functionality…

Hey, it’s pretty cool. :slight_smile:

A couple of comments on the SDL code…

    if ( SDL_Init() < 0 ) exit(1);

It’s usually helpful to print an error message so the person running the
program knows why it quit. In this case, not everyone knows that the SDL
library has to be in your LD_LIBRARY_PATH (or installed in /usr/lib)

    screen = SDL_SetVideoMode(640, 480, 15, 0);

The 15 bit mode is relatively rare. This will end up almost always being
emulated and converted on every frame update – slow!
Why not use 16 bit video?

   while ( !done && (SDL_PullEvent(&event) >= 0) ) {

If you want a continuous loop, either use
(SDL 0.6j)
while ( !done ) {
if ( SDL_NumEvents() > 0 ) {
SDL_PullEvent(&event);
}
or
(SDL 0.6k prepatch)
while ( !done && (SDL_PollEvent(&event) >= 0) ) {

Nifty swirling effect. :slight_smile:

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Ok, here’s a new version without the random broken bits…

njh
-------------- next part --------------
#include <stdlib.h>
#include <stdio.h>

#include “SDL.h”

#define n 1000
main(int argc, char *argv[])
{
int i, done, jump;
double x[n], y[n];
SDL_Surface *screen;
SDL_Color palette[256];
Uint32 videoflags;
SDL_Event event;
double esp = 0, sima;

if ( SDL_Init() < 0 ) {
	fprintf(stderr, "error occured\n");
	exit(1);
}
atexit(SDL_Quit);


/* Initialize the display */
screen = SDL_SetVideoMode(320, 240, 8, 0);
if (  screen == NULL ) {
	fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
						SDL_GetError());
	exit(1);
}
    /* Set a gray colormap, reverse order from white to black */
    for ( i=0; i<256; ++i ) {
            palette[i].r = i;
            palette[i].g = i;
            palette[i].b = i;
    }
    SDL_SetColors(screen, palette, 0, 256);

for(i = 0; i < n; i++) {
	x[i] = (rand() / (double)(0x7fffffff));
	y[i] = (rand() / (double)(0x7fffffff));
}
    done = 0;
    while (!done) {
	int i,j;
	char *buffer=(char *)screen->pixels;
	if ( SDL_NumEvents() > 0 ) {
		SDL_PullEvent(&event);

            switch (event.type) {
                    case SDL_MOUSEBUTTONEVENT:
                            if ( event.button.state == SDL_PRESSED ) {
                            }
                            break;
                    case SDL_KEYEVENT:
                            /* Ignore key releases */
                            if ( event.key.state == SDL_RELEASED ) {
                                    break;
                            }
                            /* Ignore ALT-TAB for windows */
                            if ( (event.key.keysym.sym == SDLK_LALT) ||
                                 (event.key.keysym.sym == SDLK_TAB) ) {
                                    break;
                            }
                            /* Center the mouse on <SPACE> */
                            if ( event.key.keysym.sym == SDLK_SPACE ) {
                                    SDL_WarpMouse(640/2, 480/2);
                                    break;
                            }
                            /* Any other key quits the application... */
                    case SDL_QUITEVENT:
                            done = 1;
                            break;
                    default:
                            break;
            }
	}
	sima = 0.1*sin(esp);
	for(jump = 0; jump < 3; jump++) 
	for(i = 0; i < n; i++)
	{
		double a, b;
		double da, db;
		int iy, ix;
		char* p;
		a = (x[i] - 0.5)*25;
		b = (y[i] - 0.5)*25;
		da = b*(3-a+b)*(5-a*b);
		db = (sima*a*a*a - a);
		x[i] += da*0.01;
		y[i] += db*0.01;
		if(x[i] < 0 || 1 < x[i] || y[i] < 0 || 1 < y[i]) {
			x[i] = (rand() / (double)(0x7fffffff));
			y[i] = (rand() / (double)(0x7fffffff));
		}
		ix = x[i] * screen->w;
		iy = y[i] * screen->h;
		p = buffer + (screen->pitch)*iy + ix;
		ix = *p + 8;
		if(ix > 255) ix = 255;
		*p = ix;
	}
	i = rand() % n;
	x[i] = (rand() / (double)(0x7fffffff));
	y[i] = (rand() / (double)(0x7fffffff));

	buffer=(char *)screen->pixels;
            
	for ( i=0; i<screen->h; ++i ) {
                    for(j = 0; j < screen->w; j++)
                    {
			int i = buffer[j]-1; 
			if(i > 0) buffer[j] = i;
                    }
                    buffer += screen->pitch;
            }
	buffer=(char *)screen->pixels;

	
	SDL_UpdateRect(screen, 0, 0, 0, 0);
	esp += 0.01;
}

}

Ok, here’s a new version without the random broken bits…

Great static… what’s it supposed to be?

hmmm, it should generate nice organic looking swirly things which move and
pulsate… could you send me a screen shot, if waiting doesn’t make it
settle down.

njhOn Wed, 6 May 1998, Sam Lantinga wrote: