I moved to a new URL! Check it out!

posts tagged with: csharp

Dev Log: SortedDictionaryList

Dev Log: SortedDictionaryList
As I transfer all of my framework making knowledge over to the land of FNA I've been trying to clean up a few things that turned out pretty dang messy in the original Otter.

In the Scene class of Otter it keeps track of Entities, and Entities have an Order and Layer. Multiple entities can be in the same layer, and can be in the same order as well. Order is the order in which they update every tick, and layer is the order in which they render. So what I usually do is have a sorted dictionary of lists. The dictionary sorts on an integer value that matches the value of the order or the layer, and the value is the list of entities that belong to that order or layer.

Old Otter has kind of a big mess of copied and pasted code for this system, so for new Otter I tried to make a cleaner version of it.
class SortedDictionaryList<TKey, TValue> : SortedDictionary<TKey, List<TValue>> {
public SortedDictionaryList(IComparer<TKey> comparer) : base(comparer) { }
public SortedDictionaryList() { }
public bool HasItem(TKey key, TValue item) {
if (!ContainsKey(key))
return false;
else
return this[key].Contains(item);
}
public TKey FindKey(TValue item) {
foreach(var key in Keys)
if (this[key].Contains(item)) return key;
return default(TKey);
}
public void AddItem(TKey key, TValue item) {
if (!ContainsKey(key))
Add(key, new List<TValue>());
this[key].Add(item);
}
public void RemoveItem(TKey key, TValue item) {
this[key].Remove(item);
}
}

Another thing I'm looking to implement is a generic version of a "buffered" list of items somehow. A lot of times in Otter I don't want to add or remove items in a list in the middle of the update, so I end up buffering adds and removes until I get to the end of the frame and then all of them execute. I end up doing this in both Scenes for Entities, and in Entities for Components, so I'm rewriting a lot of the same code in both spots, but I'm still questioning if its worth it to try a generic solution for this or if it's fine the way it is.

Anyway I'm still on "break" for another couple of days. At this point I'm looking forward to my vacation being over!

Dev Log: FNA Font Stuff

Dev Log: FNA Font Stuff
I'm currently in the midst of an actual break from development but I'll try to catch up on some recent developments while I'm loungin' about.

I'm still developing my new version of Otter using FNA instead of SFML. I'm slowly but hopefully surely finding out the best way to go about certain things like rendering and input. Fonts and text graphics are working pretty well now and I also have some basic input stuff hooked up for things like keyboard, mouse, and text entry.

Image


I'm using a C# binding of freetype for rendering fonts and text, and recently I implemented a system to handle huge font sizes. I say system but all it's really just a list of textures for the font instead of just one texture. Unfortunately the upper limit of font sizes is still the maximum size of the texture though... so right now 2048 I think.

I've almost got enough in my FNA engine to make a simple game aside from a few missing collision overlap checks and a handful of bugs. As much as I hate to say it I think I actually like SFML's rendering system better because so far using SpriteBatch feels like a giant pain in the butt most of the time. I understand why SpriteBatch is so important, but it seems like it'd be easier just to pass my own vertices and textures into the renderer... but for some reason that's mega slow. Right now one of the bugs I have to look into is the fact that rendering 10 or so primitives already starts to take a toll on performance, and it's only rendering a bunch of untextured vertices! Nothin' makes sense.

FNA and SDL Events

FNA and SDL Events
I've been poking away at the new version of Otter which will be using FNA instead of SFML. At first I thought it would be super easy and cool to use the XNA "state" systems for input. Stuff like KeyboardState, MouseState, etc. The problem with this though is that at lower framerates this way of detecting input can totally miss inputs.

I couldn't find any examples on how to plug into FNA's SDL event loop, but I eventually stumbled into something that worked. All I knew was that I had to use SDL_AddEventWatch. So here's a quick example on how to use SDL events with FNA:
SDL_AddEventWatch((data, ev) => {
var sdlEvent = (SDL_Event)System.Runtime.InteropServices.Marshal.PtrToStructure(ev, typeof(SDL_Event));
switch(sdlEvent.type) {
case SDL_EventType.SDL_KEYDOWN:
Console.WriteLine("keydown: {0}", sdlEvent.key.keysym.sym);
break;
case SDL_EventType.SDL_KEYUP:
Console.WriteLine("keyup: {0}", sdlEvent.key.keysym.sym);
break;
}
return 0;
}, IntPtr.Zero);
I added this code in the Initialize method of Game, and I'm now getting my own key up and down events. Eventually this will be hooked into my input classes and I should have the same input functionality that SFML Otter has.

Dev Log: Early FNA Experiments

Dev Log: Early FNA Experiments
While I'm taking a sort of break from Super Sky Sisters I've been messing around with something called FNA which is a recreation of XNA but in an open source style with maximum portability in mind. It differs in Monogame in that it tries to be as close to XNA as it can possibly be and doesn't branch off into its own thing.

Why FNA? I mean, I'm totally not sure. The only thing I can concretely say is that it's been recommended to be from developers that I highly respect. People often say that XNA is dead because it is no longer supported by Microsoft, but there is still a huge community of people making games with XNA and that is driving things like Monogame and FNA to exist. I feel a lot better about using something that seems to be more in the forefront of game development back ends.

One major point of FNA that I like is that it's only a csproj or a bunch of dll files. No need to install project templates or content pipelines or any of that stuff. One of my goals with Otter is to make it as easy as possible to get something up and running without having to install anything if possible, and I believe FNA is the best bet for something like that.

Image


I've made it mostly through the first pass of text rendering, I've managed to get some basic shader stuff working, and now I'm working on getting primitives to show up. These are the three things that I'm most scared of so if I can get them out of the way it may be smooth sailing from there.

Basically when I do start getting serious about my next game I want to be working in something that has the possibility to be ported to other platforms. SFML does have that potential but it is not used very much in the game development community, so less people are familiar with it, and that means when it comes time to port I'm kinda up the creek without a boat.

More on this as it develops! You may even catch me working on this on one of my fancy game dev streams (warning: it's all programming and not super exciting.)

Otter Updates

Otter Updates
Okay apparently it's been like three months since I've posted about Otter updates, and there's been a lot since then. I guess since my life was in chaos for the past few months I slipped up. Oh well! Here's a quick breakdown on changes and fixes to my C# game making framework thing.

* Fixed a bug with rendering a BoxCollider with a width or height of 1 or 0.

* Fixed a rendering bug caused by changing the font size of a RichText object at runtime.

* Added flags for Sound and Music to check if they're currently playing audio. Sound.IsPlaying and Music.IsPlaying will be true if they're playing back audio.

* Added Color.Shade(float) which will return a color from black to white with the specified float. For example Color.Shade(0.5f) will return a gray color where 0.5f is set for red green and blue.

* Fixed a bug in Input.CharToKey() (a key was missing...)

* Fixed a bug in CollideEntities where an Entity could collide with itself.

* Added Collider.Overlap(x, y, px, py) which will do an Overlap test with a specified point (px, py) Very useful for doing UI stuff and checking for a point overlapping a collider.

* Fixed a bug with LastPressed and LastReleased timers on Button.

* Added Util.ListCombine<T>() to take a bunch of lists and combine them into one list. I don't know if this is already a C# thing so whatever.

* Added Color.FromBytes()

* Added Axis.Reset() to totally clear an Axis's input state.

* Added Button.Reset() to totally clear a Button's input state.

* Fixed a bug where Gradient wasn't blending with its Color.

* Added DataSaver.FileExists().

* Added Particle.ActiveCount but I'm not totally sure if this works as intended.

* Fixed a bug where creating a Text object would not accept an SFML.Graphics.Font.

* Added MouseDeltaX and MouseDeltaY to get the mouse movement since the last update. Mostly used for locking the mouse inside the game window.

* Util.GetFieldValue works for static fields now.

* Added Scene.CameraFocus which if set to an Entity the Scene will then follow that Entity.

* Added Rand.IntXY which returns a vector2 of ints.

* Reworked EventQueue component into EventStack and EventQueue.

* Added GraphicList which is a Graphic type that is a list of graphics.

* Added Anim.OnNewFrame() which is invoked whenever the current frame of an Anim changes. Useful for tying code to specific frames of an animation.

* Huge debugger api changes which uses attributes now instead of RegisterCommand with CommandTypes. Check out the example here to see the new style.

* Added a bunch of stuff to the Debugger like tab auto complete and parameter help when typing in a command.

* Entity.UpdatedOnce is now public (private set.)

* Scene.RemoveNextFrame(Entity) will remove an entity with a one frame delay.

Wow that's a lot of stuff! I'm really happy with the new debugger API that uses all kinds of crazy reflection stuff now instead of a bunch of RegisterCommand method calls.

If you've been using Otter then you should drop by the official slack sometime and hang out with your fellow otter lovers!

Otter Example: Tweening

Otter Example: Tweening
Did you know that Otter supports tweening? Now you do! Tweening is one of the coolest things in the world in my opinion and it's one of the easiest ways to create sweet effects and animations in games.

Image

(I don't know what's up with the end of this gif so whatever.)

Otter makes use of the Glide tweening library by Jacob Albano. It has a similar syntax to TweenLite from the AS3 days of using FlashPunk, and since Otter is based off of FlashPunk having a syntax similar to a flash tweening library makes a lot of sense to me.

Check out the full source code of this example on the Otter example page!