public virtual void Draw(SpriteBatch batch, Camera cam) { if (_speaker != null) _origin = _speaker.MidPosition - Vector2.UnitY * _speaker.Rect.Height * 0.5f; //CalculateTextStart(); _textStart = _origTextStart; Rectangle rect = cam.ViewSpace; if (cam.ViewSpace.Y > _textStart.Y) _textStart.Y += 32 + _textSize.Y; if (cam.ViewSpace.X > _textStart.X && _textStart.X + _textSize.X * 0.5f > cam.ViewSpace.X) _textStart.X += 0.9f * (cam.ViewSpace.X - _textStart.X); else if (cam.ViewSpace.Right < _textStart.X + _textSize.X * 0.5f && _textStart.X < cam.ViewSpace.Right) _textStart.X += 0.9f * (cam.ViewSpace.Right - _textStart.X - _textSize.X * 0.5f); if (_textStart.X > rect.Right || _textStart.X + _textSize.X * 0.5f < rect.Left || _textStart.Y > rect.Bottom || _textStart.Y + _textSize.Y*0.5f < rect.Top) inSight = false; if (_effect == null) { _effect = new BasicEffect(batch.GraphicsDevice); _effect.VertexColorEnabled = true; _effect.LightingEnabled = false; _effect.TextureEnabled = false; } CalculateVertices(); _effect.World = Matrix.Identity; _effect.View = cam.ViewMatrix; _effect.Projection = cam.ProjectionMatrix; _effect.CurrentTechnique.Passes[0].Apply(); batch.GraphicsDevice.RasterizerState = RasterizerState.CullNone; batch.GraphicsDevice.BlendState = BlendState.Opaque; batch.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, _vertices, 0, _vertices.Length / 3); batch.GraphicsDevice.DepthStencilState = DepthStencilState.Default; batch.GraphicsDevice.BlendState = BlendState.AlphaBlend; batch.DrawString(_font, _text, _textStart, Color.Black,0, new Vector2(0,0),0.5f,SpriteEffects.None,0.11f); }
private void UnShadowTiles(Camera cam, SpriteBatch batch, Map map) { shadowEffect.CurrentTechnique = shadowEffect.Techniques["Tile"]; shadowEffect.CurrentTechnique.Passes[0].Apply(); batch.Begin(SpriteSortMode.Immediate, generateShadow, null, preventShadowStencil, RasterizerState.CullNone, shadowEffect); map.DrawTileHighlight(cam, batch); batch.End(); batch.Begin(SpriteSortMode.Immediate, generateShadow, null, preventShadowStencil, RasterizerState.CullNone, shadowEffect); map.DrawForegroundTileHighlight(cam, batch, shadowEffect); batch.End(); }
void DrawPrecalculatedLightMap(Camera cam, SpriteBatch batch) { Color slightlyDimmer = new Color(240, 240, 240); slightlyDimmer = Color.White; batch.Begin(SpriteSortMode.FrontToBack, BlendState.Opaque, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullNone, null, cam.ViewMatrix); batch.Draw(lightMap, Vector2.Zero, null, slightlyDimmer, 0, Vector2.Zero, lightMapDownSample, SpriteEffects.None, 0.5f); batch.End(); }
public void DrawShadows(Camera cam, SpriteBatch batch, float intensity) { shadowEffect.CurrentTechnique = shadowEffect.Techniques["Glow"]; batch.Begin(SpriteSortMode.Immediate, addLight, null, DepthStencilState.None, RasterizerState.CullNone, shadowEffect); for (int x = 0; x < _glows.Count; x++) { batch.Draw(_unusedTexture, new Rectangle((int)(_glows[x].position.X - _glows[x].radius), (int)(_glows[x].position.Y - _glows[x].radius), (int)(_glows[x].radius * 2), (int)(_glows[x].radius * 2)), Color.Gray); } batch.End(); device.SetRenderTarget(null); }
//Draws the lightmap public void DrawLights(float intensity, Camera cam, SpriteBatch batch, RenderTarget2D target = null) { //Intensity is considered to be between 0 and 1 outside of this routine //but we only want values between 0 and 0.5 in here intensity /= 2; //Set appropriate rendering paramaeters shadowEffect.Parameters["View"].SetValue(cam.ViewMatrix); shadowEffect.Parameters["Projection"].SetValue(cam.ProjectionMatrix); shadowEffect.Parameters["shadowDarkness"].SetValue(0.9f - intensity); shadowEffect.Parameters["viewDistance"].SetValue((float)(1 / (0.001f + 0.999 * intensity))); //Set graphics device states and render target device.DepthStencilState = clearStencil; device.RasterizerState = RasterizerState.CullNone; device.BlendState = BlendState.Opaque; if (target == null) device.SetRenderTarget(shadowTarget); //Clear the backbuffer if (target == null) shadowEffect.Parameters["Projection"].SetValue(Matrix.Identity); else shadowEffect.Parameters["Projection"].SetValue(Matrix.CreateScale(0)); shadowEffect.CurrentTechnique = shadowEffect.Techniques["Clear"]; device.SetVertexBuffer(clearVBuffer); device.Indices = clearIBuffer; shadowEffect.CurrentTechnique.Passes[0].Apply(); if (!_hasLightmap) device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); shadowEffect.Parameters["Projection"].SetValue(cam.ProjectionMatrix); if (_hasLightmap) DrawPrecalculatedLightMap(cam, batch); //Make sure no server messages can change the light around while drawing _lightMutex.WaitOne(); for (int x = 0; x < _glows.Count; x++) { //Draws a single light, I am a lazy bum so I am assuming for lights with timeout that we run close enough to 60fps //to not notice the difference DrawLight(cam, _glows[x], map, batch); if (_glows[x].timeOut > 0) { _glows[x].timeOut -= 16; //Previous comment referring to this _glows[x].brightness = (_glows[x].timeOut / _glows[x].timeOutStart); if (_glows[x].timeOut <= 0) { _glows.RemoveAt(x); x--; } } } _lightMutex.ReleaseMutex(); //Blur the shadow edges to get softer shadows using 5x5 kernel gaussian blur if (target == null) BlurShadowTarget(batch, shadowTarget, shadowTarget2); //Disable shadowing collision tiles //This must be done so that there are no weird shadows overlapping parts of geometry //if (target == null) //Unset rendertarget, we are done calculating the light map if (target == null) device.SetRenderTarget(null); device.DepthStencilState = DepthStencilState.None; }
public void DrawLight(Camera cam, LightSource light, Map map, SpriteBatch batch) { //Clear stencil buffer device.Clear(ClearOptions.Stencil, Color.White, 0, 1); if (light.shadows) { Vector2 camPosition = cam.Position; //cam.MoveTo(light.position, 1); UnShadowTiles(cam, batch, map); //cam.MoveTo(camPosition,1); //Build shadow geometry for the light int vertexCount = map.CreateShadowGeometry(light.position, new Rectangle((int)(light.position.X - light.radius), (int)(light.position.Y - light.radius), (int)(2*light.radius), (int)(2*light.radius)), shadowVB); //Draw Shadows into stencil buffer device.BlendState = generateShadow; device.DepthStencilState = generateShadowStencil; shadowEffect.CurrentTechnique = shadowEffect.Techniques["Shadow"]; device.SetVertexBuffer(shadowVB); device.Indices = shadowIB; shadowEffect.CurrentTechnique.Passes[0].Apply(); //Check necessary because empty draw calls cause crashes if (vertexCount > 0) device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexCount, 0, 4 * vertexCount / 6); } //Draw glow over everything that has not been stenciled out shadowEffect.CurrentTechnique = shadowEffect.Techniques["Glow"]; batch.Begin(SpriteSortMode.Immediate, addLight, null, drawShadowedLight, RasterizerState.CullNone, shadowEffect); //I need to specify a texture to use this call, but since I use a custom shader it is never used, it might actually be null I am not sure batch.Draw(_unusedTexture, new Rectangle((int)(light.position.X - light.radius), (int)(light.position.Y - light.radius), (int)(light.radius * 2), (int)(light.radius * 2)), Color.Lerp(light.color, Color.Black, 1-light.brightness)); batch.End(); }
public void ComputeLightMap(SpriteBatch batch, Map map, string Path) { Camera cam = new Camera(Vector2.Zero, map.MapBoundaries); cam.Zoom(-1); Vector2 viewFieldDiagonal = new Vector2(cam.ViewSpace.Width *0.5f, cam.ViewSpace.Height *0.5f); cam.SetPos(viewFieldDiagonal); lightMapTarget = new RenderTarget2D(device, map.MapBoundaries.Width, map.MapBoundaries.Height,false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8); Matrix baseProjection = cam.ProjectionMatrix; map.SetUpLightMapPrecomputeLights(); device.SetRenderTarget(lightMapTarget); //Clear Target first device.DepthStencilState = clearStencil; device.RasterizerState = RasterizerState.CullNone; device.BlendState = BlendState.Opaque; shadowEffect.Parameters["View"].SetValue(cam.ViewMatrix); shadowEffect.Parameters["Projection"].SetValue(Matrix.Identity); shadowEffect.Parameters["shadowDarkness"].SetValue(0.9f - 1); shadowEffect.Parameters["viewDistance"].SetValue((float)(1 / (0.001f + 0.999 ))); shadowEffect.CurrentTechnique = shadowEffect.Techniques["Clear"]; device.SetVertexBuffer(clearVBuffer); device.Indices = clearIBuffer; shadowEffect.CurrentTechnique.Passes[0].Apply(); device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); //Matrix UpperLeftShift = Matrix.CreateTranslation(-1+(float)cam.ViewSpace.Width / (float)map.MapBoundaries.Width, 1-(float)cam.ViewSpace.Height / (float)map.MapBoundaries.Height, 0); //cam.ProjectionMatrix = baseProjection * Matrix.CreateScale((float)cam.ViewSpace.Width / map.MapBoundaries.Width, (float)cam.ViewSpace.Height / map.MapBoundaries.Height, 1) * UpperLeftShift * Matrix.CreateTranslation(cam.ViewSpace.Left / map.MapBoundaries.Width, cam.ViewSpace.Top / map.MapBoundaries.Height, 0); DrawLights(1, cam, batch, lightMapTarget); //cam.Move(new Vector2(0, viewFieldDiagonal.Y * 2)); RenderTarget2D blurTarget = new RenderTarget2D(device,lightMapTarget.Width,lightMapTarget.Height); shadowEffect.Parameters["xTexelDist"].SetValue(1.0f / (blurTarget.Width / lightMapDownSample)); shadowEffect.Parameters["yTexelDist"].SetValue(1.0f / (blurTarget.Height / lightMapDownSample)); BlurShadowTarget(batch, lightMapTarget, blurTarget); BlurShadowTarget(batch, lightMapTarget, blurTarget); shadowEffect.Parameters["xTexelDist"].SetValue(1.0f / (device.PresentationParameters.BackBufferWidth / lightMapDownSample)); shadowEffect.Parameters["yTexelDist"].SetValue(1.0f / (device.PresentationParameters.BackBufferHeight / lightMapDownSample)); device.SetRenderTarget(null); Clear(); System.IO.FileStream fs = System.IO.File.Open(Path, System.IO.FileMode.Create); lightMapTarget.SaveAsPng(fs, lightMapTarget.Width, lightMapTarget.Height); fs.Close(); lightMap = lightMapTarget; _hasLightmap = true; }
public void Draw(SpriteBatch batch, Vector2 pos, Camera cam) { pos.X = (int)pos.X; pos.X += 8; pos.Y = (int)pos.Y; Vector2 targetEnd = pos; targetEnd.Y -= 8; Rectangle viewSpace=cam.ViewSpace; Vector2 diff = targetEnd - cam.Position; if(Math.Abs(diff.X)>viewSpace.Width/2 || Math.Abs(diff.Y)>viewSpace.Height/2) { if (Math.Abs(diff.X) / viewSpace.Width > Math.Abs(diff.Y) / viewSpace.Height) { diff *= (viewSpace.Width / 2) / Math.Abs(diff.X); } else { diff *= (viewSpace.Height / 2) / Math.Abs(diff.Y); } targetEnd = cam.Position + diff; } targetEnd.X = (int)targetEnd.X; targetEnd.Y = (int)targetEnd.Y; float angle = (float)Math.Atan2(pos.Y - targetEnd.Y, pos.X - targetEnd.X); batch.Draw(tex, targetEnd, null, color, angle, _textureOffset, 0.5f, SpriteEffects.None, 0.1f); }
public void Initialize(GraphicsDevice Device, ScreenManager manager) { GraphicsDevice = Device; spriteBatch = new SpriteBatch(Device); cam = new Camera(new Vector2(520, 440), new Rectangle(0, 0, 800, 600)); _lightMap = new LightMap(Device); MapManager.LoadMaps(Device); //map = MapManager.GetMapByID(0); objectManager = new ObjectManager(map); objectManager.SetLight(_lightMap); pm = new ParticleManager(); TextManager.SetUp(ContentInterface.LoadFont("font")); Player.font = ContentInterface.LoadFont("font"); font = ContentInterface.LoadFont("font"); bar = ContentInterface.LoadTexture("White"); expBorder = ContentInterface.LoadTexture("ExpBar"); crosshair = ContentInterface.LoadTexture("Crosshair"); _spriteEffect = ContentInterface.LoadShader("ShadowEffect"); _spriteEffect.CurrentTechnique = _spriteEffect.Techniques["Sprite"]; _manager = manager; _mapMutex = new System.Threading.Mutex(); input = new InputManager(); WorldController.SetObjectManager(objectManager); WorldController.SetGame(this); WorldController.Start(); _GUIOverlay = new UI.GUIScreen(); manager.AddScreen(_GUIOverlay); }
public void Update(float ms, Camera cam) { Rectangle drawRect = cam.ViewSpace; int minX = drawRect.X / tileSize; int minY = drawRect.Y / tileSize; int maxX = (drawRect.Width + drawRect.X) / tileSize + 1; int maxY = (drawRect.Height + drawRect.Y) / tileSize + 1; minX = minX >= 0 ? minX : 0; minY = minY >= 0 ? minY : 0; maxX = maxX < _layers[1].GetLength(0) ? maxX : _layers[1].GetLength(0); maxY = maxY < _layers[1].GetLength(1) ? maxY : _layers[1].GetLength(1); for (int x = minX; x < maxX; x++) for (int y = minY; y < maxY; y++) if (_events[x, y] != null) _events[x, y].Update(ms); }
public void DrawTileHighlight(Camera cam, SpriteBatch batch) { Rectangle drawRect = cam.ViewSpace; int minX = drawRect.X / tileSize; int minY = drawRect.Y / tileSize; int maxX = (drawRect.Width + drawRect.X) / tileSize + 1; int maxY = (drawRect.Height + drawRect.Y) / tileSize + 1; minX = minX >= 0 ? minX : 0; minY = minY >= 0 ? minY : 0; maxX = maxX < _layers[1].GetLength(0) ? maxX : _layers[1].GetLength(0); maxY = maxY < _layers[1].GetLength(1) ? maxY : _layers[1].GetLength(1); for (int x = minX; x < maxX; x++) for (int y = minY; y < maxY; y++) if (_layers[1][x, y] != null) _layers[1][x, y].Draw(0.5f, batch); }
public void DrawForegroundTileHighlight(Camera cam, SpriteBatch batch, Effect effect) { effect.CurrentTechnique = effect.Techniques["ForegroundTile"]; effect.Parameters["TextureSize"].SetValue(new Vector2(tileSet.Width, tileSet.Height)); effect.Parameters["ScreenTexture"].SetValue(tileSet); effect.CurrentTechnique.Passes[0].Apply(); Rectangle drawRect = cam.ViewSpace; int minX = drawRect.X / tileSize; int minY = drawRect.Y / tileSize; int maxX = (drawRect.Width + drawRect.X) / tileSize + 1; int maxY = (drawRect.Height + drawRect.Y) / tileSize + 1; minX = minX >= 0 ? minX : 0; minY = minY >= 0 ? minY : 0; maxX = maxX < _layers[1].GetLength(0) ? maxX : _layers[1].GetLength(0); maxY = maxY < _layers[1].GetLength(1) ? maxY : _layers[1].GetLength(1); for (int x = minX; x < maxX; x++) for (int y = minY; y < maxY; y++) if (_layers[2][x, y] != null) _layers[2][x, y].Draw(0.4f, batch); }
/* public void SpawnItem(int id) { if (_spawnPoints.ContainsKey(id)) { Point p; p = _spawnPoints[id][random.Next(0, _spawnPoints[id].Count)]; Tile targetTile = _tiles[p.X / tileSize, p.Y / tileSize]; switch (id) { case 'b' - 'a': targetTile.SetChild(new PartPickup(new Legs(_content.Load<Texture2D>("FeetIcon")), this, new Rectangle(p.X, p.Y, tileSize, tileSize))); break; case 'c' - 'a': targetTile.SetChild(new Exit(_content.Load<Texture2D>("Exit"), this, new Rectangle(p.X, p.Y, tileSize, tileSize))); break; } } } public void TimeSpawn(int id, float countdown) { SpawnCountdown cd = new SpawnCountdown(); cd.countDown = countdown; cd.id = id; _spawns.Add(cd); } */ public void Draw(SpriteBatch batch, Camera cam) { Rectangle drawRect = cam.ViewSpace; int minX = drawRect.X / tileSize; int minY = drawRect.Y / tileSize; int maxX = (drawRect.Width + drawRect.X) / tileSize + 1; int maxY = (drawRect.Height + drawRect.Y) / tileSize + 1; minX = minX >= 0 ? minX : 0; minY = minY >= 0 ? minY : 0; maxX = maxX < _layers[1].GetLength(0) ? maxX : _layers[1].GetLength(0); maxY = maxY < _layers[1].GetLength(1) ? maxY : _layers[1].GetLength(1); for (int layer = 0; layer < 3; layer++) for (int x = minX; x < maxX; x++) for (int y = minY; y < maxY; y++) if (_layers[layer][x, y] != null) { _layers[layer][x, y].Draw(0.6f - layer / 10.0f, batch); } for (int x = minX; x < maxX; x++) for (int y = minY; y < maxY; y++) { if (_events[x, y] != null) _events[x, y].Draw(batch); } }