Source

py-sdl2 / sdl2 / test / render_test.py

Marcus von Appen c21c202 





Marcus von Appen d75539b 
Marcus von Appen c21c202 
Marcus von Appen 8159897 



Marcus von Appen d5a140a 
Marcus von Appen c21c202 





























































Marcus von Appen d75539b 
Marcus von Appen c21c202 


















Marcus von Appen d5a140a 




Marcus von Appen c21c202 












































Marcus von Appen d5a140a 

Marcus von Appen c21c202 




























Marcus von Appen d5a140a 

Marcus von Appen c21c202 

Marcus von Appen 8159897 

Marcus von Appen c21c202 





























Marcus von Appen 3040c6d 


Marcus von Appen c21c202 





Marcus von Appen 3040c6d 

Marcus von Appen c21c202 





















Marcus von Appen d5a140a 

Marcus von Appen c21c202 

Marcus von Appen d5a140a 


Marcus von Appen c21c202 





Marcus von Appen 8159897 

Marcus von Appen c21c202 











Marcus von Appen d5a140a 



Marcus von Appen c21c202 














Marcus von Appen d5a140a 

Marcus von Appen c21c202 


















Marcus von Appen d5a140a 



Marcus von Appen c21c202 

























Marcus von Appen d5a140a 



Marcus von Appen c21c202 























Marcus von Appen d75539b 
Marcus von Appen c21c202 


Marcus von Appen d75539b 
Marcus von Appen c21c202 







Marcus von Appen d5a140a 

Marcus von Appen c21c202 













Marcus von Appen d5a140a 

Marcus von Appen c21c202 

















Marcus von Appen d5a140a 

Marcus von Appen c21c202 




































Marcus von Appen d5a140a 

Marcus von Appen c21c202 



























Marcus von Appen d5a140a 

Marcus von Appen c21c202 















Marcus von Appen d5a140a 


Marcus von Appen c21c202 
Marcus von Appen d5a140a 

Marcus von Appen c21c202 













Marcus von Appen d5a140a 

Marcus von Appen c21c202 






































Marcus von Appen d75539b 

Marcus von Appen 390d83d 

Marcus von Appen c21c202 


















Marcus von Appen d75539b 












Marcus von Appen c21c202 
Marcus von Appen d75539b 
Marcus von Appen c21c202 

















































Marcus von Appen f095c03 



Marcus von Appen 7572957 


Marcus von Appen c21c202 

  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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import sys
import unittest
from ctypes import byref, POINTER, c_int
from .. import SDL_Init, SDL_Quit, SDL_INIT_EVERYTHING
from ..stdinc import Uint8, Uint32, SDL_TRUE, SDL_FALSE
from .. import render, video, surface, pixels, blendmode, rect
from ..ext.pixelaccess import PixelView

_ISPYPY = hasattr(sys, "pypy_version_info")
if _ISPYPY:
    import gc


# TODO: mostly positive tests, improve this!
class SDLRenderTest(unittest.TestCase):
    __tags__ = ["sdl"]

    def setUp(self):
        SDL_Init(SDL_INIT_EVERYTHING)

    def tearDown(self):
        SDL_Quit()

    def test_SDL_RendererInfo(self):
        info = render.SDL_RendererInfo()
        self.assertIsInstance(info, render.SDL_RendererInfo)

    def test_SDL_Renderer(self):
        val = render.SDL_Renderer()
        self.assertIsInstance(val, render.SDL_Renderer)

    def test_SDL_Texture(self):
        val = render.SDL_Texture()
        self.assertIsInstance(val, render.SDL_Texture)

    def test_SDL_GetNumRenderDrivers(self):
        val = render.SDL_GetNumRenderDrivers()
        self.assertGreaterEqual(val, 1)

    def test_SDL_GetRenderDriverInfo(self):
        success = False
        drivers = render.SDL_GetNumRenderDrivers()
        for x in range(drivers):
            info = render.SDL_RendererInfo()
            ret = render.SDL_GetRenderDriverInfo(x, byref(info))
            self.assertEqual(ret, 0)
            # We must find at least one software renderer
            if info.name == b"software":
                success = True
        self.assertTrue(success, "failed on retrieving the driver information")

