Skip to content

Commit

Permalink
New particle system
Browse files Browse the repository at this point in the history
In this commit I added particle system from Microsoft XNA samples
(converted to SFML ofc). Also, I removed SpriteBatch (unoptimized one)
and made VertexBatch as default.
  • Loading branch information
deathbeam committed Mar 2, 2014
1 parent 837d45b commit f453068
Show file tree
Hide file tree
Showing 24 changed files with 1,237 additions and 968 deletions.
43 changes: 37 additions & 6 deletions Audio/AudioManager.cs
@@ -1,45 +1,60 @@
using SFML.Audio;
using System;
/* File Description
* Original Works/Author: Thomas Slusny
* Other Contributors: None
* Author Website: http://indiearmory.com
* License: MIT
*/

using System;
using System.IO;
using System.Collections.Generic;
using SFGL.Window;
using SFML.Audio;

namespace SFGL.Audio
{
////////////////////////////////////////////////////////////
/// <summary>
/// Can be used for managing, playing and loading of sound and music.
/// Can be used for managing, playing and loading of sounds
/// and music.
/// </summary>
////////////////////////////////////////////////////////////
public class AudioManager : ILoadable, IDisposable
{
private Dictionary<string, SoundBuffer> _sounds = new Dictionary<string, SoundBuffer>();
private Music _currentMusic;

////////////////////////////////////////////////////////////
/// <summary>
/// Directory from where will audio manager load sounds.
/// </summary>
public string SoundDirectory = "Sounds";
////////////////////////////////////////////////////////////
public string SoundDirectory { get; set; }

////////////////////////////////////////////////////////////
/// <summary>
/// Extension of sounds what will audio manager load and play.
/// </summary>
public string SoundExtension = "ogg";
////////////////////////////////////////////////////////////
public string SoundExtension { get; set; }

////////////////////////////////////////////////////////////
/// <summary>
/// Creates new instance of audio manager.
/// </summary>
public AudioManager() { }
////////////////////////////////////////////////////////////
public AudioManager()
{
SoundDirectory = "Sounds";
SoundExtension = "ogg";
}


////////////////////////////////////////////////////////////
/// <summary>
/// Loads audio files from specified audio folder to cache.
/// </summary>
////////////////////////////////////////////////////////////
public void LoadContent()
{
if (!Directory.Exists(SoundDirectory)) return;
Expand All @@ -55,71 +70,87 @@ public void LoadContent()
}
}

////////////////////////////////////////////////////////////
/// <summary>
/// Loads and plays music from specified music path.
/// </summary>
////////////////////////////////////////////////////////////
public void PlayMusic(string musicName)
{
if(_currentMusic != null) _currentMusic.Dispose();
_currentMusic = new Music(musicName);
_currentMusic.Play();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Stops current music.
/// </summary>
////////////////////////////////////////////////////////////
public void StopMusic()
{
_currentMusic.Stop();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Pause current music.
/// </summary>
////////////////////////////////////////////////////////////
public void PauseMusic()
{
_currentMusic.Pause();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Resumes current paused music.
/// </summary>
////////////////////////////////////////////////////////////
public void ResumeMusic()
{
_currentMusic.Play();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Plays sound stored in sound cache once.
/// </summary>
////////////////////////////////////////////////////////////
public void PlaySound(string soundName)
{
Sound sound = new Sound(_sounds[soundName]);
sound.Loop = false;
sound.Play();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Play sound stored in sound cache once or repeated.
/// </summary>
////////////////////////////////////////////////////////////
public void PlaySound(string soundName, bool repeat)
{
Sound sound = new Sound(_sounds[soundName]);
sound.Loop = repeat;
sound.Play();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Returns sound from sound cache.
/// </summary>
////////////////////////////////////////////////////////////
public Sound GetSound(string soundName)
{
return new Sound(_sounds[soundName]);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Disposes this instance of audio manager class.
/// </summary>
////////////////////////////////////////////////////////////
public void Dispose()
{
StopMusic();
Expand Down
20 changes: 19 additions & 1 deletion Content/ContentManager.cs
Expand Up @@ -9,6 +9,8 @@
using System.Collections.Generic;
using System.Linq;
using SFGL.Window;
using SFML.Graphics;
using SFGL.Particles;

namespace SFGL.Content
{
Expand All @@ -32,7 +34,23 @@ public string Directory
/// <summary>
/// Creates new instance of Content Manager.
/// </summary>
public ContentManager() { }
public ContentManager()
{
/// Initialize content loaders
ContentProvider loader;

loader = new ContentProvider(typeof(Texture), "textures", "png");
loader.Load = (str) => new Texture(str);
AddLoader(loader);

loader = new ContentProvider(typeof(Font), "fonts", "ttf");
loader.Load = (str) => new Font(str);
AddLoader(loader);

loader = new ContentProvider(typeof(ParticleSettings), "particles", "sfp");
loader.Load = (str) => new ParticleSettings(str);
AddLoader(loader);
}

/// <summary>
/// Loads file to content manager, or if file is already loaded, returns file data.
Expand Down
85 changes: 85 additions & 0 deletions Graphics/AnimatedSprite.cs
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using SFML.Graphics;
using SFGL.Time;

namespace SFGL.Graphics
{
public class AnimatedSprite : Sprite, IUpdateable
{
private int activeFrame;
private List<Rectangle> frames;
private Rectangle currentFrame;
private GameTime elapsedTime;
private float frameDelay;

// how long it has been since initialize was called
private float timeSinceStart;

/// <summary>
/// List with the frames of the animation
/// </summary>
public List<Rectangle> Frames
{
get { return frames; }
set { frames = value; }
}

/// <summary>
/// Defines for how long will be one frame drawn
/// </summary>
public float FrameDelay
{
get { return frameDelay; }
set { frameDelay = value; }
}

public AnimatedSprite (Texture texture, IntRect rectangle) : base (texture, rectangle)
{
}

public AnimatedSprite (Sprite copy) : base (copy)
{
}

public AnimatedSprite () : base ()
{
}

public AnimatedSprite (Texture texture) : base (texture)
{
}

/// <summary>
/// Allows the game component to update itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public void Update(GameTime gameTime)
{
// calculate dt, the change in the since the last frame. the particle
// updates will use this value.
float dt = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

timeSinceStart += dt;

// it's time to a next frame?
if (timeSinceStart > frameDelay)
{
timeSinceStart -= frameDelay;
activeFrame++;
if (activeFrame == frames.Count)
{
activeFrame = 0;
}
// Get the current frame
currentFrame = frames[activeFrame];
base.TextureRect = new IntRect (
currentFrame.X,
currentFrame.Y,
currentFrame.Width,
currentFrame.Height);
}
}
}
}

26 changes: 23 additions & 3 deletions Graphics/Camera.cs
Expand Up @@ -56,6 +56,16 @@ public class Camera : IUpdateable
////////////////////////////////////////////////////////////
public Vector2 Position { get; set; }

////////////////////////////////////////////////////////////
/// <summary>
/// Transforms position based on current camera position
/// </summary>
////////////////////////////////////////////////////////////
public Vector2 Transform(Vector2 point)
{
return point - ActualPosition;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Calculates the area the camera should display
Expand All @@ -72,6 +82,16 @@ public Rectangle Bounds
}
}

////////////////////////////////////////////////////////////
/// <summary>
/// Checks if object is visible in current camera area
/// </summary>
////////////////////////////////////////////////////////////
public bool ObjectIsVisible(Rectangle bounds)
{
return (Bounds.Intersects(bounds));
}

////////////////////////////////////////////////////////////
/// <summary>
/// Creates new instance of Camera class using Rectangle
Expand Down Expand Up @@ -131,9 +151,9 @@ public void Update(GameTime gameTime)
{
if (Smooth)
{
var dir = VectorMath.Direction(ActualPosition, Position);
var len = VectorMath.Distance(ActualPosition, Position);
ActualPosition += VectorMath.LengthDir(dir, len * Smoothness);
var dir = Vector2.Direction(ActualPosition, Position);
var len = Vector2.Distance(ActualPosition, Position);
ActualPosition += Vector2.LengthDir(dir, len * Smoothness);
}
else
ActualPosition = Position;
Expand Down

0 comments on commit f453068

Please sign in to comment.