import sdl.core.*;
import sdl.video.*;
import sdl.event.*;
import java.util.*;

public class testsprite {
	final int NUM_SPRITES = 100;
	final int MAX_SPEED = 3;
	final int DEBUG_FLIP = 1;
	private Video sdl;
	private SDLSurface screen;
	private SDLSurface sprite;
	private boolean sprites_visible;
	private int numsprites;
	private SDLRect[] positions;
	private SDLRect[] velocities;
	private SDLRect[] sprite_rects;
	private Random rand;
	private boolean done;
	private SDLRect screen_size;
	private SDLRect sprite_size;

	private void SDL_Quit(){
		done = true;
	}
	private int LoadSprite(String path){
		SDLSurface tmp = new SDLSurface(path);
		tmp.setColorKey(Video.SDL_SRCCOLORKEY,
						tmp.mapRGB((byte)255, (byte)255, (byte)255));
		tmp.displayFormat();
		sprite = tmp;
		sprite_size = new SDLRect(0, 0, tmp.m_Width, tmp.m_Height);
		return 0;
	}
	private void MoveSprites(/*Uint32 background*/){
		if(sprites_visible){
			screen.fillRect(screen_size,
							screen.mapRGB((byte)0, (byte)0, (byte)0));
		}
		int nupdates = 0;
		for(int i = 0; i < numsprites; i++){
			SDLRect position = positions[i];
			SDLRect velocity = velocities[i];
			position.m_X += velocity.m_X;
			if(position.m_X < 0 || position.m_X >= (screen.m_Width - sprite.m_Width)){
				velocity.m_X = (short)-velocity.m_X;
				position.m_X += velocity.m_X;
			}
			position.m_Y += velocity.m_Y;
			if(position.m_Y < 0 || position.m_Y >= (screen.m_Height - sprite.m_Height)){
				velocity.m_Y = (short)-velocity.m_Y;
				position.m_Y += velocity.m_Y;
			}
			sprite.blitSurface(sprite_size, screen, position);
			sprite_rects[nupdates++] = position;
			positions[i] = position;
			velocities[i] = velocity;
		}
		screen.flip();
		sprites_visible = true;
	}

	public void Start()
		{
			Main.SDLInit(Main.SDL_INIT_VIDEO|Main.SDL_INIT_TIMER);
			sdl = new Video();
			numsprites = NUM_SPRITES;
			screen = sdl.setVideoMode(640, 480, 16, Video.SDL_SWSURFACE);
			screen_size =
				new SDLRect(0, 0, screen.m_Width, screen.m_Height);
			LoadSprite("icon.bmp");
			sprite_rects = new SDLRect[numsprites];
			positions = new SDLRect[numsprites];
			velocities = new SDLRect[numsprites];

			rand = new Random();
			for(int i = 0; i < numsprites; i++){
				positions[i] = new SDLRect(
					(short)rand.nextInt(screen.m_Width - sprite.m_Width),
					(short)rand.nextInt(screen.m_Height - sprite.m_Height),
					(short)sprite.m_Height,
					(short)sprite.m_Width);
				velocities[i] = new SDLRect(0, 0, 0, 0);
				while(velocities[i].m_X == 0 && velocities[i].m_Y == 0){
					velocities[i].m_X = (short)(rand.nextInt(MAX_SPEED*2+1) - MAX_SPEED);
					velocities[i].m_Y = (short)(rand.nextInt(MAX_SPEED*2+1) - MAX_SPEED);
				}
			}
			// background
			long then = System.currentTimeMillis();
			EventDispatcher dispatch = new EventDispatcher(true);
			dispatch.registerEventListener(new sdl.event.EventListener() {
					public boolean incomingEvents(SDLCustomEvent event)
						throws SDLEventException {
						if (event instanceof SDLQuitEvent) {
							done = true;
						}
						else if (event instanceof SDLKeyboardEvent) {
							done = true;
						}
						return true;
					}
				});
			dispatch.startItNow();
			done = false;
			int frames = 0;
			sprites_visible = false;
			while(!done)
			{
				++frames;
				MoveSprites();
			}
			long now = System.currentTimeMillis();
			if(now > then){
				System.out.println(((double)frames * 1000.0) / (now - then) + " frames per second");
			}
			this.SDL_Quit();
		}
	public static void main(String args[]) {
		testsprite instance = new testsprite();
		instance.Start();
	}
}