#        self.assertRaises((AttributeError, TypeError),
#                          render.SDL_GetRenderDriverInfo, None)
#        self.assertRaises((AttributeError, TypeError),
#                          render.SDL_GetRenderDriverInfo, "Test")
#        self.assertRaises((AttributeError, TypeError),
#                          render.SDL_GetRenderDriverInfo, self)
#        #self.assertRaises(sdl.SDLError, render.SDL_GetRenderDriverInfo, -1)

    def test_SDL_CreateWindowAndRenderer(self):
        window = POINTER(video.SDL_Window)()
        renderer = POINTER(render.SDL_Renderer)()
        ret = render.SDL_CreateWindowAndRenderer \
            (10, 10, video.SDL_WINDOW_HIDDEN, byref(window), byref(renderer))
        self.assertEqual(ret, 0)

        render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

        # TODO: the code below works, too - is that really expected from SDL?
        #window, renderer = render.SDL_CreateWindowAndRenderer \
        #   (-10, -10, video.SDL_WINDOW_HIDDEN)
        #self.assertIsInstance(window, video.SDL_Window)
        #self.assertIsInstance(renderer, render.SDL_Renderer)

    def test_SDL_CreateDestroyRenderer(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                     video.SDL_WINDOW_SHOWN)
        self.assertIsInstance(window.contents, video.SDL_Window)

        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer(window, i,
                                              render.SDL_RENDERER_ACCELERATED)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)
            render.SDL_DestroyRenderer(renderer)

            # TODO: using -1 as index for the call below leads to random
            # access violations on Win32
            renderer = render.SDL_CreateRenderer(window, i,
                                              render.SDL_RENDERER_SOFTWARE)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)
            render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

    def test_SDL_CreateSoftwareRenderer(self):
        sf = surface.SDL_CreateRGBSurface(0, 100, 100, 32,
                                          0xFF000000,
                                          0x00FF0000,
                                          0x0000FF00,
                                          0x000000FF)
        renderer = render.SDL_CreateSoftwareRenderer(sf)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)
        render.SDL_DestroyRenderer(renderer)
        surface.SDL_FreeSurface(sf)

        #self.assertRaises((AttributeError, TypeError),
        #                  render.SDL_CreateSoftwareRenderer, None)
        #self.assertRaises((AttributeError, TypeError),
        #                  render.SDL_CreateSoftwareRenderer, 1234)

    def test_SDL_GetRenderer(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_GetRenderer(window)
        self.assertFalse(renderer)
        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer(window, i,
                                                 render.SDL_RENDERER_SOFTWARE)
            ren = render.SDL_GetRenderer(window)
            self.assertIsInstance(ren.contents, render.SDL_Renderer)
            render.SDL_DestroyRenderer(renderer)
            self.assertFalse(render.SDL_GetRenderer(window))

        video.SDL_DestroyWindow(window)
        self.assertFalse(render.SDL_GetRenderer(window))
        #self.assertRaises((AttributeError, TypeError),
        #                  render.SDL_GetRenderer, None)
        #self.assertRaises((AttributeError, TypeError),
        #                  render.SDL_GetRenderer, "Test")

    def test_SDL_GetRendererInfo(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)

        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer(window, i,
                                                 render.SDL_RENDERER_SOFTWARE)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)
            info = render.SDL_RendererInfo()
            ret = render.SDL_GetRendererInfo(renderer, byref(info))
            self.assertEqual(ret, 0)
            render.SDL_DestroyRenderer(renderer)

            #self.assertRaises(sdl.SDLError, render.SDL_GetRendererInfo,
            #                  renderer)

        video.SDL_DestroyWindow(window)
        self.assertRaises((AttributeError, TypeError),
                          render.SDL_GetRendererInfo, None)
        self.assertRaises((AttributeError, TypeError),
                          render.SDL_GetRendererInfo, "Test")

    def test_SDL_CreateDestroyTexture(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_SOFTWARE)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)

        formats = (pixels.SDL_PIXELFORMAT_ARGB8888,
                   pixels.SDL_PIXELFORMAT_RGB555,
                   pixels.SDL_PIXELFORMAT_RGBA4444,
                   pixels.SDL_PIXELFORMAT_RGBA8888,
                   pixels.SDL_PIXELFORMAT_ARGB2101010,
                   pixels.SDL_PIXELFORMAT_YUY2
                   )
        access = (render.SDL_TEXTUREACCESS_STATIC,
                  render.SDL_TEXTUREACCESS_STREAMING,
                  render.SDL_TEXTUREACCESS_TARGET)
        for fmt in formats:
            for acc in access:
                for w in range(1, 300, 5):
                    for h in range(1, 300, 5):
                        tex = render.SDL_CreateTexture(renderer, fmt, acc,
                                                       w, h)
                        self.assertIsInstance(tex.contents, render.SDL_Texture)
                        render.SDL_DestroyTexture(tex)
                    if _ISPYPY and (w % 50) == 0:
                        gc.collect()

        #self.assertRaises(sdl.SDLError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, 1, -10, 10)
        #self.assertRaises(sdl.SDLError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, 1, 10, -10)
        #self.assertRaises(sdl.SDLError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, 1, -10, -10)
        #self.assertRaises(ValueError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, -5, 10, 10)
        #self.assertRaises(ValueError, render.SDL_CreateTexture, renderer,
        #                  - 10, 1, 10, 10)
        #self.assertRaises((AttributeError, TypeError),
        #                  render.SDL_CreateTexture, None,
        #                  pixels.SDL_PIXELFORMAT_RGB555, 1, 10, 10)
        #self.assertRaises((AttributeError, TypeError),
        #                  render.SDL_CreateTexture, "Test",
        #                  pixels.SDL_PIXELFORMAT_RGB555, 1, 10, 10)
        #self.assertRaises(ValueError, render.SDL_CreateTexture, renderer,
        #                  "Test", 1, 10, 10)
        #self.assertRaises(ValueError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, None, 10, 10)
        #self.assertRaises(ValueError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, "Test", 10, 10)

        render.SDL_DestroyRenderer(renderer)
        #self.assertRaises(sdl.SDLError, render.SDL_CreateTexture, renderer,
        #                  pixels.SDL_PIXELFORMAT_RGB555, 1, 10, 10)
        video.SDL_DestroyWindow(window)

    def test_SDL_CreateTextureFromSurface(self):
        sf = surface.SDL_CreateRGBSurface(0, 100, 100, 32, 0xFF000000,
                                          0x00FF0000, 0x0000FF00, 0x000000FF)
        self.assertIsInstance(sf.contents, surface.SDL_Surface)
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_SOFTWARE)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)
        tex = render.SDL_CreateTextureFromSurface(renderer, sf)
        self.assertIsInstance(tex.contents, render.SDL_Texture)

    def test_SDL_QueryTexture(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_SOFTWARE)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)

        formats = (pixels.SDL_PIXELFORMAT_ARGB8888,
                   pixels.SDL_PIXELFORMAT_RGB555,
                   pixels.SDL_PIXELFORMAT_RGBA4444,
                   pixels.SDL_PIXELFORMAT_ARGB2101010,
                   pixels.SDL_PIXELFORMAT_YUY2
                   )
        access = (render.SDL_TEXTUREACCESS_STATIC,
                  render.SDL_TEXTUREACCESS_STREAMING,
                  render.SDL_TEXTUREACCESS_TARGET)
        for fmt in formats:
            for acc in access:
                for w in range(1, 300, 5):
                    for h in range(1, 300, 5):
                        tex = render.SDL_CreateTexture(renderer, fmt, acc,
                                                       w, h)
                        self.assertIsInstance(tex.contents, render.SDL_Texture)
                        qf, qa, qw, qh = Uint32(), c_int(), c_int(), c_int()
                        ret = render.SDL_QueryTexture(tex, byref(qf),
                                                      byref(qa), byref(qw),
                                                      byref(qh))
                        self.assertEqual(ret, 0)
                        self.assertEqual(qf.value, fmt)
                        self.assertEqual(qa.value, acc)
                        self.assertEqual(qw.value, w)
                        self.assertEqual(qh.value, h)
                        render.SDL_DestroyTexture(tex)
                    if _ISPYPY and (w % 50) == 0:
                        gc.collect()

        render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetTextureColorMod(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_SOFTWARE)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)

        tex = render.SDL_CreateTexture(renderer,
                                       pixels.SDL_PIXELFORMAT_ARGB8888,
                                       render.SDL_TEXTUREACCESS_STREAMING,
                                       10, 10)
        self.assertIsInstance(tex.contents, render.SDL_Texture)
        colors = ((16, 22, 185),
                  (32, 64, 128),
                  (64, 32, 128),
                  (64, 32, 255),
                  (255, 32, 64),
                  (255, 32, 128),
                  (0, 0, 0),
                  (255, 255, 255),
                  (128, 128, 128),
                  )
        for r, g, b in colors:
            ret = render.SDL_SetTextureColorMod(tex, r, g, b)
            self.assertEqual(ret, 0)
            tr, tg, tb = Uint8(), Uint8(), Uint8()
            ret = render.SDL_GetTextureColorMod(tex, byref(tr), byref(tg),
                                                byref(tb))
            self.assertEqual(ret, 0)
            self.assertEqual((tr.value, tg.value, tb.value), (r, g, b))

        render.SDL_DestroyTexture(tex)
        #self.assertRaises(sdl.SDLError, render.SDL_SetTextureColorMod, tex,
        #                  10, 20, 30)
        #self.assertRaises(sdl.SDLError, render.SDL_GetTextureColorMod, tex)

        render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetTextureAlphaMod(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_SOFTWARE)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)

        tex = render.SDL_CreateTexture(renderer,
                                       pixels.SDL_PIXELFORMAT_ARGB8888,
                                       render.SDL_TEXTUREACCESS_STREAMING,
                                       10, 10)
        self.assertIsInstance(tex.contents, render.SDL_Texture)

        for alpha in range(0, 255):
            ret = render.SDL_SetTextureAlphaMod(tex, alpha)
            self.assertEqual(ret, 0)
            talpha = Uint8()
            ret = render.SDL_GetTextureAlphaMod(tex, byref(talpha))
            self.assertEqual(ret, 0)
            self.assertEqual(talpha.value, alpha)

        render.SDL_DestroyTexture(tex)
        #self.assertRaises(sdl.SDLError, render.SDL_SetTextureColorMod, tex,
        #                  10, 20, 30)
        #self.assertRaises(sdl.SDLError, render.SDL_GetTextureColorMod, tex)

        render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetTextureBlendMode(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_SOFTWARE)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)

        tex = render.SDL_CreateTexture(renderer,
                                       pixels.SDL_PIXELFORMAT_ARGB8888,
                                       render.SDL_TEXTUREACCESS_STREAMING,
                                       10, 10)
        self.assertIsInstance(tex.contents, render.SDL_Texture)

        modes = (blendmode.SDL_BLENDMODE_NONE,
                 blendmode.SDL_BLENDMODE_ADD,
                 blendmode.SDL_BLENDMODE_BLEND,
                 blendmode.SDL_BLENDMODE_MOD,
                 )
        for mode in modes:
            ret = render.SDL_SetTextureBlendMode(tex, mode)
            self.assertEqual(ret, 0)
            tmode = blendmode.SDL_BlendMode()
            ret = render.SDL_GetTextureBlendMode(tex, byref(tmode))
            self.assertEqual(ret, 0)
            self.assertEqual(tmode.value, mode)

        render.SDL_DestroyTexture(tex)
        #self.assertRaises(sdl.SDLError, render.SDL_SetTextureBlendMode, tex,
        #                  modes[2])
        #self.assertRaises(sdl.SDLError, render.SDL_GetTextureBlendMode, tex)

        render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

    @unittest.skip("not implemented")
    def test_SDL_UpdateTexture(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_LockUnlockTexture(self):
        pass

    def test_SDL_RenderTargetSupported(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)

        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer\
                (window, i, render.SDL_RENDERER_ACCELERATED)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)

            val = render.SDL_RenderTargetSupported(renderer)
            self.assertIn(val, (SDL_TRUE, SDL_FALSE))
            render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetRenderTarget(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)

        skipcount = 0
        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer \
                (window, i, render.SDL_RENDERER_ACCELERATED)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)

            supported = render.SDL_RenderTargetSupported(renderer)
            if not supported:
                skipcount += 1
                render.SDL_DestroyRenderer(renderer)
                continue

            ret = render.SDL_SetRenderTarget(renderer, None)
            self.assertEqual(ret, 0)
            self.assertFalse(render.SDL_GetRenderTarget(renderer))

            tex = render.SDL_CreateTexture(renderer,
                                           pixels.SDL_PIXELFORMAT_ARGB8888,
                                           render.SDL_TEXTUREACCESS_TARGET,
                                           10, 10)
            ret = render.SDL_SetRenderTarget(renderer, tex)
            self.assertEqual(ret, 0)
            tgt = render.SDL_GetRenderTarget(renderer)
            self.assertIsInstance(tgt.contents, render.SDL_Texture)
            render.SDL_DestroyTexture(tex)

            # TODO: Check in the SDL codebase, why the code below does
            # not fail...
            # tex2 = render.SDL_CreateTexture(renderer,
            #                              pixels.SDL_PIXELFORMAT_ARGB8888,
            #                              render.SDL_TEXTUREACCESS_STREAMING,
            #                              10, 10)
            # self.assertRaises(SDLError, render.SDL_SetRenderTarget, renderer,
            #                   tex2)
            # render.SDL_DestroyTexture(tex2)

            render.SDL_DestroyRenderer(renderer)
        video.SDL_DestroyWindow(window)

        if skipcount == render.SDL_GetNumRenderDrivers():
            self.skipTest("None of the renderers supports render targets")

    def test_SDL_RenderGetSetViewport(self):
        rects = (rect.SDL_Rect(0, 0, 0, 0),
                 rect.SDL_Rect(0, 0, 10, 10),
                 rect.SDL_Rect(3, 3, 5, 5),
                 rect.SDL_Rect(-5, -5, 10, 10),
                 rect.SDL_Rect(10, 10, 10, 10),
                 rect.SDL_Rect(0, 0, -10, -10),
                 rect.SDL_Rect(-10, 0, 10, 10),
                 rect.SDL_Rect(0, -10, 10, 10),
                 rect.SDL_Rect(-10, -10, 10, 10),
            )
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN |
                                        video.SDL_WINDOW_BORDERLESS)
        self.assertIsInstance(window.contents, video.SDL_Window)

        failcount = 0
        port = rect.SDL_Rect()
        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer \
                (window, i, render.SDL_RENDERER_ACCELERATED)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)
            ret = render.SDL_RenderSetViewport(renderer, None)
            self.assertEqual(ret, 0)
            render.SDL_RenderGetViewport(renderer, byref(port))
            self.assertEqual(port, rect.SDL_Rect(0, 0, 10, 10))
            for r in rects:
                if r.w == r.h == 0:
                    # http://bugzilla.libsdl.org/show_bug.cgi?id=1622
                    # OpenGL renderers cause a exception here.
                    continue
                ret = render.SDL_RenderSetViewport(renderer, r)
                self.assertEqual(ret, 0)
                render.SDL_RenderGetViewport(renderer, byref(port))
                if port != r:
                    failcount += 1

            render.SDL_DestroyRenderer(renderer)
        if failcount > 0:
            unittest.skip("""for some reason, even with correct values, this
seems to fail on creating the second renderer of the window, if any""")
        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetRenderDrawColor(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)

        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer \
                (window, i, render.SDL_RENDERER_ACCELERATED)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)

            colors = ((16, 22, 185, 217),
                      (32, 64, 128, 255),
                      (64, 32, 128, 255),
                      (64, 32, 255, 128),
                      (255, 32, 64, 128),
                      (255, 32, 128, 64),
                      (0, 0, 0, 0),
                      (255, 255, 255, 255),
                      (128, 128, 128, 255),
                      )
            for r, g, b, a in colors:
                ret = render.SDL_SetRenderDrawColor(renderer, r, g, b, a)
                self.assertEqual(ret, 0)
                dr, dg, db, da = Uint8(), Uint8(), Uint8(), Uint8()
                ret = render.SDL_GetRenderDrawColor(renderer, byref(dr),
                                                    byref(dg), byref(db),
                                                    byref(da))
                self.assertEqual(ret, 0)
                self.assertEqual((dr.value, dg.value, db.value, da.value),
                                 (r, g, b, a))
            render.SDL_DestroyRenderer(renderer)
            #self.assertRaises(sdl.SDLError, render.SDL_SetRenderDrawColor,
            #                  renderer, 10, 20, 30, 40)
            #self.assertRaises(sdl.SDLError, render.SDL_GetRenderDrawColor,
            #                  renderer)

        video.SDL_DestroyWindow(window)

    def test_SDL_GetSetRenderDrawBlendMode(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)

        for i in range(render.SDL_GetNumRenderDrivers()):
            renderer = render.SDL_CreateRenderer \
                (window, i, render.SDL_RENDERER_ACCELERATED)
            self.assertIsInstance(renderer.contents, render.SDL_Renderer)

            modes = (blendmode.SDL_BLENDMODE_NONE,
                     blendmode.SDL_BLENDMODE_ADD,
                     blendmode.SDL_BLENDMODE_BLEND,
                     blendmode.SDL_BLENDMODE_MOD,
                     )
            for mode in modes:
                ret = render.SDL_SetRenderDrawBlendMode(renderer, mode)
                bmode = blendmode.SDL_BlendMode()
                ret = render.SDL_GetRenderDrawBlendMode(renderer, byref(bmode))
                self.assertEqual(ret, 0)
                self.assertEqual(bmode.value, mode)
            render.SDL_DestroyRenderer(renderer)
            #self.assertRaises(sdl.SDLError, render.SDL_SetRenderDrawBlendMode,
            #                  renderer, video.SDL_BLENDMODE_ADD)
            #self.assertRaises(sdl.SDLError, render.SDL_GetRenderDrawBlendMode,
            #                  renderer)
        video.SDL_DestroyWindow(window)

    def test_SDL_RenderClear(self):
        window = video.SDL_CreateWindow(b"Test", 10, 10, 10, 10,
                                        video.SDL_WINDOW_HIDDEN)
        self.assertIsInstance(window.contents, video.SDL_Window)
        renderer = render.SDL_CreateRenderer(window, -1,
                                             render.SDL_RENDERER_ACCELERATED)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)

        ret = render.SDL_RenderClear(renderer)
        self.assertEqual(ret, 0)
        render.SDL_DestroyRenderer(renderer)
        #self.assertRaises(sdl.SDLError, render.SDL_RenderClear, renderer)
