Game Development Reference
In-Depth Information
Content.RootDirectory = "Content";
position = Vector2.Zero;
}
protected override void
LoadContent()
{ spriteBatch =
new
SpriteBatch(GraphicsDevice);
background = Content.Load<Texture2D>("background");
}
protected override void
Draw(GameTime gameTime)
{ GraphicsDevice.Clear(Color.White);
spriteBatch.Begin();
spriteBatch.Draw(
this
.background,
this
.position, Color.White);
spriteBatch.End();
}
}
The background sprite has the same height as the screen, but it is a lot wider.
As a result, we can only draw a part of the sprite. The goal of this exercise is to
implement side scrolling. We do this by using the mouse position. If the mouse
is positioned left to the screen, the background moves to the right. If the mouse is
positioned right to the screen, the background moves to the left. Write the
Update
method that results in this behavior.
8.
Decorator streams
The
Stream
class has (among others) the following methods:
−
1 if there is no more byte
int
ReadByte();
// returns the next byte, or
int
Read(
byte
[] goal,
int
n);
/
∗
reads a maximum of n bytes and puts them in goal,
and returns the number of bytes read.
∗
/
In many cases it is more efficient to read an entire block with the
Read
method.
On the other hand, it's easier to read a separate byte whenever you need it.
The class
BufferedStream
can help out here. The constructor method of these class
has a
Stream
object as a parameter. When the
ReadByte
method is called on an
object of type
BufferedStream
,itusesthe
Stream
object it manages to read 1000
bytes. It then returns the first one it read, the rest is saved temporarily in an array
(in other words, a buffer). The next time the
ReadByte
method is called, the byte
can be retrieved from the array and we don't have to access the underlying file.
Only when there are no more bytes left in the array, a new block of 1000 bytes is
read.
Implement the
BufferedReader
class, containing a constructor method and the
ReadByte
method.