Home » Posts tagged "C++"

Multidimensional Arrays without Subscript Operator

Some time ago friend of mine asked me how to operate on array without subscript operator, and – to be honest – I supposed it’ll took less than few hours…

NOTE as for all pointer arithmetic it’s rather inadvisable to use this trick in ‘common life projects’.
But I have some more knowledge so I’m sharing! (:
And if you want something more about pointer arithmetic you can check my previous article. Read more

Strategy pattern in MPD-Helper

Here are some thoughts about last refactoring step for my MPD-Helper (repo’s here, post’s here) – replacing code type with Strategy.

I’ll try to explain pattern (I’ll base on Gang of Four brilliant book), my motivation for this refactoring and (in short) implementation. Read more

Basic Void pointer arithmetics

Okay, as promissed before my last trek journey (polish only) I’ll share some C/C++ informations I found interesting.

Few hours before trip a friend of mine asked to write bubble sort. Easy. So I wondered where’s the catch – this guy created img processing tool in java. Our conversation beyond this point was something like:

I: “Why don’t you write it down yourself? You are better than me”
Bob: “I need to write it in C++”
I: “Ok, that’s some point. But Java and C++ syntaxes aren’t that different you know”
Bob: “But header for this sort function must be the same as qsort’s.”

And that was the catch. You need to work on void pointers, and that can be confusing for some.

void qsort (void* base, size_t num, size_t size,
            int (*compar)(const void*,const void*));

Call me good-hearted but I did that. I needed to finish packing, cooking my version of „iron ration” and so on, and doing bubble sort seemed easier than explaining how to do it.
Read more

One thing at one time.

I decided to do more often, but shorter sessions. It suits these short free time gaps I have lately.
These last sessions were another Refactoring ones with aim of extracting functions, so one could do only one thing, instead of all purpose procedures like DeleteSpritesAndFonts(), or two purpose DrawScene().

Soo little changes list:

-> Cutted PrepareSpritesAndFonts() and DeleteSpritesAndFonts() to four functions:
PrepareSprites, PrepareFonts, DeleteSprites, DeleteFonts.
Now all that’s inside PrepareSpritesAndFonts is:

bool Game::PrepareSpritesAndFonts()
{

	if( PrepareSprites())
	{
		bool ret = PrepareFonts();
		return ret;
	}
	else
		return false;
} 

I think it looks better this way. Now it’s easy to tell what’s really inside, and extracting DeleteSprites() helped to delete redundant cleaning after failing with preparing fonts.

-> Also I properly named arguments in headers, so that intelisense can help what’s the meaning of what variable passed to methods (yeah, the fact I haven’t done it at the begining sucks).

-> Bid farewell to victory, running and pause variables, and welcome our new datatype GameState enum! Now with all new Defeat state.
Although it was a little confusing to replace them at first, because of

while(running)
{
   // Loop with redrawing and listening to keyboard events
} 

loop, that created a little confusion. If I’d replace ‘running’ with a condition like (gameState != Victory && gameState != Defeat) it’d be confusing. Why game should run in paused state, but should not in Victory, nor Defeat?
After putting some thoughts I decided to make while a simple infinite loop with breaks after goint into Victory or Defeat. I bet it’s still far from perfection but I like it. For now.

-> Extracted “DrawPause()” from DrawScene – it was unnecessary to check all the time whether game is running and then if it’s paused.

-> Probably it’s a bit obvious, but al_flip_display() can be called only once. It’s unnecessary to do something like that:

	if (gameState == GameState::Running)
		DrawScene();
	else if (gameState == GameState::Pause) 
		DrawPause();
	else 
		DrawEnd();

with ending all procedures with al_flip_display. It’s enough to call it at the end of Draw calling function.

-> Slight renaming. Variable Hp is now known as Lives, and pong is now a pongSprite.

As always you can find changes in repository.

A new begining.

Soo… hello.

The sole reason for creating this developement blog was to widen audience, whom can read my thoughts and ideas. For the time being ideas for One game a month challenge, maybe from time to time about past projects done for graduation. Time will tell what exactly you’ll find on this devblog in future (:

For the moment:
Here is a link to repository with a Pongoid, simple (done in a ~1 hour) arcanoid clone done with cpp and allegro library.

It’s still more a draft than finished project, so I didn’t uploaded it to ogam yet, but… maybe in a few weeks after exams (: