{"id":267,"date":"2009-07-12T23:26:39","date_gmt":"2009-07-12T17:56:39","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=267"},"modified":"2011-04-12T23:26:58","modified_gmt":"2011-04-12T17:56:58","slug":"transparent-blitting-directdraw-part-5","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2009\/07\/12\/transparent-blitting-directdraw-part-5\/","title":{"rendered":"Transparent Blitting, DirectDraw, Part 5"},"content":{"rendered":"<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">All this <a href=\"javascript:void(0);\/*1246801928914*\/\">while<\/a>, we have been blitting the spaceship image:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"64\" height=\"64\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/Spaceship.JPG\" alt=\"\" \/>\n<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">onto a black surface:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"240\" height=\"320\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/BackgroundBlack.JPG\" alt=\"\" \/>\n<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">giving an image which looks like:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"240\" height=\"320\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/SpaceshipOnBlack.JPG\" alt=\"\" \/>\n<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Thus giving an illusion that only the spaceship has been Blt on to the screen. But if our background was anything but black, things would look a lot different. For e.g. if our background was blue:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"240\" height=\"320\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/BackgroundBlue.JPG\" alt=\"\" \/>\n<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">the resulting Blt() would have looked like this:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"240\" height=\"320\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/SpaceshipOnBlue.JPG\" alt=\"\" \/>\n<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">which, as you can see, is not very appealing to the eyes. Enter the concept of transparent blitting!<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">With transparent blitting, you can specify any arbitrary color, on the source surface or the destination surface or both, as a transparent color. So for e.g. in the above case we would specify the color BLACK on the source surface (the spaceship) as the transparent color. Therefore, when the spaceship is being Blt onto the destination surface, the color Black will be treated as transparent and the background will be preserved. So in short whenever the color black is encountered while Blting, its not Blt onto the destination surface, instead the Blue background color is displayed.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Color keys are used to specify which color you want to be treated as transparent. You can specify a range of colors to be transparent but, color ranges are not supported by HEL (Hardware Emulation Layer). Color keys can be specified:<\/span><\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">1) While creating the surface<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">2) Calling SetColorKey() function explicitely on a surface<\/span><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">3) While Blting the surface<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Color Keys are specified using the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDCOLORKEY<\/span> structure. This structure has two DWORD members, <\/span><em style=\"font-family: Comic Sans MS;\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">dwColorSpaceLowValue<\/span> <\/em><span style=\"font-family: Comic Sans MS;\">and <\/span><em style=\"font-family: Comic Sans MS;\"><span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">dwColorSpaceHighValue<\/span>. <\/em><span style=\"font-family: Comic Sans MS;\">Unless you are specifying a range of colors, both these values must be the same. If a surface is in palettized format then the color is specified as an index or a range of indices. If the surface&#8217;s pixel format is specified using a code then the color keys have to be specified using appropriate means like <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">RGB(r, g, b)<\/span> etc.<\/span><\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-weight: bold; font-family: Comic Sans MS;\">Color Keys while creating a surface<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Assign the appropriate color values to <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">ddckCKSrcBlt<\/span> or <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">ddckCKDestBlt<\/span> members of the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDSURFACEDESC<\/span> structure. Also, the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">dwFlags<\/span> member must include <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDSD_CKSRCBLT<\/span> or <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDSD_CKDESTBLT<\/span> or both depending on whther you want to specify source color key, destination color key or both.<\/span><\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-weight: bold; font-family: Comic Sans MS;\">Using SetColorKey()<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Another way to specify color keys is by calling <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">SetColorKey()<\/span> function on a surface directly. This function takes a pointer to <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDCOLORKEY<\/span> structure where you specify your color keys. In the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">dwFlags<\/span> parameter you set either <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDCKEY_SRCBLT<\/span> or <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDCKEY_DESTBLT<\/span> or both to indicate whether you are setting a source color key or a destination color key or both.<\/span><\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-weight: bold; font-family: Comic Sans MS;\">While Bltting the surface<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">If the color keys for the surfaces you are bltting was already specified using one of the methods above then you just need to set the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">dwFlags<\/span> parameter to <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDBLT_KEYSRC<\/span> or <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDBLT_KEYDEST<\/span> or both. Alternatively, if you want to override the color keys while blitting then you can specify the source and destination color keys by setting the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">ddckDestColorKey<\/span> and <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">ddckSrcColorKey<\/span> members of the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDBLTFX<\/span> structure, you will also need to specify <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DBLT_KEYSRCOVERRIDE<\/span> or <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DBLT_KEYDESTOVERRIDE<\/span> flags in the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">dwFlags<\/span> parameter of the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDBLTFX<\/span> structure.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">In our <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">UpdateFrame()<\/span> function specify the color keys as:<\/span><\/div>\n<p><br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; ddbltfx.ddckSrcColorkey.dwColorSpaceLowValue = RGB(0, 0, 0);<\/span><br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; ddbltfx.ddckSrcColorkey.dwColorSpaceHighValue = RGB(0, 0, 0);<\/span><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">Note that both the values are same. Here, the color black is specified as the source color key.<\/span><\/p>\n<p><span style=\"font-family: Comic Sans MS;\">And the blitting,<\/span><br \/>\n<br style=\"font-family: Verdana; color: rgb(0, 0, 128);\" \/><br \/>\n<span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; hRet = g_pDDSBack-&gt;Blt(&amp;dest, g_pDDSOne, NULL, DDBLT_ROP | DDBLT_KEYSRCOVERRIDE, &amp;ddbltfx);<\/span><br \/>\n<br style=\"font-family: Comic Sans MS;\" \/><br \/>\n<span style=\"font-family: Comic Sans MS;\">The <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">DDBLT_KEYSRCOVERRIDE<\/span> flag is specified to use the color key from the <span style=\"font-family: Verdana; color: rgb(0, 0, 128);\">ddbltfx<\/span> structure.<\/span><\/div>\n<p>&nbsp;<\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">So now when I Blit:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"82\" height=\"82\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/Ball.JPG\" alt=\"\" \/><\/p>\n<div style=\"text-align: justify;\"><span style=\"font-family: Comic Sans MS;\">onto the same Blue surface, the resultant image is:<\/span><\/div>\n<p>\n<img decoding=\"async\" loading=\"lazy\" width=\"354\" height=\"550\" src=\"\/images\/geekswithblogs_net\/TechTwaddle\/DirectDraw5 TrnsprntBlt\/BallOnBlue.JPG\" alt=\"BallOnBlue\" \/><\/p>\n<p><span style=\"font-family: Comic Sans MS;\">For more information on Transparent Blitting, take a look at <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa451400.aspx\">MSDN<\/a>.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>All this while, we have been blitting the spaceship image: onto a black surface: giving an image which looks like: Thus giving an illusion that only the spaceship has been Blt on to the screen. But if our background was anything but black, things would look a lot different. For e.g. if our background was &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2009\/07\/12\/transparent-blitting-directdraw-part-5\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Transparent Blitting, DirectDraw, Part 5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-4j","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/267"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=267"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/267\/revisions\/268"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}