demo03.lpr 6.1 KB

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