_tk.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*--------------------------------------------------------------------------
  2. DEPUI-GFX-TK 3.0 - GPL portable source code libraries
  3. http://www.deleveld.dds.nl/depui.htm
  4. See file docs/copying for copyright details
  5. ---------------------------------------------------------------------------*/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "detk/detk.h"
  9. #include "deds/deds.h"
  10. /*
  11. MODULE: VECTOR - An almost typesafe C vector
  12. -----------------------------------------------
  13. This module is an almost typesafe C vector class. You use it by defining a
  14. structure of what type of elements you want to have in the vector. For example
  15. lets make a vector of doubles. You use the vector module by defining a type
  16. to carry your data. Only the mx_vector_insert function is not typesafe.
  17. REQUIRES: nothing
  18. */
  19. #define MXMODULE_VECTOR
  20. /* Make a vector of the type that you want */
  21. typedef MX_VECTOR(double) DOUBLEVECTOR;
  22. static void print_vector(DOUBLEVECTOR * vector)
  23. {
  24. unsigned i;
  25. fprintf(stderr, "Vector has size %i\n", (int)mx_vector_size(vector));
  26. for (i = 0; i < mx_vector_size(vector); i++) {
  27. const double element = vector->data[i];
  28. fprintf(stderr, "%f ", element);
  29. }
  30. fprintf(stderr, "\n");
  31. }
  32. static void test_vector(void)
  33. {
  34. double firstpart[5] = { 0., 1., 2., 3., 4. };
  35. double middle = 5.;
  36. double lastpart[5] = { 6., 7., 8., 9., 10. };
  37. DOUBLEVECTOR myvector;
  38. fprintf(stderr, "\n\nTesting module VECTOR\n");
  39. /* Construct the vector */
  40. mx_vector(&myvector);
  41. /* Add some data */
  42. mx_vector_append(&myvector, firstpart, 5);
  43. print_vector(&myvector);
  44. mx_vector_append(&myvector, &middle, 1);
  45. print_vector(&myvector);
  46. mx_vector_append(&myvector, lastpart, 5);
  47. print_vector(&myvector);
  48. /* Remove some data and put it back */
  49. mx_vector_remove(&myvector, 2, 1);
  50. print_vector(&myvector);
  51. mx_vector_insert(&myvector, &firstpart[2], 1, 2);
  52. print_vector(&myvector);
  53. /* Destroy the vector */
  54. mx_vector_free(&myvector);
  55. }
  56. /*
  57. MODULE: UTF8 - Some simple UTF8 decoding functions
  58. ------------------------------------------
  59. This module is some *very* simple utf8 functions.
  60. REQUIRES: nothing
  61. */
  62. #define MXMODULE_UTF8
  63. static void test_utf8(void)
  64. {
  65. int clen;
  66. const char *message = "This is a nice string.";
  67. const char *ptr = message;
  68. fprintf(stderr, "\n\nTesting module UTF8\n");
  69. /* Start of the string */
  70. clen = mx_utf8_len(message);
  71. while (clen) {
  72. const long c = mx_utf8_char(ptr, clen);
  73. fprintf(stderr, "\'%c\' = %i\n", (char) c, (int) c);
  74. /* Get the next character */
  75. ptr += clen;
  76. clen = mx_utf8_len(ptr);
  77. }
  78. }
  79. /*
  80. MODULE: REGION - Rectangular region clipping function
  81. -----------------------------------------------------
  82. This module handles clipping in rectangular regions.
  83. REQUIRES: VECTOR
  84. */
  85. #define MXMODULE_REGION
  86. static void test_region(void)
  87. {
  88. unsigned i;
  89. MX_REGION region;
  90. MX_RECT whole = { 0, 0, 640, 480 };
  91. MX_RECT part1 = { 100, 100, 300, 300 };
  92. MX_RECT part2 = { 200, 200, 400, 400 };
  93. fprintf(stderr, "\n\nTesting module REGION\n");
  94. /* Start with the whole area */
  95. mx_vector(&region);
  96. mx_vector_append(&region, &whole, 1);
  97. /* Remove some parts */
  98. mx_region_clip(&region, &part1);
  99. mx_region_clip(&region, &part2);
  100. for (i = 0; i < mx_vector_size(&region); i++) {
  101. const MX_RECT *rect = &region.data[i];
  102. fprintf(stderr, "{ %5i, %5i, %5i, %5i }\n", rect->x1, rect->y1,
  103. rect->x2, rect->y2);
  104. }
  105. fprintf(stderr, "\n");
  106. }
  107. /*
  108. MODULE: ATOM - Reference counting and delete locking
  109. -----------------------------------------------------
  110. This module adds reference counting and delete locking to another data struct.
  111. REQUIRES: nothing
  112. */
  113. #define MXMODULE_ATOM
  114. /* A string type derived from atom */
  115. typedef struct STRINGATOM {
  116. union {
  117. MX_ATOM atom;
  118. } base;
  119. const char *string;
  120. } STRINGATOM;
  121. /* Destructor */
  122. static void stringatom_destruct(void *atom)
  123. {
  124. STRINGATOM *theatom = (STRINGATOM *) atom;
  125. fprintf(stderr, "-> destruct %s\n", theatom->string);
  126. }
  127. /* Constructor */
  128. STRINGATOM *stringatom(STRINGATOM * theatom, const char *string)
  129. {
  130. theatom =
  131. (STRINGATOM *) mx_atom(theatom, stringatom_destruct,
  132. sizeof(STRINGATOM));
  133. if (theatom) {
  134. theatom->string = string;
  135. fprintf(stderr, "-> construct %s\n", theatom->string);
  136. }
  137. return theatom;
  138. }
  139. static void test_atom(void)
  140. {
  141. MX_LOCK lock1;
  142. MX_LOCK lock2;
  143. STRINGATOM atom;
  144. fprintf(stderr, "\n\nTesting module ATOM\n");
  145. /* Two different ways to construct an stringatom */
  146. stringatom(&atom, "String one");
  147. fprintf(stderr, "Locked atoms cant be deleted\n");
  148. mx_lock(&lock1, &atom);
  149. mx_lock(&lock2, &atom);
  150. mx_delete(&atom);
  151. fprintf(stderr, "Even if you try to delete more than once\n");
  152. mx_delete(&atom);
  153. mx_delete(&atom);
  154. mx_delete(&atom);
  155. fprintf(stderr, "There is still a lock present\n");
  156. mx_unlock(&lock1);
  157. mx_delete(&atom);
  158. mx_delete(&atom);
  159. fprintf(stderr, "Atoms get deleted only when the last lock is removed\n");
  160. mx_unlock(&lock2);
  161. fprintf(stderr, "Atom is deleted in last unlock\n");
  162. }
  163. /*
  164. MODULE: DRS - Dirty rectangle system
  165. -----------------------------------------------------
  166. This module handles a dirty rectangle system.
  167. REQUIRES: REGION (VECTOR)
  168. */
  169. #define MXMODULE_DRS
  170. static void flush(MX_RECT * rect)
  171. {
  172. fprintf(stderr, "{ %5i, %5i, %5i, %5i }\n", rect->x1, rect->y1, rect->x2,
  173. rect->y2);
  174. }
  175. static void test_drs(void)
  176. {
  177. MX_RECT part1 = { 100, 100, 300, 300 };
  178. MX_RECT part2 = { 200, 200, 400, 400 };
  179. fprintf(stderr, "\n\nTesting module DRS\n");
  180. /* Set the entire area */
  181. mx_drs_area(639, 479);
  182. mx_drs_update(0);
  183. /* Mark some areas for updating */
  184. mx_drs_dirty(&part1, true);
  185. mx_drs_dirty(&part2, true);
  186. /* Tell the dirty parts */
  187. mx_drs_update(flush);
  188. }
  189. /*
  190. TEST ALL THE MODULES
  191. --------------------
  192. */
  193. /* Bring in all the modules */
  194. #include "detk/detk.c"
  195. #include "deds/deds.c"
  196. int main(void)
  197. {
  198. test_vector();
  199. test_utf8();
  200. test_region();
  201. test_atom();
  202. test_drs();
  203. return 0;
  204. }