XNA C# How do I make my model blinking? - c#

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.

Related

How do I add a new image to the window of an already running game in XNA C# using an if statement in the Update method?

I am pretty sure something like this has been asked before, but I haven't been able to find it. Anyway, I am making a simple game animation with two moving objects (halloween themed). The objects bounce off the walls when they hit them, but I also need an image to be displayed when the two objects hit eachother. I have tried multiple ways, but none of them work. They either have no effect or raise an error. Anyway, here is the last thing I tried:
public PumpkinCheckCollisionPumpkin(GameTime gameTime)
{
if (pumpkin1.BoundingBox.Intersects(pumpkin2.BoundingBox))
{
pumpkinCollide = True;
Draw(gameTime);
}
I then tried passing the that bool (which I set to false earlier) to the draw section, but it did not work. The above method is called within the Update method.
I tried having the draw method invoked in the Update (GameTime gameTime) part, but that didn't work either. How do I trigger another image to be displayed in addition to what is already is displayed when my two objects collide? (also, that public method was originally private but made it public so another tactic I tried might work(didn't work)).
The answer is pretty simple, never directly call the Draw function.
In this case, I would set a class level state variable that holds the fact that a collision graphic should be drawn on the next draw call, which can then happen.
You erase every time you draw anyways, so even if the call did work correctly, the next Draw call (which will happen really soon) would erase it. For the same reason, don't unset the flag after the Draw call happens, because it will get erased faster than your players can see it. You need to keep it on the screen for some time.
You could do this by setting a variable that holds the time when the flag was set, then checking it against the current time until enough "visible" time has passed. This would then clear the "show collision" flag, and the graphic would no longer draw.
Just let the framework handle the timing of that call, and let the state you set drive what is drawn.

Correct/Incorrect usage of delegates for achieving extensibility

I'm trying to give users of my GUI library unrestricted customization of in/out transition effects while still maintaining simplicity / preventing misuse (for when a Control enters or exits the view).
To do so, I added a delegate to the Control class, which would take a Control reference and a transition-completion percent, so that the user would be able to smoothly translate a control's position / opacity in any way he wanted, based on the given percent. All he'd have to do is subscribe a transition function before control entrance/exit.
However, I realized it would be impossible to transition / animate the controls using only the current completion percent, because you'd have to store and compare the control's initial position as well.
In order make this storage requirement apparent, should I force usage of a delegate-functor?
If so, how might I do that in a minimalistic / clean way?
Feel free to suggest another way to allow users to apply custom transition animations!
If I understood you correctly, your Control invokes Animation(calculation) delegate (from time to time, probably on each frame) and passes transition Competition percent.
The Animation delegate then calculates and returns/applies translation and position to the Control.
Is this correct?
Assuming that above is correct there are several solutions:
When animating only position and opacity:
Beside competition percent, you must also send initial state of control's position and opacity when calling delegate. Initial state must be remembered on the transition start and sent into delegate in each call.
When animating arbitrarily properties in general:
Beside competition percent, you also provide State property (type of Object or even better Dictionary). This State property is fully controlled by delegate and it's animation logic.
To your Control, State property would not have any semantics or meaning.
Your Control only MUST retain value of State property between subsequent calls to delegate.
Putting it all together, The Delegate fills the State with the initial values on the first call, uses these values on subsequent calls - does anything it wants. Delegate also applies calculated values to Control. Note that all properties that can be used in delegate must be public.
IMO you don't have to provide the user of the control with the initial position of the control since he can position it relatively to the initial position:
negative numbers are for left and top, and positive numbers are for right and bottom.
The following code is a function for a fast transition:
Point FastTranDiagonial(float Percentage){
Thread.Sleep(10);
int pixelsDist = (1 - Percentage)* 300;//300 is the maximum distance
return new Point(-pixelsDist ,pixelsDist);
}
When you invoke the delegate you have to add the Point to the initial position of the control. You have to notice that the delegate contains a Thread.Sleep(X), this must be in control of the user since he might want to do a fast or a slow transaction.
You might also want to consider adding sequential transitions like jQuery so one transition starts after an other's completion.
good luck
I think you need to pass in at least the following parameters to the delegate:-
the control itself
the container that contains the control (eg. a panel)
the completion percent
By passing the control itself, the user will have all its initial state information (such as position). Also, if the user need to set any property of the control, he will definitely need the reference to the control itself.
The container may be needed by the user if he needs its size/position information, or if he needs to do something special to it for the control.

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?

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.

Manual control over when to redraw the screen

I'm trying to make a turn-based roguelike engine thing for XNA. I'm basically porting the game over from a previous work I did using an SDL-based roguelike library called libtcod. How can I modify the basic XNA template thing to make the game not redraw the screen every frame, but, instead, when I want?
The correct method for doing this is to call GraphicsDevice.Present() whenever you want to draw the back buffer onto the screen.
Now the difficulty here is that the Game class automatically calls Present for you (specifically in Game.EndDraw), which is something you don't want it to do. Fortunately Game provides a number of ways to prevent Present from being called:
The best way would be to override BeginDraw and have it return false, to prevent a frame from being drawn (including preventing Draw and EndDraw from being called), like so:
protected override bool BeginDraw()
{
if(readyToDraw)
return base.BeginDraw();
else
return false;
}
The other alternatives are to call Game.SuppressDraw, or to override EndDraw such that it does not call base.EndDraw() until you are ready to have a frame displayed on screen.
Personally I would simply draw every frame.
You don't need to inherit from Game class, it is entirely optional. You can write your own class which redraws the screen only when you want. There's some useful code in the class though, so you can use Reflector to copy code from there and then change its logic.

Categories

Resources