_gfx.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #define MXMODULE_BITLINE
  2. #define MXMODULE_BITDRAW
  3. #define MXMODULE_FONTDRAW
  4. #define MXMODULE_FONT8X14
  5. #define MXMODULE_FONT8X16
  6. #define MXMODULE_LOADFONT
  7. #define MXMODULE_LOADTGA
  8. #define MXMODULE_FONT_ALL
  9. #define MXMODULE_DRIVER_ALL
  10. #include "degfx/degfx.c"
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <time.h>
  15. #define NUMBALLS 50
  16. static char message[200];
  17. static MX_BITMAP *ball, *ball2;
  18. static void draw_intro(const MX_RECT * r)
  19. {
  20. mx_rectfill(r->x1, r->y1, r->x2, r->y2, MXCOLOR_mediumslateblue);
  21. mx_blit(ball, 0, 0, 1, 1, mx_w(ball), mx_h(ball));
  22. mx_font_drawblock(0, message, -1, 1, 5 + mx_h(ball), MXCOLOR_white);
  23. }
  24. static MX_FONT *font = 0;
  25. static MX_FONT *font2 = 0;
  26. static MX_FONT *font3 = 0;
  27. static void draw_font(const MX_RECT * r)
  28. {
  29. mx_rectfill(r->x1, r->y1, r->x2, r->y2, MXCOLOR_mediumslateblue);
  30. mx_font_draw(font, "Loaded antialiased font", -1, 1, 1, MXCOLOR_white);
  31. mx_font_draw(font2, "Another loaded antialiased font", -1, 1, 30,
  32. MXTRANS(MXCOLOR_white, 32));
  33. mx_font_draw(font3, "Loaded bitfont", -1, 1, 50, MXCOLOR_white);
  34. mx_font_draw(&mx_font_8x16, "Builtin 8x16 font", -1, 1, 80, MXCOLOR_white);
  35. mx_font_draw(&mx_font_8x14, "Builtin 8x14 font", -1, 1, 110,
  36. MXTRANS(MXCOLOR_white, 222));
  37. mx_font_draw(&mx_font_8x8, "Builtin 8x8 font", -1, 1, 140, MXCOLOR_white);
  38. mx_font_draw(0, "Press key to continue", -1, 1, 170, MXCOLOR_white);
  39. }
  40. static void draw_trans(const MX_RECT * r)
  41. {
  42. mx_rectfill(r->x1, r->y1, r->x2, r->y2, MXCOLOR_mediumslateblue);
  43. mx_font_draw(font, "Transparency support", -1, 1, 1, MXCOLOR_white);
  44. mx_blit(ball, 0, 0, 1, 30, MXDEFAULT, MXDEFAULT);
  45. mx_blit(ball2, 0, 0, 30, 60, MXDEFAULT, MXDEFAULT);
  46. mx_blittrans(ball, 0, 0, 60, 45, MXDEFAULT, MXDEFAULT, 205);
  47. mx_font_draw(0, "Press key to continue", -1, 1, 150, MXCOLOR_white);
  48. }
  49. static MX_RECT rect[NUMBALLS];
  50. static int rate[NUMBALLS];
  51. static int trans[NUMBALLS];
  52. static int framecount, fps;
  53. static void draw_scene(const MX_RECT * r)
  54. {
  55. int i;
  56. /* Draw background */
  57. mx_rectfill(r->x1, r->y1, r->x2, r->y2, MXCOLOR_mediumslateblue);
  58. /* Draw sprites on background */
  59. for (i = 0; i < NUMBALLS; i++) {
  60. const int t1 = (trans[i] & 0xff);
  61. const int t = (trans[i] & 0x100) ? (0xff - t1) : (t1);
  62. mx_blittrans(ball, 0, 0, rect[i].x1, rect[i].y1, MXDEFAULT, MXDEFAULT,
  63. t & 0xff);
  64. }
  65. mx_font_drawblock(font, message, -1, 1, 1, MXCOLOR_white);
  66. }
  67. typedef struct DRIVER {
  68. const MX_DRIVER *driver;
  69. const char *name;
  70. } DRIVER;
  71. int main(int argc, char *argv[])
  72. {
  73. int i;
  74. MX_GFX_ARGS args;
  75. time_t lasttime = clock();
  76. unsigned updatemessage = true;
  77. MX_BITMAP_ITER start, end;
  78. int drivercount = 0;
  79. DRIVER driver[] = {
  80. {&mx_driver_fullscreen, "Fullscreen"},
  81. {&mx_driver_windowed, "Windowed"},
  82. {&mx_driver_vga, "Vga"},
  83. {&mx_driver_13h, "13H"},
  84. {0, 0}
  85. };
  86. (void) argc;
  87. (void) argv;
  88. /* Set some startup values */
  89. memset(&args, 0, sizeof(args));
  90. /* args.w = 1024; */
  91. /* args.h = 768; */
  92. args.c = 16;
  93. args.driver = driver[drivercount].driver;
  94. /* args.buffer = mx_bitmap(1024,768); */
  95. /* args.buffer = mx_bitmap(640, 15); */
  96. /* Start the graphic system */
  97. if (!mx_gfx_start(&args))
  98. return 1;
  99. /* Load the bitmap */
  100. ball = mx_bitmap_tga("title.tga");
  101. if (!ball) {
  102. mx_gfx_stop();
  103. perror("Could not load title.tga");
  104. exit(1);
  105. }
  106. /* Draw the intro screen */
  107. sprintf(message,
  108. "Welcome to the DEGFX library\n\n" " Driver: %s\n"
  109. " Screen: %i x %i x %i\n" "Buffersize: %i x %i\n"
  110. "\nPress key to continue", args.title, args.w, args.h, args.c,
  111. mx_w(args.buffer), mx_h(args.buffer));
  112. mx_gfx_redraw(draw_intro);
  113. while (mx_gfx_poll()) {
  114. mx_gfx_pointer(0, 0, 0);
  115. if (mx_gfx_key(0, 0))
  116. break;
  117. }
  118. /* Do the font screen */
  119. font = mx_font_pcx("./font/font.pcx", 32);
  120. font2 = mx_font_pcx("./font/font2.pcx", 32);
  121. font3 = mx_font_pcx("./font/arial.pcx", 32);
  122. mx_gfx_redraw(draw_font);
  123. while (mx_gfx_poll()) {
  124. mx_gfx_pointer(0, 0, 0);
  125. if (mx_gfx_key(0, 0))
  126. break;
  127. }
  128. /* Do the transparency screen */
  129. ball2 = mx_bitmap_clone(ball);
  130. start = mx_bitmap_begin(ball2);
  131. end = mx_bitmap_end(ball2);
  132. while (start != end) {
  133. *start = MXTRANS(*start, 64);
  134. ++start;
  135. }
  136. mx_gfx_redraw(draw_trans);
  137. while (mx_gfx_poll()) {
  138. mx_gfx_pointer(0, 0, 0);
  139. if (mx_gfx_key(0, 0))
  140. break;
  141. }
  142. /* Setup the movement */
  143. for (i = 0; i < NUMBALLS; i++) {
  144. rect[i].x1 = rand() % (args.w - mx_w(ball));
  145. rect[i].y1 = rand() % (args.h - mx_h(ball));
  146. rect[i].x2 = rect[i].x1 + mx_w(ball);
  147. rect[i].y2 = rect[i].y1 + mx_h(ball);
  148. rate[i] = 0;
  149. while (rate[i] == 0)
  150. rate[i] = rand() % 20 - 10;
  151. trans[i] = rand() % (0xff);
  152. }
  153. strcpy(message, "");
  154. /* The main gfx loop */
  155. mx_gfx_redraw(draw_scene);
  156. while (mx_gfx_poll()) {
  157. MX_RECT both;
  158. time_t thistime;
  159. args = *mx_gfx_info();
  160. /* Move objects */
  161. for (i = 0; i < NUMBALLS; i++) {
  162. /* FIX ME: Separate declaration from initialisation to avoid C68
  163. compiler problem */
  164. MX_RECT *ptr;
  165. MX_RECT old;
  166. ptr = &rect[i];
  167. old = *ptr;
  168. if (ptr->y1 >= args.screen.y2) {
  169. ptr->y1 = -mx_h(ball);
  170. ptr->y2 = 0;
  171. }
  172. if (ptr->y2 < 0) {
  173. ptr->y1 = args.screen.y2;
  174. ptr->y2 = args.screen.y2 + mx_h(ball);
  175. }
  176. ptr->y1 += rate[i];
  177. ptr->y2 += rate[i];
  178. trans[i] += 4;
  179. /* Mark objects as dirty */
  180. MXRECT_UNION(old, *ptr, both);
  181. mx_gfx_dirty(&both);
  182. }
  183. /* Next driver when a key is hit */
  184. if (mx_gfx_key(0, 0)) {
  185. ++drivercount;
  186. /* fputs(message, stderr);
  187. break; */
  188. if (driver[drivercount].driver) {
  189. args.driver = driver[drivercount].driver;
  190. if (!mx_gfx_start(&args))
  191. break;
  192. } else
  193. break;
  194. }
  195. /* Measure frames per second */
  196. ++framecount;
  197. thistime = clock();
  198. if (thistime > (lasttime + CLOCKS_PER_SEC)) {
  199. fps = framecount;
  200. framecount = 0;
  201. lasttime = thistime;
  202. updatemessage = true;
  203. }
  204. if (updatemessage) {
  205. int w = mx_w(args.buffer);
  206. int h = mx_h(args.buffer);
  207. MX_RECT textrect;
  208. sprintf(message, "Buffer %i x %i (%i Kb)\n%s -> %s\nFPS: %i ", w, h,
  209. (w * h * 4) / 1024, driver[drivercount].name, args.title,
  210. fps);
  211. mx_font_blocksize(font, message, -1, &w, &h, 0);
  212. textrect.x1 = 1;
  213. textrect.y1 = 1;
  214. textrect.x2 = 1 + w;
  215. textrect.y2 = 1 + h;
  216. mx_gfx_dirty(&textrect);
  217. updatemessage = false;
  218. }
  219. mx_gfx_pointer(0, 0, 0);
  220. }
  221. if (font)
  222. mx_delete(font);
  223. if (font2)
  224. mx_delete(font2);
  225. if (font3)
  226. mx_delete(font3);
  227. if (ball2)
  228. mx_delete(ball2);
  229. if (ball)
  230. mx_delete(ball);
  231. mx_gfx_stop();
  232. return 0;
  233. }