This is also posted
here on the SDN, but seeing as I'm not sure how active those forums are, and that I can not do anything until this is fixed, I'm also posting here.
I am having some problems with the event handeling in the game engine I am writing:
I need to listen to key press events and release events and put them in to some Sets, which are read from the step()
method every frame and transalated into platform-independent GFKeyEvents for inturnal use.
The reason for doing so is to get rid of key repeating so the game objects just see "press, hold, hold, hold, release" no mater what the OS is doing.
As you will see the key event methods are synchronized as is step() yet some how I still get
1 2 3 4 5
| Exception in thread "Event thread 1" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810) at java.util.HashMap$KeyIterator.next(HashMap.java:845) at com.glassFlame.event.EventSystem.step(EventSystem.java:77) at com.glassFlame.event.EventThread.run(EventThread.java:36) |
I have also wrapped the Sets using
1
| Collections.synchronizedSet() |
here is the class with some of the irelivent methods removed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| package com.glassFlame.event; import java.awt.Component; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.HashSet; import java.util.Vector; import java.util.Collections; import java.util.Set; import com.glassFlame.GFObject; import com.glassFlame.renderer.RenderManager; import com.glassFlame.renderer.Renderer; import com.glassFlame.scene.Scene; import com.glassFlame.scene.bounding.BoundingStructure; public class EventSystem implements KeyListener, MouseListener, MouseMotionListener{ private BoundingStructure graph; private EventThread pump; private Component source; private Scene scene; private RenderManager ren; private boolean clear; private Vector<GFObject> objs; private Vector<GFKeyListener> klobjs; private Set<GFKeyEvent> keysHeld, keysPressedThisFrame, keysReleasedThisFrame, keysReleasedLastFrame, keysPressedLastFrame; private Vector<GFKeyEvent> keyPresses, keyHolds, keyReleases; private Object keyLock = new Object(); public EventSystem(BoundingStructure graph,Component source,RenderManager ren,int millsPerFrame) { this.clear = true; this.graph = graph; this.source = source; this.ren = ren; pump = new EventThread(this,millsPerFrame); objs = new Vector<GFObject>(100); klobjs = new Vector<GFKeyListener>(100); keysHeld = Collections.synchronizedSet( new HashSet<GFKeyEvent>()); keysPressedThisFrame = Collections.synchronizedSet( new HashSet<GFKeyEvent>()); keysReleasedThisFrame = Collections.synchronizedSet( new HashSet<GFKeyEvent>()); keysReleasedLastFrame = Collections.synchronizedSet( new HashSet<GFKeyEvent>()); keysPressedLastFrame = Collections.synchronizedSet( new HashSet<GFKeyEvent>()); keyPresses = new Vector<GFKeyEvent>(); keyHolds = new Vector<GFKeyEvent>(); keyReleases = new Vector<GFKeyEvent>(); this.source.addKeyListener(this); this.source.addMouseListener(this); this.source.addMouseMotionListener(this); } public synchronized void addObject(GFObject obj) { graph.addObject(obj); objs.add(obj); if(obj instanceof GFKeyListener) klobjs.add((GFKeyListener) obj); } public synchronized void step(long millsPassed) { for(GFKeyEvent ke : keysHeld) { if( (!keysPressedThisFrame.contains(ke) && !keysReleasedThisFrame.contains(ke)) && keysReleasedLastFrame.contains(ke)) { System.out.println(ke.toString()); keysHeld.remove(ke); keyReleases.add(ke); } else keyHolds.add(ke); } for(GFKeyEvent ke : keysPressedThisFrame) { if(keysPressedLastFrame.contains(ke) == false) keyPresses.add(ke); } for(GFKeyListener kl : klobjs) { for(GFKeyEvent ke : keyReleases) { kl.keyReleased(ke); } } for(GFKeyListener kl : klobjs) { for(GFKeyEvent ke : keyHolds) { kl.keyHeld(ke); } } for(GFKeyListener kl : klobjs) { for(GFKeyEvent ke : keyPresses) { if(!keysHeld.contains(ke)) { kl.keyPressed(ke); ke.setID(GFKeyEvent.KEY_TYPED); keysHeld.add(ke); } } } Set<GFKeyEvent> tem; tem = keysPressedLastFrame; keysPressedLastFrame = keysPressedThisFrame; keysPressedThisFrame = tem; tem = keysReleasedLastFrame; keysReleasedLastFrame = keysReleasedThisFrame; keysReleasedThisFrame = tem; keysPressedThisFrame.clear(); keysReleasedThisFrame.clear(); keyPresses.clear(); keyHolds.clear(); keyReleases.clear(); if(clear) ren.setBuffer(Renderer.BUFFER_DEFAULT); ren.clear(); ren.start(); for(int i = 0; i < objs.size();i++) { objs.get(i).draw(ren); } ren.stop(); ren.done(); } public void start() { pump.start(); } public void stop() { pump.pause(); } public boolean running() { return pump.running(); } @Override public synchronized void keyPressed(KeyEvent e) { keysPressedThisFrame.add(new GFKeyEvent(e,KeyEvent.KEY_PRESSED)); System.out.println("press " + KeyEvent.getKeyText(e.getKeyCode()) ); } @Override public synchronized void keyReleased(KeyEvent e) { keysReleasedThisFrame.add(new GFKeyEvent(e,KeyEvent.KEY_RELEASED)); System.out.println("release " + KeyEvent.getKeyText(e.getKeyCode()) ); } @Override public synchronized void keyTyped(KeyEvent e) { } } |
The class is construted and then the method start() is called which calls start() on the EventThread which in turn calls step about 30 times a second.