Solutions to some problems when using Qt for game development (1)

Source: Internet
Author: User

From: http://blog.csdn.net/wsrlyk/article/details/5631573

Qt is a good library. Therefore, in some cases, you can build programs and game frameworks based on Qt.

 

The following describes the problems and solutions of Qt as a game framework.

 

(1) buttons

 

Reload the keyPressEvent, keyReleaseEvent, mousePressEvent, mouseReleaseEvent, and mouseMoveEvent functions in the Widget.

 

However, there is a problem with keyPressEvent. In Windows (I don't know about other environments ......), When you press and hold a key, the system will first respond, pause for a while, and then start to respond continuously. In the game, this feature shows that a person takes a step first, stops, and continues.

 

This feature affects the gaming experience, so the common solution is: press, set a flag to true, release, set flag to false, then, in the game rendering loop, the actions of the character are determined based on the flag value (that is, by waiting rather than interrupting)

 

However, there is still a problem with the Qt keyboard function. It does not trigger the keyPressEvent only when the key is pressed, but triggers the keyReleaseEvent only when the key is played. Instead, it triggers the keyPressEvent before the message is output, trigger keyReleaseEvent after output ". When you press and hold a key, press, release, press, press, and release constantly ......

 

Fortunately, Qt provides another function, which provides autorepeat judgment in the keyboard event class QKeyEvent, that is, the keyboard events triggered when the key is pressed belong to the autorepeat type. Therefore, you can exclude the press and release in the middle.

 

However, Qt Keyboard Events still have a very strong phenomenon (I don't know why). When holding down a key:

 

1. Trigger keyPressEvent, isAutoRepeat () returns false

2.NoTrigger keyReleaseEvent, pause for a moment

3. Trigger keyPressEvent, isAutoRepeat () returns true

4. Trigger keyReleaseEvent

5. If the button is not released, isAutoRepeat () returns true, and 3; releases the button, and isAutoRepeat () returns false

 

So sometimes you need to set a flag to avoid the impact of step 2.

 

The final code is as follows:

 

KeyPress

 

[Cpp]
View plaincopyprint?
  1. VoidMyWidget: keyPressEvent (QKeyEvent * evt)
  2. {
  3. Switch(Evt-> key ()){
  4. CaseQt: Key_W:
  5. If(! Evt-> isAutoRepeat ()&&! MKeyW ){
  6. MKeyW =True;
  7. // Press the event processing statement w.
  8. }
  9. Break;
  10. Default:Break;
  11. }
  12. QWidget: keyPressEvent (evt );
  13. }

Void MyWidget: keyPressEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (! Evt-> isAutoRepeat ()&&! MKeyW) {<br/> mKeyW = true; <br/> // The event processing statement for pressing w <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyPressEvent (evt); <br/>}< br/>

KeyRelease


[C-sharp]
View plaincopyprint?
  1. VoidMyWidget: keyReleaseEvent (QKeyEvent * evt)
  2. {
  3. Switch(Evt-> key ()){
  4. CaseQt: Key_W:
  5. If(MKeyW &&! Evt-> isAutoRepeat ()){
  6. MKeyW =False;
  7. // Release w's event processing statement.
  8. }
  9. Break;
  10. Default:Break;
  11. }
  12. QWidget: keyReleaseEvent (evt );
  13. }

Void MyWidget: keyReleaseEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (mKeyW &&! Evt-> isAutoRepeat () {<br/> mKeyW = false; <br/> // release the w event processing statement <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyReleaseEvent (evt); <br/>}< br/> 

 

 

To be continued.

 

From: http://blog.csdn.net/wsrlyk/article/details/5631573

Qt is a good library. Therefore, in some cases, you can build programs and game frameworks based on Qt.

 

The following describes the problems and solutions of Qt as a game framework.

 

(1) buttons

 

Reload the keyPressEvent, keyReleaseEvent, mousePressEvent, mouseReleaseEvent, and mouseMoveEvent functions in the Widget.

 

However, there is a problem with keyPressEvent. In Windows (I don't know about other environments ......), When you press and hold a key, the system will first respond, pause for a while, and then start to respond continuously. In the game, this feature shows that a person takes a step first, stops, and continues.

 

This feature affects the gaming experience, so the common solution is: press, set a flag to true, release, set flag to false, then, in the game rendering loop, the actions of the character are determined based on the flag value (that is, by waiting rather than interrupting)

 

However, there is still a problem with the Qt keyboard function. It does not trigger the keyPressEvent only when the key is pressed, but triggers the keyReleaseEvent only when the key is played. Instead, it triggers the keyPressEvent before the message is output, trigger keyReleaseEvent after output ". When you press and hold a key, press, release, press, press, and release constantly ......

 

Fortunately, Qt provides another function, which provides autorepeat judgment in the keyboard event class QKeyEvent, that is, the keyboard events triggered when the key is pressed belong to the autorepeat type. Therefore, you can exclude the press and release in the middle.

 

However, Qt Keyboard Events still have a very strong phenomenon (I don't know why). When holding down a key:

 

1. Trigger keyPressEvent, isAutoRepeat () returns false

2.NoTrigger keyReleaseEvent, pause for a moment

3. Trigger keyPressEvent, isAutoRepeat () returns true

4. Trigger keyReleaseEvent

5. If the button is not released, isAutoRepeat () returns true, and 3; releases the button, and isAutoRepeat () returns false

 

So sometimes you need to set a flag to avoid the impact of step 2.

 

The final code is as follows:

 

KeyPress

 

[Cpp]
View plaincopyprint?
  1. VoidMyWidget: keyPressEvent (QKeyEvent * evt)
  2. {
  3. Switch(Evt-> key ()){
  4. CaseQt: Key_W:
  5. If(! Evt-> isAutoRepeat ()&&! MKeyW ){
  6. MKeyW =True;
  7. // Press the event processing statement w.
  8. }
  9. Break;
  10. Default:Break;
  11. }
  12. QWidget: keyPressEvent (evt );
  13. }

Void MyWidget: keyPressEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (! Evt-> isAutoRepeat ()&&! MKeyW) {<br/> mKeyW = true; <br/> // The event processing statement for pressing w <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyPressEvent (evt); <br/>}< br/>

KeyRelease


[C-sharp]
View plaincopyprint?
  1. VoidMyWidget: keyReleaseEvent (QKeyEvent * evt)
  2. {
  3. Switch(Evt-> key ()){
  4. CaseQt: Key_W:
  5. If(MKeyW &&! Evt-> isAutoRepeat ()){
  6. MKeyW =False;
  7. // Release w's event processing statement.
  8. }
  9. Break;
  10. Default:Break;
  11. }
  12. QWidget: keyReleaseEvent (evt );
  13. }

Void MyWidget: keyReleaseEvent (QKeyEvent * evt) <br/>{< br/> switch (evt-> key () {<br/> case Qt: Key_W: <br/> if (mKeyW &&! Evt-> isAutoRepeat () {<br/> mKeyW = false; <br/> // release the w event processing statement <br/>}< br/> break; <br/> default: break; <br/>}< br/> QWidget: keyReleaseEvent (evt); <br/>}< br/> 

 

 

To be continued.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.