Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Made a Function to scroll multiple sections of a background.
#1
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
assigned Background Section & Background Scroll Speed Arrays.

\param bg[] - an array that contains one or more Background scroll sections(sections must be first setup in raster_callback()).
\param bgspeed[] - an array of user-defined Background scroll speeds.
\param factor - a float value that assigns which direction to scroll the background sections.
(+ value for right or - value for left)
*/
void TLN_SetBackgroudScrolls(float bg[], float  bgspeed[], float factor)
{
int scroll_section; // Holds the current section number of the horizontal scroll area.
int num_sections = *(&bg + 1) - bg; // Total number of sections, based off the size of the assigned bg[].

for (scroll_section = 0; scroll_section < num_sections; scroll_section++)
bg[scroll_section] += (bgspeed[scroll_section] * factor);
}
Reply
#2
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:
  • "direction" parameter should be called "factor", because it acts a scaling factor for the base array, not just a binary choice
  • "num_sections" should be of integer type (int), not floating point (float)
  • "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.
Keep going!
Reply
#3
(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.
[/list]
Keep going!

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!
Reply
#4
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
float bg_offsets[NUM_SECTIONS];
float bg_speeds[NUM_SECTIONS];

/* call your function */
TLN_SetBackgroudScrolls(bg_offsets, bg_speeds, NUM_SECTIONS, 1.0f);

/* function definition specifying size as a parameter */
void TLN_SetBackgroudScrolls(float bg[], float  bgspeed[], int num_sections, float factor)
{
   ...
}

Cheers!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)