{"id":26,"date":"2013-06-22T16:59:59","date_gmt":"2013-06-22T16:59:59","guid":{"rendered":"https:\/\/blogs.scummvm.org\/t0by\/?p=26"},"modified":"2022-05-24T17:01:44","modified_gmt":"2022-05-24T17:01:44","slug":"a-plot-twist-or-that-thing-with-muhammad-and-the-mountain","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/t0by\/2013\/06\/22\/a-plot-twist-or-that-thing-with-muhammad-and-the-mountain\/","title":{"rendered":"A plot twist \u2013 or, that thing with muhammad and the mountain."},"content":{"rendered":"<p><a href=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-28\" src=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic.jpg\" alt=\"\" width=\"2054\" height=\"972\" srcset=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic.jpg 2054w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-300x142.jpg 300w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-1024x485.jpg 1024w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-768x363.jpg 768w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-1536x727.jpg 1536w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-2048x969.jpg 2048w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-1200x568.jpg 1200w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/maggic-1980x937.jpg 1980w\" sizes=\"auto, (max-width: 2054px) 100vw, 2054px\" \/><\/a><\/p>\n<p>My faithful readers \u2013 that is, those who have read my one previous post \u2013 will remember how I showed that a surface can be first rotated, then stretched and then correctly positioned by doing a few vector operations..<br \/>\nWell, I was lying.<br \/>\nThe thing is, while mathematically the idea is perfectly sound, in practice there is a problem.<br \/>\nWell, two.<\/p>\n<p>Problem one: we\u2019d be doing that with integers.<br \/>\nThis means that a. we have single pixel accuracy (which, at best, would mean nearest neighbour) b. we have rounding errors building up.<br \/>\nWe need subpixel accuracy for this.<\/p>\n<p>Problem two: I mentioned a magic rotating function that would rotate sprites for me.<br \/>\nWell, we do have one, and it\u2019s called SDL_rotozoom.cpp.<br \/>\nIt only has one teensy-weensy problem.<br \/>\nIt is not as precise as we\u2019d want when it comes to the bounding box around the rotated sprite.<br \/>\nBasically, what you\u2019d normally expect is this: a different corner of the original sprite \u2018pushing\u2019 the edges and causing the resulting sprite to be larger.<br \/>\nLook at this drawing I made for my reference and posted behind my monitor (I can never remember for which range I must use which corner):<\/p>\n<p><a href=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/stuffffffff.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29\" src=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/stuffffffff.jpg\" alt=\"\" width=\"800\" height=\"687\" srcset=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/stuffffffff.jpg 800w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/stuffffffff-300x258.jpg 300w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/stuffffffff-768x660.jpg 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a>This (possibly suboptimal but working) code would give you the exact amount of offset thus produced:<\/p>\n<p><a href=\"https:\/\/github.com\/tobiatesan\/scummvm\/blob\/d98529c5aa108488399d41ce7125dc1b421c6a14\/engines\/wintermute\/graphics\/transform_tools.cpp\" rel=\"nofollow\">https:\/\/github.com\/tobiatesan\/scummvm\/blob\/d98529c5aa108488399d41ce7125dc1b421c6a14\/engines\/wintermute\/graphics\/transform_tools.cpp<\/a><\/p>\n<p>Now, look at this instead:<\/p>\n<p><a href=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/adfasdfadsfasdfadsfasdfasdfasdfasdf.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-30\" src=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/adfasdfadsfasdfadsfasdfasdfasdfasdf.png\" alt=\"\" width=\"640\" height=\"803\" srcset=\"https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/adfasdfadsfasdfadsfasdfasdfasdfasdf.png 640w, https:\/\/blogs.scummvm.org\/t0by\/wp-content\/uploads\/sites\/43\/2013\/06\/adfasdfadsfasdfadsfasdfasdfasdfasdf-239x300.png 239w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a>See that?<br \/>\nYep.<br \/>\nThere is a 1px offset on top and a 2x offset pushing the sprite right.<\/p>\n<p>Those are probably due to rounding errors and various stuff going on in the fixed point calculations done in SDL_rotozoom.cpp (home to the magic function I\u2019ve been talking about till now).<br \/>\nIt is exceedingly difficult to compensate for those errors on a case by case basis, to the point that, I realized, the code is practically useless, only good for really low-end devices which benefit from some rather lightweight code.<\/p>\n<p>Could I be completely wrong?<\/p>\n<p>Possibly. I often am.<br \/>\nAt the suggestion of my mentor I looked into how the same feature is done in WME:Lite.<br \/>\nTurns out they use SDL to do it \u2013 they specify the hotspot and the positioning, and SDL draws \u201caround\u201d the hotspot using fixed point and possibly hardware rendering.<\/p>\n<p>We can\u2019t afford this luxury, though; the code being as it is, we cannot draw \u201caround\u201d the origin.<br \/>\nAll pixels for a sprite must be below and right of the origin.<\/p>\n<p>What I\u2019m gonna attempt next will thus be:<\/p>\n<p>1. Calculate the boundaries of the box first and place it firmly where it will end up in the end.<br \/>\n2. In a single pass do an (interpolating?) transform.<\/p>\n<p>This should save me from all the issues above.<\/p>\n<p>Stay tune, more to come.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My faithful readers \u2013 that is, those who have read my one previous post \u2013 will remember how I showed that a surface can be first rotated, then stretched and then correctly positioned by doing a few vector operations.. Well, I was lying. The thing is, while mathematically the idea is perfectly sound, in practice [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":2,"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":31,"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/posts\/26\/revisions\/31"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/t0by\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}