Post
by fr0stbyte124 » Fri May 03, 2013 5:03 pm
I don't know. It would depend a lot on how it is implemented, and I can't tell enough from your description whether you are doing it correctly or not. I'd have to see the code firsthand to really understand.
I would recommend having a longer interval than 10ms if you are still having performance issues. The events will begin piling up in the queue if they don't finish before the next one is invoked.
Also, avoid any direct communication between threads unless you really know what you are doing; it can cause instability issues if you are not careful. Instead, use one of those thread-safe collections I linked you to store your entities. To handle moving after rotating, just introduce a value to track the action state inside the tank class. If you are ROTATING and hit the correct orientation, change the state to MOVING and continue the action on each subsequent tick. Don't try to use Thread.Sleep() in the drawing thread.
--------------------
Actually, I'm wondering how much you actually need threading. The way Minecraft does it is it runs draw frames back-to-back as quickly as it can. During this process, mouse interrupts are read and the camera is updated, and terrain and entities are drawn. Entities all have a velocity component, and during each frame their position is tweaked based on the amount of time since the last tick. When a set amount of time has passed, the tick routine is executed at the end of draw routine. If you look at the profiler in the F3 menu, the game ticks are the white marks and the red/green marks are the frame draws. Both run on the same thread.
Say your game logic tick is 10/sec, or 100ms intervals. If you wanted to do the non-threaded version, do this:
-At the start of your program record System.DateTime.Now.
-Add 100 ms to that time and make it your TargetTime.
-Run draw updates back to back until DateTime.Now > TargetTime.
-Add another 100ms to TargetTime. More accurately, keep adding 100ms until TargetTime >DateTime.Now if something unexpected happened. (Going off the previous target time instead of the current time will give us a more consistent average tick rate.)
Mouse events could pose a problem if everything is running in the UI thread, as the main loop would starve the other UI events, which I am beginning to suspect is what you were describing before. Instead of looping, try manually raising the same event once it is completed. This should give the other events a chance to fire, but I am not completely sure this won't cause threading issues. You'll need to try it and find out.
Once you have that, I would start trying to optimize the drawing method some more. For a 2D sprite game, it should be very easy to run on a modern computer.