Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Класс
- Gamepad::Gamepad()
- {
- key.button=0;
- key.state=GAMEPAD_RELEASE;
- device=-1;
- length=sizeof(input_event);
- memset(&input,0,length);
- }
- Gamepad::~Gamepad()
- {
- if(device!=-1) close(device);
- }
- void Gamepad::read_input()
- {
- while (read(device,&input,length)>0)
- {
- if (input.type==EV_KEY)
- {
- key.button=input.code;
- key.state=input.value;
- if(key.state!=GAMEPAD_HOLDING) break;
- }
- }
- }
- void Gamepad::clear_state()
- {
- key.button=0;
- key.state=GAMEPAD_RELEASE;
- }
- bool Gamepad::check_state(const GAMEPAD_BUTTONS button,const unsigned short int state)
- {
- bool result;
- result=false;
- if (key.state==state)
- {
- if (key.button==button) result=true;
- }
- return result;
- }
- void Gamepad::initialize()
- {
- device=open("/dev/event0",O_RDONLY|O_NONBLOCK|O_NOCTTY);
- if (device==-1)
- {
- Halt("Can't get access to gamepad");
- }
- }
- void Gamepad::update()
- {
- this->clear_state();
- this->read_input();
- }
- bool Gamepad::check_hold(const GAMEPAD_BUTTONS button)
- {
- return this->check_state(button,GAMEPAD_HOLDING) || this->check_state(button,GAMEPAD_PRESS);
- }
- bool Gamepad::check_press(const GAMEPAD_BUTTONS button)
- {
- return this->check_state(button,GAMEPAD_PRESS);
- }
- bool Gamepad::check_release(const GAMEPAD_BUTTONS button)
- {
- return this->check_state(button,GAMEPAD_RELEASE);
- }
- // Демка
- #include "dinguxgdk.h"
- int main(void)
- {
- long int x,y,screen_width,screen_height;
- char perfomance[8];
- DINGUXGDK::Screen screen;
- DINGUXGDK::Gamepad gamepad;
- DINGUXGDK::Timer timer;
- DINGUXGDK::Image image;
- DINGUXGDK::Background space;
- DINGUXGDK::Sprite ship,font;
- DINGUXGDK::Text text;
- screen.initialize();
- screen_width=screen.get_width();
- screen_height=screen.get_height();
- x=screen_width/2;
- y=screen_height/2;
- image.load_tga("space.tga");
- space.load_image(image);
- image.load_tga("ship.tga");
- ship.load_image(image);
- image.load_tga("font.tga");
- font.load_image(image);
- text.load_font(font.get_handle());
- gamepad.initialize();
- space.initialize(screen.get_handle());
- ship.initialize(screen.get_handle());
- font.initialize(screen.get_handle());
- space.resize_image(screen_width,screen_height);
- space.set_kind(NORMAL_BACKGROUND);
- screen.clear_screen();
- ship.set_frames(2);
- ship.set_kind(HORIZONTAL_STRIP);
- text.set_position(font.get_width(),font.get_height());
- timer.set_timer(1);
- memset(perfomance,0,8);
- while(1)
- {
- screen.update();
- gamepad.update();
- if(gamepad.check_press(BUTTON_X)==true) break;
- if(gamepad.check_press(BUTTON_A)==true) ship.mirror_image(MIRROR_HORIZONTAL);
- if(gamepad.check_press(BUTTON_B)==true) ship.mirror_image(MIRROR_VERTICAL);
- if(gamepad.check_hold(BUTTON_UP)==true) y-=4;
- if(gamepad.check_hold(BUTTON_DOWN)==true) y+=4;
- if(gamepad.check_hold(BUTTON_LEFT)==true) x-=4;
- if(gamepad.check_hold(BUTTON_RIGHT)==true) x+=4;
- if((x<=0)||(x>=screen_width)) x=screen_width/2;
- if((y<=0)||(y>=screen_height)) y=screen_height/2;
- sprintf(perfomance,"%ld",screen.get_fps());
- space.draw_background();
- text.draw_text(perfomance);
- ship.set_position(x,y);
- ship.draw_sprite();
- if (timer.check_timer()==true)
- {
- ship.step();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment