#include <stdio.h>
typedef long MYenum;
enum {
SINGLES,
PAIRS,
TRIPLES,
};
int *array = NULL;
/* mimic the call to glVertexAttribPointer */
void array_pointer(void *p)
{
array = p;
}
/* mimicks the behavior of glDrawArrays */
void print_arrays(MYenum mode, int first, size_t count)
{
size_t N, i, j;
if( !array ) return;
switch(mode) {
default: return;
case SINGLES: N = 1; break;
case PAIRS: N = 2; break;
case TRIPLES: N = 3; break;
}
for(i = 0; i <= count-N;) {
for(j = 0; j < N; ++j, ++i) {
printf("%d", array
[ first
+ i
] );
}
if( i
+ 1 <= count
-N
+1 ) printf(", "); }
}
/* mimics the behavior of glDrawElements */
void print_elements(MYenum mode, size_t count, int const *indices)
{
size_t N, i, j;
if( !array
|| !indices ) return;
switch(mode) {
default: return;
case SINGLES: N = 1; break;
case PAIRS: N = 2; break;
case TRIPLES: N = 3; break;
}
for(i = 0; i <= count-N;) {
for(j = 0; j < N; ++j, ++i) {
printf("%d", array
[ indices
[i
] ] );
}
if( i
+ 1 <= count
-N
+1 ) printf(", "); }
}
int main(int argc, char *argv[])
{
int singles[4] = {
11,
12,
13,
14
};
int single_elements[10] = {
0, 1, 0, 2, 3,
3, 0, 0, 2, 1
};
int pairs[6] = {
21, 22,
31, 32,
41, 42
};
int pairs_elements[12] = {
0, 1,
0, 2,
2, 3,
2, 4,
4, 5,
4, 0
};
int triples[9] = {
51, 52, 53,
61, 62, 63,
71, 72, 73
};
int triple_elements[15] = {
0, 1, 2,
3, 4, 5,
6, 7, 8,
7, 4, 1,
0, 3, 6
};
printf("== a few chains of singles ==\n"); array_pointer(singles);
printf("print array starting with 0, 3 consecutive elements:\n"); print_arrays(SINGLES, 0, 3);
printf("print array starting with 1, 3 consecutive elements:\n"); print_arrays(SINGLES, 1, 3);
printf("print 10 elements by index:\n"); print_elements(SINGLES, 10, single_elements);
printf("\n== a few chains of pairs ==\n"); array_pointer(pairs);
printf("print array starting with 0, 6 consecutive elements:\n"); print_arrays(PAIRS, 0, 6);
printf("print array starting with 1, 3 consecutive elements (should drop one element at the end since 3%%2 = 1):\n"); print_arrays(PAIRS, 1, 3);
printf("print 12 elements by index:\n"); print_elements(PAIRS, 12, pairs_elements);
printf("\n== a few chains of triples ==\n"); array_pointer(triples);
printf("print array starting with 0, 9 consecutive elements:\n"); print_arrays(TRIPLES, 0, 9);
printf("print array starting with 2, 6 consecutive elements:\n"); print_arrays(TRIPLES, 2, 6);
printf("print 15 elements by index:\n"); print_elements(TRIPLES, 15, triple_elements);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp0eXBlZGVmIGxvbmcgTVllbnVtOwoKZW51bSB7CglTSU5HTEVTLAoJUEFJUlMsCglUUklQTEVTLAp9OwoKaW50ICphcnJheSA9IE5VTEw7CgovKiBtaW1pYyB0aGUgY2FsbCB0byBnbFZlcnRleEF0dHJpYlBvaW50ZXIgKi8Kdm9pZCBhcnJheV9wb2ludGVyKHZvaWQgKnApCnsKCWFycmF5ID0gcDsKfQoKLyogbWltaWNrcyB0aGUgYmVoYXZpb3Igb2YgZ2xEcmF3QXJyYXlzICovCnZvaWQgcHJpbnRfYXJyYXlzKE1ZZW51bSBtb2RlLCBpbnQgZmlyc3QsIHNpemVfdCBjb3VudCkKewoJc2l6ZV90IE4sIGksIGo7CgoJaWYoICFhcnJheSApIHJldHVybjsKCglzd2l0Y2gobW9kZSkgewoJZGVmYXVsdDogcmV0dXJuOwoKCWNhc2UgU0lOR0xFUzogTiA9IDE7IGJyZWFrOwoJY2FzZSBQQUlSUzogICBOID0gMjsgYnJlYWs7CgljYXNlIFRSSVBMRVM6IE4gPSAzOyBicmVhazsKCX0KCglmb3IoaSA9IDA7IGkgPD0gY291bnQtTjspIHsKCQlwcmludGYoIigiKTsKCQlmb3IoaiA9IDA7IGogPCBOOyArK2osICsraSkgewoKCQkJcHJpbnRmKCIlZCIsIGFycmF5WyBmaXJzdCArIGkgXSApOwoKCQkJaWYoIGogKyAxIDwgTiApIHByaW50ZigiLCAiKTsKCQl9CgkJc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JSUyID0gMSk6XG4iKTsKCXByaW50X2FycmF5cyhQQUlSUywgMSwgMyk7CglwcmludGYoInByaW50IDEyIGVsZW1lbnRzIGJ5IGluZGV4OlxuIik7CglwcmludF9lbGVtZW50cyhQQUlSUywgMTIsIHBhaXJzX2VsZW1lbnRzKTsKCQoJcHJpbnRmKCJcbj09IGEgZmV3IGNoYWlucyBvZiB0cmlwbGVzID09XG4iKTsKCWFycmF5X3BvaW50ZXIodHJpcGxlcyk7CglwcmludGYoInByaW50IGFycmF5IHN0YXJ0aW5nIHdpdGggMCwgOSBjb25zZWN1dGl2ZSBlbGVtZW50czpcbiIpOwoJcHJpbnRfYXJyYXlzKFRSSVBMRVMsIDAsIDkpOwoJcHJpbnRmKCJwcmludCBhcnJheSBzdGFydGluZyB3aXRoIDIsIDYgY29uc2VjdXRpdmUgZWxlbWVudHM6XG4iKTsKCXByaW50X2FycmF5cyhUUklQTEVTLCAyLCA2KTsKCXByaW50ZigicHJpbnQgMTUgZWxlbWVudHMgYnkgaW5kZXg6XG4iKTsKCXByaW50X2VsZW1lbnRzKFRSSVBMRVMsIDE1LCB0cmlwbGVfZWxlbWVudHMpOwoKCXJldHVybiAwOwp9Cg==
== a few chains of singles ==
print array starting with 0, 3 consecutive elements:
(11), (12), (13)
print array starting with 1, 3 consecutive elements:
(12), (13), (14)
print 10 elements by index:
(11), (12), (11), (13), (14), (14), (11), (11), (13), (12)
== a few chains of pairs ==
print array starting with 0, 6 consecutive elements:
(21, 22), (31, 32), (41, 42)
print array starting with 1, 3 consecutive elements (should drop one element at the end since 3%2 = 1):
(22, 31)
print 12 elements by index:
(21, 22), (21, 31), (31, 32), (31, 41), (41, 42), (41, 21)
== a few chains of triples ==
print array starting with 0, 9 consecutive elements:
(51, 52, 53), (61, 62, 63), (71, 72, 73)
print array starting with 2, 6 consecutive elements:
(53, 61, 62), (63, 71, 72)
print 15 elements by index:
(51, 52, 53), (61, 62, 63), (71, 72, 73), (72, 62, 52), (51, 61, 71)