#! /usr/bin/env python
#
#    <one line to give the program's name and a brief idea of what it does.>
#    Copyright (C) 2001  Michael Urman
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import pygame
from pygame.locals import *

pygame.display.init()
screen = pygame.display.set_mode((640,480), HWSURFACE|DOUBLEBUF)

bg = pygame.image.load('/home/mu/pic/haiku.jpg').convert()
x = 0
y = 0
w = bg.get_width()
h = bg.get_height()
dx = 1
dy = 1

quit = 0
update = 1

blits = 0
updates = 0

pygame.time.set_timer(USEREVENT, 20)
screen_blit = screen.blit
pygame_event_wait = pygame.event.wait
pygame_event_peek = pygame.event.peek
pygame_event_get = pygame.event.get
pygame_display_flip = pygame.display.flip

while 1:

    events = [pygame_event_wait()]
    if pygame_event_peek():
        events.extend(pygame_event_get())
    for event in events:
        if event.type == USEREVENT:
            update += 1
        elif event.type == QUIT:
            quit = 1
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                quit = 1
            elif event.key == K_RETURN:
                pygame.display.toggle_fullscreen()

    if update:

        blits += 1

        #screen_blit(bg, (0,0), (x,y, 640, 480))
        for sx in range(20):
            for sy in range(15):
                screen_blit(bg, (sx<<5, sy<<5), ((19-sx<<5)+x, (14-sy<<5)+y, 32, 32))

        pygame_display_flip()

        while update:
            updates += 1
            update -= 1
            if 0 <= x+dx < w-640:
                x += dx
            else:
                dx = -dx

            if 0 <= y+dy < h-480:
                y += dy
            else:
                dy = - dy

    if quit:
        break

print blits, 'blits and', updates, 'updates =>', updates-blits, 'drops'