You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2017-06-08-ios-refactoring.md 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. Title: iOS tutorial
  2. Date: 2017-06-08 10:00
  3. Category: News
  4. Slug: ios-tutorial
  5. Lang: en
  6. ![Earth and a rocket](../../images/2017-06-08-ios-refactoring.png)
  7. This article describes problems we faced during the creation of iOS tutorial in May 2017.
  8. [This February](https://twitter.com/OpenGameStudio/status/826816343433498627) we managed to get simple model rendered under iOS in just a few days. We expected to finish iOS tutorial in no time. However, the reality reminded us: it's easy to come up with a hackish demo that works for one person, but it's hard to create a concise example that works for everyone.
  9. **Native library**
  10. The first question we had to answer was: should the sample application be part of Xcode project or be a separately built library?
  11. We had to consider the following facts:
  12. 1. Xcode project can use C++ directly (thanks to Objective-C++) without stuff like JNI
  13. * There's no need for a separate library (+ application)
  14. * Creating a separate library is an additional work (- library)
  15. 1. OpenSceneGraph builds libraries
  16. * It's easier to use standard build process (+ library)
  17. * It's harder to create custom build process just for a single platform (- application)
  18. 1. OpenSceneGraph uses CMake build system, which is not supported by Xcode
  19. * Xcode project can't include CMake files (- application)
  20. * It's easy to create custom CMake file that includes OpenSceneGraph CMake file to build a single library (+ library)
  21. 1. CMake can generate Xcode project
  22. * It's possible to create a CMake file that builds both OpenSceneGraph and the sample application (+ application)
  23. * Xcode is the de-facto tool to create Xcode projects; it's easier to use standard build process (+ library)
  24. After evaluating the pros and cons of each approach, we decided to turn the sample application into a library and include it in Xcode project. The downside of this approach is that simulator and real device builds need separate library builds.
  25. **Refactoring**
  26. The second question we had to answer was: should there be a single source code base for all platforms or several ones, one for each platform?
  27. While doing Android tutorial we used single source code base because it worked fine for desktop and Android. As we started to work through iOS tutorial, it became apparent that particular features may or may not work on some platforms. For example, one feature may work on desktop and iOS, but not Android. Another feature may work on iOS and Android, but not desktop. Since we didn't want to pollute the code with #ifdefs, we started to put each platform combination into a separate file. The number of files grew rapidly. The files were reusable, but it became extremely hard to see the whole picture.
  28. At this point, we realized there's the second question. We reminded ourselves that the main purpose of the sample source code is to teach how to do basic OpenSceneGraph things, not create a reusable library with API that is stable across several years.
  29. That's when our home grown feature tool came into play. With its help, we separated the code into several parts, which in the end produce just two files for each platform:
  30. 1. functions.h - contains reusable classless functions
  31. 1. main.h - contains the rest of the sample application code
  32. Their contents differ slightly for each platform, but it's easy to see the whole picture now.
  33. That's it for describing problems we faced during the creation of iOS tutorial in May 2017.