游戏循环 - 线程 [英] game loop - threads

查看:147
本文介绍了游戏循环 - 线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java程序中的游戏循环是否总是在事件派发线程中运行?
因为我的keylisteners在AWT事件线程中运行,我希望在与游戏其余部分相同的线程中处理关键事件,以避免基于线程的错误。
如果我没有在事件线程中运行游戏循环,我可以使用ArrayBlockingQueue将事件从事件处理程序发送到游戏循环吗?有更好的解决方案吗?
谢谢

should a game loop in a java program always run in the event dispatch thread? Because my keylisteners are running in the AWT event thread and I want the key events to be processed in the same thread as the rest of the game to avoid thread based bugs. If I don't run the game loop in the event thread can I use an ArrayBlockingQueue to send events from the event handler to the game loop? Is there a better solution? thanks

也许是这样的?

ConcurrentLinkedQueue<AWTEvent> eventQueue = new ConcurrentLinkedQueue<AWTEvent>(); 

JFrame frame = new JFrame() {
    @override
    protected void processEvent(AWTEvent e) {
        if(e instanceof InputEvent) eventQueue.add(e);
        else super.processEvent(e);
    }
}

//game loop
while(true) {
    while(!eventQueue.isEmpty()) frame.super.processEvent(eventQueue.poll());
    ...
}

编辑:谢谢,我想我现在知道怎么做。我要这样做

edit: Thank you, I think I now know how to do this. I'm gonna do it like this

import java.util.concurrent.ConcurrentLinkedQueue;
import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

public class QueueTest {

    static ConcurrentLinkedQueue<AWTEvent> eventQueue = new ConcurrentLinkedQueue<AWTEvent>(); 

    public static void main(String[] args) {
        final JPanel panel = new JPanel() {
            @Override
            protected void processEvent(AWTEvent e) {
                eventQueue.add(e);
            }
            {
                enableEvents(  AWTEvent.MOUSE_EVENT_MASK
                             | AWTEvent.MOUSE_MOTION_EVENT_MASK
                             | AWTEvent.KEY_EVENT_MASK);
            }
        };
        final JFrame frame = new JFrame();
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                panel.requestFocusInWindow();
            }
        });
        frame.add(panel);
        panel.setPreferredSize(new Dimension(800,600));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

        while(true) {
            while(!eventQueue.isEmpty()) System.out.println(eventQueue.poll()+"\n");
        }
    }
}


推荐答案

我在我写的游戏引擎中遇到了类似的问题。

I had a similar problem in the game engine I wrote.

最后我必须为我的游戏引擎循环设置一个单独的线程。当AWT事件被解雇时,我必须存储它们,并在游戏循环中的下一个逻辑更新期间触发它们。

In the end I had to have a separate thread for my game engine loop. When AWT events were fired I had to store them, and fire them during the next logical update within the game loop.

这是我的解决方案但是,我相信事件的最简单事实是实际调查它们!:)

This was my solution however, I believe that the 'easiest' thing for events is to actually poll them! :)

这篇关于游戏循环 - 线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