February 2007 - Posts

Ever wondered how you can create a resizable array in C/C++? I will not be discussing the C stuff here since I'm not  at all that good in C. So let's try to take a look on how to make dynamic memory allocation in C++.

1
2
3
4
5
6
7
8
9
10
11
class MyClass
{
private:
int *myvararray;

public:
MyClass(int n)
{
myvararray = new intNo;
}
}


Here, you create a pointer to an int. Then using the new operator(C++ only) create an array
with n size. That's how easy it is actually. But how about when you need to make dynamic
memory allocation for pointer types? Here is a snippet just on how to do that. This is taken
from the Animation class that I created for my game.

Make a pointer, to the pointer

1
BITMAP** animationFrames; //this is a member


then for example, you in your constructor you can do it like this.

1
2
3
4
5
6
7
    animationFrames = new BITMAP*[maxFrames];

for (int i = 0; i < maxFrames; i++)
{
animationFramesIdea = create_sub_bitmap(bitmap, currentX, 0, frameWidth, frameHeight);
currentX += frameWidth;
}

 
Simple isn't it? Hope that helps! ;)

Posted by lamia | with no comments
Filed under:
It's February. And unless I run out of budget or our client brings up another killer request, nothing's gonna stop me from taking the SCJP 1.5 this coming March. I feel a little down when I scored low(actually, failed) on the examulator mock exam (trial version). The good thing though is that I realized my weakpoints!

- regular expressions
- threading
- generics

and other new features in Java 5.

I'm pretty confident that I have a solid understanding of Java(1.4) fundamentals and throught for sometime that I should take 1.4 instead of 1.5. But I don't really have the privilege of doing that since by March, there will be a new project and we'd probably be busy fixing some bugs on our current client's project. As a sort of strategy, like many other developers who review for SCJP I do have a study guide:

Sybex - Complete Java 2 Certification - Study Guide, 5Th Ed - 2005

Which I think, is one of the cheapest but quite decent book for review. It may not be as good as K&B's book, but it's sufficient(according to my teamlead who is SCJP for 1.4). Aside from that, I also take mock exams from other websites such as:

http://forums.javabeat.net/index.php?topic=5.0


It's fun and more and more I come to prove to myself that there really is still a lot of things to learn about the basics. I also try to code to prove the theories I learned. I try to separate the topics in different packages such as the one shown below.





I'm really feeling lazy typing in notepad and compiling in the command line so I prefer using an IDE like eclipse. Although, compiling from the commandline would probably be more effective for some other people.

It's also helpful to ask in the javaranch SCJP forums. That's it! I don't want to be wasting $200 just to fail the test!
Posted by lamia | 1 comment(s)
Filed under: ,
I'm used to doing just <c:import url="/viewaccount.do?id=${account.accountId}"/> in JSP. So when I was asked to make a simple PHP script I thought the same thing was possible in PHP using the include() command. I was wrong though and thought of using session globals as a workaround. It's a good thing that I searched the internet first for a more elegant solution.

What I needed was for the $_GET["id"] variable to be visible in one of my included file. The solution? It's a basic one, really. Just create a function that accepts the id as an argument


1
2
3
4
function showAccount($accountId)
{
//some complicated stuff here...

return;
}



And suppose the function above is in a file called functions.php. Just include it your code and make a call to showAccount like this


1
showAccount($_GET["id"]);



I wish I was doing Java instead of PHP though... [SIGH]
Posted by lamia | 2 comment(s)
Filed under:
Since I'm mostly exposed to enterprise development(or not-so-enterprise) I deal mostly with how an application would scale. This left me wondering how everything is done in Java 2 Micro Edition or  J2ME which is the mobile platform for Java. I remember in my first(and short) job as a Symbian C++ programmer, my teamlead always told me to keep/make as few classes as possible because this could affect the performance of the application I'm making.

Sumpre J2ME gives some good advice on how to effectively program in J2ME. I really can't wait to have a project involving this technology. Perhaps someday in my career I will again come back as a mobile applications developer using Java or Symbian C++.
Posted by lamia | with no comments
Filed under: ,
Our client asked for a short portion of their php code to be modified. Since I'm a Java guy and so is my team lead, we asked our new developer (web developer) to do the dirty stuff for us. Good, he was able to finish quick. But then, when I tested the code he wrote, the php script would just show as plain text. It's been years since I programmed in PHP. But it's a good thing I still remember some stuff about it. From the php.ini file it said:

; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = Off


I was used to doing <?php ?> instead of  <? ?>. The short_open_tag or <? ?> makes your code less readable IMHO. It's also unsafe since short_open_tag is not enabled by default on other servers. Thanks to garri of devpinoy for pointing out that I can configure this on the server. Well, not actually the server but php.ini.
Posted by lamia | 11 comment(s)
Filed under:
Whew! Finally I was able to solve the bug I was tring to fix in my game. To explain briefly, I have an std::map<char*, BITMAP*> which is a static member of a class. Now, I have a Manager class which loads resources that somehow looked like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BITMAP* BitmapManager::addBitmap(char* key, char* fileName)
{
BITMAP* bitmap = load_bitmap(fileName, NULL);

bitmapTable.insert( std::pair<char*, BITMAP*>(key, bitmap) );

return bitmap;
}

BITMAP* BitmapManager::getBitmap(char* key)
{
std::map<char*, BITMAP*>::iterator it = bitmapTable.find(key);

BITMAP* bitmap = it->second;

return bitmap;
}


As my code goes deeper and deeper and I pass a reference to a BITMAP by calling the BitmapManager's getBitmap function, I somehow encountered a situation when the pointer already became unreacheable and my program would crash when I make a call to BITMAP's(bitmap is a struct) w member which is equivalent to the bitmap width . The call to getBitmap would return NULL or 0.

Thanks to some guy in the allegro.cc forums who advised me to use std::string instead of char*. I don't know the exact reason why this would happen, but the solu worked anyway.


In the end, I didn't use a char* or std::string at all and just used an enum type(implicit integer) for my map key. Big Smile


The final code look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
BITMAP* BitmapManager::addBitmap(BitmapKeys key, char* fileName)
{
BITMAP* bitmap = load_bitmap(fileName, NULL);

bitmapTable.insert( std::pair<BitmapKeys, BITMAP*>(key, bitmap) );

return bitmap;
}

BITMAP* BitmapManager::getBitmap(BitmapKeys key)
{
std::map<BitmapKeys, BITMAP*>::iterator it = bitmapTable.find(key);

BITMAP* bitmap = it->second;

return bitmap;
}

Posted by lamia | 4 comment(s)
Filed under: