render.c 27.5 KB
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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
/**
 * Automated SDL_Surface test.
 *
 * Written by Edgar Simo "bobbens"
 *
 * Released under Public Domain.
 */


#include "SDL.h"
#include "../SDL_at.h"

#include "../common/common.h"


/*
 * Pull in images for testcases.
 */
#include "../common/images.h"


#define SCREEN_W     80
#define SCREEN_H     60

#define FACE_W       img_face.width
#define FACE_H       img_face.height


/*
 * Prototypes.
 */
static int render_compare( const char *msg, const SurfaceImage_t *s, int allowable_error );
static int render_isSupported( int code );
static int render_hasDrawColor (void);
static int render_hasBlendModes (void);
static int render_hasTexColor (void);
static int render_hasTexAlpha (void);
static int render_clearScreen (void);
/* Testcases. */
static int render_testReadWrite (void);
static int render_testPrimitives (void);
static int render_testPrimitivesBlend (void);
static int render_testBlit (void);
static int render_testBlitColour (void);
static int render_testBlitAlpha (void);
static int render_testBlitBlendMode( SDL_Texture * tface, int mode );
static int render_testBlitBlend (void);


/**
 * @brief Compares screen pixels with image pixels.
 *
 *    @param msg Message on failure.
 *    @param s Image to compare against.
 *    @return 0 on success.
 */
static int render_compare( const char *msg, const SurfaceImage_t *s, int allowable_error )
{
   int ret;
   SDL_Rect rect;
   Uint8 pix[4*80*60];
   SDL_Surface *testsur;

   /* Read pixels. */
   /* Explicitly specify the rect in case the window isn't expected size... */
   rect.x = 0;
   rect.y = 0;
   rect.w = 80;
   rect.h = 60;
   ret = SDL_RenderReadPixels( &rect, FORMAT, pix, 80*4 );
   if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
      return 1;

   /* Create surface. */
   testsur = SDL_CreateRGBSurfaceFrom( pix, 80, 60, 32, 80*4,
                                       RMASK, GMASK, BMASK, AMASK);
   if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", testsur!=NULL ))
      return 1;

   /* Compare surface. */
   ret = surface_compare( testsur, s, allowable_error );
   if (SDL_ATassert( msg, ret==0 ))
      return 1;

   /* Clean up. */
   SDL_FreeSurface( testsur );

   return 0;
}

#if 0
static int dump_screen( int index )
{
   int ret;
   char name[1024];
   Uint8 pix[4*80*60];
   SDL_Surface *testsur;
   SDL_RendererInfo info;

   /* Read pixels. */
   ret = SDL_RenderReadPixels( NULL, FORMAT, pix, 80*4 );
   if (SDL_ATassert( "SDL_RenderReadPixels", ret==0) )
      return 1;

   /* Create surface. */
   testsur = SDL_CreateRGBSurfaceFrom( pix, 80, 60, 32, 80*4,
                                       RMASK, GMASK, BMASK, AMASK);
   if (SDL_ATassert( "SDL_CreateRGBSurfaceFrom", testsur!=NULL ))
      return 1;

   /* Dump surface. */
   SDL_GetRendererInfo(&info);
   sprintf(name, "%s-%s-%d.bmp", SDL_GetCurrentVideoDriver(), info.name, index);
   SDL_SaveBMP(testsur, name);

   /* Clean up. */
   SDL_FreeSurface( testsur );

   return 0;
}
#endif

/**
 * @brief Checks to see if functionality is supported.
 */
static int render_isSupported( int code )
{
   return (code == 0);
}


/**
 * @brief Test to see if we can vary the draw colour.
 */
static int render_hasDrawColor (void)
{
   int ret, fail;
   Uint8 r, g, b, a;

   fail = 0;

   /* Set colour. */
   ret = SDL_SetRenderDrawColor( 100, 100, 100, 100 );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetRenderDrawColor( &r, &g, &b, &a );
   if (!render_isSupported(ret))
      fail = 1;
   /* Restore natural. */
   ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
   if (!render_isSupported(ret))
      fail = 1;

   /* Something failed, consider not available. */
   if (fail)
      return 0;
   /* Not set properly, consider failed. */
   else if ((r != 100) || (g != 100) || (b != 100) || (a != 100))
      return 0;
   return 1;
}


/**
 * @brief Test to see if we can vary the blend mode.
 */
