fork download
  1. #include <stdio.h>
  2.  
  3. typedef long MYenum;
  4.  
  5. enum {
  6. SINGLES,
  7. PAIRS,
  8. TRIPLES,
  9. };
  10.  
  11. int *array = NULL;
  12.  
  13. /* mimic the call to glVertexAttribPointer */
  14. void array_pointer(void *p)
  15. {
  16. array = p;
  17. }
  18.  
  19. /* mimicks the behavior of glDrawArrays */
  20. void print_arrays(MYenum mode, int first, size_t count)
  21. {
  22. size_t N, i, j;
  23.  
  24. if( !array ) return;
  25.  
  26. switch(mode) {
  27. default: return;
  28.  
  29. case SINGLES: N = 1; break;
  30. case PAIRS: N = 2; break;
  31. case TRIPLES: N = 3; break;
  32. }
  33.  
  34. for(i = 0; i <= count-N;) {
  35. printf("(");
  36. for(j = 0; j < N; ++j, ++i) {
  37.  
  38. printf("%d", array[ first + i ] );
  39.  
  40. if( j + 1 < N ) printf(", ");
  41. }
  42. printf(")");
  43. if( i + 1 <= count-N+1 ) printf(", ");
  44. }
  45. printf("\n");
  46. }
  47.  
  48. /* mimics the behavior of glDrawElements */
  49. void print_elements(MYenum mode, size_t count, int const *indices)
  50. {
  51. size_t N, i, j;
  52.  
  53. if( !array
  54. || !indices ) return;
  55.  
  56. switch(mode) {
  57. default: return;
  58.  
  59. case SINGLES: N = 1; break;
  60. case PAIRS: N = 2; break;
  61. case TRIPLES: N = 3; break;
  62. }
  63.  
  64. for(i = 0; i <= count-N;) {
  65. printf("(");
  66. for(j = 0; j < N; ++j, ++i) {
  67.  
  68. printf("%d", array[ indices[i] ] );
  69.  
  70. if( j + 1 < N ) printf(", ");
  71. }
  72. printf(")");
  73. if( i + 1 <= count-N+1 ) printf(", ");
  74. }
  75. printf("\n");
  76. }
  77.  
  78. int main(int argc, char *argv[])
  79. {
  80. int singles[4] = {
  81. 11,
  82. 12,
  83. 13,
  84. 14
  85. };
  86. int single_elements[10] = {
  87. 0, 1, 0, 2, 3,
  88. 3, 0, 0, 2, 1
  89. };
  90.  
  91. int pairs[6] = {
  92. 21, 22,
  93. 31, 32,
  94. 41, 42
  95. };
  96. int pairs_elements[12] = {
  97. 0, 1,
  98. 0, 2,
  99. 2, 3,
  100. 2, 4,
  101. 4, 5,
  102. 4, 0
  103. };
  104.  
  105. int triples[9] = {
  106. 51, 52, 53,
  107. 61, 62, 63,
  108. 71, 72, 73
  109. };
  110. int triple_elements[15] = {
  111. 0, 1, 2,
  112. 3, 4, 5,
  113. 6, 7, 8,
  114. 7, 4, 1,
  115. 0, 3, 6
  116. };
  117.  
  118. printf("== a few chains of singles ==\n");
  119. array_pointer(singles);
  120. printf("print array starting with 0, 3 consecutive elements:\n");
  121. print_arrays(SINGLES, 0, 3);
  122. printf("print array starting with 1, 3 consecutive elements:\n");
  123. print_arrays(SINGLES, 1, 3);
  124. printf("print 10 elements by index:\n");
  125. print_elements(SINGLES, 10, single_elements);
  126.  
  127. printf("\n== a few chains of pairs ==\n");
  128. array_pointer(pairs);
  129. printf("print array starting with 0, 6 consecutive elements:\n");
  130. print_arrays(PAIRS, 0, 6);
  131. printf("print array starting with 1, 3 consecutive elements (should drop one element at the end since 3%%2 = 1):\n");
  132. print_arrays(PAIRS, 1, 3);
  133. printf("print 12 elements by index:\n");
  134. print_elements(PAIRS, 12, pairs_elements);
  135.  
  136. printf("\n== a few chains of triples ==\n");
  137. array_pointer(triples);
  138. printf("print array starting with 0, 9 consecutive elements:\n");
  139. print_arrays(TRIPLES, 0, 9);
  140. printf("print array starting with 2, 6 consecutive elements:\n");
  141. print_arrays(TRIPLES, 2, 6);
  142. printf("print 15 elements by index:\n");
  143. print_elements(TRIPLES, 15, triple_elements);
  144.  
  145. return 0;
  146. }
  147.  
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
== 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)