def test_pygame2_sdl_time_remove_timer(self):

        # __doc__ (as of 2010-01-06) for pygame2.sdl.time.remove_timer:

        # remove_timer (timerobj) -> None
        #
        # Removes a previously added timer callback.
        #
        # Removes a previously added timer callback and throws an
        # exception, if the passed object is not a matching timer
        # object.

        def _timercb(flag):
            flag.append(1)
            return 10

        self.assertRaises(pygame2.Error, sdltime.remove_timer, _timercb)
        sdltime.init()
        self.assertRaises(TypeError, sdltime.remove_timer, None)

        flag1 = []
        tobj = sdltime.add_timer(10, _timercb, (flag1,))
        self.assertTrue(tobj != None)
        t1 = t2 = sdltime.get_ticks()
        while t2 - t1 < 50:
            sdltime.delay(1)
            t2 = sdltime.get_ticks()
        sdltime.remove_timer(tobj)
        self.assertRaises(ValueError, sdltime.remove_timer, tobj)

        flag2 = []
        tobj = sdltime.add_timer(100, _timercb, (flag2,))
        sdltime.remove_timer(tobj)
        self.assertRaises(ValueError, sdltime.remove_timer, tobj)
        self.assertTrue(tobj != None)
        t1 = t2 = sdltime.get_ticks()
        while t2 - t1 < 50:
            sdltime.delay(1)
            t2 = sdltime.get_ticks()
        sdltime.quit()
        self.assertRaises(pygame2.Error, sdltime.remove_timer, _timercb)

        # This is important. If the test
        self.assertTrue(len(flag1) != 0)
        self.assertTrue(len(flag2) == 0)
    def test_pygame2_sdl_time_add_timer(self):

        # __doc__ (as of 2010-01-06) for pygame2.sdl.time.add_timer:

        # add_timer (interval, callable[, data]) -> CObject
        #
        # Adds a timer callback to be called periodically.
        #
        # Adds a timer callback to be called periodically using the
        # specified *interval*. *callable* can be any callable objet,
        # method or function. On invocation, the optional *data* will be
        # passed to the callable.
        #
        # This will return an CObject that acts as unique id for the
        # timer callback.
        setargs = []

        def _timercb(l, arg1, arg2):
            l.append(arg1)
            l.append(arg2)
            return 10

        self.assertRaises(pygame2.Error, sdltime.add_timer, _timercb)

        sdltime.init()
        tobj = sdltime.add_timer(10, _timercb, (setargs, "Hello", "World"))
        if tobj is None:
            sdltime.quit()
            self.fail()
        t1 = t2 = sdltime.get_ticks()
        while t2 - t1 < 50:
            sdltime.delay(1)
            t2 = sdltime.get_ticks()
        sdltime.quit()
        self.assertRaises(pygame2.Error, sdltime.add_timer, _timercb)

        # This is important - if we run the assertion test before quitting,
        # the time module won't be quitted and the timers will still exist,
        # causing thread issues in other tests.
        self.assertTrue(len(setargs) > 5)
    def test_pygame2_sdl_time_get_ticks(self):

        # __doc__ (as of 2010-01-06) for pygame2.sdl.time.get_ticks:

        # get_ticks () -> long
        #
        # Gets the number of milliseconds since the initialization of
        # the underlying SDL library.
        #
        # Gets the number of milliseconds since the initialization of
        # the underlying SDL library. The value will wrap if the program
        # runs for more than ~49 days.

        # Can't test this with useful information...
        self.assertRaises(pygame2.Error, sdltime.get_ticks)
        sdltime.init()
        sdltime.delay(20)
        self.assertTrue(sdltime.get_ticks() > 0)
        sdltime.quit()
    def draw(self, surface, bgd=None):
        """draw all sprites in the right order onto the passed surface.
        LayeredDirty.draw(surface, bgd=None): return Rect_list

        You can pass the background too. If a background is already set, 
        then the bgd argument has no effect.
        """
        # speedups
        _orig_clip = surface.get_clip()
        _clip = self._clip
        if _clip is None:
            _clip = _orig_clip
        
        
        _surf = surface
        _sprites = self._spritelist
        _old_rect = self.spritedict
        _update = self.lostsprites
        _update_append = _update.append
        _ret = None
        _surf_blit = _surf.blit
        _rect = Rect
        if bgd is not None:
            self._bgd = bgd
        _bgd = self._bgd
        
        _surf.set_clip(_clip)
        # -------
        # 0. deside if normal render of flip
        start_time = get_ticks()
        if self._use_update: # dirty rects mode
            # 1. find dirty area on screen and put the rects into _update
            # still not happy with that part
            for spr in _sprites:
                if 0 < spr.dirty:
                    # chose the right rect
                    if spr.source_rect:
                        _union_rect = _rect(spr.rect.topleft, spr.source_rect.size)
                    else:
                        _union_rect = _rect(spr.rect)
                        
                    _union_rect_collidelist = _union_rect.collidelist
                    _union_rect_union_ip = _union_rect.union_ip
                    i = _union_rect_collidelist(_update)
                    while -1 < i:
                        _union_rect_union_ip(_update[i])
                        del _update[i]
                        i = _union_rect_collidelist(_update)
                    _update_append(_union_rect.clip(_clip))
                    
                    _union_rect = _rect(_old_rect[spr])
                    _union_rect_collidelist = _union_rect.collidelist
                    _union_rect_union_ip = _union_rect.union_ip
                    i = _union_rect_collidelist(_update)
                    while -1 < i:
                        _union_rect_union_ip(_update[i])
                        del _update[i]
                        i = _union_rect_collidelist(_update)
                    _update_append(_union_rect.clip(_clip))
            # can it be done better? because that is an O(n**2) algorithm in
            # worst case
                    
            # clear using background
            if _bgd is not None:
                for rec in _update:
                    _surf_blit(_bgd, rec, rec)
                
            # 2. draw
            for spr in _sprites:
                if 1 > spr.dirty:
                    if spr._visible:
                        # sprite not dirty, blit only the intersecting part
                        _spr_rect = spr.rect
                        if spr.source_rect is not None:
                            _spr_rect = Rect(spr.rect.topleft, spr.source_rect.size)
                        _spr_rect_clip = _spr_rect.clip
                        for idx in _spr_rect.collidelistall(_update):
                            # clip
                            clip = _spr_rect_clip(_update[idx])
                            _surf_blit(spr.image, clip, \
                                       (clip[0]-_spr_rect[0], \
                                            clip[1]-_spr_rect[1], \
                                            clip[2], \
                                            clip[3]), spr.blendmode)
                else: # dirty sprite
                    if spr._visible:
                        _old_rect[spr] = _surf_blit(spr.image, spr.rect, \
                                               spr.source_rect, spr.blendmode)
                    if spr.dirty == 1:
                        spr.dirty = 0
            _ret = list(_update)
        else: # flip, full screen mode
            if _bgd is not None:
                _surf_blit(_bgd, (0, 0))
            for spr in _sprites:
                if spr._visible:
                    _old_rect[spr] = _surf_blit(spr.image, spr.rect, spr.source_rect,spr.blendmode)
            _ret = [_rect(_clip)] # return only the part of the screen changed
            
        
        # timing for switching modes
        # how to find a good treshold? it depends on the hardware it runs on
        end_time = get_ticks()
        if end_time-start_time > self._time_threshold:
            self._use_update = False
        else:
            self._use_update = True
            
##        # debug
##        print "               check: using dirty rects:", self._use_update
            
        # emtpy dirty reas list
        _update[:] = []
        
        # -------
        # restore original clip
        _surf.set_clip(_orig_clip)
        return _ret