Moving the Main character placement to a function, or How Jimmy Buffet helps me program.

12-24-2017
So Jimmy Buffet did not sing Changes in Latitude and Changes in Longitude…

In a prior blog post, I had toyed around with the main character placement, noting the placement was in hexadecimal.

I was kinda OK with that, seeing that the Windows OS has a calculator built in that will convert hex to decimal and vice versa. So, why don’t we just deal with hex and create a function that will place your character on the screen?

I create a variable to track our level

unsigned char level;//which game level are we on

I created a static array near the top with all of my other globals.

short levelPositioning [3] = {0,0,0};

This should hold our values. The first slot of this array will hold our players X position, the second will hold our players Y position and finally, the third slot will hold our level.

My thinking is that when the function is eventually called, the first 2 slots will be empty and the third slot will hold the level that you are going to. The function will then populate the first two slots depending on the data in the 3rd slot. The first two slots will be in hex and the third slot will be in decimal. The array will then return the values to the main loop and read the values.

Sounds reasonable and fairly easy.

Boy, was I in for a ride.

So let’s make a function.

void startingPosition (short levelPositioning[])
{
}

How do we want to tackle this? Well, we know that we are dealing with a hex(base 16) number on the first two slots of the array and a decimal (base 10) number. So, all we should have to do is read in the 3rd slot and then populate the first two slots. Trivial.

void startingPosition (short levelPositioning[])
{
    short xxx = levelPositioning[0];
    short yyy = levelPositioning[1];
    short Level = levelPositioning[2];

    if (Level == 0)
    {
        //x = 0x3200;
        //y = 0x1000;
        levelPositioning[0] = 0x3200;
        levelPositioning[1] = 0x1000;
        M_OUTP(0xfe, 2);//changes border color
    }
    //returns
    levelPositioning[0] = xxx;
    levelPositioning[1] = yyy;    
}

Finally, make a call to the function.

levelPositioning[2] = ((short)level);//recast level to fit properly in array
startingPosition(levelPositioning);//call the function                
x = levelPositioning[0];
y = levelPositioning[1];

Anyone else see what the issue is?

Queue up the game show music.

Well, neither did I.

And try and try as I might, I did not see any issue. It failed over and over despite my tweaks to the code. I tried changing the data types, I tried rewriting the function and the call to the function, I made all of the data types the exact same. Every time it all compiled fine. Every time I checked the code with CPPCheck, it all looked good. Every time, I was disappointed in the results.

Where to now.

Well, what I like to do is not stare constantly at the code. I step away, watch TV, do some chores while thinking about it, yet not thinking about it. That what I did. I then will only approach it again when I can change my attitude about the problem.

Queue in the Jimmy Buffet song Changes in Latitude, Changes in Attitude.

And that what I do, change my Attitude on how I approach the problem.

Let’s revisit how we are doing this with our data types and create a print function to see what is happening.

void printtester3(short info, short info2)
{
    char text[20];
    PrintStr("            ", 321);    
    sprintf(text, "%u:%u", info,info2);
    //sprintf(text, "%x:%x", info,info2);
    PrintStr(text, 321);
}

So that’s what I do, above is our little code for printing the data.

The commented sprintf is a hexadecimal version of the print. What do I discover? Some sort of error to return a hexadecimal from the function. The array type does not like returned hex values. Arrgh!

I then again need to step away from the problem. Blah! Obviously, this is a classic PEBKAC (problem exists between chair and keyboard).

Meanwhile, while AFK (away from Keyboard) in comes a text from my Spec-Chum Robin Verhagen-Guest. We usually chat about a variety of subject when I mentioned how much Hex irritates me. Robin being a much more experienced and talented programmer than I am pointed out this little concept.

0x3100 = (0x31 * 256) + 0x00

It’s as if someone turned a light bulb on above my head and a choir started singing behind me.

I can kill two birds with one stone here. I can return the value in decimal, I can change my data type back to a short and not use all the convoluted bologna I had stuffed in my code to try to resolve the issue, by making a quick and simple calculation. Thank goodness.

Time for some code clean-up and try Robin’s formula.

Array stays the same.

short levelPositioning [3] = {0,0,0};

my level variable stays the same

unsigned char level;//which game level are we on

My calling function stays the same

levelPositioning[2] = ((short)level);//recast level to fit properly in array
startingPosition(levelPositioning);//call the function                
x = levelPositioning[0];
y = levelPositioning[1];

However, my function will change.

Amazingly, it really does not change very much.

void startingPosition (short levelPositioning[])
{
    short xxx = 0;// = levelPositioning[0];// to be used for output
    short yyy = 0;// = levelPositioning[1];
    short Level = levelPositioning[2];

    if (Level == 0)
    {
        xxx = 50;
        yyy = 120;//yyy = 16;
        M_OUTP(0xfe, 2);//changes border color
    }
    
    //to adjust to scale.
    xxx = xxx * 256;
    yyy = yyy * 256;
    
    //returns
    levelPositioning[0] = xxx;
    levelPositioning[1] = yyy;    
}

What’s going on here? Our function is a type void (just like my other functions) which is ok because arrays are passed as reference, not as an actual variable. That’s standard with C. We just are modifying the contents of the array but that modification really doesn’t happen directly. There are books that explain that much better than I do.

We create variables xxx and yyy and initialize them. We read in the level via slot 2 in the levelPositioning array and place it in variable Level.

Look at the Level and see if it is level 0 that we are dealing with.
If it is, then we assign the character initial position in decimal value (0 to 255) in X and 0 to 192 in Y (actually a little less than that since we don’t want to start on the very edges).

We change the border color, just for the hell of it, no particular reason.

After the level check is done, we place in the formula that Robin gave me.

Finally, we modify the array with the calculation.

And it works.

Light bulb turns off, choir stops singing and I seriously question my sanity.

Yep, I still have it (sanity I mean).

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s