Hurray! A video tutorial! And a short one! How luck you are :p
How to make a texture tilable? Well, check the video out! Tile Next Time! Later!
Hurray! A video tutorial! And a short one! How luck you are :p
How to make a texture tilable? Well, check the video out! Tile Next Time! Later!
After I graduated in June, I have been laying low for a while, working on various projects. Recently, I started leaving my chambers more often for various reasons, including applying for a job (Finally!), attending events (BarcampKW), and meeting with clients. So? you may ask. Well, it seems most people I meet end up trying to know me through my blog. So, I have to fix my blog!
I have been thinking for a very long time on how I am suppose to provide this proper facade through my blog. So, I spent around 30 seconds thinking! (Usually, my blog posts are spontaneous, but not this one!).
No, this is not a movie review, I am gonna give a brief intro about me.
| Name: | Mazyad Alabduljalil |
| Degree: | B.S Computer Engineering, Kuwait University. |
| Occupation: | self employed |
| Interests: | Game Dev., iOS Dev., cocos2d |
| Hobbies: | Tennis, Japanese Language |
| Kuwait Prayer Times | |
| Initial Release: | November 2010 |
| Progress: | Working on v5.0. |
| Dama: لعبة الدامة | |
| Initial Release: | August 2011 |
| Progress: | Working on v3.0 |
| + Android version | |
| Islamicaster | |
| Expected: | January 2013 |
| Progress: | Testing & QA on iOS |
| + Mac app Dev. | |
One of the things that makes my blood boil while programming is benchmarking. First, let us see what benchmarking is, and which type am I talking about… Rejoice! No crude introduction. Not this time, at least!
Ehm, so, benchmarking is “evaluate or check (something) by comparison with a standard“, straight out of the dictionary. In software development, it has become common that the standard is nothing but your yet-to-be-optimized program. So, after all the main aspects of the software are laid out and implemented, the program’s performance is measured and used as a standard, then the excitement begins! Who shall overcome this standard, and by how much!
Why do I find it so exciting? I’ll just jump to the example (which actually happened to me one day). I had a TableView in my iOS app, and the scrolling was simply horrible. Something had to be optimized in order to increase the speed of the TableView’s scrolling. I measured the amount it takes to generate one row of the table, and it was 450 – 550 ms! That was simply unacceptable. Hence, the optimization and benchmarking began. By simply adding NSLog statements between different part of the row’s initialization process, I was able to find the culprit. It was because I was drawing the image manually in CoreGraphics, instead of using a normal ImageView. That’s not the point, the point was how awesome it felt when I fixed this part, and the generation time fell all the way down to 150 – 200 ms !! It really feels like I accomplished something :p. Further optimization were also made, and it was (overall) a fun process.
OK, so now, it is even more exciting than ever. The Dama AI (Arificial Intelligence/Bot) player is slow, and I am finally taking the bold step of optimizing it. Unlike the previous case, the bot cannot be further optimized by playing around with the code alone. Some logical changes are necessary. By logical changes, I mean making the AI smarter by ignoring bad moves and going deeper in promising moves. In order to do all that properly, data mining is required 8).
I am not really gonna “data mine”, that’s too much for a small project like this one. However, I need to prepare proper test cases, and automate the process of extracting the data from the test cases and comparing them with other cases. Makes sense? No? Hmmm… I am gonna write my whole plan in a sec, so it should make it clear.
First, let us see our candidates, or basically the AI players that shall be benchmarked.
Name: QDBot
Age: 1 year 1 month
Bio: Original Bot written with the initial game. It received many criticism for its slow Hard level, which predicts 4 moves + the eats. It is mainly a brute-force algorithm, exploring the whole search space, but does have minimal optimization.
Name: PikiBot
Age: -1 days
Bio: The Bot that I am hoping to complete by tomorrow. It kicks off where QDBot left off, and selectively decides which path to traverse, instead of blindly exploring the whole search space. This selective nature of his earned him the “Picky” name.
Name: iBot
Age: TBA
Bio: It is phenomenal. The best I have ever written at that time (sounds familiar?). This test candidate should come with many goodies, however, it might take a while to achieve. It is still uncertain whether it’ll actually make it for the benchmarks.
… On with the test cases. Unfortunately, I couldn’t visualize the cases, so it will be in plain text:
Each Bot’s speed shall be tested on all the difficulties. The depth will be equal on bots.
Each Bot shall try to come up with the best move within a certain time constraint. 1 sec, 3 sec, and 5 sec.
The Arena! I shall buy some popcorn, along with my favorite drink, and watch the bots in their battle of survival! xD
EOF
Mukashi, Mukashi .. There lived two young kids, a boy (names KVC) and his sister (named KVO). You see, they were born twins, and the laws of mortality did not apply to them. (Plain and simple, they never aged for some reason). They lived happily in the forest, where they survived by utilizing their two main skills:
Countless years passed, while KVC & KVO enjoyed their peaceful lives in the forest. Until one day, they had an unexpected visitor. They were baffled by his white tuxedo, his silver pocket watch and his shiny shoes. “My name is Mr. Appolous, but you could just call me Apple”, said the stranger with a gleeful smirk. The kids just stood there gazing upon Apple.
“Say”, Apple broke the silence, “Would you two like to help me by being part of a very successful programming language?” — BZZT. Wait, say what?!
Ok, enough with the silly introduction, and onto the blog post :p. I just hope this silly story somehow sticks to your mind and helps you comprehend to some extent what KVC and KVO are, until we dwell into their guts in a bit.
Before writing the blog post about KVC & KVO … Man, has it been a while!! >.< This Islamicaster project of mine has been ever so stressful with a never ending TODO list. The good news is, it’s still full of new things to learn! And thanks to that, I have completely mastered KVC & KVO! (jk, I am no where near a master of them, but I do have enough knowledge to give an overview on their awesomeness
)
“KVC & KVO: Replacing Pillars with Magic” is the title, but what does it really mean? Well, keep that question in one corner of your mind till we explore what KVC & KVO are, then we shall answer this question.
First off, KVC & KVO are not always used together. Each has its own divine purpose, and can be used separately. The awesomeness comes from combining them. However, for the sake of this blog post, we shall tackle them one at a time.
As you might have deduced from the boring tale above, KVC does two main things:
… OK, so what’s so cool about that? Well, the way we set and get the values is what’s cool. To put it clear and simple, “KVC allows us to get and set the variables of an object as though it was a dictionary” (That’s not 100% accurate, but leave it like this for now). Consider the following example:
@interface Foo : NSObject {
id var1;
id var2;
...
id varN;
}
Now, with this class having all theses iVars, it would be a pain to do something like:
Foo* foo = [[Foo alloc] init]; foo.var1 = ...; foo.var2 = ...; ...
What’s the alternative? KVC! Here is how it works:
for (int i = 1 ; i<=N ; i++) {
NSString* varName = [NSString stringWithFormat:@"var%d", i];
[foo setValue:[NSNumber numberWithInt:22] forKey:varName];
}
// And inorder to read the vars in a similar fashion:
int var1 = [[foo valueForKey:@"var1"] intValue];
Notice two things:
Regarding the first note, it’s fine. KVC is smart enough to send the NSNumber as an int to the instance variable. The second case is similarly handled by KVC, in the sense that it will call your custom setters & getters, no worries. However, the third case is tough. It is true that most of the time you probably don’t have such a convenient value to set.We will see about that after exploring KVO.
Remember what was KVO’s job? Observe. Thus, it is quite simply useful to observe any changes on the instance variable that we have. This is a useful alternative for delegates in simple cases, where defining a protocol and setting up a delegate is just an overkill. Imagine a game with a gun. No, not a game holding a gun, but rather a game that contains a gun that the player can fire. Now, imagine that this gun cannot fire two simultaneous shots. Now, here are some approaches that are available:
// Polling approach. Yuck!
// Inside the gun class:
- (void)fireBullet {
// check if bullet exited the screen
if (bullet.ready || bullet.position.x > 400) {
[self reloadBullet:bullet];
[bullet fireWithAngle:zelta];
[bullet setReady:NO];
}
}
Some of us might started thinking, what’s wrong with that? Nice, simple and it works! Well, not exactly. What if the player fires a shot and stops? The bullet would eventually go off screen and continue moving with no reason at all!! Even more, what if we wanted to show the user whether he has a bullet reloaded? It won’t be possible without a delegate:
// Polling approach, with delegate. Gross!
// Inside bullet class:
// this method updates the bullet position each frame
// thus we see an animation of the bullet moving:
- (void)update {
self.position.x += 1;
if (self.position.x > 400) {
[delegate bulletDidExitScreen:self];
}
}
// Inside the gun class:
// handle delegate method:
- (void)bulletDidExitScreen:(Bullet *)bullet {
[self showBulletAvailable];
[bullet setReady:YES];
}
- (void)fireBullet {
// No longer need to check if bullet exited the screen
if (bullet.read) {
[self reloadBullet:bullet];
[bullet fireWithAngle:zelta];
[self showBulletUnavailable];
}
}
OK, with that crap out of the way, let’s see how KVO does it:
- (void)fireBullet {
// check if bullet exited the screen
if (bullet.ready) {
[self reloadBullet:bullet];
[bullet fireWithAngle:zelta];
[self showBulletUnavailable];
[bullet addObserver:self
forKeyPath:@"position.x"
options:NSKeyValueObservingOptionNew
context:NULL];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
float newX = [[change objectForKey:NSKeyValueChangeNewKey] floatValue];
// do stuff...
}
Just awesome
I won’t say more.
Now, let me show you how I recently used these two to end up with a pretty-darn darn-pretty design
.
… I ran out of gas this time ^^; … Hopefully next time I’ll continue.
And, I have finally pushed myself to publish a video tutorial! I am not sure whether it will be useful or not but I’d be happy if just a handful of people learn a thing or two
.
Now, I am going to simply rant and blabber until the video is exported and published to YouTube, then, I will add it, and stop this ranting
. So, what will the ranting be about? The tutorial, of course!
I was very excited recently when I wrote a kinda modular class that provides Infinite Scrolling illusion to a UIScrollView. So, I really wanted to share what I learned to the masses. It is, however, a long journey, and requires many concepts to be explained in detail, so I can’t really make it a single short tutorial. Thus, I have prepared today the first part, which is waaay easier than the second part. Especially in these busy days, I cannot manage to publish the second part without sacrificing quality, so it will have to wait a week or two till its released. (Not yet Bob? [Bob is my rendering engine. You see, I give different parts of my computer different names. For example, my sound output is called "Wolf". My family often hear me yelling, "Shut up, Wolf!", and they would think I went nuts... Aaaanyways.]).
Wow, Bob has quiet down .. I am sure he’s done by now … Half way though, you say? Oh, he passed the job to Thomas (My network card
). Bummer, Thomas takes his time to upload .. I think I’ll go do something useful, and stop wasting time
.
…
And the tutorial you have been waiting for!!
And the Source Code which you have been REALLY waiting for!
GAAAAAH!!! I just had to yell it out!
I am over with the hardest parts of my graduation project, over with the toughest finals, and most importantly, I am just furious at myself for not writing for so long! [-__-']
On with the post, the sneak peak! Today, though shalt expose parts of thy project!! (My inner literature just goes nuts when I am excited).
Without further ado .. Let us look at this project!
Islamicaster .. Inspired from all the “caster” applications out there, me and my mates in the project decided to call our project “Islamic – caster”, with the c being mutual. And please, it is pronounced ISLAMIC and not IZLAMIC.
So, what’s so super cool about this project? Number 1 coolest thing probably is that we were fortunate enough to work with a VERY talented Kuwaiti artist. Sir Ahmad Al-Refaie, whom can be found at Owaikeo.com, is currently employed at Pink Coffee & PR, and these guys were kind enough to take care of the graphics design part of the application! This made us work in full potential on the programming side of the project. Still, the graphics are simply breath taking and “outstounding”. (I just made that up, which is a mix of two words that fit that place perfectly. I leave it as an exercise for the reader to figure these two words out :p).
In my opinion, the second best thing about this project is “Portability”. Before you start throwing me with nuts for introducing computer Jargon here, let me clear it out. Thy project shalt run consistently on iOS, Mac and Android! YES! (As much as possible). The fact is we have already finished much of the Mac app, and the mobile platforms remain. Point is, we are already on three platforms before going official! Hopefully expanding ever more as the project grows larger with more human resources :p.
I am not going to say anymore than that! If you would really like to know more about Islamicaster, please come visit us at the Engineering Exhibition!
The first day will be from 12:30pm – 10:00pm, while the second day will be from 9:00am till 8:00pm
.
| Location: | Marina Hotel, Salwa Al-Sabah Hall. |
| Date: | 6 & 7 June. |
| Time: | Day1: 12:30pm – 10:00pm Day2: 9:00am – 8:00pm |
We really look forward to seeing you there!
** WARNING: This post is not intended for the faint heart and is also not intended for people who hate blabbers .. I am in a mood that tells me I am about blabber a LOT **
That warning reminded of the green screen warning you see before a movie trailer .. anyways, going on to the post!
Through the strive to move forward (Susume in Japanese, as I believe), I decided to take a Bit step forward.. Sometimes a Bit is better than a Byte! That is not a (Ga6ah), but it’s the Shinri!! I mean, It’s the Truth!!
While programming in objective-C, I have taken interest in something while looking at apple’s header files code, and other awesome people’s code around the net. They had very attractive code, at least to me, that I decided to try out today. With that being said, I have tried to apply it once in my Dama game, but failed horribly, whether because of my ignorance or it wasn’t the appropriate place to use that thing..
If you are not jumping up and down on the chair, gritting your teeth, while pulling your hair from excitement, well go ahead and do it.. It’s a really exciting topic!
… Done? Ok, let’s start the real deal!
While coding the project that I have been talking about in some of the previous posts, yes the one I am yet to disclose, a very annoying problem popped up. Interestingly, it also has to do with the settings, which implies the settings journey wasn’t over after all!
The problem was the large and ugly amount of primitives that are associated with the settings view that I had. (What the heck is that guy talking about?!) Don’t worry, that was suppose to be a bit ambiguous
. So, let’s take an example, shall we? Take a good look at the following settings view:
Ok, after looking at it, imagine how are we going to save the checkboxes states and the popup buttons selections. Don’t worry about how to save the font, size, and colors for now. (NOTE: Without using Cocoa Bindings. I have my reasons for not using them).
Done? Ok, so, the checkboxes should obviously be saved as a boolean whether they are checked or not, and the popup button, we can just save the index selected. good enough?
Now imagine we have to pass these values all around the application. On top of that, we have 6 more settings view other than this one!! Previously, I implemented each setting by itself. As in, add a new class and call it StatusItemSettings with instance variables: boolean isEnabled, boolean isIconEnabled … etc. that was hellishly annoying!
So here comes the awesome Bit! (For the record, the code has been changed for brevity sake, and partial closure of the idea
)
//The binary awesomeness:
// 1-bit: Icon
// 1-bit: Name
// 2-bit: Extras
typedef enum EStatusItemType {
//Masking awesomeness
EStatusItemOff = 0,
EStatusItemIcon = 1 << 0, //xxx1
EStatusItemName = 1 << 1, //xx1x
//Just a start point, the rest are not masks
EStatusItemExtrasNone = 0 << 2, //00xx
EStatusItemExtrasRemaining = 1 << 2, //01xx
EStatusItemExtrasTimeLeft = 2 << 2, //10xx
EStatusItemExtrasTimeRem = 3 << 2, //11xx
//Their awesome mask:
EStatusItemExtrasMask = 3 << 2, //11xx
EStatusItemFontMask = 7 << 1 //111x
} EStatusItemType;
Wth… ? Well, erm, let me try to explain
. We shall save the setting in a single robust, reliable variable! YES, it is possible!
From what I figured, enums have two useful useness:
Now, in the second case, it is most likely we cannot enumerate the enums, but check out they can be used!
We divide the enums in a binary string such that each each substring has some significance .. Let’s look at our enum:
//Shift the 1 to the left by 0, making it take the first bit EStatusItemIcon = 1 << 0, //xxxx1 //Shift the 1 to the left by 1, making it take the second bit EStatusItemName = 1 << 1, //xxx1x
So, that is basically how you reserve a bit for your setting option. Now, what if the setting option for the icon was selected and we wanted to save that? Easy!
//Start with a fresh type. It looks like '0000'
EStatusItemType type = 0;
if ( iconIsSelected )
type = type | EStatusItemIcon;
//That's it!
Nifty ! That’s the bit-wise OR operator. By using this operator, we effectively stored that the checkbox is checked in the type variable. This also applies to the Name checkbox, too!
Now, comes the other trick. The Popup button shows a list that is mutually exclusive. In other words, only one index can be selected at any one time. We can use the previous idea, and reserve a bit for each option, but that would be a waste! In my case, I have 4 options in that button, and that would mean I have to reserve 4 bits. Instead, I decided to reserve 2 bits, and make use of all the combinations! 00, 01, 10, 11
Of course, I do not want to bother calculating how much is 0000, 0100, 1000, 1100 (because the first two bits are reserved), so, I just put 0, 1, 2 ,3 .. all shifted to the left by two bits
Now, onto the idea of how to use this new technique. Unlike the OR operator, we have to do things a bit (small case b) differently.
type = type | (selectedIndex << 2);
//That's it!!! xD
Notice how simple it is!! Of course, we just have to map the indices to the enum counterparts, but that’s all!
The small caveat here is a bit of maintainability issues due to the brain stretching that would happen if you were to tamper with it to change/add/remove settings.