opentk / C# keyboard state - c#

Using OpenTK, I'm having issues with the Keyboard State. I'm trying to use Keyboard.GetState() but it fails.
Basically what I'm trying to achieve is a single 'click'. Because the state-check is in UpdateFrames, the checks are milliseconds apart, meaning a single keypress will trigger the event multiple times.
I dont want to use KeyRepeat.False because I still want keys like W, S, A, and D to update per frame. I just want some of the keys to be single-checked

for now I switched to XNA to handle input, as I dont think th OpenTK input is fully developed yet. much easier on that side, just set up a KeyboardState and test the state of the last from with the current one. if they aren't the same, the button was just pushed.

Related

Using another GameComponent to measure the elapsed times

Okay, so I started developing a game. The first thing I wanted to set up was an extensive Debug viewer, which can ultimately show things like buttons pressed (only those that I want to listen to of course) and a graph containing information about frametime, a histogram of sorts. However, I wanted to do this the correct way. It occurred to me that I wanted to use the DrawableGameComponent class for the debugviewer, and draw the debug information there. However it seems that it can't measure the Update and Draw times correctly, as I start the update timer at the beginning of the update and end the update at the beginning of the draw. However, I shortly thereafter realized that first the main game is executed (update - draw - wait) and then are all the other components executed (update component 1, draw component 1), so they aren't intertwined. This means that I can't calculate the elapsedtime (for both Update, Draw and overhead). As we can read in this blog it's better to measure frametime as opposed to frames per seconds.
So enough with the back story and on to my main question: How do I measure the frametimes needed for both the Update, the Draw and the overhead from the main game in another DrawableGameComponent, or should I just use a class and update that in the game?
I hope that everything is clear, have a great day.
i found this http://gamedevwithoutacause.com/?p=876 which explaines exactly what you need.
If you can't measure the Update and Draw times correctly, using the update timer, can't you simply use DateTime.Now? You get it at the beginning of your Update, then every next cycle (after Update, Draw and overhead) you just subtract the DateTime.Now measured previously.

C# keyboard and word prediction

I'm currently developing a C# desktop application which is a simple virtual keyboard with word prediction facility.
The prediction process will start after typing the first three letters of the word, then provide the suggestions. I need to track the caret while typing, and I tried to use richTextBox events such as SelectionChanged but it requires regular expression check and position tracking manually ( declaring variables ... ).
My questions: is there any suggestions that can help me in doing this task ? What about Listeners? are they helpful?
Note: I have no long experience with .NET framework and I didn't use Listeners before.
Also note that the input method is eye gaze ! which means non of key- events will work !
Thank you.
Will this be in WPF or WinForms? I would tackle this problem as follows. Maybe not the fastest way but worth to try until you have something else.
OnKeyDown event of your RTB check if the last char was a space. If not get whole word from last space and check against list of words and update the list on screen.
to check where the cursor is at the time in your word just do the same as above and try to get the current word and than the indexof specific key.

I want to force a render, but only draw as fast as possible (InvalidateVisual / CompositionTarget.Rendering)

