demo03.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #define ZGL_IMPORT
  2. #include <memory.h>
  3. #include "zglHeader.h"
  4. zglPFont fntMain;
  5. int joyCount;
  6. const char *userInput;
  7. bool trackInput;
  8. zglTRect inputRect;
  9. byte lineAlpha;
  10. char resource[256];
  11. char* GetResource( char* FileName )
  12. {
  13. sprintf_s( resource, "../data/%s", FileName );
  14. return resource;
  15. }
  16. void Init()
  17. {
  18. fntMain = font_LoadFromFile( GetResource( "font.zfi" ) );
  19. inputRect.X = 400 - 192;
  20. inputRect.Y = 300 - 100 - 32;
  21. inputRect.W = 384;
  22. inputRect.H = 96;
  23. // RU: Инициализируем обработку ввода джойстиков и получаем количество подключенных джойстиков.
  24. // EN: Initialize processing joystick input and get count of plugged joysticks.
  25. joyCount = joy_Init();
  26. }
  27. void Draw()
  28. {
  29. float w;
  30. char text[64];
  31. text_Draw( fntMain, 0, 0, "Escape - Exit" );
  32. // RU: Координаты мыши можно получить при помощи функций mouse_X и mouse_Y.
  33. // EN: Mouse coordinates can be got using functions mouse_X and mouse_Y.
  34. sprintf_s( text, "Mouse X, Y: %i; %i", mouse_X(), mouse_Y() );
  35. text_Draw( fntMain, 0, 16, text );
  36. // RU: Выводим введённый пользователем текст.
  37. // EN: Show the inputted text.
  38. pr2d_Rect( inputRect.X, inputRect.Y, inputRect.W, inputRect.H, 0xFFFFFF, 255 );
  39. if ( trackInput )
  40. {
  41. text_Draw( fntMain, 400, 300 - 100, "Press Enter to stop track text input:", TEXT_HALIGN_CENTER );
  42. w = text_GetWidth( fntMain, userInput );
  43. pr2d_Rect( 400 + w / 2 + 2, 300 - 70, 10, 20, 0xFFFFFF, lineAlpha, PR2D_FILL );
  44. }
  45. else
  46. text_Draw( fntMain, 400, 300 - 100, "Click here to enter text(maximum - 24 symbols):", TEXT_HALIGN_CENTER );
  47. text_Draw( fntMain, 400, 300 - 70, userInput, TEXT_HALIGN_CENTER );
  48. // RU: Вывод состояния осей и кнопок первого джойстика в системе.
  49. // EN: Show the state of axes and buttons of first joystick in the system.
  50. sprintf_s( text, "JOYSTICK ( Found: %i )", joyCount );
  51. text_Draw( fntMain, 400, 360, text, TEXT_HALIGN_CENTER );
  52. sprintf_s( text, "Axis X: %1.2f", joy_AxisPos( 0, JOY_AXIS_X ) );
  53. text_Draw( fntMain, 100, 400, text );
  54. sprintf_s( text, "Axis Y: %1.2f", joy_AxisPos( 0, JOY_AXIS_Y ) );
  55. text_Draw( fntMain, 100, 420, text );
  56. sprintf_s( text, "Axis Z: %1.2f", joy_AxisPos( 0, JOY_AXIS_Z ) );
  57. text_Draw( fntMain, 100, 440, text );
  58. sprintf_s( text, "Axis R: %1.2f", joy_AxisPos( 0, JOY_AXIS_R ) );
  59. text_Draw( fntMain, 100, 460, text );
  60. sprintf_s( text, "Axis U: %1.2f", joy_AxisPos( 0, JOY_AXIS_U ) );
  61. text_Draw( fntMain, 100, 480, text );
  62. sprintf_s( text, "Axis V: %1.2f", joy_AxisPos( 0, JOY_AXIS_V ) );
  63. text_Draw( fntMain, 100, 500, text );
  64. sprintf_s( text, "POVX: %1.2f", joy_AxisPos( 0, JOY_POVX ) );
  65. text_Draw( fntMain, 100, 520, text );
  66. sprintf_s( text, "POVY: %1.2f", joy_AxisPos( 0, JOY_POVY ) );
  67. text_Draw( fntMain, 100, 540, text );
  68. sprintf_s( text, "Button1: %s", joy_Down( 0, 0 ) ? "TRUE" : "FALSE" );
  69. text_Draw( fntMain, 400, 400, text );
  70. sprintf_s( text, "Button2: %s", joy_Down( 0, 1 ) ? "TRUE" : "FALSE" );
  71. text_Draw( fntMain, 400, 420, text );
  72. sprintf_s( text, "Button3: %s", joy_Down( 0, 2 ) ? "TRUE" : "FALSE" );
  73. text_Draw( fntMain, 400, 440, text );
  74. sprintf_s( text, "Button4: %s", joy_Down( 0, 3 ) ? "TRUE" : "FALSE" );
  75. text_Draw( fntMain, 400, 460, text );
  76. sprintf_s( text, "Button5: %s", joy_Down( 0, 4 ) ? "TRUE" : "FALSE" );
  77. text_Draw( fntMain, 400, 480, text );
  78. sprintf_s( text, "Button6: %s", joy_Down( 0, 5 ) ? "TRUE" : "FALSE" );
  79. text_Draw( fntMain, 400, 500, text );
  80. sprintf_s( text, "Button7: %s", joy_Down( 0, 6 ) ? "TRUE" : "FALSE" );
  81. text_Draw( fntMain, 400, 520, text );
  82. sprintf_s( text, "Button8: %s", joy_Down( 0, 7 ) ? "TRUE" : "FALSE" );
  83. text_Draw( fntMain, 400, 540, text );
  84. sprintf_s( text, "Button9: %s", joy_Down( 0, 8 ) ? "TRUE" : "FALSE" );
  85. text_Draw( fntMain, 550, 400, text );
  86. sprintf_s( text, "Button10: %s", joy_Down( 0, 9 ) ? "TRUE" : "FALSE" );
  87. text_Draw( fntMain, 550, 420, text );
  88. sprintf_s( text, "Button11: %s", joy_Down( 0, 10 ) ? "TRUE" : "FALSE" );
  89. text_Draw( fntMain, 550, 440, text );
  90. sprintf_s( text, "Button12: %s", joy_Down( 0, 11 ) ? "TRUE" : "FALSE" );
  91. text_Draw( fntMain, 550, 460, text );
  92. sprintf_s( text, "Button13: %s", joy_Down( 0, 12 ) ? "TRUE" : "FALSE" );
  93. text_Draw( fntMain, 550, 480, text );
  94. sprintf_s( text, "Button14: %s", joy_Down( 0, 13 ) ? "TRUE" : "FALSE" );
  95. text_Draw( fntMain, 550, 500, text );
  96. sprintf_s( text, "Button15: %s", joy_Down( 0, 14 ) ? "TRUE" : "FALSE" );
  97. text_Draw( fntMain, 550, 520, text );
  98. sprintf_s( text, "Button16: %s", joy_Down( 0, 15 ) ? "TRUE" : "FALSE" );
  99. text_Draw( fntMain, 550, 540, text );
  100. }
  101. void Timer()
  102. {
  103. if ( lineAlpha > 5 )
  104. lineAlpha -= 10;
  105. else
  106. lineAlpha = 255;
  107. // RU: Проверить нажата ли левая кнопка мыши в пределах inputRect и начать отслеживать ввод текста.
  108. // EN: Check if left mouse button was pressed inside inputRect and start to track text input.
  109. if ( mouse_Click( M_BLEFT ) && col2d_PointInRect( (float)mouse_X(), (float)mouse_Y(), inputRect ) )
  110. {
  111. trackInput = TRUE;
  112. key_BeginReadText( userInput, 24 );
  113. }
  114. // RU: Если был нажат Enter прекращаем отслеживать ввод текста.
  115. // EN: Finish to track text input if Enter was pressed.
  116. if ( key_Press( K_ENTER ) )
  117. {
  118. trackInput = FALSE;
  119. key_EndReadText();
  120. }
  121. // RU: Получаем введённый пользователем текст.
  122. // EN: Get inputted by user text.
  123. if ( trackInput )
  124. userInput = key_GetText();
  125. // RU: По нажатию Escape завершить приложение.
  126. // EN: If Escape was pressed - shutdown the application.
  127. if ( key_Press( K_ESCAPE ) ) zgl_Exit();
  128. // RU: Обязательно очищаем состояния всех подсистем ввода.
  129. // EN: Necessarily clear all the states of input subsystems.
  130. mouse_ClearState();
  131. key_ClearState();
  132. joy_ClearState();
  133. }
  134. int CALLBACK WinMain (
  135. __in HINSTANCE hInstance,
  136. __in_opt HINSTANCE hPrevInstance,
  137. __in_opt LPSTR lpCmdLine,
  138. __in int nShowCmd
  139. )
  140. {
  141. if ( !zglLoad( libZenGL ) ) return 0;
  142. timer_Add( (void*)&Timer, 16 );
  143. zgl_Reg( SYS_LOAD, (void*)&Init );
  144. zgl_Reg( SYS_DRAW, (void*)&Draw );
  145. wnd_SetCaption( "03 - Input" );
  146. wnd_ShowCursor( TRUE );
  147. scr_SetOptions( 800, 600, REFRESH_MAXIMUM, FALSE, FALSE );
  148. zgl_Init();
  149. zglFree();
  150. return 0;
  151. }