demo03.dpr 5.8 KB

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