static int render_hasBlendModes (void)
{
   int fail;
   int ret;
   SDL_BlendMode mode;

   fail = 0;

   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_BLEND );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetRenderDrawBlendMode( &mode );
   if (!render_isSupported(ret))
      fail = 1;
   ret = (mode != SDL_BLENDMODE_BLEND);
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_ADD );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetRenderDrawBlendMode( &mode );
   if (!render_isSupported(ret))
      fail = 1;
   ret = (mode != SDL_BLENDMODE_ADD);
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetRenderDrawBlendMode( &mode );
   if (!render_isSupported(ret))
      fail = 1;
   ret = (mode != SDL_BLENDMODE_NONE);
   if (!render_isSupported(ret))
      fail = 1;

   return !fail;
}


/**
 * @brief Loads the test face.
 */
static SDL_Texture * render_loadTestFace (void)
{
   SDL_Surface *face;
   SDL_Texture *tface;

   /* Create face surface. */
   face = SDL_CreateRGBSurfaceFrom( (void*)img_face.pixel_data,
         img_face.width, img_face.height, 32, img_face.width*4,
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
         0xff000000, /* Red bit mask. */
         0x00ff0000, /* Green bit mask. */
         0x0000ff00, /* Blue bit mask. */
         0x000000ff /* Alpha bit mask. */
#else
         0x000000ff, /* Red bit mask. */
         0x0000ff00, /* Green bit mask. */
         0x00ff0000, /* Blue bit mask. */
         0xff000000 /* Alpha bit mask. */
#endif
         );
   if (face == NULL)
      return 0;
   tface = SDL_CreateTextureFromSurface( 0, face );
   SDL_FreeSurface(face);

   return tface;
}


/**
 * @brief Test to see if can set texture colour mode.
 */
static int render_hasTexColor (void)
{
   int fail;
   int ret;
   SDL_Texture *tface;
   Uint8 r, g, b;

   /* Get test face. */
   tface = render_loadTestFace();
   if (tface == 0)
      return 0;

   /* See if supported. */
   fail = 0;
   ret = SDL_SetTextureColorMod( tface, 100, 100, 100 );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetTextureColorMod( tface, &r, &g, &b );
   if (!render_isSupported(ret))
      fail = 1;

   /* Clean up. */
   SDL_DestroyTexture( tface );

   if (fail)
      return 0;
   else if ((r != 100) || (g != 100) || (b != 100))
      return 0;
   return 1;
}


/**
 * @brief Test to see if we can vary the alpha of the texture.
 */
static int render_hasTexAlpha (void)
{
   int fail;
   int ret;
   SDL_Texture *tface;
   Uint8 a;

   /* Get test face. */
   tface = render_loadTestFace();
   if (tface == 0)
      return 0;

   /* See if supported. */
   fail = 0;
   ret = SDL_SetTextureAlphaMod( tface, 100 );
   if (!render_isSupported(ret))
      fail = 1;
   ret = SDL_GetTextureAlphaMod( tface, &a );
   if (!render_isSupported(ret))
      fail = 1;

   /* Clean up. */
   SDL_DestroyTexture( tface );

   if (fail)
      return 0;
   else if (a != 100)
      return 0;
   return 1;
}


/**
 * @brief Clears the screen.
 *
 * @note We don't test for errors, but they shouldn't happen.
 */
static int render_clearScreen (void)
{
   int ret;

   /* Set colour. */
   ret = SDL_SetRenderDrawColor( 0, 0, 0, SDL_ALPHA_OPAQUE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   */

   /* Clear screen. */
   ret = SDL_RenderFillRect( NULL );
   /*
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;
   */

   /* Set defaults. */
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, SDL_ALPHA_OPAQUE );
   /*
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   */

   return 0;
}


/**
 * @brief Test reading and writing framebuffer
 */
static int render_testReadWrite (void)
{
   int ret;
   SDL_Rect rect;

   /* Write pixels. */
   rect.x = 0;
   rect.y = 0;
   rect.w = 80;
   rect.h = 60;
   ret = SDL_RenderWritePixels( &rect, SDL_PIXELFORMAT_RGB24, img_primitives.pixel_data, img_primitives.width*img_primitives.bytes_per_pixel );
   if (SDL_ATassert( "SDL_RenderWritePixels", ret==0) )
      return 1;

   /* See if it's the same. */
   if (render_compare( "Read/write output not the same.", &img_primitives, ALLOWABLE_ERROR_OPAQUE ))
      return -1;

   return 0;
}


/**
 * @brief Tests the SDL primitives for rendering.
 */
