demo03.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. program demo03;
  2. {$I zglCustomConfig.cfg}
  3. uses
  4. {$IFDEF USE_ZENGL_STATIC}
  5. zgl_main,
  6. zgl_screen,
  7. zgl_window,
  8. zgl_timers,
  9. zgl_mouse,
  10. zgl_keyboard,
  11. zgl_joystick,
  12. zgl_primitives_2d,
  13. zgl_font,
  14. zgl_text,
  15. zgl_textures_png,
  16. zgl_math_2d,
  17. zgl_collision_2d,
  18. zgl_utils
  19. {$ELSE}
  20. zglHeader
  21. {$ENDIF}
  22. ;
  23. var
  24. dirRes : UTF8String {$IFNDEF MACOSX} = '../data/' {$ENDIF};
  25. fntMain : zglPFont;
  26. joyCount : Integer;
  27. userInput : UTF8String;
  28. trackInput : Boolean;
  29. inputRect : zglTRect;
  30. lineAlpha : Byte;
  31. procedure Init;
  32. begin
  33. fntMain := font_LoadFromFile( dirRes + 'font.zfi' );
  34. inputRect.X := 400 - 192;
  35. inputRect.Y := 300 - 100 - 32;
  36. inputRect.W := 384;
  37. inputRect.H := 96;
  38. // RU: Инициализируем обработку ввода джойстиков и получаем количество подключенных джойстиков.
  39. // EN: Initialize processing joystick input and get count of plugged joysticks.
  40. joyCount := joy_Init();
  41. end;
  42. procedure Draw;
  43. var
  44. w : Single;
  45. begin
  46. text_Draw( fntMain, 0, 0, 'Escape - Exit' );
  47. // RU: Координаты мыши можно получить при помощи функций mouse_X и mouse_Y.
  48. // EN: Mouse coordinates can be got using functions mouse_X and mouse_Y.
  49. text_Draw( fntMain, 0, 16, 'Mouse X, Y: ' + u_IntToStr( mouse_X() ) + '; ' + u_IntToStr( mouse_Y() ) );
  50. // RU: Выводим введённый пользователем текст.
  51. // EN: Show the inputted text.
  52. pr2d_Rect( inputRect.X, inputRect.Y, inputRect.W, inputRect.H, $FFFFFF, 255 );
  53. if trackInput Then
  54. begin
  55. text_Draw( fntMain, 400, 300 - 100, 'Press Enter to stop track text input:', TEXT_HALIGN_CENTER );
  56. w := text_GetWidth( fntMain, userInput );
  57. pr2d_Rect( 400 + w / 2 + 2, 300 - 70, 10, 20, $FFFFFF, lineAlpha, PR2D_FILL );
  58. end else
  59. text_Draw( fntMain, 400, 300 - 100, 'Click here to enter text(maximum - 24 symbols):', TEXT_HALIGN_CENTER );
  60. text_Draw( fntMain, 400, 300 - 70, userInput, TEXT_HALIGN_CENTER );
  61. // RU: Вывод состояния осей и кнопок первого джойстика в системе.
  62. // EN: Show the state of axes and buttons of first joystick in the system.
  63. text_Draw( fntMain, 400, 360, 'JOYSTICK ( Found: ' + u_IntToStr( joyCount ) + ' )', TEXT_HALIGN_CENTER );
  64. text_Draw( fntMain, 100, 400, 'Axis X: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_X ) ) );
  65. text_Draw( fntMain, 100, 420, 'Axis Y: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Y ) ) );
  66. text_Draw( fntMain, 100, 440, 'Axis Z: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Z ) ) );
  67. text_Draw( fntMain, 100, 460, 'Axis R: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_R ) ) );
  68. text_Draw( fntMain, 100, 480, 'Axis U: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_U ) ) );
  69. text_Draw( fntMain, 100, 500, 'Axis V: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_V ) ) );
  70. text_Draw( fntMain, 100, 520, 'POVX: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVX ) ) );
  71. text_Draw( fntMain, 100, 540, 'POVY: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVY ) ) );
  72. text_Draw( fntMain, 400, 400, 'Button1: ' + u_BoolToStr( joy_Down( 0, 0 ) ) );
  73. text_Draw( fntMain, 400, 420, 'Button2: ' + u_BoolToStr( joy_Down( 0, 1 ) ) );
  74. text_Draw( fntMain, 400, 440, 'Button3: ' + u_BoolToStr( joy_Down( 0, 2 ) ) );
  75. text_Draw( fntMain, 400, 460, 'Button4: ' + u_BoolToStr( joy_Down( 0, 3 ) ) );
  76. text_Draw( fntMain, 400, 480, 'Button5: ' + u_BoolToStr( joy_Down( 0, 4 ) ) );
  77. text_Draw( fntMain, 400, 500, 'Button6: ' + u_BoolToStr( joy_Down( 0, 5 ) ) );
  78. text_Draw( fntMain, 400, 520, 'Button7: ' + u_BoolToStr( joy_Down( 0, 6 ) ) );
  79. text_Draw( fntMain, 400, 540, 'Button8: ' + u_BoolToStr( joy_Down( 0, 7 ) ) );
  80. text_Draw( fntMain, 550, 400, 'Button9: ' + u_BoolToStr( joy_Down( 0, 8 ) ) );
  81. text_Draw( fntMain, 550, 420, 'Button10: ' + u_BoolToStr( joy_Down( 0, 9 ) ) );
  82. text_Draw( fntMain, 550, 440, 'Button11: ' + u_BoolToStr( joy_Down( 0, 10 ) ) );
  83. text_Draw( fntMain, 550, 460, 'Button12: ' + u_BoolToStr( joy_Down( 0, 11 ) ) );
  84. text_Draw( fntMain, 550, 480, 'Button13: ' + u_BoolToStr( joy_Down( 0, 12 ) ) );
  85. text_Draw( fntMain, 550, 500, 'Button14: ' + u_BoolToStr( joy_Down( 0, 13 ) ) );
  86. text_Draw( fntMain, 550, 520, 'Button15: ' + u_BoolToStr( joy_Down( 0, 14 ) ) );
  87. text_Draw( fntMain, 550, 540, 'Button16: ' + u_BoolToStr( joy_Down( 0, 15 ) ) );
  88. end;
  89. procedure Timer;
  90. begin
  91. if lineAlpha > 5 Then
  92. DEC( lineAlpha, 10 )
  93. else
  94. lineAlpha := 255;
  95. // RU: Проверить нажата ли левая кнопка мыши в пределах inputRect и начать отслеживать ввод текста.
  96. // EN: Check if left mouse button was pressed inside inputRect and start to track text input.
  97. if mouse_Click( M_BLEFT ) and col2d_PointInRect( mouse_X(), mouse_Y(), inputRect ) Then
  98. begin
  99. trackInput := TRUE;
  100. key_BeginReadText( userInput, 24 );
  101. end;
  102. // RU: Если был нажат Enter прекращаем отслеживать ввод текста.
  103. // EN: Finish to track text input if Enter was pressed.
  104. if key_Press( K_ENTER ) Then
  105. begin
  106. trackInput := FALSE;
  107. key_EndReadText();
  108. end;
  109. // RU: Получаем введённый пользователем текст.
  110. // EN: Get inputted by user text.
  111. if trackInput Then
  112. userInput := key_GetText();
  113. // RU: По нажатию Escape завершить приложение.
  114. // EN: If Escape was pressed - shutdown the application.
  115. if key_Press( K_ESCAPE ) Then zgl_Exit();
  116. // RU: Обязательно очищаем состояния всех подсистем ввода.
  117. // EN: Necessarily clear all the states of input subsystems.
  118. mouse_ClearState();
  119. key_ClearState();
  120. joy_ClearState();
  121. end;
  122. Begin
  123. {$IFNDEF USE_ZENGL_STATIC}
  124. if not zglLoad( libZenGL ) Then exit;
  125. {$ENDIF}
  126. timer_Add( @Timer, 16 );
  127. zgl_Reg( SYS_LOAD, @Init );
  128. zgl_Reg( SYS_DRAW, @Draw );
  129. wnd_SetCaption( '03 - Input' );
  130. wnd_ShowCursor( TRUE );
  131. scr_SetOptions( 800, 600, REFRESH_MAXIMUM, FALSE, FALSE );
  132. zgl_Init();
  133. End.