I am looking to get started with some 3D programming in C or C++. The problem I have is that it seems like the only tutorials I can find for Mac OS use objective C and Cocoa frameworks. I want to obtain the same environment as Windows users, more or less.
If I try to use a text editor and g++ compiler, I am missing headers, but, if I try to use Xcode, I am forced to grapple with Cocoa, which is frustrating to me. I don't really see any reason why the OpenGL/GLUT that comes pre-installed on Mac should force me to use Xcode, but it seems I can't get the header files without it.
How can I get through all of the Apple 'developer friendly' interfaces to write some old-fashioned code with full cross-platform portability?
Some portion of Objective-C is inevitable if you want to use the latest benefits of the OSX/Cocoa. The easiest way to port an existing application to MacOS would be the following:
Write the "bare bones" nibless application in Objective-C. It would only be a single AppDelegate class and a little setup in the main() function
Add the custom NSGLView descendant in your window which you create in the AppDelegate's didFinishLaunching event handler
Setup the CVDisplayLink and rendering callback in the NSGLView initialization
Use your existing OpenGL rendering code in the CVDisplayLink's callback
Now for the interesting part: where to get all of this ?
Surprisingly, a good nibless application sample is the UI for OSX's port of QEMU (yes, the emulator). Also the Apple's official GLEssenstialPractices demo shows all the information you need to set up OpenGL rendering pipeline. All the rest is up to you.
The detailed and modern introduction to system-level OSX programming can be found in the "Advanced Mac OS X Programming" book by Mark Dalrymple. It explains many things and after reading all of this I've understood most of the design decisions in the OS (it really makes you accept all the "non-standard" things if you think from the performance viewpoint).
To get through the "nibless" programming I would recommend you to read the blog posts like this one http://blog.kleymeyer.com/2008/05/creating-cocoa-applications-programatically-ie-nib-less/ The google search helps a lot.
The same tricks apply to the CocoaTouch/iOS and there are a lot of questions answered on SO, like this one Cocoa touch/Xcode - generating NIB-less graphics context
If you want to create cross-platform applications you could create a project with the Command Line Tool template.
Next, import the OpenGL and GLUT framework. This will get you a "blank" C++ project with the required OpenGL and GLUT headers.
Lighthouse 3D gives you some tips about portability and how to initiate your first project.
http://www.lighthouse3d.com/tutorials/glut-tutorial/initialization/
I have created a software layer (named cocoglut) that allows the translatation of basic or essential GLUT calls to COCOA. This library allows creating/destroying windows and register callbacks from a C/C++ application, just by using GLUT calls, without the need for nib files or for XCode project files (and can be compiled from the command line). This option uses full retina display resolution. The source is on GitHub.
I have access to both OSX and Windows. I have built my demo in Visual Studio 2010 using C++ and DirectX 10. I have read that C++ can be run on the iPhone using XCode, but that any input has to handled in objective-c.
At present there is no input so that's not an issue right now.
What are the steps I would have to take to get it running on the iPhone?
also - was not quite sure how to tag this question. by all means edit them if they're wrong.
Yes, you can run C++ code fine on an iPhone. I've released a couple games which have a large C++ component. Of course, the graphics will need to be redone.
Likely you'll need a little bit of Objective-C++ code to communicate between the UI layer and the underlying engine.
There's no DirectX on iOS (being that it's made by Microsoft) so any code that calls DirectX is going to need to be ported over to something that can be run on iOS, like OpenGL.
Other than that you'll be treating your code like a library. With a layer of objective-c that sets up the app, and calls the necessary parts of your C++ code.
Some googling has led me to believe that C++ is the best language for real-time 2D graphics programming, but since the Android is Java-based, is that still the best option? Or us the fact that I have to use NDK going to slow it down or something? My program also has a lot of scientific computing and I know C++ is best/fastest for that...
I've never done anything with the Android before so I'm really helpless right now. If I'm just going about it the wrong way, please give me other suggestions... Some other vocab I came across is OpenGL (which I have experience with, but that's more for 3D, right?) and Canvas (don't quite get this)? If I could get access to GPU-like capabilities that would be great.
Android applications are written Java, yes - however the Android NDK allows you to write performance-critical sections of your program in C or C++. From the Android NDK website,
The Android NDK is a companion tool to
the Android SDK that lets you build
performance-critical portions of your
apps in native code. It provides
headers and libraries that allow you
to build activities, handle user
input, use hardware sensors, access
application resources, and more, when
programming in C or C++.
That said, using the NDK appropriately will most likely not slow your program down.
OpenGL works for 3D and 2D graphics - if you're only interested in 2D you will want to look at using an Orthographic Projection - see glOrtho for more information. The Android Canvas, on the other hand, is the Java method for drawing raster graphics to the screen. It will let you render 2D graphics, but at a slower rate (and with frequent interruptions from the Android Garbage Collector).
Keep in mind that if you want to use C++, as of writing, there is no STL implementation available. There are, however unofficial ports that provide most of the functionality. STLPort is one that I have tried with some success. The biggest reason to move code to C/C++ is because of interruptions from the Android Java Garbage Collector - if you're not overly careful with your code, it will interrupt your program frequently to clean up objects you've left lying around. In practice this can limit game or simulation framerates drastically.
All that said, I would strongly recommend you look into one of the few open source android game engines that are cropping up. The best one I've tried is libGDX. It takes care of all the messy NDK details and lets you code your game / simulation purely in Java. It automatically runs the performance-heavy parts of the game engine in native code to get the fastest possible performance with the ease of coding in Java. Best of all, you can write your application code once and have it automatically run on Windows, Linux, OSX and Android - which makes testing your applications much, much easier than using the Android Emulator.
If you really want to look into the NDK yourself, or need to have really fine control on what OpenGL is doing, I would recommend you download the Android SDK and NDK, get eclipse set up, and then start with the NDK samples. There is an OpenGL demo there that shows you how to get everything set up. Another good starting point would be the SpinningCube google project.
EDIT: I'm not really sure if what you mean by 'GPU-like capabilities', but With libGDX, you can compile vertex and fragment shaders under OpenGL ES 2.0 - you could use this to run embarrassingly parallel code using the device's GPU.
You're making the assumption that the Android system will be too slow to do what you want, without any data to back that up. Write some tests in Java, and test out the performance first. You don't want to make assumptions about performance without any basis.
Premature optimization is the root of
all evil. - Knuth
How to do it: C-programmers guide to Java and JNI in Android
Good question, I put myself like 1½ years ago, found it very annoying. I feel a lot for you!
And because of this I like to give a hands on answer.
But look it is easy if you follow this guideline step by step (you will have much less struggle). I did it.
Learning this, you can in no time learn easily write Java Netbeans GUI with JNI apps (for any other OS) also, you are in the Java GUI JNI development world?
The basis is “knowing C/C++ very well, not knowing Java or Android programming at all, coming from for instance the Win32 SDK culture and making the first Android app”.
Android GUI is Java – you have to yield
Thing is that the GUI of Android is Java. Java like your app and the app is more or less in practice a piece of the GUI/OS environment completely integrated calling Java GUI SDK code all the time. The debugger even goes into the Java GUI SDK source code (feels like going off road, real odd for a Win32 SDK programmer).
In short, no way writing any Android apps, but in Java.
But it is pretty easy in anyway, you got the JNI
There are three very good shortcuts:
The Android sample library (is very rich), especially the Hello JNI sample is really important
The fact Java is in syntax very much alike C/C++ (but very different in its soul, read below, you need to understand)
You got the JNI C-code opportunity in doing the heavy work (and you major share of the code)
You have to write a small Java Android GUI user interface for your app (by thieving form samples) and the major part JNI, where you write your C/C++-code doing all the work.
Start with the Hello JNI sample program
Install Java and Android Studio and that it is all you need in tools (available in Win10, OSX or Ubuntu, they are the same).
Start Android Studio and its generic picture is a menu, select the lowest, Import an Android code sample.
The main sample program for a traditional C/C++ programmer is Hello JNI. Select it and say next to everything.
The Android Studio editor will open with the sample code (chews somewhat first see status line low).
Up to the right you (might need to push the Android tab to) see the content of your project Hello JNI, app. It consists of:
Manifests (app declaration, today of minor importance because most is now in Gradle script declarations (below))
Java tab for the Java files
Cpp-tab for your C-code files
Resources (texts, menus and icons)
Gradle compiling scripts
C-make-files
The Android studio might ask you for installation plugin upgrades, say yes to everything until it is done (see status line below).
Then run the Hello JNI sample code, to make certain your installation is OK
Pushing the green arrow in a cogwheel (mid top).
You get a dialogue box. Here you can try the sample on:
A physical Android device (via a cable from your PC, might need a Samsung win driver from their support)
A HAXM Android unit emulator (is reel good)
But you need to switch off the Hyper-V feature (used for XP emulation) in Win10 to make it run. Android studio might do it automatically for you asking, else control panel/Programs and functions/Windows functions.
Select Create new Virtual device. You might need to do some installation plugin upgrades more, say yes to everything.
When the Hello JNI app is working in the device, you have an OK installation.
C-code, check hello-jni.c
Look in the cpp-folder Up right) and you will find a C-file with a function.
Here and in any other C-file you can fill it with regular C-code.
Everything but the ANSI C locale features are supported (must get locale data from Java and transport them to the C-environment).
You need to edit the CMakeLists.txt file to add any additional C/C++-files (it is the makefile, look up to the right under the Gradle scripts).
Memory heaps, Java with JNI and C are different
You must be aware that everting in the data jstrings delivered by the JNI interface is located in the Java memory heap. This is also the case after the data has been in delivered C-format by JNI.
You will have fuzz if you don’t copy string data (and arrays) from the Java heap to the C-heap. Make a basic function like this to copy the data and release the JNI data:
char *GetStringfromJniString(JNIEnv *env, jstring jniString)
{
const char *TempString = (*env)->GetStringUTFChars(env, jniString, 0);
char *String = calloc(strlen(TempString) + 1, sizeof(char));
strcpy(String, TempString);
(*env)->ReleaseStringUTFChars(env, jniString, TempString);
return String;
}
When you send data to Java jstrings you need to contain it to the JNI transport by using the
return (*env)->NewStringUTF(env, pCstring);
Long funny names of the JNI-functions
The function name is Java_com_example_hellojni_HelloJni stringFromJNI(…).
There “Java” is always there
com_example_hellojni means it communicate with your app in the Java com.example.hellojni Java package (read about it in the tutorial)
The HelloJni is the name of the Java file the JNI function is declared
And stringFromJNI(…) is the actual function name (functions in Java are called Methods).
If naming is OK the declaration of the C JNI-functions in the Java file is black text, if not red text. (Give it a chance, it stores and checks the code continuously in the Java environment, might take a few seconds to verify it is right (becoming black).
You must declare the C JNI-functions in java
In the Java file here HelloJni.java the JNI-functions must declared as Java Methods (functions in Java).
public native String stringFromJNI(…);
Where return values and function/method switches are the same as in C.
After that you can use the JNI-functions as Java methods as they are declared in the Java declaration. Note that char * is String in java.
Constants must be declared on both sides
#define CONSTANT_A = 24
Is in Java
static final int CONSTANT_A = 24;
And you can use them in Java and in C as usually.
if(!variable) does not work in Java
You need to do
if(variable==0)
Test other samples
The sample library is full of fully nice functioning Java samples. Try them one by one until you find a user interface of your like. It is actually nice to test samples.
There are a lot of sample in Github but a lot of them need fixes of Gradle scripts or comes from Eclipse (the past Android dev environment) and is harder to try (often need fixes). Other Github sample are just fragments and a lot of work making them working. Of course nothing for a Java Android star but we are not there yet. But Github a very good even larger source of samples.
Copy the Java stuff into your Hello-JNI
Found anything nice, copy it!
Theft always pays as JS Bach used to do!
It is quite easy to just copy stuff from one sample to another. When copying in the Android studio, Android studio adds automatically the includes (asking first) and just accept. Sometimes it does it asking you to enter Alt-Enter, do it.
Editing several projects at the same time
The File/Open recent - Own windows-command, make you having two projects up at the same time, easier to copy stuff in between.
Making your own application
After some testing you create your own app.
Go to the generic Android studio picture (file/close project), select Start a new Android Studio project. Put your Package tree data (think that over, read about it in the tutorial) and create it. Then copy the stuff you tested to your own app.
Learn Java, the Android developer tutorial is real good
Now you have made your environment working and spotted some good user interfaces, learn Java!
The Android Studio tutorial is real good. Try it, and soon after a few lessons you will be able to play with Java code and very soon you are a Java programmer.
Getting stuck, google and you find most answers in Stackoverflow, just search.
You are never the first with a problem and Stackoverflow is real good. Read all the answers and think, you find what fits you best.
The Java environment and Android OS is very different from Windows
You need a feeling for the culture and environment. Like a Swede coming to Norway, the same food taste slightly different, even if it looks the same.
The major difference is that in Java nothing crash as good as in pure C in a C environment. Finding bugs can be hell because of non-consistent crashing. The program continue to run and crash later, but you find the bugs.
There are Java log files for debugging, but it takes quite an effort to penetrate it. Like learning the assembly code in a Windows debugger.
A good idea is to really debug the C-code in a pure C-environment first and handle the Java stuff in Android Studio.
I can amend C-code in Android Studio but writing a large chunk of C-code is really looking for troubles debugging it. Like asking a Norwegian make a Swedish dinner, impossible to get it right, looks the same but taste different. Nothing is wrong but different culture.
The soft crashing style of Java includes even security soft crash features like the try/catchy/finally stuff. (You have to google and read about it yourself. It is unbelievable for a hard core C-programmer, but it is real.)
There are no h-files and #if-compiler switches in Java
Instead of h-files and common declarations of functions/variables as in C in java you have to refer to them with a filename followed by a dot and the method name, using them from another file.
If declared private variables and methods can only be accessed from the same file, with public can be used with file reference. Static is something very different in Java.
There are no compiler #if-switches so you need to use regular variables/constants (for constants put final before in the declaration).
There are no pointers in Java, but there are strings
There are no pointers in Java but a String (and array) variable is in fact a char pointer
But you can’t handle pointers as in C with * and &, but in JNI you certainly can. The memory management is completely different, nothing is fixed in Java.
You don’t need to free data, Java wipes it all for you
Java cleans unused data automatically.
You don’t free data just delete (=0;) the string-“pointers” or just leave the method (Java function) and it cleans it.
So if you have a string pointer and just replace it with something else the old stuff is automatically wiped.
The Java environment lives its own life, but the Android studio debugger includes a monitor of the data used.
Size limitations of Java apps and JNI code
Good to know is that there are some restrictions in how large a Java app can be (but as usual nothing is written in stone in Java) but the JNI heap can be as large as the HW can take.
But you are not alone in a phone, an incoming call might come and you don’t want to mess that up? If you clean well it is a larger chance your app is intact returning to it and not killed.
Android apps don’t die by exit
If you exit an app, the app is still running even though the GUI is gone (like in Macintosh). Thing is that you have to be aware of that, read about it if you make serious apps.
Android kills apps
But on the other hand you can never trust that an app is not wiped by the OS and you have to rebuild. If the OS need memory it just starts to kill apps. But the JNI part usually survive and you can store data there for a recovery.
There is only one JNI instance (WinSDK term) but there might be many Java instances
And them all connect to the same JNI at the same time. Only one app instance can be active (shown) but the others might make background tasks. In JNI you need to keep track of them.
Normally there is only one Android instance of every app. But if you start the app by a work files from for instance like an email client, you in fact have another instance of the app (with its own memory and everything (but the JNI that is in only one common instance)).
Starting an appended file there will be two or more Android instances. Each related to each email instance, by the intents procedures (learn about intents in Android tutorial).
A smart thing is keeping track of the instances by number them when the app is created if savedInstanceState == null.
Copy data to the emulated unit
You might need to upload some data to test your app. In a regular phone you just attach the USB-cable and enter the Phone storage from Windows explorer. With the emulated units you must use the adb-terminal script functionality (can be used in bat-files in Windows and a copy bat file looks like this:
PATH=%PATH%;C:\Users\You\AppData\Local\Android\sdk\platform-tools
adb push C:\Users\You\Documents\v.txt /storage/emulated/0/Android/data/com.yours.app/files/v.txt
Very limited privileges to read and write files
Thing is however that you are only allowed to open your own files stored by your app in the emulated SD card. Else nothing comes reading them.
Use standard ANSI C file functions rather than Java
Best in a C-heavy app is to use standard C- file functions, as a C-programmer you are used to them and their behavior.
The C-file functions works fine and is easier to get binary code or transparent text files with ctrl-characters etc (Is not allowed in Java Strings but in java bit arrays). Take some effort learning Java file handling if you try that.
JNI performance is no issue
The C-code is much faster than the Java code so a lot of developers not from the C-culture must make JNI-functions and pure C-code to perform in heavy apps, needing performance. In short JNI performance is no issue.
However I have the impression that the Android GUI is faster than the WinSDK GUI in many cases. Working with ListViews and sometimes very long lists, Android is far better optimised. It looks like WinSDK is loading every line of the list, but Android only the lines visual, and in long lists it makes a huge difference.
I am impressed by performance in Android even with tiny ARM processors. Apps do perform very well. Intel Android units are real good in performance. I wouldn't hesitate trying to make a heavy duty app in Android, as long as I use JNI. Does not come short compared to Windows.
But notice, that running HAXM in an Intel PC the Intel emulated units runs much faster than the ARM than in real HW-units, because it runs intel code straight and not over an emulator layer as with ARM emulated units.
I am amazed that the Android x86 is not marketed heavier as a competitor to Windows on netbooks, for the common man, doing some documents, some calculations, email and Google? I believe the Crome OS is too alien for most people and most people have Android phones, feeling home. As a SW vendor I would like to see Android netbooks marketed much more and I would put more effort in SW development. Same thing with the Android TV without menus is closing the market for just the old Smart-TV approach. Something I can't understand having a boxed Win10 on the back of my TV since years, using a lot of programs there. The lack of Android graphics driver for the Raspberry-pie is also hard to understand, one with a regular Android would be smashing attached to the TV. In Sweden where I live, people under 40 don't watch aired TV, they stream, public service play, YouTube and Cable TV operators packages over internet. Having regular Android in the TV would generate usage of a lot of other apps. I see a market opportunity for SW vendors is closed. This when MS is making suicide with leased SW (normal people don't pay) mixing the culture of large accounts with the common man, a huge marketing window is opened. And the Android app performance is real good and nothing to worry about. It is not performance but marketing policies that limits the app market.
Quick you become a Java and Android master
You will be surprised how easy Java is for a C-programmer, doing it the right way (like following this guideline).
It might be slightly harder for a C++-programmer because the Classes in java and C++ culture looks to vary, might be easier to learn Java classes from scratch? But if you see C++-code in Win32 and OSX environment they look usually from different planets, so they might use to it.
The major disadvantage is that you wouldn’t get a diploma for your CV as doing a programmers course?
Does any one know how porting is done. Actually I want to port cocos2d to Qt platform & I have no idea about this. If anyone know please help me.
What you want to look at is not Cocos2D itself (which is pure python and will require a platform for which Python is available, including OpenGL / SDL functionality), but rather Cocos2D-X. That provides a cross-platform C++ API closely mapped to Cocos2D's python interfaces.
See:
http://www.cocos2d-x.org/projects/cocos2d-x/wiki
and specifically for Symbian / "Qt Platform", check:
http://www.cocos2d-x.org/boards/6/topics/373
Cocos2D-x already works with Marmalade, so if you want to take your Cocos2D game to iOS, Android, Symbian, webOS, bada, RIM QNX and more, check out Marmalade:
http://madewithmarmalade.com
I want to create a C++ UI framework (something like QT or like ubuntu unity Desktop)
How is programmed , is it using OpenGL or lets take plasma ui of QT (how is this programmed )?
Direct answers , reference links anything will be helpful.
Some interesting opengl based UI I founf on the web
LiquidEngine
http://www.youtube.com/watch?v=k0saaAIjIEY
Libnui
en.wikipedia.org/wiki/Libnui
Some UI frameworks render everything themselves, and work based on some kind of clipping-window-within-the-host-systems-screen. Non-display aspects (such as input event handling) have to be translated to/from the host systems underlying APIs.
Some UI frameworks translate as much as possible to some underlying framework.
wxWidgets can do both. You can choose a native version (e.g. wxMSW if you're on Windows) and most wxWidgets controls will be implemented using native Windows controls. Equally, you can choose the wxUniversal version, where all controls are implemented by the wxWidgets library itself.
The trouble is that typical GUI frameworks are huge. If you want a more manageable example to imitate, you might look at FLTK. I haven't got around to studying it myself, but it has a reputation for being consise.
There are also some GUI toolkits that are specifically aimed at games programming, such as Crazy Eddies GUI. My guess - these are probably as idependent of the underlying API as possible, so that particular applications can implement the mapping to whichever underlying API they happen to target (OpenGL, DirectX, SDL, whatever) and can be the boss of the GUI rather than visa versa.
http://www.wxwidgets.org/
http://www.fltk.org/
http://www.cegui.org.uk/wiki/index.php/Main_Page
"no really, don't write your own wm or toolkit"
The #Xorg-devel guys on irc.freenode.org
doing one anyway means that you have to test against a wide range of more or less buggy WMs and X implementations, and that you have to frequently update to be compatible with the latest Xorg server and X protocol features (like Xinput 2.1)
understandably, the Xorg people are tired to support old, unmaintained toolkits and applications. They already have enough bugs.
The GUI frameworks are very dependant on a windows system, which dictates what is allowed and how windows are created and rendered. For example, pass a specific option to create a borderless or full-screen window.
Since you mentioned opengl and ubuntu, I guess you want to start on a linux platform. You should study xlib, for which you can find reference here.
Since the qt library is open source, you can download it and peek into it's sources.
A UI library isn't developed from scratch. It relies on the OS' windowing system, which relies on the driver from your graphics adapter, which relies on the OS kernel, which relies on... and so on.
To develop any software "from scratch", you can start by writing your own BIOS. Once you're done with that, move on to writing an OS, and then you should be just about ready to write the software you wanted. Good luck.
And this is assuming you're willing to cheat, of course, and use a compiler you didn't write from scratch.
Before you do that, it's worth that you spend one week on thinking:
1, Do you really know how to do it? I doubt that.
2, Do you really need to do it? I doubt that too.