I'm working on a real-time WPF/Silverlight (and soon WP7) visualization component and I'm looking for the best solution to force a redraw of the entire component in a Game-loop style. Redraw should be on-demand, but I don't want to back up the message pump with re-draw calls. Most of the drawing in my component is done using non-WPF primitives (e.g. Bitmap Interop, Direct2D) so my code does not use InvalidateVisual, and as a result, currently looks like this
// Pseudocode, doesnt compile, just to convey the meaning
public void InvalidateElement()
{
if (CurrentlyDrawing)
return;
Dispatcher.BeginInvoke(() =>
{
CurrentlyDrawing = true;
DoDrawInternal();
CurrentlyDrawing = false;
}
}
Ok so this is great. If I call InvalidateElement lots of times I get good responsiveness. However, what I want to do is ensure I can push data to my visualization component as fast as possible but only draw when the component is able to draw, and not keep drawing to catch up with the data once the input stream completes.
No I can't override OnRender, I'm using non-WPF drawing inside WPF ;-)
Basically what I want is something like the old Invalidate() / OnPaint in WindowsForms, or better yet, a game loop in DirectX.
At the moment I get the situation where if I have an external thread that pushes data to the visualization component at a high rate then if I Stop pushing data I get another 20 seconds worth of refreshes to get through before the component stops drawing. I want to stop drawing as soon as data has gone in.
Another idea I had was to handle CompositionTarget.Rendering in the visualization component then implement some sort of rudimentary Queue to push data to and the Rendering event consumes this data as fast as it can.
In Summary
Given a WPF visualization component, V, and a datasource which pushes it data every 1ms, D, how can I ensure that no matter the datarate of D, V draws data at 30FPS (or whatever it can do) and updates itself in chunks, sort of how a game render loop does in DirectX?
When the data stops, V should redraw everything it has up to now in one go. When the data is too fast, V draws larger chunks at a time to compensate.
If you need more information I'd be happy to share it. Right now I've just posted a synopsis to gauge if there are any quick fixes but a fuller Q with code examples can be provided on request.
Best regards,
You might want to consider rendering on the CompositionTarget.Rendering event and throttling on the invalidated state.
Silverlight game loop example (F#):
/// Run game
let runGame () =
let state = gameState.GetEnumerator()
let rate = TimeSpan.FromSeconds(1.0/50.0)
let lastUpdate = ref DateTime.Now
let residual = ref (TimeSpan())
CompositionTarget.Rendering.Add (fun x ->
let now = DateTime.Now
residual := !residual + (now - !lastUpdate)
while !residual > rate do
state.MoveNext() |> ignore
residual := !residual - rate
lastUpdate := now
)
Play the game: http://trelford.com/blog/post/LightCycles.aspx
Read the source: https://bitbucket.org/ptrelford/lightcycles
You can listen to the CompositionTarget.Rendering event, which is triggered right before WPF renders the UI, and do your drawing in there.
Another tidbit.. InvalidateVisuals() is nothing like Form.Invalidate(), as it also causes re-layout which is expensive. If you want something like Form.Invalidate(), then create a DrawingGroup (or bitmap image) "backingStore", place it in the DrawingContext during OnRender(), and then update it whenever you want. WPF will automatically update and repaint the UI.
Have you thought of using a dispatch timer running at 30FPS, then take a snapshot of the current data and rendering it at each timer tick? If you want to avoid redrawing if nothing has changed, you can simply keep timestamps for LastChanged and LastRendered, only performing an actual redraw if LastChanged > LastRendered. Basically updating the data and rendering the data are decoupled from one-another; the main trick is making sure you can somehow get a coherent snapshot of the data when the rendering thread wants to render it (i.e. you'll need some sort of locking.)
I was recently working with a project that required a game loop like style. Although my example is purely in F#, you can figure it out how you can do that way in C# too, may be use some interop code to initialize the timer and hooking up events as given in this below link,
http://dl.dropbox.com/u/23500975/Demos/loopstate.zip
The sample doesn't show how to redraw, it just updates the underlying stock data for every 500ms, It should pretty much work for any kind of drawing mechanisms with WPF. The core idea is to use composable events, in F# an event is a first-class citizen + an IObservable (reactive extensions for C#), so we can easily compose functions that in-turn return a set of events or a single event. There is a function Observable.await, which takes in an Observable and also has a state to return.
eventSource
|> Observable.await(fun (t:State.t) e ->
// return the modified state back on every check or in the end
match e with
// start button click
| Choice1Of3(_) ->
{t with start=true}
// stop button click
| Choice2Of3(_) ->
{t with start=false}
// timer tick event,
| Choice3Of3(_) ->
if t.start = true then
handleStockUpdate(t)
else t
) (state)
I just used some of FP terms here, but it should work just fine with normal C# (OO) way of doing things here.
Hope this helps!
-Fahad
I'm not sure why you would use WPF for your front-end if you're drawing using non-WPF elements and require the Invalidate() method that was provided by WinForms? Can't you just switch the UI to use WinForms?

XNA C# How do I make my model blinking?

I'm quite new in XNA C# and I would like to know how do I create a model in XNA C# that will blink every second. I'm trying to make an invulnerability effect for my model.
Currently, my own idea is that I will set the visible of my model to false and true every second.
Thanks.
EDIT: I cannot find any model.visible = false in XNA C#??
Your idea is fine, but you'll need to track whether it should be visible or not yourself, and only draw it when it's visible. Every object gets explicitly redrawn by your code every frame; so simply don't draw it when it shouldn't be visible.
There is no built-in way to do this (that I know of); it wouldn't make much sense if there were, since you'd be calling a drawing function on invisible objects. Not drawing invisible objects in the first place makes more sense.
To get the blinking to work, you'll need to track how much time has elapsed since the last time the visibility was flipped, and toggle the visibility when that time exceeds one second. For example, in your Update() method, you'd have something like this:
if (gameTime.TotalGameTime.TotalMilliseconds >= nextBlinkTime) {
modelVisibility = !modelVisibility;
nextBlinkTime = gameTime.TotalGameTime.TotalMilliseconds + 1000;
}
For more complex scenarios (e.g. multiple models need visibility toggled, etc.), I suggest you abstract this behaviour away into a reusable class.

Simple 2D 'Space Invaders' Clone In Silverlight

I want to make a simple 2d game in Silverlight, but it seems like things have changed since the last time I tried to make a game using mode 13h graphics. Can someone give me a run-down of how you'd go about it.
I just mean at a high-level, focusing on the silverlight-specific aspects; not general game design.
A fictional example might be:
'The main game loop shouldn't be a loop, use a DispatchTimer instead. Use a Canvas as the main drawing object; but realize that we don't bother drawing individual pixels - all of your in-game objects should be represented by controls. Be sure to set the 'UseHardwareFlag' to true'. Etc, etc...
If you want to stick to the mode 13 way of programming have a look at the WriteableBitmap.
Some very nice demos here
I succeeded in porting Wolf3D (2 and a half D) to Silverlight this way.
I used the CompositionTarget.Rendering event
EDIT
I also found this, it is less mode 13 and more in line with your example.

Categories

Resources