Managing Content and the Joys of Smart Pointers

Hello everyone, I figured I’d write a post about something I’ve been working on for the last week or so, partly as a bit of advice for some budding programmer wanting to write a bit more low level games programming stuff, but also to try and explain why it has been several weeks since I last posted about a small prototype I’ve been working on, and why I have been posting less stuff on Twitter than usual.

To put it basically, a lot of C++ work, mostly involving Gemstone Keeper. I decided to go back to it one more time after a lot of bugs and feedback came in. By the time I’ve posted this, Gemstone Keeper has been updated to version 1.0.3, which includes bug fixes, UI updates and two new enemy types.

j7fcaz6

 

While most of this wasn’t planned (most of this would have happened much earlier if I was notified on Steam within days of it being reported), but it had been on mind for a particular reason.

To put it simply, I have been updating my C++ game framework, the Vigilante Game Framework. This is the framework that I had built up alongside the game it was made for, Gemstone Keeper, before open-sourcing it in the hopes that it could be improved for more projects. I noticed a problem with the content management system, and as such I had spent the last two weeks researching and implementing a new way that would make it more optimal while keeping it stable. Because of this, Gemstone Keeper will soon be updated with this improvement among other bug fixes, and any future games using the VFramework will feature this too.

dytc50n


Here is the original code for loading and setting an sf::Texture in my content system, according to the repository for Gemstone Keeper this was initally written around Feburary 2016, and aside from one change in April this remained how it was:

bool VContent::LoadTexture(const sf::String& name, sf::Texture &texture)
{
   if (FindTexture(name))
   {
      texture = textureDir[name];
      return true;
   }
   else
   {
      if (texture.loadFromFile(name))
      {
         VLog("%s loaded", name.toAnsiString().c_str());
         textureDir.insert(textureDir.begin(), std::pair<sf::String, sf::Texture>(name, texture));
         return true;
      }
   }

   VLog("Error loading texture: %s", name.toAnsiString().c_str());
   return false;
}

The approach should be fairly straightforward:

  • If a texture exists, set it to the texture object and return true.
  • If it doesn’t exist, load it using the texture object. Return true if load is successful.
  • If it cannot load, return false.

However there is one problem, setting the texture object doesn’t set the reference, despite passing the object in as a reference (hence the &). Instead it creates a copy, with a new texture ID and references.

Performance wise, this doesn’t have much of an issue, however memory wise there could be a concern with creating several sprites with the intention of using the same texture, but it isn’t. The intent of a content manager is to load, unload and store a single instance of an asset, with any objects merely referencing that content. I investigated a number of approaches (and spoke with eXpl0it3r of the SFML team) to find the right one.

The approach I finally went with required the following changes:

  • The load function should return the object itself as a non-pointer reference, not a bool. This is to ensure that if the loading or finding process goes wrong, the program should let you know and fail. The reason for a non-pointer is to ensure you wouldn’t return a NULL object. Returning a reference makes sure not to return a newly created copy.
  • If it cannot be loaded, an exception should be thrown. Exception handling is an ideal alternative to responding to function returning false, because it’ll make a function closing on failure much easier.
  • The texture should be stored as a unique pointer.

The reason for a unique pointer, instead of a regular or “raw” pointer, is to ensure that the object only has one true owner, and that once it’s dereferenced (either by closing the game or unloading it) it’ll be properly destroyed and unable to be used by other objects, all with little to no difference in performance. While a unique pointer cannot have more than one reference, the raw pointer can still be obtained and used, just make sure that once the content is unloaded there aren’t any active objects still referencing it.

This is what the texture loading function looks like now (from the Github Repository):

sf::Texture& VContent::LoadTexture(const sf::String& name)
{
   auto found = textureDir.find(name);
   if (found == textureDir.end())
   {
      std::unique_ptr<sf::Texture> texture = std::unique_ptr<sf::Texture>(new sf::Texture());
      if (texture->loadFromFile(name))
      {
         VBase::VLog("%s loaded", name.toAnsiString().c_str());
         textureDir[name] = std::move(texture);
      }
      else
      {
 #ifndef __linux__
         throw std::exception(("Error loading texture: " + name.toAnsiString()).c_str());
 #else
         throw ("Error loading texture: " + name.toAnsiString());
 #endif
      }
   }

   return *textureDir[name];
}

