I moved to a new URL! Check it out!

posts tagged with: csharp

Dev Log: Code Snippets

Dev Log: Code Snippets
Yesterday I posted about my amazing tile collisions, so I thought I'd post a quick code example on some of the things involved with setting it up. This isn't any of the code from the actual collision detection, but it's code that most users would see if they ever use this framework.

This is the code for the moving white box:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Otter;

namespace HelloSFML {
class BoxDude : Entity {

BasicMovement movement = new BasicMovement(500, 500, 50);
Image img;
BoxCollider hitbox;
Axis axis = new Axis();

uint size = 50;

public BoxDude() {
refreshSize();

AddComponent(movement);
AddComponent(axis);

movement.CollidesWith.Add(1);

axis.AddKeys(Key.W, Key.D, Key.S, Key.A);

movement.Axis = axis;
}

void refreshSize() {
RemoveCollider(hitbox);
RemoveGraphic(img);
img = Image.CreateRectangle(size);
hitbox = new BoxCollider(size, size, 0);
AddCollider(hitbox);
SetGraphic(img);
movement.Collider = hitbox;
}

public override void Update() {
base.Update();

img.Color = Color.White;
if (Collide(X, Y, 1) != null) {
img.Color = Color.Red;
}

if (Input.Instance.KeyPressed(Key.Period)) {
size -= 10;
refreshSize();
}
if (Input.Instance.KeyPressed(Key.Comma)) {
size += 10;
refreshSize();
}
}
}
}

Dev Log: Tile Collisions!

Image


Finally after weeks of putting off the task I got around to implementing the tile based collision system in my C# SFML framework. Right there in that animation above is a simple movement that is actively colliding with the tiles, stopping the movement. I can scale up and down the hit box and sometimes it does get caught on an overlap as a result which is why it turns red.

I used a couple of references to get this working including Flashpunk's own tile collision which can be found in the source code, and I also took a peek at C# Punk to see how it was implemented there as well. My solution is similar, and maybe worse, but for now it works and that's all that matters.

With the tile based collision in, I have all the power in the world to make a platformer if I wanted... well, as long as it didn't have any slopes.

Dev Log: Code Sample

Dev Log: Code Sample
I'm slowly getting back into the groove of programming again after a long hiatus. In case you're just tuning in, I'm working on a C# SFML framework for 2d games. Right now I'm calling the framework Otter, and eventually when I get it to a more presentable state I'll be open sourcing it on BitBucket somewhere so other people can use it, and contribute to it.

For now here's a small teaser of how some code looks when using the framework. This is for the entity that the player controls with mouse and keyboard or a PC game controller. It flies around in a top down view in the scene. It's a big chunk of code, so I'm putting it behind the "read more" link!

Dev Log: Boring Stuff

Dev Log: Boring Stuff
My work with C# and SFML pushes on. I'm also trying out a new thing which is working remotely! I need to break free of the shackles of my bedroom and be more comfortable with working at over locations, so right now I'm making this blog post from a local cafe with my laptop as I work on C# stuff and rant about how Nintendo sucks.

Every time I travel my work schedule get's totally screwed up and it takes me days to get back into a groove, so in an attempt to try and correct this I'm getting used to working in different locations. Anyway...

Image


Yesterday I added a bunch of structure code to Gaiaden (tentative title!) for combat. My dream right now is to have a bunch of different damage types, armor, shields, and all that kinda crap, but we'll see how much of that makes it into the final game code. Usually that kind of stuff gets cut in the end because it ends up being way too much work that doesn't add a whole lot to the game, but right now I'm letting myself dream big!

Dev Log: Game Loops

Dev Log: Game Loops
I pretty much spent the whole day learning that certain video cards, or hardware configurations, seem to really hate rendering large textures.

Image


I've been messing around with some early versions of the background for my new game. I'm composing it from a number of images -- there's the ground, a couple of gradients for water and haze, and a couple of cloud layers. All of my desktop machines seem to have no issue running this (the render time is 1 to 2 ms) but my laptop seems to get very upset about this with a render time of nearly 15ms.

I also discovered that using the SetFramerateLimit() function in SFML is bad news, at least in C# land. I sent my current build to a friend who ran it on his Macbook (running parallels to boot into Windows) and it ran at about 30 frames per second (short of the desired 60.) His Macbook has no problem playing any other Windows games, so this was a bit odd. After messing around in the game for about 5 minutes the framerate jumped to 60 for no reason that we could see. Super weird.

After these weird results I went back to my code and ripped out the SetFramerateLimit() in favor for my own timing code that would limit the framerate. Not only does this seem to just work better than SetFramerateLimit(), but it also fixed the framerate on my friend's Macbook.

Image


Eventually I will be releasing the full source of my framework for people to laugh at, or possibly use and contribute to. I haven't yet reached the point where I think I can do that comfortably, but I would imagine that sometime this month I'll be ready to show it off. It's very similar to Flashpunk, and is basically a big mash up of a lot of the engines and frameworks I've seen or used. In the mean time, you can also check out #Punk by Jacob Albano which is more of a faithful port of Flashpunk to C# using SFML as the core.

SFML Text Pixel Fix

SFML Text Pixel Fix
Yesterday I posted about an annoying little bug in the SFML Text class which displays a stray pixel on the upper left hand corner of the first character of the string.

Right now the theory is that this section of code is responsible. After some discussion with some smart people, a temporary work around was found.

When the font texture is created, apparently it will create the texture based on the string that you're using. If your string starts with "Q" then the first letter of the texture will be "Q", and since SFML also creates a 2x2 set of pixels on the upper left of the texture, those pixels will be visible at certain positions because of pixel interpolation.

The work around is to first create a dummy text class that starts with a character that you'll never use. In my code I'm using a random ascii character. I'll probably never ever use that ascii character in my text, so if the white pixel is inside the character then it doesn't matter since I will never use it!

After the dummy text character is created then you're safe to create your intended text object -- as long as you're using the same font and character size, that is! So make sure for each combination of font and character size you're creating a dummy text instance. You never have to draw the dummy text, just draw your actual intended text.

This is kind of a sketchy work around, but for now as far as I can tell it totally works.

I posted a thread on the SFML forum documenting this as well.