Since Rules then Since then and Tuesdays, scribbling and haphazard interaction - I managed after what felt like weeks of distraction, to finally piece together a 2D an animated sprite which faithfully iterates through 11 sequential keyframes within the same picture.
This is an example of what my green alien sprite sheet looks like. Later on I'll show you what it looks like animated.
A lot of the planning around this piece of work was around the algorithm.
In the end, the original algorithm I used to pan the view-port across the sprite sheet started fairly complex but gradually turned into a more simple one which, instead of trying to calculate the dimensions of the next frame based on a uniformly fixed key-frame size on the sprite-sheet, I instead decided to just hard-coded the 11 key-frame rectangle co-ordinates straight in as a simple array to demonstrate that animation is possible.
The following series of C++ of code shows the process of changing the frame being drawn (update), within a frame lifetime threshold period, and selecting the appropriate frame dimensions (as a rectangle) encoded in an array to set the frame correctly (setFrameRect):
void Sprite::update()
{
unsigned long timeSincelastFrame = timeGetTime() - m_TimeLastFrame;
if(timeSincelastFrame >= m_Speed) {
m_CurrentFrame++;
if(m_CurrentFrame >= m_TotalFrames)
m_CurrentFrame = m_StartFrame;
setFrameRect(m_CurrentFrame);
m_TimeLastFrame = timeGetTime();
}
}
As mentioned above, that bit just determines when I should be switching to the next frame based on how long the old frame has been showing for.
Any longer than a threshold(m_Speed) and its time to move the animation on to the next frame. This is all kicked off by playing() the animation on the game object:
void Sprite::play()
{
auto resource = GetResource();
auto TmpSurface = resource->m_Surface;
m_CurrentFrame = m_StartFrame;
setFrameRect(m_CurrentFrame);
m_TimeLastFrame = timeGetTime();
}
void Sprite::stop()
{
stopped = true;
}
And then my little hack comes in next to set the desired view port on the resource i.e the sprite sheet shown previously:
void Sprite::setFrameRect(uint FrameNumber)
{
SDL_Rect typicalFrame[] = {
{0,0,66,92},
{66,0,66,93},
{132,0,66,93},
{0,93,66,93},
{66,93,66,93},
{132,93,71,92},
{0,186,71,93},
{71,186,71,93},
{142,186,70,92},
{0,279,71,93},
{71,279,66,97},
};
GetResource()->m_viewPort.x = typicalFrame[FrameNumber].x;
GetResource()->m_viewPort.y = typicalFrame[FrameNumber].y;
GetResource()->m_viewPort.w = typicalFrame[FrameNumber].w;
GetResource()->m_viewPort.h = typicalFrame[FrameNumber].h;
}
It's not great because its hard-coded but it makes the concept work, which is the whole point of this exercise. It seems like it took me ages to do this, but there are a lot of other things I find competes for my time. Anyway, the full source code is here, its under the simplification branch which, is me trying to make my code as readable as possible (refactoring is one of the joys of programming in my opinion and calms me like zen).
The issue, that prompted this (hard coding) was that I had found some sprite-sheets online, which as it turns out were not uniform - each series in the animation was located in slightly different offset positions, so my original algorithm was no longer useful. The downloaded sprite-sheets do all have a mixture of XML and JSON associated key-frame coordinates code into meta files, which I realise now is because the sprite-sheets are not uniform...
So moving forward, I think what I’ll do, is cater for this by implementing both a JSON and a XML loader that can read in the associated coordinates from the meta files for the sprite-sheet and dynamically using them to determine the frame coordinates. For now though, I’m going to focus on other aspects of my engine...
Here is the basic idea, which also shows some scene management and resource management i.e swapping resources in/out of memory on scene/level changes and you'll notice that my last scene has a cute animated green little alien - air walking.
Though while it is very basic and not very exciting, it's very interesting from a programming perspective, particularly the management of resources and key-frame animation.
I’ve got to work on the event management system which currently holds a list of events that other parts of the engine send to it (like the keyboard events, the scene change events and the logic processing events), however I’ve stored them in a vector of shared pointers to event objects which I never remove even after I’ve processed them - so I need to fix that among other things.
Apart from all of this, and changing track a bit now - I went for a 10 km run home the other day after my train broke down partway to work. I posted my fastest 10 km which at a pace of 4’28/km and a training effect of 3.9 was quite pleasing.
We broke down at round just past Ickenham station and all had to get up and shuffle in a straight line off the train, through the driver's compartment, which was on the other side of the train. I never quite figured out what happened but we were told to walk to the next station which was West Ruislip which was about 30 mins away. It was nice however to see it again as it was a frequent part of my previous running-to-work journeys.
This is the route I ran:
So that was good(I like running).
I have found a mother duck and her two ducklings about outside recently, just below my balcony, and every day at around 6:30 she leaves her ducklings and they go bonkers, tweeting and wandering around aimlessly around the courtyard, searching for her. She inevitably comes back at some point as I see them the next day. They start tweeting at about 6:30am which is useful as that's when I should be getting up.
In other more alternative news, I had my first food shop in Lidl this evening, and it was quite interesting. I decided I’d try it out because they’ve opened up within the local shopping centre and I’d walked past it a few times going to the Cinema on weekends. Though, it always seemed really busy and I didn’t really want to be faffing about in the frozen-veg aisle while its busy and I'm on a weekend break. But today, after coming home and getting out of the station, I thought I’d do an “alternative shop”. So instead of turning right towards Tesco, I did 270 to Lidl. Interesting experiance...
They have this pretty whacky middle aisle setup which is amongst all the food shelves and has everything from kayaks and school wear to Tupperware and solar-panelled garden lamps. This is really strange as you're browsing for cereal, coffee, cheese or whatever and straight out of nowhere, boom you're faced with beach-balls or mini-outdoor trampolines. Everything was pretty well priced.
I didn’t get much but I did circle the shop-floor about 4 times and each time I found something else that was interesting. It probably helped that the shopping-baskets were on wheels and although not great at cornering, are really fun to use! I always carry my metal shopping basket around, the idea of racing my food around the aisles was cool. Note to self, I must grow up.
I bought a new book on Game AI programming which I started reading on the way home the other day which was quite interesting, though it was a bit specific and having not spent a lot of time understanding utility theory and/or implementations thereof or to a great extend, finite-state machines to simulate intelligence - it was a slow read.
I did find the introductory interesting about how AI not so much trying to simulate absolute intelligence, because if you did that, games really wouldn’t be fun – they’d be accurate but actually seem a little out of place. The brain is more forgiving about large discrepancies in intelligence and actually enjoys it when its within the context of suspending one-belief, so sometimes the time and effort to be 100% accurate is not particularly well spent. The book is a bit big so I’m going to leave it at home. I'll add it to my chain on books that I jump around.
I’m currently in the process of investigating Kali Linux as I’m going to use it to understand more about analysing file systems.
I installed it on a virtual machine on my laptop this morning on the way to work on the train. I had to carry my laptop out of the train half-open because it's still hasn’t installed grub boot loader by the time the train pulled into Liverpool Street station. I’d spend most of the weekend studying about how aspects of the Window OS can be interrogated to determine a timeline of events which might otherwise constitute criminal activity – things like how the hibernate file works, what gets put into it, what is installed in various parts of the registry etc.
I’ve got some practical things to do around undeleting some files and ‘carving’ out deleted files from ‘unallocated’ clusters so I’m basically prepping for this.
I also spent some time recently working on a tricky computer problem. Specifically, it was to do with package references and dependencies in a .Net project, which really sent me around the houses. I spend nearly an entire day 'playing with it' and after figuring it out, reminded me of a paper I recently read. And this is going to sound boring but bear with it:
The paper was about how people learn, and specifically about how experience ie doing something facilitates deeper understanding and how tacit information is unlocked whilst in the act of doing it - what has previously been called "dynamic affordance". This might sound pretty boring and even pretty obvious: that doing is an important component of experience however its more than that - it talked about how really the key to the experience, is bringing in multi-faceted approaches coupled with this doing process - 'playing' and 'trying' is one thing, but doing it with a certain state of mind, having an inquisitive intent is hugely important. Coupled that with actually enjoying the act of problem finding makes any experience gained almost next-level.
It was a long annoying problem and sometimes, that noise I'm hearing from the computer sounds not like the hard drive turning but my computer chuckling at me - but that's ok - as long as its having a good time.
I've also been reading a book about small and helpful mathematics concepts that are explained beautifully which inspired me to write about Rules then Since then.
For example, recently I've been reading about statistics, specifically about standard deviation and variances from it are important in assessing the 'normality' of result sets.
I also dabbled in considering tiny magnitudes such as femtometers that can be used to measure things like the space between two atoms and/or neutrinos and others for massive things like the size of the universe through AU's or astronomical units - which is the distance from the centre of the earth to the sun. Pretty interesting stuff.
I'm also learning about other aspects of arithmetic and in particular calculating the returns on portfolios which I'm really just getting to the beginning of. For example, recently I've been seeing how a return on an investment can be decomposed into the time and money invested in that investment, which then can indicate how that investment really performed. Still early days.
There is a lot of interesting stuff out there to learn.