Wednesday 30 November 2011

The End of Multi Threaded Programming

I warn you: this is a provocation. I really want to have replies to this post. Because I'm talking about one of most famous themes for computer-science students: threads and multithreading programming.
A special thanks to Andrea "MEgrez" Talon whom solved some doubts I had about Node.js

Wednesday 23 November 2011

Build your C/C++ programs everywhere with SCons



Maximum Power!

In these days I'm trying to realize a small and portable game engine using SDL+OpenGL, fascinated by their power. I begun writing some C files and a simple Makefile.
Portability is a fundamental requisite, so I decided to move to a portable building system. I thought CMake could be a good choice, mainly because it's used by several open-source project: KDE, Inkscape, OGRE, MySQL use CMake. So, I decided to write my platform-indipendent CMakefile. It was one of most boring and error-prone task I've ever saw. After some days, I still can't have a functionally CMake file: I've got less problems writing then a python script for compiling it. Frustrating.

Monday 14 November 2011

Homebrew, everything you need






I am a Mac OS X user. I like my OS and I love how I can install/remove applications just with a drag'n drop. But some useful programs can't be find and this could be very annoying. Command-line programs like netcat or sdl-config or chocolate-doom still haven't a Mac OS X package.

Thursday 3 November 2011

The Robot and the Baby

I would like to salute John McCarthy, father of lisp recently passed away, in a more constructive way than a simple dedication.
McCarthy wrote a beautiful science-fiction story, The Robot and the Baby, talking about robot's perceptions.
I think it's quite lovely: inviting you to read it could be better than write a simple necrology.
Farewell, mr. McCarthy.

Thursday 13 October 2011

Farewell, Master


Today I read the latest sad news. Denis Ritchie, co-writer and co-ideator of UNIX and C programming language, died at 70.
Thank you for your immense contribute to computer science.

int main(int argc, char** argv){
   printf("Farewell mr. Ritchie.\n");
   printf("You made our tools.\n");
   printf("You made the True Operating System.\n");
   printf("You made us programmers.\n");
   printf("You made us hackers.\n");
   return 0;
}

Thursday 6 October 2011

Thank you

Thank you for NeXT and its codebase.
Thank you for the Macintosh, a real personal computer, wich does what a PC should do in 2011.
Thank you for iPhone.
Thank you for iPad, the "magical" device.
Thank you for creating this framework, giving me the opportunity to work as a Real Programmer in a Real Programming Language.
Thank you, Steve.

Wednesday 14 September 2011

iPhone SDK Troubles: ios SDK blurry fonts

A very annoying thing is when you programmatically create contents for a UITableViewCell and text in UIlabels appears blurry. Why? The answer is simpler than you can think: you positioned your UILabels in the wrong place. Probably, you positioned your UILabel:
//At allocation time
UILabel* l=[[UILabel alloc] initWithRect:CGRectMake(x,y,w,h)];

//or after
l.frame=CGRectMake(x,y,w,h);
Now, be careful: what's the meaning of (x,y,w,h)? They're floating point values. When placed in a UIView, they must have no fractional part. If you specify it with (e.g.)
(1.0f, 3.0f, 200.0f, 400.0f)
your label will not appears blurry. If you calculate UILabel's frame, you must call floor function to force to zero fractional part.
float a=1.0f;
float b=3.0f;
float c=200.0f;
float d=400.0f;

l.frame=CGRectMake(
        floor(x/3.0f),
        floor(y/3.0f),
        floor(w/3.0f),
        floor(h/3.0f)
    );
Texts appear blurry when coordinates aren't approximate to nearest integer. Floor force this approximation. Now your text will appear sharp as a japanese sword :)

Tuesday 9 August 2011

iPhone SDK Troubles: Rotating scrollview image gallery



Everybody love iOS interface and its "magic" appeal: fluid rotations, fluid swipe, ecc. A iOS programmer knows that there's a lot of work under that magic. If you try to realize a simple image gallery, you'll have some surprises when you'll try to rotate it:

  1. your image will not be located on view's center, but on a corner.
  2. if you forced your gallery to scroll to a certain page with scrollToRect, then you'll see all your precedent images in a fast scroll, even if you set parameter Animated to NO
  3. if you need to scroll after rotation, you'll see a rotation around a corner
Solutions I found are:
  1. Point 1: when detecting a rotation, (willRotateToInterfaceOrientation), you have to resize:
    • Images on the scrollview
    • scrollview content

  2. Points 2 & 3: you must cover gallery's scrollview with a UIImageView containing current gallery image, hiding all scrollview animations. The "cover" will rotate around screen center, giving a more professional (and Apple) look.



Thursday 4 August 2011

Hey Emacs! (an excursion on Emacs' tab-key)

One on main difficulties on Emacs is how it manages tab-key. A user whom comes from VI/VIM or another editor feels disoriented because Emacs ignores a tab and mantain stubbornly the selected line in the same place.
A bug? No. It's a feature. Hard to understand, but a feature. This is because there's three ways to manage a key-tab event:

  1. a way to insert a TAB (\t) character
  2. a way to insert 4 (or more) spaces
  3. a command to indent selected line(s)

VIM and other editors mean tab-key as 1 or 2. Emacs, instead, binds tab-key whith the "indent according to mode" command. This means a more sophisticated (and elegant) tool, but also means to understand it.

Modes

Emacs works with Modes. A C file will be interpreted with the c-mode; a Java file with java-mode, ecc. Every mode has its own configuration. E.g., a C file could be indented according to GNU style, BSD style, Kerningan and Ritchie style and others (a full list is avaiable on wikipedia).
But is an advantages? Well, yes, when you understand how a mode works. If it's properly configured, it helps programmer as no other editor. You can detect if you forget a parenthesis becaus Emacs will indent wrong a line. It's usefull.

But my colleagues uses VIM

If a VIM users sends you a C file, all new lines you'll add will be indented using current Emacs mode and style. It's boring, but there's a solution. You can change Emacs configuration just for a file writing on top of it the famous "Hey Emacs" line. If you write on top

/* Hey Emacs! -*- mode: java; c-basic-offset:4; indent-tabs-mode: t;default-tab-width:4 -*- */


Emacs will understand:
  • it's reading a Java file (enable Java mode)
  • indent lenght is 4 spaces (c-basic-offset)
  • tabs are 4 spaces long
  • when idents, Emacs must use a tab character (indent-tabs-mode:t)

This is just an example: there's many other variables you can set. I think adding this line it's a small price to pay. Emacs is really good.
And, if you want, there's always VIM ;)

Update
There's a way to insert brutally a tab character: pressing META+i. But I suggest you to use it carefully.

Thursday 28 July 2011

iPhone SDK Troubles: Error from debugger: Error launching remote program: security policy error


When I started programming with Cocoa-Touch a guy who teached me Objective-C's basis said «Everybody loves Cocoa. Not much people loves XCode».
Forgetting some boring and stressful troubles (mainly about memory management), I agree with him, because last "iPhone SDK Troubles" came from XCode. It gives me this error:

Error from debugger: Error launching remote program: security policy error

It taken me a lot to resolve it, because it seemed an error related to Info.plist or a bad-configured provisioning profile.

Instead it was easy: just delete all expired provisioning profiles from your device (iPhone/iPad). Go to Configuration->General->Profiles and delete them.
I was tired, so I deleted all profiles and I rebooted the device, but after I was able to install applications from XCode to iPad.

Friday 22 July 2011

Pyglet on Windows 2000. Yes, you can!

So, you're a "pythonist", right? And you get tired of pygame, right? And you want to play with pyglet but it doesn't work beacuse there's an error with GDI+?
Relax, man! I'll explain you how to resolve ;)
  1. go to MSDN site and download the GDI+ packages
  2. Decompress it on a directory. It's not important wich one: we have to get the gdiplus.dll file
  3. Copy gdiplus.dll from /asms\10\msft\windows\gdiplus
  4. Paste it on your python directory. I have it on C:\python26.

It's all! Happy coding!

Wednesday 13 July 2011

«From Python to Ruby» aka «Yet Another Infamous Comparison»!

In these days I'm interesting about Ruby and Ruby On Rails. I already know PHP and Python, but Ruby seems more active, widespread and interesting. For "interesting" I mean PHP still appears to me as a easier Java without compilation and classpath. Some friends of mine use Symphony, but I am not so enthusiastic about it. I like Python's abstraction level, but I admit it has too many web frameworks and none of them seem "cool" enough.
Rails, instead, uses an high level language (Ruby) and creates a homogeneous, well designed and complete environment for web application development.
These are my first impressions: maybe I am wrong.

This post is written for another religion war: Python vs Ruby. I belive it's a war without sense and I think it's usefull learning both.

Always learn at least one scripting language

It's fundamental to learn at least one scripting language. It could be lisp, javascript, python, ruby, perl, ecc. But it must read/write files, opens sockets and do everything a real language must do. It will help you to write some usefull scripts (e.g. create and populate a database from text files, download and save XML from network to local disk, ecc.)
Will learn more than a language will help me? yes! Because...

...you must use the more confortable tool...

...for a specific task. C and C++ are usefull for real-time applications and for videogames. They're not very good to write CGI or web applications. Java is good on servers, but I think isn't good for heavyweight audio manipulations.
I found myself comfortable with Python in many cases, but it is very likely I will use Ruby more often. Remember: the silver bullet is an anti-pattern. Learning more languages will give you more instruments to work better and faster. In programming there's not a Holy Grail, just the right tool in the right place.

Tuesday 5 July 2011

iPhone SDK Troubles: a missing feature in XCode 4

XCode4.0.2 hasn't got a "Search and Replace in Selection" tool. It's one of most important tools for some very boring tasks, such as adding a @synthesize to hundred of lines or using the same lines to initialize them with a [[NSString alloc] init];
I hope Apple soon adds it: I wouldn't to continue switching between XCode and VIM to replace some text!

Tuesday 28 June 2011

Eclipse vs Netbeans

Oh yes... another (in)famous deathmatch between two developers tools. Eclipse is supported by IBM and uses a custom library to give an GUI, the famous Standard Widget Toolkit (SWT). Netbeans is based on Swing and is sponsored by Oracle.
Two years ago I switched from Eclipse to Netbeans, just for Netbeans' GUI editor (Metisse), a program I waited for long.
Soon, I discovered some things I liked of Netbeas:

  1. An easy way to install plugins
  2. A powerful refractor tool

Despite it's written using Swing, Netbeans works well. I still have doubts on developing applications using Swing, but Netbeans demonstrates it's possible. It's well integrated with desktop (Mac OS X, Windows 2000 and Ubuntu).

BUT (and it's a recent news), Eclipse Foundation releases a new version of its IDE, Indigo. Indigo has very interesting features, and seems it's going to be very near to Netbeans.
I'll examine it better next days. I'm very curious about it.
Meanwhile, I continue using Netbeans, because I'm used to work with it and because (as I learned some weeks ago), it's better to wait a bit before start using a new major version.

Monday 6 June 2011

A look to XCode 4


«Boss, we update to XCode 4...»


If you're an iPhone developer (or a Mac OS X one), then you're using XCode. Recently, Apple released XCode new version (4), introducing a lot of improvements. I list just the more "impressive" and useful.

Refactor

Refactor is one feature I loved in Netbeans and I'm happy to have (finally) on XCode. Select a class name or a class property, right-click and choose "Refactor": XCode will search that word, how and where is used and will replace it, saving a lot of time and boring debug sessions.

New Interface

XCode 4 comes with a new user interface. Still based on Aqua, it looks like iTunes and it's more "rational" and ordered. Even targetoptions are subdivided with a drop-down interface, making easy searching a specific option.

Embeded Interface Builder

The interface builder is no more a stand-alone program: now it's a "feature" of XCode. Clicking on a interface file it will open inside the SDK, showing all informations (connectors, widgets, ecc.) on right side.

Drawbacks - Heavyweight

Obviously, there aren't just good things: XCode 4 is an heavyweight program: on my Mac Book Pro it eats 173MB and slows down my system. It's no a so drammatic, but it's boring.


Drawbacks - New Configuration

Do you remember how you sign your App to submit it to App Store? Well, forget it. Now the procedure is totally changed. Somebody says there's from a long time, but I seriously don't remember it. I don't like the new way to configure build and distribution. Maybe it's better, but it's certanly very different: a "soft" way to learn it would be useful. A good step-by-step guide was written on Stack Overflow.

Drawbacks - Immaturity

XCode 4 comes with a lot of new features. New features means less tests. Less tests mean more problems. I have many examples: with XCode 4.0 was impossibile to upload new binary with the embeded Application Loader. It gives an error related to com.apple.transporter.util.StreamUtil.readBytes(Ljava/io/InputStream;)[B.
I downgraded it to 1.3 version (as you can see here).
Another one was some crashes. Another one is when you "delete" a file from class tree: it's really deleted. No hopes to recover it, neither looking in trash can.
Another one is the mysterious "removed references" in a project. I can't understand why, but almost all references were deleted on a project. VERY boring!


Drawbacks - Slow

Finally (and more important), XCode 4 is slow. Damn slow! And heavy as an elephant. It makes slow my Mac Book Pro 2009 easly. All system suffers this memory-and-cpu hungry program.

Conclusions

XCode 4 has some interesting features, but suffers its youth. Probably this is the main reason for maintain a link to XCode 3.x on Apple Developers site. I hope XCode will be optimized, because its slowness is its main drawback.

Wednesday 1 June 2011

iPhone Tutorials - "Duplicate Symbol _OBJ_IVAR_"

I am working on a personal review about XCode 4. Please, be patient!

XCode 4 gives me many troubles. The last one was a linker error

Duplicate Symbol _OBJ_IVAR_ a class

What happens?

  • Maybe you declare #import "Classname.m" (with a .m instead than .h)
  • XCode lists "Classname.h" and "Classname.m" two times. You have to "remove reference" for a copy from XCode.

It seems XCode4 gives many problems to iOS developers. I'll talk better in another post.

Tuesday 19 April 2011

VIM vs EMACS - again


«Two snakes facing each other!»


Looking to my blog statistics, I realized visitors come here mostly because they're looking to "vim vs emacs". It's comprensible: this is one of most (in)famouse computer "wars".
In last months I used a lot VIM, because I had some problems with Aquamacs Emacs. So, I decided to write another post to these two max-weight editors, maybe helping you to choose.

EMACS

I became an Emacs fan since 1999, when I first heard about "a free, open source, configurable, programmable editor". I gave Emacs many tries, just because it was Emacs. But, let's admit it, it hasn't a quiete learning curve. Emacs has more options and controls than an SR-71 and has it's own default settings. This is the "beauty" of Emacs: to discover that a simple function (one-tab if I type tab key, e.g.) needs to be configurated.

VIM

Usually, everybody tries VIM after some frustration with Emacs and find a bit confortable with it. Editing file in a modal program is a bit weird, but become quite fast. You can have problems with some "complex" tasks, such as search-and-replace. To do this in VIM you have to type

:s^$/text to find/text to replace/gc

where, last 'c' stands for "confirm".

Conclusion

When I have to write an iPhone App, I must use XCode. When I'm working on a win32 platform, Notepad++ is the choice. To develop a Java application, I prefer Netbeans (and some friends of mine use it also for PHP development). I admit Emacs or VIM are fundamental to a programmer, but to use them for huge programs could be a delirium. So, as general line-guide I say:

  1. VIM or Emacs are good to write small/medium scripts
  2. For large applications, I'll use a SDK (Netbeans? Eclipse? XCode?)
  3. Emacs with cua-mode is easier than VIM, but you could have troubles with other tasks
  4. Do not be ashamed to use a simpler editor as gEdit, Kate, Bluefish or JEdit. Use what you prefear. A text-editor is just a tool. Real Programmers write programs

And happy programming! :)

Monday 18 April 2011

Nobody can choose how to trash



Today I read an interesting letter sent from Ted Evans of Techrunch to Steve Jobs. He said


Please give us garbage collection



When you say "garbage collection" you always think to Java, to its slowness and to its inefficency. I think, instead, about days spent to detect a memory-leak error, when I was near to the deploy deadline. Working with iOS is wonderfull, but can also gives you headache if you aren't enough careful. Useless to say, when you have to realize a 4000-rows-of-code in two days, it's hard to be "enough careful"!

Ok, I admit a garbage collector could makes programs slow. But if I could use it when I want and if I could dealloc objects when I want, I could realize a "slow" version in time. Slow as you want, but it will not crash because of memory overflow, segmentation faults, ecc.
In next version, with more time, I could analize the code, optimizing memory use and detecting overflows and errors.
So, yes: I would like a Garbage Collector. But activable only when I want.

PS:
Can you recognize the man on the "wanted" poster on this TechRunch article? Well, our prime minister (I am italian) isn't very admired in other countries

Monday 21 March 2011

On Arduino, Mindstorms and other Robotic stuff


Touch down!


In these months I gave some tech consults to a friend of mine whom is working on a university experiment. This experiment involve an autonomous probe sent 35 km in altitude with a baloon.
I was pretty surprised about how much funny is to work on real-time systems. Just for fun, I looked for some "robot hack", thinking how to realize a simple automatic rover. I'll talk about software details in another post: today I asked to myself what's best to use: homemade hardware with arduino board or a Lego Mindstorm kit. I think the answer is inside of you: are you able enough to drill and cut aluminum? Fix motors and actuators with bolts, taking measuring with minimum accuracy of one millimeter? If yes, Arduino is your choice.
If no, Mindstorms are better.


But wait: what about just prototyping your robot? Even if you're a master in smithing, it could be usefull to use Lego prefabricated components to realize a low-cost, reusable and easy-to-modify prototype. It will helps to test your software "on the road" without loosing your precious hardware if something going wrong. It's also useful to give a live demonstration of your robot. It could be more impressive to see something real (it's not important how ugly it looks) than looking something moving on a monitor.


Some links

Friday 4 March 2011

Tutorial: iPad Should Rotate



The iPad is a wonderful device. It's really magic. Even if you dislike (or hate) Apple, you must admit it's a very funny gizmo.
My first iPad App was refused because iPad Guidelines are a bit different than iPhone's ones. iPad hasn't got a right way to be picked on, so you must manage rotations.
Well, if your App requires just a "portait" view (as mine), you have just to add this code to every ViewController in your app

- (BOOL) shouldAutorotateToInterfaceOrientation:
    (UIInterfaceOrientation)interfaceOrientation{
        return UIInterfaceOrientationIsPortait(interfaceOrientation);
}

Thursday 3 March 2011

Never Reinvent the Wheel

In these days I'm writing a small roguelike. I started it in Java, but soon I moved to LÖVE, a framework for 2D games based on OpenGL and Lua. After some days I've got "something working" (moving camera, smooth characters, entities, doors, items) so I reflected about the "reinventing the wheel anti-pattern".
As a programmer, I enjoy to write code. I tried many times to write an OpenGL 3D engine; I tried many times to write a 2D platform game engine. But after all, is a good idea to write yourself all this code when somebody already did it?
Obviously no, but believe me: to reinvent the wheel is a strong temptation and, often, we follow it unconsciously.
Don't invent your own configuration language: there's already Lua.
Don't write your own 3D engine: there's Panda 3D, Crystal Space, Irrlicht Engine, Ogre 3D, the various ID Tech (now they're under GPL).
You'll earn time and you'll see result sooner then you think, allowing you to give more attention to the most important things of your program.

Wednesday 2 March 2011

The endless VIM vs EMACS debate

Many programmers prefer using VIM instead of EMACS. They claims it's better because it's faster, smaller and easier to configure.
What to say about?
In these days I wrote a small iPhone framework in python. It requires a TAB-separated-Table as input. I used Aquamacs to write this input file and my program crashed. Why? Aquamans inserted some tab characters, I don't know why.
I rewrite that input with VIM and all worked well.
Another "life case": I tried to install lua-mode on a Windows 2000 workstation and on my MacBook Pro. I failed in both cases. I think after two tries, a system is not enough user friendly, so i give up.
Confused? It's normal. VIM has a easier learning curve. It's boring to write tons of code, but it could be more usefull than EMACS. EMACS is good if you accept an "evolutionary model", where you write day-by-day your .emacs file.

And if you are tired, there's always JEdit :)

Wednesday 16 February 2011

No classes and objects in Lua. So...



So let's use old-C structs :D
How?
Image a struct to define a 2d point. It will looks like

struct Point2DStruct{
    double x;
    double y;
};

typedef struct Point2DStruct Point2D;

Point2D* new_Point2D(double x, double y){
    Point2D* result;
    result=(Point2D*)malloc(sizeof(Point2D));
    result->x=x;
    result->y=y;
    return result;
}

How we can do something similiar in Lua? With a table!

function new_Point2D(newx,newy)
    result={x=newx,y=newy};
    return result;
end

Easy, fast, and good looking. Well, you can also implement objects and classes using metatables. But I prefer using this "structured" way, because it's easier to read a to write.

Tuesday 15 February 2011

No Memory on iPhone?

A rumor about new iPhone nano: they could be sold without memory, downloading dinamically from network. A storage location already exists: it's MobileMe.
It could be an interesting idea: but how much will cost in Italy, where our 3G network is expensive as gold?