File: blit_test.py

package info (click to toggle)
pygame 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 42,624 kB
  • sloc: ansic: 66,926; python: 48,742; javascript: 1,153; objc: 224; sh: 121; makefile: 59; cpp: 25
file content (192 lines) | stat: -rw-r--r-- 6,343 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import unittest

import pygame
from pygame.locals import *

from time import time


class BlitTest(unittest.TestCase):
    def test_SRCALPHA(self):
        """SRCALPHA tests."""
        # blend(s, 0, d) = d
        s = pygame.Surface((1, 1), SRCALPHA, 32)
        s.fill((255, 255, 255, 0))

        d = pygame.Surface((1, 1), SRCALPHA, 32)
        d.fill((0, 0, 255, 255))

        s.blit(d, (0, 0))
        self.assertEqual(s.get_at((0, 0)), d.get_at((0, 0)))

        # blend(s, 255, d) = s
        s = pygame.Surface((1, 1), SRCALPHA, 32)
        s.fill((123, 0, 0, 255))
        s1 = pygame.Surface((1, 1), SRCALPHA, 32)
        s1.fill((123, 0, 0, 255))
        d = pygame.Surface((1, 1), SRCALPHA, 32)
        d.fill((10, 0, 0, 0))
        s.blit(d, (0, 0))
        self.assertEqual(s.get_at((0, 0)), s1.get_at((0, 0)))

        # TODO: these should be true too.
        # blend(0, sA, 0) = 0
        # blend(255, sA, 255) = 255
        # blend(s, sA, d) <= 255

    def test_BLEND(self):
        """BLEND_ tests."""

        # test that it doesn't overflow, and that it is saturated.
        s = pygame.Surface((1, 1), SRCALPHA, 32)
        s.fill((255, 255, 255, 0))

        d = pygame.Surface((1, 1), SRCALPHA, 32)
        d.fill((0, 0, 255, 255))

        s.blit(d, (0, 0), None, BLEND_ADD)

        # print("d %s" % (d.get_at((0,0)),))
        # print(s.get_at((0,0)))
        # self.assertEqual(s.get_at((0,0))[2], 255 )
        # self.assertEqual(s.get_at((0,0))[3], 0 )

        s.blit(d, (0, 0), None, BLEND_RGBA_ADD)
        # print(s.get_at((0,0)))
        self.assertEqual(s.get_at((0, 0))[3], 255)

        # test adding works.
        s.fill((20, 255, 255, 0))
        d.fill((10, 0, 255, 255))
        s.blit(d, (0, 0), None, BLEND_ADD)
        self.assertEqual(s.get_at((0, 0))[2], 255)

        # test subbing works.
        s.fill((20, 255, 255, 0))
        d.fill((10, 0, 255, 255))
        s.blit(d, (0, 0), None, BLEND_SUB)
        self.assertEqual(s.get_at((0, 0))[0], 10)

        # no overflow in sub blend.
        s.fill((20, 255, 255, 0))
        d.fill((30, 0, 255, 255))
        s.blit(d, (0, 0), None, BLEND_SUB)
        self.assertEqual(s.get_at((0, 0))[0], 0)


class BlitsTest(unittest.TestCase):
    """Tests for pygame.Surface.blits"""

    def setUp(self):
        self.NUM_SURFS = 255
        self.PRINT_TIMING = 0
        self.dst = pygame.Surface((self.NUM_SURFS * 10, 10), SRCALPHA, 32)
        self.dst.fill((230, 230, 230))
        self.blit_list = self.make_blit_list(self.NUM_SURFS)

    def make_blit_list(self, num_surfs):
        """Generate a list of tuples representing surfaces and destinations
        for blitting"""

        blit_list = []
        for i in range(num_surfs):
            dest = (i * 10, 0)
            surf = pygame.Surface((10, 10), SRCALPHA, 32)
            color = (i * 1, i * 1, i * 1)
            surf.fill(color)
            blit_list.append((surf, dest))
        return blit_list

    def custom_blits(self, blit_list):
        """Custom blits method that manually iterates over the blit_list and blits
        each surface onto the destination."""

        for surface, dest in blit_list:
            self.dst.blit(surface, dest)

    def test_custom_blits_performance(self):
        """Checks time performance of the custom blits method"""

        t0 = time()
        results = self.custom_blits(self.blit_list)
        t1 = time()
        if self.PRINT_TIMING:
            print(f"python blits: {t1 - t0}")

    def test_blits_performance(self):
        """Checks time performance of blits"""

        t0 = time()
        results = self.dst.blits(self.blit_list)
        t1 = time()
        if self.PRINT_TIMING:
            print(f"Surface.blits: {t1 - t0}")

        # Measure time performance of blits with doreturn=0
        t0 = time()
        results = self.dst.blits(self.blit_list, doreturn=0)
        t1 = time()
        if self.PRINT_TIMING:
            print(f"Surface.blits doreturn=0: {t1 - t0}")

        # Measure time performance of blits using a generator
        t0 = time()
        results = self.dst.blits(((surf, dest) for surf, dest in self.blit_list))
        t1 = time()
        if self.PRINT_TIMING:
            print(f"Surface.blits generator: {t1 - t0}")

    def test_blits_correctness(self):
        """Checks the correctness of the colors on the destination
        after blitting and tests that the length of the results list
        matches the number of surfaces blitted."""

        results = self.dst.blits(self.blit_list)
        for i in range(self.NUM_SURFS):
            color = (i * 1, i * 1, i * 1)
            self.assertEqual(self.dst.get_at((i * 10, 0)), color)
            self.assertEqual(self.dst.get_at(((i * 10) + 5, 5)), color)

        self.assertEqual(len(results), self.NUM_SURFS)

    def test_blits_doreturn(self):
        """Tests that when doreturn=0, it returns None"""

        results = self.dst.blits(self.blit_list, doreturn=0)
        self.assertEqual(results, None)

    def test_blits_not_sequence(self):
        """Tests that calling blits with an invalid non-sequence None argument
        raises a ValueError."""

        dst = pygame.Surface((100, 10), SRCALPHA, 32)
        with self.assertRaises(ValueError):
            dst.blits(None)

    def test_blits_wrong_length(self):
        """Tests that calling blits with an invalid sequence containing a single surface
        (without a destination) raises a ValueError."""

        dst = pygame.Surface((100, 10), SRCALPHA, 32)
        with self.assertRaises(ValueError):
            dst.blits([pygame.Surface((10, 10), SRCALPHA, 32)])

    def test_blits_bad_surf_args(self):
        """Tests that calling blits with a sequence containing an invalid tuple of
        None arguments raises a TypeError."""

        dst = pygame.Surface((100, 10), SRCALPHA, 32)
        with self.assertRaises(TypeError):
            dst.blits([(None, None)])

    def test_blits_bad_dest(self):
        """Tests that calling blits with a sequence containing an invalid tuple with a
        destination of None raises a TypeError."""

        dst = pygame.Surface((100, 10), SRCALPHA, 32)
        with self.assertRaises(TypeError):
            dst.blits([(pygame.Surface((10, 10), SRCALPHA, 32), None)])


if __name__ == "__main__":
    unittest.main()