repeat83

Untitled

Feb 14th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. // Класс
  2.  
  3. Gamepad::Gamepad()
  4. {
  5. key.button=0;
  6. key.state=GAMEPAD_RELEASE;
  7. device=-1;
  8. length=sizeof(input_event);
  9. memset(&input,0,length);
  10. }
  11.  
  12. Gamepad::~Gamepad()
  13. {
  14. if(device!=-1) close(device);
  15. }
  16.  
  17. void Gamepad::read_input()
  18. {
  19. while (read(device,&input,length)>0)
  20. {
  21. if (input.type==EV_KEY)
  22. {
  23. key.button=input.code;
  24. key.state=input.value;
  25. if(key.state!=GAMEPAD_HOLDING) break;
  26. }
  27.  
  28. }
  29.  
  30. }
  31.  
  32. void Gamepad::clear_state()
  33. {
  34. key.button=0;
  35. key.state=GAMEPAD_RELEASE;
  36. }
  37.  
  38. bool Gamepad::check_state(const GAMEPAD_BUTTONS button,const unsigned short int state)
  39. {
  40. bool result;
  41. result=false;
  42. if (key.state==state)
  43. {
  44. if (key.button==button) result=true;
  45. }
  46. return result;
  47. }
  48.  
  49. void Gamepad::initialize()
  50. {
  51. device=open("/dev/event0",O_RDONLY|O_NONBLOCK|O_NOCTTY);
  52. if (device==-1)
  53. {
  54. Halt("Can't get access to gamepad");
  55. }
  56.  
  57. }
  58.  
  59. void Gamepad::update()
  60. {
  61. this->clear_state();
  62. this->read_input();
  63. }
  64.  
  65. bool Gamepad::check_hold(const GAMEPAD_BUTTONS button)
  66. {
  67. return this->check_state(button,GAMEPAD_HOLDING) || this->check_state(button,GAMEPAD_PRESS);
  68. }
  69.  
  70. bool Gamepad::check_press(const GAMEPAD_BUTTONS button)
  71. {
  72. return this->check_state(button,GAMEPAD_PRESS);
  73. }
  74.  
  75. bool Gamepad::check_release(const GAMEPAD_BUTTONS button)
  76. {
  77. return this->check_state(button,GAMEPAD_RELEASE);
  78. }
  79.  
  80. // Демка
  81.  
  82. #include "dinguxgdk.h"
  83.  
  84. int main(void)
  85. {
  86. long int x,y,screen_width,screen_height;
  87. char perfomance[8];
  88. DINGUXGDK::Screen screen;
  89. DINGUXGDK::Gamepad gamepad;
  90. DINGUXGDK::Timer timer;
  91. DINGUXGDK::Image image;
  92. DINGUXGDK::Background space;
  93. DINGUXGDK::Sprite ship,font;
  94. DINGUXGDK::Text text;
  95. screen.initialize();
  96. screen_width=screen.get_width();
  97. screen_height=screen.get_height();
  98. x=screen_width/2;
  99. y=screen_height/2;
  100. image.load_tga("space.tga");
  101. space.load_image(image);
  102. image.load_tga("ship.tga");
  103. ship.load_image(image);
  104. image.load_tga("font.tga");
  105. font.load_image(image);
  106. text.load_font(font.get_handle());
  107. gamepad.initialize();
  108. space.initialize(screen.get_handle());
  109. ship.initialize(screen.get_handle());
  110. font.initialize(screen.get_handle());
  111. space.resize_image(screen_width,screen_height);
  112. space.set_kind(NORMAL_BACKGROUND);
  113. screen.clear_screen();
  114. ship.set_frames(2);
  115. ship.set_kind(HORIZONTAL_STRIP);
  116. text.set_position(font.get_width(),font.get_height());
  117. timer.set_timer(1);
  118. memset(perfomance,0,8);
  119. while(1)
  120. {
  121. screen.update();
  122. gamepad.update();
  123. if(gamepad.check_press(BUTTON_X)==true) break;
  124. if(gamepad.check_press(BUTTON_A)==true) ship.mirror_image(MIRROR_HORIZONTAL);
  125. if(gamepad.check_press(BUTTON_B)==true) ship.mirror_image(MIRROR_VERTICAL);
  126. if(gamepad.check_hold(BUTTON_UP)==true) y-=4;
  127. if(gamepad.check_hold(BUTTON_DOWN)==true) y+=4;
  128. if(gamepad.check_hold(BUTTON_LEFT)==true) x-=4;
  129. if(gamepad.check_hold(BUTTON_RIGHT)==true) x+=4;
  130. if((x<=0)||(x>=screen_width)) x=screen_width/2;
  131. if((y<=0)||(y>=screen_height)) y=screen_height/2;
  132. sprintf(perfomance,"%ld",screen.get_fps());
  133. space.draw_background();
  134. text.draw_text(perfomance);
  135. ship.set_position(x,y);
  136. ship.draw_sprite();
  137. if (timer.check_timer()==true)
  138. {
  139. ship.step();
  140. }
  141.  
  142. }
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment