Buy Me A Coffee!

GoPay

Wednesday, October 23, 2013

Today's Lab Of Cocos2d-X

I was experimenting to deploy my Cocos2d-X’s projects lab to my Android device. Surprisingly there were so many obstacles and ridiculous things bumped me. Here are 4 of them:

1. Compile configuration to Android

The configuration itself is not a piece of cake, especially on my newbie mind. Here are the step that you have to do

Part 1. Eclipse configurations


  1. Make sure you have your project (of course you have, you won’t compile it before you have something to compile, will you?)
  2. Import them into your Eclipse (the proj.android folder)
  3.  Import Cocos2d-X library for Android. It’s in [Your Cocos2d-X folder]/cocos2dx/platform/android/java.
  4. Right click your project (NOT THE LIBRARY), and select properties. Select “Android” tab and make sure you have ticked the target.
  5. Open project.properties, set the “target” to your target platform. For example I make mine this
  6. Open AndroidManifest.xml, and set the android:minSdkVersion to 14
  7. Right click your project folder, select “Android Tools” > “Fix Project Properties

Part 2. Compile commands configurations

  1. Go to your project’s android folder (proj.android folder).
  2. Open “build_native.sh” in your favorite text editor and at the top add this line
    NDK_ROOT="/Users/YOURUSERNAME/Development/android-ndk-r8e"
    a friend of mine told me it was unnecessary. Well it is necessary for Mac (I don’t know how to configure NDK_ROOT variable in Bash for default, but anyway, it works).
    Notes: As you can see I am using r8e version of android NDK. This is the only version that can compile Cocos2d-X 2.1.4 right.
  3. Head to “jni” folder
  4. Open “Android.mk” file. Make sure you have put all your class in LOCAL_SRC_FILES (it will contain HelloWorld and AppDelegeate class for default). This is the example from me:
    LOCAL_SRC_FILES := hellocpp/main.cpp \
                       ../../Classes/AppDelegate.cpp \
                       ../../Classes/Ball.cpp \
                       ../../Classes/GameScene.cpp \
                       ../../Classes/MenuScene.cpp \
    
  5. Open “Application.mk” and add this line for targeting your desired platform
    APP_PLATFORM := android-17

Part 3. Compiling


  1. Back to Eclipse. Select your folder, and run it. It will take a moment, so grab a snack or coffee. BEWARE OF ERROR (Google is your friend. No. Best Friend. No. It's your wife).
  2. Select your target device and scream in joy because you’ve deployed your app in your device.

2. Directly instantiate the initialization

So I have this game that need background of white. I simply add white layer first
CCLayerColor *layerWithColor;
layerWithColor->create(ccc4(255, 255, 255, 255));
addChild(layerWithColor);
Well, that’s looks okay. It’s compile smoothly on my Mac target, but no in Android target. The error is trivial and pops up when you run the game. You know what? You have to do this instead:

CCLayerColor *layerWithColor = CCLayerColor::layerWithColor->create(ccc4(255, 255, 255, 255));
addChild(layerWithColor);
That's insane!

3. No images in my device

So I was using a sprite sheet that sized 3000x500. But unfortunately in my device it won’t drawn and shown as black box instead. Why? Because the LogCat told me that:

cocos2d: WARNING: Image (3000 x 500) is bigger than the supported 2048 x 2048

4. Configuring path to Box2D

One of my experiment using Box2D. To compile it you have to tweak “Android.mk” file and add Box2D in your LOCAL_C_INCLUDES. I’ve found mine like this:
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes \
                    $(LOCAL_PATH)/../../../../external/Box2D \

UNSOLVED

Can’t call parent destructor from override

I’ve try to make game in Box2D and it require manual deallocation being called in destructor. In Mac target, I can write it like this

GameScene::~GameScene(){
    delete _world;
    _body = NULL;
    _world = NULL;
    CCLayer::~CCLayer();
}

But the compiler won’t accept ~CCLayer();. Hmmh… I don’t know why. If you know about this issue please tell me ASAP.

So that’s it! Enjoy your journey of Cocos2d-X!

Oh by the way, I am taking Game Programming course and using LibGDX. This engine is so simple and powerful. Have you ever using it? It’s in JAVA. Give it a try; you’ll love it too.

Saturday, September 28, 2013

C++: Header Guard

Pas praktikum PBO dan lagi pakai C++ ada praktikan yang pake #include dan error. Katanya ada kelas yang redeclare! Ooh dan ternyata harus pakai Header Guard supaya nggak error.

Dalam C++ untuk pembuatan kelas dibagi menjadi dua, yakni header dan implementasi. Misalnya kita memiliki kelas-kelas sebagai berikut:
Kendaraan
Mobil
Motor

Berarti kita akan membuat 6 file seperti ini:
Kendaraan.h
Kendaraan.cpp
Mobil.h
Mobil.cpp
Motor.h
Motor.cpp

Misalkan Motor dan Mobil masing-masing inherit Kendaraan. Berarti kedua kelas tersebut harus di "include" ke file Mobil dan Motor kan? Nah disini masalahnya

Case Study


Dimisalkan kita memiliki kelas Kendaraan sebagai berikut

Kendaraan.h
//Kendaraan.h
class Kendaraan{
	private:
		int kecepatan;
	public:
		Kendaraan();
		~Kendaraan();
		void setKecepatan(int kecepatan);
		int getKecepatan();
};

Kendaraan.cpp
Kendaraan::Kendaraan(){
	
}
Kendaraan::~Kendaraan(){
	
}
void Kendaraan::setKecepatan(int kecepatan){
	this->kecepatan = kecepatan;
}

int Kendaraan::getKecepatan(){
	return this->kecepatan;
}

Dan kelas anaknya adalah sebagai berikut

Motor.h
//Motor.h
#include "Kendaraan.h"

class Motor:public Kendaraan{
	private:

	public:
		Motor();
		~Motor();
};

Motor.cpp
#include "Motor.h"

Motor::Motor(){

}

Motor::~Motor(){
	
}

Mobil.h
//Mobil.h
#include "Kendaraan.h"

class Mobil:public Kendaraan{
	private:
		
	public:
		Mobil();
		~Mobil();
};

Mobil.cpp
#include "Mobil.h"

Mobil::Mobil(){

}

Mobil::~Mobil(){
	
}
Dan program main nya seperti ini Main.cpp
#include <iostream>

#include "Motor.h"
#include "Mobil.h"

using namespace std;

int main(string args[]){
	Motor motor;
	Mobil mobil;

	return 0;
}

Ketika di compile, yang terjadi adalah error seperti ini

In file included from Mobil.h:1,
                 from Main.cpp:4:
Kendaraan.h:2: error: redefinition of ‘class Kendaraan’
Kendaraan.h:2: error: previous definition of ‘class Kendaraan’

"redefinition of class"! Berarti kita mendefinisikan kelas lebih dari sekali. Ya, soalnya kita sudah mendefiniskan kelas Kendaraan di Motor (dengan #include "Kendaraan.h") kemudian di-include lagi di Mobil, walhasil jadinya dua kali include.

Nah karena itulah kita memerlukan Header guard.

Header Guard

Header guard, atau Include Guard merupakan directives, yaitu perintah yang diawali # dan diakhiri dengan baris baru.

Nah, kita bisa membuat Header Guard dengan memberikan directive berikut di awal tiap header


#ifndef KENDARAAN_H
#define KENDARAAN_H

//Kendaraan.h
class Kendaraan{
	private:
		int kecepatan;
	public:
		Kendaraan();
		~Kendaraan();
		void setKecepatan(int kecepatan);
		int getKecepatan();
};

#endif

directive yang kita tulis menyatakan jika belum didefinisikan (#ifndef) maka definisikan (#define) kelas kita. Jangan lupa beri #endif di akhir

beri header guard ini di setiap header kelas yang kamu buat. Dengan pemberian header guard ini kalau kamu #include kelas lebih dari sekali nggak ada lagi issue redefinition of class

referensi:
http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/
http://en.wikipedia.org/wiki/Include_guard
http://en.wikipedia.org/wiki/Header_file

Tuesday, September 24, 2013

Sebuah Tugas SI: Frameworks, Business Process, dan Jobs

Mata kuliah Sistem Informasi mengajarkan peran sebuah software dalam sebuah sistem. Bukan berarti sebuah software adalah sistem ya! Software merupakan alat dalam "sistem informasi" yang saling terkoneksi dalam sebuah institusi.
Ternyata Sistem Informasi menarik untuk dikaji lebih mendalam. Saya baru tahu bahwa perusahaan dalam menerapkan sistem informasi menggunakan framework. Framework adalah bagan rancangan penggunaan sistem informasi yang teratur. Ahli-ahli sistem informasi telah menerapkan framework-framework dan digunakan luas oleh perusahaan-perusahaan besar.
Saya kurang begitu ngeuh juga sih... Saya kurang bisa menangkap cepat kalau mata kuliahnya sangat teori. Tapi begitu lah ya pokoknya... mudah-mudahan kamunya nangkep.
Berikut ini adalah tugas mata kuliah SI untuk mencari istilah-istilah yang ada dalam SI.

Just check it out

Thursday, September 19, 2013

Finally got the Cocos2D-X running

Kyaa Kyaa

The problem on libcocos2dx when imported to eclipse is the target was aimed to 10, but my SDK need 17. So, just open up project.properties in your libcocos2dx and change the Project Target to your desired version (mine is 17).

This is Cocos2D-X 3.0 by the way. I can't resolved the error when using ./build_native.sh on 2.1.4 version. The error was something like this:

/proj.android/../../../cocos2dx/platform/android/CCCommon.cpp:54:77: error: format not a string literal and no format arguments [-Werror=format-security]

I've googled around and some of the forums told me that it was the NDK problem. It can't be built in r9 neither r8. Now I'm downloading the r8e version.

Phew. The latest version work anyway, so that's it, let's jumpin' into Cocos2D-X awesomeness.



Saturday, September 7, 2013

Beginning OpenCV

OpenCV adalah library untuk memanipulasi citra visual. Dengan OpenCV kita bisa memanipulasi gambar tanpa harus coding di low-level (misalnya load gambar kemudian ekstraksi decode dari file gambar, dsb).

Kenapa ingin menggunakan OpenCV?

I've ever try to manipulate image directly from its binary file. Cuma pengen bikin gambar 2x2 pixel yang isinya titik doang. Iye, pengen ngedecode hex file nya si jpg. Bump! Ternyata nggak gampang (nggak ngerti), mwahaha! Kemudian dari hasil searching di internet OpenCV lah jawabannya untuk mempermudah manipulasi gambar.

Kemudian bidang citra visual sangat menarik untuk diteliti. Mata manusia yang bekerjasama dengan otak manusia menghasilkan informasi baru sangat misterius! Bagaimana manusia bisa mengetahui sebuah benda walaupun sebagian benda saja yang tampak? Bagaimana manusia bisa memprediksi pergerakan balon? Darimana manusia mengetahui ada benda-benda di atas meja? Seperti apa manusia mengenali gerakan-gerakan tangan seperti menunjuk dan melambai? Kok bisa manusia mengetahui wajah yang murung, sedih, ceria? Itu semua misteri! Dari pandangan ilmiah hal ini bisa dipelajari di sub bidang Computer Science yakni Computer Vision.

Dan.. eum... Saya ingin memulai penelitian saya mengenai pengenalan objek dari citra visual (uwow, skripsi uwow).

Persiapan OpenCV


Tutorial installasi saya dapat di https://sites.google.com/site/learningopencv1/ yang tampaknya web mata kuliah tentang Computer Vision dari Harvey Mudd College (HMC). Kebetulan juga OS yang dipakai OSX. Makasih banyak ya yang udah bikin. Disini saya tambahkan beberapa hal dari pengalaman saya
Saya pernah mencoba menginstall di OSX Snow Leopard, tapi ternyata gagal. Setelah saya upgrade jadi Mountain Lion sekarang bisa, kurang tahu kenapa.

Pertama-tama download dulu OpenCV. Versi yang saya download setelah menulis post ini adalah Opencv 2.4.5. Kamu bisa mendownloadnya di http://opencv.org/

Pastikan juga kamu sudah memiliki Macports. Macports adalah package management system untuk Mac untuk aplikasi-aplikasi open source. Downloadnya bisa di http://www.macports.org

Sudah oke? Mari kita lanjutkan

Build OpenCV


  1. Ekstrak OpenCV, taruh di /Users/userkamu, dan rename foldernya menjadi opencv
  2. Buka Terminal
    1. Pertama-tama update dulu port kamu:
      sudo port -v selfupdate
    2. Kemudian instal subversion:
      sudo port install subversion
    3. Kemudian install cmake. Saya sendiri skip yang bagian cmake soalnya setelah memasukkan command cmake di terminal ternyata sudah ada. :
      sudo port install cmake
  3. cd ke folder opencv.
  4. Buat folder build:
    mkdir build
  5. kemudian ketik ini:
    cmake -G "Unix Makefiles" ..
    make -j8
    sudo make install

Project XCode

  1. Buka XCode, pilih New Project > Command Line Tool
  2. Pilih Type C++, beri nama proyeknya dan simpan
  3. Klik project, pada tab Build Settings, cari header "Search Path".
    1. Pada Header search path isi: /usr/local/include
    2. Pada Library search path isi: /usr/local/lib
  4. Klik kanan project di sisi kiri (tab project navigator), klik New Group, beri nama OpenCV Frameworks
  5. Klik File > Add Files
  6. Ketik /usr/local/lib
  7. Pilih libopencv_core.dylib dan libopencv_highgui (Pilih banyak sambil tekan CMD). Kedepannya bila kamu memerlukan library tertentu bisa memilh lagi
Sekarang kamu bisa mencoba memulai coding. Ini contoh kode dari HMC yang bisa dipakai

#include <"iostream">
#include <"opencv2/opencv.hpp">

int main(int argc, char *argv[])
{
    // Open the file.
    
    IplImage *img = cvCreateImage( cvSize(100,200), IPL_DEPTH_8U, 3); //if (!img) {
    
    //    printf("Error: Couldn't open the image file.\n");
    //    return 1;
    //}
    
    // Display the image.
    cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
    cvShowImage("Image:", img);
    
    // Wait for the user to press a key in the GUI window.
    cvWaitKey(0);
    // Free the resources.
    cvDestroyWindow("Image:");
    cvReleaseImage(&img);
    
    return 0;
}


Command+B untuk Build, Command+R untuk Run. Hasilnya seperti ini:

Next Step

Yeeey! Jalan! Sampai sejauh ini saya bisa memasukkan input kamera dan mencoba efek blur pada gambar. Waktu menggunakan efek blur sempat tidak ditemukan methodnya, ternyata lupa untuk memasukkan library (ingat kan saat memasukkan library ke dalan group "OpenCV Frameworks"?).

Sekarang tinggal nyari tutorial buat belajar fitur dasar library ini terus belajar metode-metode Computer Vision deh. Ganbare!
nyoba pakai kamera


Sumber:
https://sites.google.com/site/learningopencv1/installing-opencv
http://tilomitra.com/opencv-on-mac-osx/#comment-1138