#        self.assertRaises((AttributeError, TypeError),
#                          render.SDL_RenderClear, None)
#        self.assertRaises((AttributeError, TypeError),
#                          render.SDL_RenderClear, "Test")
#        self.assertRaises((AttributeError, TypeError),
#                          render.SDL_RenderClear, 123456)

    @unittest.skipIf(hasattr(sys, "pypy_version_info"),
                     "PyPy's ctypes can't do byref(value, offset)")
    @unittest.skipIf(sys.platform=="cli",
                     "IronPython can't cast values array values correctly")
    def test_SDL_RenderDrawPoint(self):
        points = ((-4, -3), (-4, 3), (4, -3),
                  (0, 0), (1, 1), (10, 10), (99, 99),
                  (4, 22), (57, 88), (45, 15),
                  (100, 100)
                  )
        r, g, b, a = 0xAA, 0xBB, 0xCC, 0xDD
        w, h = 100, 100
        sf = surface.SDL_CreateRGBSurface(0, w, h, 32, 0xFF000000, 0x00FF0000,
                                          0x0000FF00, 0x000000FF)
        color = pixels.SDL_MapRGBA(sf.contents.format, r, g, b, a)
        renderer = render.SDL_CreateSoftwareRenderer(sf)
        self.assertIsInstance(renderer.contents, render.SDL_Renderer)
        ret = render.SDL_SetRenderDrawColor(renderer, r, g, b, a)
        self.assertEqual(ret, 0)
        for x, y in points:
            ret = render.SDL_RenderDrawPoint(renderer, x, y)
            self.assertEqual(ret, 0)
        render.SDL_RenderPresent(renderer)
        view = PixelView(sf.contents)
        for x, y in points:
            npx = max(x + 1, w)
            npy = max(y + 1, h)
            ppx = max(x - 1, 0)
            ppy = max(y - 1, 0)
            if x < 0 or x >= w or y < 0 or y >= h:
                continue
            self.assertEqual(hex(view[y][x]), hex(color))
            if (npx, npy) not in points:
                self.assertNotEqual(hex(view[npy][npx]), hex(color))
            if (ppx, ppy) not in points:
                self.assertNotEqual(hex(view[ppy][ppx]), hex(color))
        render.SDL_DestroyRenderer(renderer)
        del view
        surface.SDL_FreeSurface(sf)

    @unittest.skip("not implemented")
    def test_SDL_RenderDrawPoints(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderDrawLine(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderDrawLines(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderDrawRect(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderDrawRects(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderFillRect(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderFillRects(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderCopy(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderReadPixels(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderPresent(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderGetSetScale(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderGetSetLogicalSize(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_RenderGetSetClipRect(self):
        pass

    @unittest.skip("not implemented")
    def test_SDL_GetRendererOutputSize(self):
        pass

if __name__ == '__main__':
    sys.exit(unittest.main())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.