To use the functions in this format, I had to change a lot of objects around the game. Textures, Images, Fonts and SoundBuffers all changed to use raw pointers, and the returned values from the functions are converted to functions as well.

texture = &VGlobal::p()->Content->LoadTexture(filename);
sprite.setTexture(*texture);

This ensured that the object being set is has the same reference as the object being returned, no more copying data, no more instantiating new objects to handle the copy.

I’ve also turned a lot of other objects into unique pointers, particularly the objects in VGlobal, which is intended to only have one instance of. It’s ideal since these ones are all destroyed at the end, and in a scenario where an object could be replaced with another one, the unique pointer can destroy the old one in order to replace the new one.

Other small changes I’ve made to update the framework includes all objects that load textures now having an optional area rectangle. This was mainly put in if multiple sprites use the same texture, but different atlases. Best example would be in the LoadGraphic and setSize functions of VSprite, where the default parameter will ensure the whole texture is used, else the animation system will restrict the render area of the sprite to that rectangle. While sf::Texture does have the ability to load a rectangle area of a texture to an object, I feel like this only adds a separate texture, instead of the one loaded and taken from VContent. At some point I’d like to use this more for Gemstone Keeper and other games, as I see reducing the texture count and having more render calls using the same texture would have additional benefits, no matter how small.

Hopefully more changes like these will benefit games I make with this framework, I’d also like to use more smart pointers, maybe shared pointers for my general objects, but for the time being, I’ve got a project to get back to.

Advertisements

All the jams!

I was meaning to post this sooner, but along with work piling up and losing track, I did quite a few game jams. Three in particular: RemakeJam, Jamchester and Ludum Dare.

  

I’ll mention Running The Marathon first, because I only finished the game two days ago from posting. This was made for Ludum Dare 39, which makes it the 10th Ludum Dare I participated (and 9th I submitted a game independently). This was a bit of a last minute entry because until Saturday evening, I had no intention of making something for LD. What changed my mind was a game idea I was coming up with as a joke, running a marathon and making sure you don’t run out of energy. What I ended up with was a bit of a joke on the Track & Field concept, by having rapid button presses make your chances at success worse. Because of how little time I worked on this, the background animations and sounds are lacking, although I do think the running animation on the player turned out okay.

4i8mLl.gif

As is the tradition of Ludum Dare, voting will be going on by all participants for a total of three weeks. Any Ludum Dare entrants can go to the game’s Ludum Dare event page to vote and comment.

Next I’ll talk about Dash Dodge, which was developed for Jamchester 2017, a professional games jam that I took part in last year. This year, I was in a team which consisted of another programmer & sound designer (Jessica Winters) and two artists ( and ). All in all, this was a great team to work with, and we had a heavily planned out game about combatting bosses using only dashing, with a time travel element, although we may have over-anticipated our goals a bit. Needless to say, we made a game with a nice visual style and some neat mechanics thrown around. Hopefully at some point in the future I would like to work with these three again at some point.

Finally, there is 8-Bit Theater: Black Mage is in a Shooting Game. This game, developed for RemakeJam, is a remake of a previous game of mine. In particular, this is a remake of the first game I ever made from back in 2010. What was great about working on this is that I still have the original source project and most of the original assets and notes, although I did have to dig through Google for a copy of GameMaker 8.1 in order to view the original project. Sadly because of lack of time (I rarely allow full days of focus for these game jams, I’ve noticed), I just about managed to remake the original two levels and boss. This wasn’t for naught though, as way back when it took me over a year to reach this far, and this time I managed to achieve the same amount in less than a week. I also took advantage of my own game framework to try out some technical challenges, such as parallax scrolling.

Game Jams are fun, I like taking part in them for those short bursts of game ideas and development challenges. Sadly participating is made more difficult when my day-job takes priority, but when I get a chance to take part I’d like to take full reigns!

Now time for some voting and other challenges!

Gemstone Keeper on Steam Right Now

