|
 |
"Bill Pragnell" <bil### [at] hotmail com> wrote:
> "Bald Eagle" <cre### [at] netscape net> wrote:
> > One thing that I did run across that is being used in this code, but isn't
> > natively supported by SDL is the referencing of a 1D "slice" of a 2D array.
> >
> > "Although it is permissible to reference an entire array as a whole, you may
> > not reference just one dimension of a multi-dimensional array."
>
> You can kind-of do this in SDL, but only if you use arrays of arrays instead of
> multi-dimensional arrays. So, instead of
>
> #declare A = array[3][2];
>
> use
>
> #declare A = array[3];
> #for (I, 0, 2)
> #declare A[I] = array[2];
> #end
>
> which may or may not be more trouble depending on how you're populating the
> arrays. However, it is indexed in exactly the same way - A[I][J] - and you can
> indeed reference A[I] as an array[2].
>
> Bill
Indeed, I had also thought to do this, but wanted to try the little macro
approach first, and that seemed to work well enough.
I've hacked the living daylights out of the code such that i make it to the end,
but I've hacked enough out that I get no results.
The first place the code goes off the rails is with a negative subscript do to a
reassigning of j. At first I puzzled over how this could possibly be working
code, but given the fact that it's C, I decided to check:
https://www.geeksforgeeks.org/overloading-subscript-or-array-index-operator-in-c/
Positive and Negative subscripts
The first element of an array is stored at index 0. The range of a C++ array is
subscripts. Negative subscripts must fall within array boundaries; if they do
not, the results are unpredictable. The following code shows positive and
negative array subscripts:
// CPP program illustrating the
// positive and negative subscripts
#include <iostream>
using namespace std;
// Driver Method
int main()
{
int intArray[1024];
for (int i = 0, j = 0; i < 1024; i++) {
intArray[i] = j++;
}
// 512
cout << intArray[512] << endl;
// 257
cout << 257 [intArray] << endl;
// pointer to the middle of the array
int* midArray = &intArray[512];
// 256
cout << midArray[-256] << endl;
// unpredictable, may crash
cout << intArray[-256] << endl;
}
Output:
512
257
256
0
So, does that mean that I can just use abs(subscript) as a C-to-SDL hack or ...
?
Post a reply to this message
|
 |