static int render_testPrimitives (void)
{
   int ret;
   int x, y;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasDrawColor())
      return 0;

   /* Draw a rectangle. */
   rect.x = 40;
   rect.y = 0;
   rect.w = 40;
   rect.h = 80;
   ret = SDL_SetRenderDrawColor( 13, 73, 200, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderFillRect( &rect );
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;

   /* Draw a rectangle. */
   rect.x = 10;
   rect.y = 10;
   rect.w = 60;
   rect.h = 40;
   ret = SDL_SetRenderDrawColor( 200, 0, 100, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderFillRect( &rect );
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;

   /* Draw some points like so:
    * X.X.X.X..
    * .X.X.X.X.
    * X.X.X.X.. */
   for (y=0; y<3; y++) {
      x = y % 2;
      for (; x<80; x+=2) {
         ret = SDL_SetRenderDrawColor( x*y, x*y/2, x*y/3, SDL_ALPHA_OPAQUE );
         if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
            return -1;
         ret = SDL_RenderDrawPoint( x, y );
         if (SDL_ATassert( "SDL_RenderDrawPoint", ret == 0))
            return -1;
      }
   }

   /* Draw some lines. */
   ret = SDL_SetRenderDrawColor( 0, 255, 0, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderDrawLine( 0, 30, 80, 30 );
   if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawColor( 55, 55, 5, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderDrawLine( 40, 30, 40, 60 );
   if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawColor( 5, 105, 105, SDL_ALPHA_OPAQUE );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_RenderDrawLine( 0, 0, 29, 29 );
   if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
      return -1;
   ret = SDL_RenderDrawLine( 29, 30, 0, 59 );
   if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
      return -1;
   ret = SDL_RenderDrawLine( 79, 0, 50, 29 );
   if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
      return -1;
   ret = SDL_RenderDrawLine( 79, 59, 50, 30 );
   if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
      return -1;

   /* See if it's the same. */
   if (render_compare( "Primitives output not the same.", &img_primitives, ALLOWABLE_ERROR_OPAQUE ))
      return -1;

   return 0;
}


/**
 * @brief Tests the SDL primitives with alpha for rendering.
 */
static int render_testPrimitivesBlend (void)
{
   int ret;
   int i, j;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour and blendmode or just skip test. */
   if (!render_hasDrawColor() || !render_hasBlendModes())
      return 0;

   /* Create some rectangles for each blend mode. */
   ret = SDL_SetRenderDrawColor( 255, 255, 255, 0 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_NONE );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFillRect( NULL );
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;
   rect.x = 10;
   rect.y = 25;
   rect.w = 40;
   rect.h = 25;
   ret = SDL_SetRenderDrawColor( 240, 10, 10, 75 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_ADD );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFillRect( &rect );
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;
   rect.x = 30;
   rect.y = 40;
   rect.w = 45;
   rect.h = 15;
   ret = SDL_SetRenderDrawColor( 10, 240, 10, 100 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_BLEND );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFillRect( &rect );
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;
   rect.x = 25;
   rect.y = 25;
   rect.w = 25;
   rect.h = 25;
   ret = SDL_SetRenderDrawColor( 10, 10, 240, 125 );
   if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
      return -1;
   ret = SDL_SetRenderDrawBlendMode( SDL_BLENDMODE_MOD );
   if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
      return -1;
   ret = SDL_RenderFillRect( &rect );
   if (SDL_ATassert( "SDL_RenderFillRect", ret == 0))
      return -1;

   /* Draw blended lines, lines for everyone. */
   for (i=0; i<SCREEN_W; i+=2)  {
      ret = SDL_SetRenderDrawColor( 60+2*i, 240-2*i, 50, 3*i );
      if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
         return -1;
      ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
            (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
      if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
         return -1;
      ret = SDL_RenderDrawLine( 0, 0, i, 59 );
      if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
         return -1;
   }
   for (i=0; i<SCREEN_H; i+=2)  {
      ret = SDL_SetRenderDrawColor( 60+2*i, 240-2*i, 50, 3*i );
      if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
         return -1;
      ret = SDL_SetRenderDrawBlendMode((((i/2)%3)==0) ? SDL_BLENDMODE_BLEND :
            (((i/2)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
      if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
         return -1;
      ret = SDL_RenderDrawLine( 0, 0, 79, i );
      if (SDL_ATassert( "SDL_RenderDrawLine", ret == 0))
         return -1;
   }

   /* Draw points. */
   for (j=0; j<SCREEN_H; j+=3) {
      for (i=0; i<SCREEN_W; i+=3) {
         ret = SDL_SetRenderDrawColor( j*4, i*3, j*4, i*3 );
         if (SDL_ATassert( "SDL_SetRenderDrawColor", ret == 0))
            return -1;
         ret = SDL_SetRenderDrawBlendMode( ((((i+j)/3)%3)==0) ? SDL_BLENDMODE_BLEND :
               ((((i+j)/3)%3)==1) ? SDL_BLENDMODE_ADD : SDL_BLENDMODE_MOD );
         if (SDL_ATassert( "SDL_SetRenderDrawBlendMode", ret == 0))
            return -1;
         ret = SDL_RenderDrawPoint( i, j );
         if (SDL_ATassert( "SDL_RenderDrawPoint", ret == 0))
            return -1;
      }
   }

   /* See if it's the same. */
   if (render_compare( "Blended primitives output not the same.", &img_blend, ALLOWABLE_ERROR_BLENDED ))
      return -1;

   return 0;
}


/**
 * @brief Tests some blitting routines.
 */
static int render_testBlit (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Texture *tface;
   int i, j, ni, nj;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasDrawColor())
      return 0;

   /* Create face surface. */
   tface = render_loadTestFace();
   if (SDL_ATassert( "render_loadTestFace()", tface != 0))
      return -1;

   /* Constant values. */
   rect.w = img_face.width;
   rect.h = img_face.height;
   ni     = SCREEN_W - img_face.width;
   nj     = SCREEN_H - img_face.height;

   /* Loop blit. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same.", &img_blit, ALLOWABLE_ERROR_OPAQUE ))
      return -1;

   return 0;
}


/**
 * @brief Blits doing colour tests.
 */
static int render_testBlitColour (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Texture *tface;
   int i, j, ni, nj;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour or just skip test. */
   if (!render_hasTexColor())
      return 0;

   /* Create face surface. */
   tface = render_loadTestFace();
   if (SDL_ATassert( "render_loadTestFace()", tface != 0))
      return -1;

   /* Constant values. */
   rect.w = img_face.width;
   rect.h = img_face.height;
   ni     = SCREEN_W - img_face.width;
   nj     = SCREEN_H - img_face.height;

   /* Test blitting with colour mod. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set colour mod. */
         ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
         if (SDL_ATassert( "SDL_SetTextureColorMod", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same (using SDL_SetTextureColorMod).",
            &img_blitColour, ALLOWABLE_ERROR_OPAQUE ))
      return -1;

   return 0;
}


/**
 * @brief Tests blitting with alpha.
 */
static int render_testBlitAlpha (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Texture *tface;
   int i, j, ni, nj;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need alpha or just skip test. */
   if (!render_hasTexAlpha())
      return 0;

   /* Create face surface. */
   tface = render_loadTestFace();
   if (SDL_ATassert( "render_loadTestFace()", tface != 0))
      return -1;

   /* Constant values. */
   rect.w = img_face.width;
   rect.h = img_face.height;
   ni     = SCREEN_W - img_face.width;
   nj     = SCREEN_H - img_face.height;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Test blitting with alpha mod. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set alpha mod. */
         ret = SDL_SetTextureAlphaMod( tface, (255/ni)*i );
         if (SDL_ATassert( "SDL_SetTextureAlphaMod", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* See if it's the same. */
   if (render_compare( "Blit output not the same (using SDL_SetSurfaceAlphaMod).",
            &img_blitAlpha, ALLOWABLE_ERROR_BLENDED ))
      return -1;

   return 0;
}


/**
 * @brief Tests a blend mode.
 */
static int render_testBlitBlendMode( SDL_Texture * tface, int mode )
{
   int ret;
   int i, j, ni, nj;
   SDL_Rect rect;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Steps to take. */
   ni     = SCREEN_W - FACE_W;
   nj     = SCREEN_H - FACE_H;

   /* Constant values. */
   rect.w = FACE_W;
   rect.h = FACE_H;

   /* Test blend mode. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {
         /* Set blend mode. */
         ret = SDL_SetTextureBlendMode( tface, mode );
         if (SDL_ATassert( "SDL_SetTextureBlendMode", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   return 0;
}


/**
 * @brief Tests some more blitting routines.
 */
static int render_testBlitBlend (void)
{
   int ret;
   SDL_Rect rect;
   SDL_Texture *tface;
   int i, j, ni, nj;
   int mode;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Need drawcolour and blendmode or just skip test. */
   if (!render_hasBlendModes() || !render_hasTexColor() || !render_hasTexAlpha())
      return 0;

   /* Create face surface. */
   tface = render_loadTestFace();
   if (SDL_ATassert( "render_loadTestFace()", tface != 0))
      return -1;

   /* Steps to take. */
   ni     = SCREEN_W - FACE_W;
   nj     = SCREEN_H - FACE_H;

   /* Constant values. */
   rect.w = img_face.width;
   rect.h = img_face.height;

   /* Set alpha mod. */
   ret = SDL_SetTextureAlphaMod( tface, 100 );
   if (SDL_ATassert( "SDL_SetTextureAlphaMod", ret == 0))
      return -1;

   /* Test None. */
   if (render_testBlitBlendMode( tface, SDL_BLENDMODE_NONE ))
      return -1;
   /* See if it's the same. */
   if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_NONE).",
            &img_blendNone, ALLOWABLE_ERROR_OPAQUE ))
      return -1;

   /* Test Blend. */
   if (render_testBlitBlendMode( tface, SDL_BLENDMODE_BLEND ))
      return -1;
   if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_BLEND).",
            &img_blendBlend, ALLOWABLE_ERROR_BLENDED ))
      return -1;

   /* Test Add. */
   if (render_testBlitBlendMode( tface, SDL_BLENDMODE_ADD ))
      return -1;
   if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_ADD).",
            &img_blendAdd, ALLOWABLE_ERROR_BLENDED ))
      return -1;

   /* Clear surface. */
   if (render_clearScreen())
      return -1;

   /* Loop blit. */
   for (j=0; j <= nj; j+=4) {
      for (i=0; i <= ni; i+=4) {

         /* Set colour mod. */
         ret = SDL_SetTextureColorMod( tface, (255/nj)*j, (255/ni)*i, (255/nj)*j );
         if (SDL_ATassert( "SDL_SetTextureColorMod", ret == 0))
            return -1;

         /* Set alpha mod. */
         ret = SDL_SetTextureAlphaMod( tface, (100/ni)*i );
         if (SDL_ATassert( "SDL_SetTextureAlphaMod", ret == 0))
            return -1;

         /* Crazy blending mode magic. */
         mode = (i/4*j/4) % 4;
         if (mode==0) mode = SDL_BLENDMODE_NONE;
         else if (mode==1) mode = SDL_BLENDMODE_BLEND;
         else if (mode==2) mode = SDL_BLENDMODE_ADD;
         else if (mode==3) mode = SDL_BLENDMODE_NONE;
         ret = SDL_SetTextureBlendMode( tface, mode );
         if (SDL_ATassert( "SDL_SetTextureBlendMode", ret == 0))
            return -1;

         /* Blitting. */
         rect.x = i;
         rect.y = j;
         ret = SDL_RenderCopy( tface, NULL, &rect );
         if (SDL_ATassert( "SDL_RenderCopy", ret == 0))
            return -1;
      }
   }

   /* Clean up. */
   SDL_DestroyTexture( tface );

   /* Check to see if matches. */
   if (render_compare( "Blit blending output not the same (using SDL_BLENDMODE_*).",
            &img_blendAll, ALLOWABLE_ERROR_BLENDED ))
      return -1;

   return 0;
}


/**
 * @brief Runs all the tests on the surface.
 *
 *    @return 0 on success.
 */
int render_runTests (void)
{
   int ret;

   /* No error. */
   ret = 0;

   /* Test functionality first. */
   if (render_hasDrawColor())
      SDL_ATprintVerbose( 1, "      Draw Color supported\n" );
   if (render_hasBlendModes())
      SDL_ATprintVerbose( 1, "      Blend Modes supported\n" );
   if (render_hasTexColor())
      SDL_ATprintVerbose( 1, "      Texture Color Mod supported\n" );
   if (render_hasTexAlpha())
      SDL_ATprintVerbose( 1, "      Texture Alpha Mod supported\n" );

   /* Software surface blitting. */
   ret = render_testReadWrite();
   if (ret)
      return -1;
   ret = render_testPrimitives();
   if (ret)
      return -1;
   ret = render_testPrimitivesBlend();
   if (ret)
      return -1;
   ret = render_testBlit();
   if (ret)
      return -1;
   ret = render_testBlitColour();
   if (ret)
      return -1;
   ret = render_testBlitAlpha();
   if (ret)
      return -1;
   ret = render_testBlitBlend();

   return ret;
}


/**
 * @brief Entry point.
 *
 * This testsuite is tricky, we're creating a testsuite per driver, the thing
 *  is we do quite a of stuff outside of the actual testcase which *could*
 *  give issues. Don't like that very much, but no way around without creating
 *  superfluous testsuites.
 */
#ifdef TEST_STANDALONE
int main( int argc, const char *argv[] )
{
   (void) argc;
   (void) argv;
#else /* TEST_STANDALONE */
int test_render (void)
{
#endif /* TEST_STANDALONE */
   int failed;
   int i, j, nd, nr;
   int ret;
   const char *driver, *str;
   char msg[256];
   SDL_Window *w;
   SDL_RendererInfo renderer;

   /* Initializes the SDL subsystems. */
   ret = SDL_Init(0);
   if (ret != 0)
      return -1;

   /* Get number of drivers. */
   nd = SDL_GetNumVideoDrivers();
   if (nd < 0)
      goto err;
   SDL_ATprintVerbose( 1, "%d Video Drivers found\n", nd );

   /* Now run on the video mode. */
   ret = SDL_InitSubSystem( SDL_INIT_VIDEO );
   if (ret != 0)
      goto err;

   /*
    * Surface on video mode tests.
    */
   /* Run for all video modes. */
   failed = 0;
   for (i=0; i<nd; i++) {
      /* Get video mode. */
      driver = SDL_GetVideoDriver(i);
      if (driver == NULL)
         goto err;
      SDL_ATprintVerbose( 1, " %d) %s\n", i+1, driver );

      /*
       * Initialize testsuite.
       */
      SDL_snprintf( msg, sizeof(msg) , "Rendering with %s driver", driver );
      SDL_ATinit( msg );

      /*
       * Initialize.
       */
      SDL_ATbegin( "Initializing video mode" );
      /* Initialize video mode. */
      ret = SDL_VideoInit( driver, 0 );
      if (SDL_ATvassert( ret==0, "SDL_VideoInit( %s, 0 )", driver ))
         goto err_cleanup;
      /* Check to see if it's the one we want. */
      str = SDL_GetCurrentVideoDriver();
      if (SDL_ATassert( "SDL_GetCurrentVideoDriver", SDL_strcmp(driver,str)==0))
         goto err_cleanup;
      /* Create window. */
      w = SDL_CreateWindow( msg, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
            80, 60, SDL_WINDOW_SHOWN );
      if (SDL_ATassert( "SDL_CreateWindow", w!=NULL ))
         goto err_cleanup;
      /* Check title. */
      str = SDL_GetWindowTitle( w );
      if (SDL_ATassert( "SDL_GetWindowTitle", SDL_strcmp(msg,str)==0))
         goto err_cleanup;
      /* Get renderers. */
      nr = SDL_GetNumRenderDrivers();
      if (SDL_ATassert("SDL_GetNumRenderDrivers", nr>=0))
         goto err_cleanup;
      SDL_ATprintVerbose( 1, "   %d Render Drivers\n", nr );
      SDL_ATend();
      for (j=0; j<nr; j++) {

         /* We have to recreate window each time, because opengl and opengles renderers */
         /* both add SDL_WINDOW_OPENGL flag for window, that was last used              */
         SDL_DestroyWindow(w);
         w = SDL_CreateWindow( msg, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
               80, 60, SDL_WINDOW_SHOWN );
         if (SDL_ATassert( "SDL_CreateWindow", w!=NULL ))
            goto err_cleanup;

         /* Get renderer info. */
         ret = SDL_GetRenderDriverInfo( j, &renderer );
         if (ret != 0)
            goto err_cleanup;

         /* Set testcase name. */
         SDL_snprintf( msg, sizeof(msg), "Renderer %s", renderer.name );
         SDL_ATprintVerbose( 1, "    %d) %s\n", j+1, renderer.name );
         SDL_ATbegin( msg );

         /* Set renderer. */
         ret = SDL_CreateRenderer( w, j, 0 );
         if (SDL_ATassert( "SDL_CreateRenderer", ret==0 ))
            goto err_cleanup;

         /*
          * Run tests.
          */
         ret = render_runTests();

         if (ret)
            continue;
         SDL_ATend();
      }

      /* Exit the current renderer. */
      SDL_VideoQuit();

      /*
       * Finish testsuite.
       */
      failed += SDL_ATfinish();
   }

   /* Exit SDL. */
   SDL_Quit();

   return failed;

err_cleanup:
   SDL_ATfinish();

err:
   return 1;
}