https://i0.wp.com/i.imgur.com/EprkPSq.gif

Check out the Steam Store, and you’ll see that Gemstone Keeper has a page where you can add to your wishlist, purchase the game and write a review for it. I greatly appreciate seeing people buying the game and giving it a good review, it really means a lot after realising that this game has been in development for nearly two years (May 2015 – April 2017). Reviews in particular are important because I’d like to collect a list of issues and make fixes, and hopefully add a bit more to the game over the course of a year. Gemstone Keeper will also be shown at Insomnia 60 at the Birmingham NEC and maybe a few more events if people find an interest.

In particular I want to thank Vincent Rubinetti for his contributions for the game. While he was the person I had in mind to do the soundtrack from the point I listened to his music in INK, I was a bit nervous about approaching him with the demo I had. However after a few emails back and forth, we made an agreement and we were underway for producing a great accompanyment to the game’s visual art style and atmosphere.

https://i2.wp.com/i.imgur.com/ajrVcHB.gif  https://i2.wp.com/i.imgur.com/24gcQz8.gif

In addition, I’d like to apologise for leaving this two days late; I couldn’t plan ahead because I spent the last few days sending out emails and twitter DMs in the hopes to get the game looked at by people; I fixed bugs and adding some last minute features such as damage numbers appearing whenever you hit something with your bullets and being able to type the seed you want to use in Score Mode.

On the launch day, I was at Rezzed, where I did talk to a few people about my game, but mainly walked around and tried out a selection of great titles. By the time 6pm rolled around, I hung out at an after party and chatted to a few developers.

Then the day after was my birthday, so I figured I would post on social media, but spend most of the time away from the game and more with friends and family to celebrate and relax.

https://i2.wp.com/i.imgur.com/prEWNzt.gif https://i2.wp.com/i.imgur.com/Fi4QFr6.gif https://i2.wp.com/i.imgur.com/DTZmFdO.gif

As for my future plans, aside from this game’s maintenance, I’m hoping to return to smaller games for a while. In particular I want to try a few more game jams and experiment a bit more, I have a few ideas I want to try out, and now I have a little less pressure on me to work a bit on them.

New Years Update

It has now been one full week of 2017, and a lot of people (including myself) have slowly gotten back to work. Since Gemstone Keeper has been getting close to release, I’ve started work as soon as we can to get stuff done.

Before I get into Gemstone Keeper, I worked on a little game for Ludum Dare 37 where the player is stuck in a porta-loo balancing in the air. That game was Danger: Mondays, and after two weeks of voting the results are in. The results for this Ludum Dare were definitely beyond my expectations. While the amount of submissions for the compo were smaller compared to past years (901 compared to 1117 at LD35), that doesn’t devalue the fact that Danger: Mondays achieved a rank just a few places shy of Top 25 in the Humour category of all categories.

capture

Reading all the comments, I was glad people found the concept amusing, but I’m completely grateful at the how well I did this time around. Thank you to everyone who voted during the day. Apologies for not posting about Ludum Dare any sooner, but I was working on a bigger game.

To be a bit more descriptive, Boss Rush will have the player beating all five bosses as fast as possible, they are able to set the stats and weapons of their explorer before hand and they regain some of their health after defeating each boss.

Score mode allows the player to go through the caverns, and like the daily run mode, will try and get the highest score possible by collecting as many gemstones and materials as they can on a single run. This time however, the player is free to set the seed they want, which will effect all aspects of the game from the levels, player stats, which weapon they have and which items they’ll have at the start.

One of the benefits of working on these game modes (from a developer’s perspective) is that we go through all the main game modes again to not only ensure they work through both the main game mode and these smaller game modes, but to find any bugs or issues that was missed out the first few times.

Another update we’ve done is on the gemstones themselves, namely how they are rendered. Originally, the Gemstone Geometry was generated using a Gemstone Mesh Generator that was developed at PROCJAM, and then rendered using a custom software approach using SFML (you can read a comprehensive write up of this on my websites in part 1 and part 2). However, over the last week of December, it was decided that it was time to update this for performance and to improve quality by changing the rendering process to an OpenGL Hardware render approach.

