Made a Function to scroll multiple sections of a background. - Printable Version +- Tilengine - The 2D retro graphics engine forum (http://tilengine.org/forum) +-- Forum: English forums (http://tilengine.org/forum/forumdisplay.php?fid=3) +--- Forum: Game development topics (http://tilengine.org/forum/forumdisplay.php?fid=13) +--- Thread: Made a Function to scroll multiple sections of a background. (/showthread.php?tid=825) |
Made a Function to scroll multiple sections of a background. - RootBeerKing - 09-19-2020 Because of Tilengine and my undying dream to make video games, I've been learning C/C++ for the past few years, and I've finally got to the point where things are clicking into place in my brain, and I'm comfortable enough to actually start coding something. So the first thing I did was I wrote a function that makes scrolling a background in multiple sections, like in the platformer demo, a little more automated/reusable. It would be great if Marc could add this to Tilengine natively, as I think it's a nice little function to have included with Tilengine and could be used for many different projects. This is my first ever time writing an original function in C. If you see anything wrong with it, or anyways it could be done better, let me know! Otherwise I've tested it with the Platformer demo included with Tilengine and it worked fine, so it should work in any other project you paste it into. Thanks for checking it out, have a great day. Code: /* \brief Scrolls an assigned Background, in user-defined sections at varying speeds, based on the size of the RE: Made a Function to scroll multiple sections of a background. - megamarc - 09-19-2020 Hi! I'm glad you're doing progress, and thanks for contributing your code. It's always good to have reusable code, that's the point of having functions and modules!. However, your function should be placed in a layer above Tilengine, but not inside the engine itself. Why? Because the engine is designed to be a general purpose virtual VDP. The engine provides tools and mechanisms to implement other things, but these new things are outside of the scope. Using raster effects to alter layer start offset can be used to create region scrolling, like your function does. But it can also be used to implement linescroll (pseudo-3D floor), heat/water wavy distortion, barrel distortion, fake vertical scaling, aiding fake rotation combined with column offset... many different applications! It wouldn't be feasible to "cook" each imaginable effect inside the engine itself. The more prebuilt functions you put inside a tool, the less flexible it becomes. That's why this kind of utility functions are placed above the engine, to be reused at your will. For example rendering text using tile-based fonts, do reflections, create a virtual camera for mode-7 like games... there are countless things that can be automated and reused and would make a great "toolkit" library. In fact Microsoft used this approach with Direct3D9: D3D9 contained the core, low-level graphics API, and D3DX9 ("x" for eXtension) provided additional functionality on top of it. Regarding your function, I understand what it does, but let me point some issues:
RE: Made a Function to scroll multiple sections of a background. - RootBeerKing - 09-20-2020 (09-19-2020, 10:26 PM)megamarc Wrote: [*]"num_sections" can't be calculated as you do. In C, size of arrays passed as pointers must always be specified separately, because the C runtime doesn't hold this information. In your code, num_sections will always compute to 4 because &bg[1] - &bg[0] is 4 bytes. Oh I see. I noticed what you mean when I tried different values and it broke. I thought it was working when I tried it on the platformer demo, because pos_background[6] and num_section was printing out 6. How would I go about this so that it would work correctly? Thank you! Cheers! RE: Made a Function to scroll multiple sections of a background. - megamarc - 09-20-2020 Hi, Usual way is to define the size, and then use the size to build the arrays and pass it as a parameter: Code: #define NUM_SECTIONS 6 Cheers! |