Below you can see the difference, on the left is the software approach, and the right is the new hardware approach:

This weekend I’ve been playing around with post-process effects, as it would be nice to have some visual effects that would appear through the entire game, although it would be possible for the player to disable certain effects if that want to. To pull this off, the framework now has a multipass post processing system where it’s possible to disable certain effects.

This allows us to apply multiple post process effects at once, and allows us to add the options we need to allow players to enable/disable certain ones.

CRT Shader

Bloom Shader

This is only a small sample of what is being planned, leading up to Gemstone Keeper’s release on March 31st 2017. I’ll also be attending London Gaming & Anime Con in early February and GDC in San Francisco later in the month, however the latter will just be as an attendee.

Here’s to 2017 being a successful year for many people!

GBJAM, Talks and Progress

Over the last month I’ve been working day and night, and having some fun in a few places as well.

From October 1st to October 10th was the GBJAM5, the games jam where the theme is the original gameboy. While the aim is to make a game that could play on an original gameboy, the only rules that matter is the resoluton being 160 x 144 pixels and only using 4 colour graphics. I’ve been taking part since the first one back in 2013, and I’ve only missed GBJAM2. It’s a jam I love because it’s small, I love the restriction of the game’s size and graphics, plus growing up with a Gameboy Color means I have a bit of nostalgia for the system.

Despite the games jam lasting 10 days, I was going to spend the weekend in Dublin so I could spend some time with my sister, who’s been travelling around Africa for a few months. I decided to work on a really simple game idea, a block moving from start to finish, avoiding some obstacles on the way. The idea ended up being so simple it took me the first day to get basic graphics moving on the screen, some obstacles and a palette swapping system. I was able to use the rest of the time updating the graphics, adding a few more objects like locks, keys and bouncepads, as well as a transitional effect and some other screens to make the game feel complete and authentic.

The end result was MonoCube, an action puzzle game where all you have to do is get to the end.   During the development process I got a surprising amount of attention, and even after submitting the game early I got several comments, the game ended up getting 7th place in gameplay! You can check out the game here.

On November 8th, I will be speaking at the Digital Technology Conference at Stoke Campus, Staffordshire University. My talk will be about Indie Game Development and the Steam Greenlight process, on what I did to get Gemstone Keeper greenlit and offering my advice. This will be the first time I’ve gone to the Stoke campus as a Graduate, and I look forward to seeing what the campus is like since the games and computing departments all moved from Stafford earlier in the year.

Now I’m going to return to Gemstone Keeper, I’ve got one planned boss remaining to work on, and I’m also focusing some time on the audio (both music and sound effects). In the meantime, I’ve updated the game’s titlescreen, as I felt the original could have had a more authentic layout, as well as some improved scrolling for the background.

Procedural Meshes: Generating Gemstones Part 1

So two years ago as a student researching for his thesis, I took part in the first procjam. Organised by Michael Cook, this is a games jam that focuses on procedural content, whether that be games, art, text, tools, anything that can make something. Last year I decided to go a bit basic, write some pre-existing noise and maze algorithms for the Haxe Programming Language, which I eventually tweaked and published on Haxelib and Github as MAN-Haxe.

Last year, I decided that for my current project, I was going to do something relevant, and this time use no pre-existing algorithms, this is where the Gemstone Generator comes in. I have images of the meshes below that show the progress from early successful generations to the final most generation test before the UI layout was cleaned up and the demo was uploaded. The generative process is now being used in Gemstone Keeper, albeit with a different rendering process considering I’m turning Unity’s Procedural Meshes into SFML meshes.

Capture Continue reading

Grow Trees… Or Something – Ludum Dare

So this is something I didn’t expect to happen, around two weeks ago was Ludum Dare, where we all had only 48 hours (or 72 for jams) to develop a game, however because I had plans to go to a party that was some distance from home, I had much less time. Despite that I still managed to finish something, although honestly was disappointed I didn’t have much to show, so I could go and vote on other entries.

You can play my entry for Ludum Dare 34 here, but here’s my post mortem as written on the Ludum Dare website.

Continue reading