0

I´m trying to adapt a particular sourcecode-example, but have trouble to understand how it keeps control over the main-loop:

// Combine some macros together to create a single macro
// to launch a class containing a run method
#define RUN_OVR_APP(AppClass) \
MAIN_DECL { \
if (!ovr_Initialize()) { \
  SAY_ERR("Failed to initialize the Oculus SDK"); \
  return -1; \
} \
int result = -1; \
try { \
result = AppClass().run(); \
} catch (std::exception & error) { \
SAY_ERR(error.what()); \
} catch (std::string & error) { \
SAY_ERR(error.c_str()); \
} \
ovr_Shutdown(); \
return result; \
}

I know stackoverflow doesnt want links, but in the this particular case it might be necceassary, in order to get more information on the used commands: https://github.com/OculusRiftInAction/OculusRiftInAction/blob/master/examples/cpp/common/ovr/OvrUtils.h

Echo88
  • 15
  • 7

3 Answers3

1

It's horrible and I wouldn't use it, but it's pretty clear.

It: (1) Tries to initialise and returns -1 if it can't and then (2) calls AppClass.run() and returns the result it gets back from it before (3) shutting things down. Plus a little spot of exception capture.

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
  • Ah, i overlooked the int result reference. now it makes sense. thanks :-) – Echo88 Jul 17 '15 at 21:48
  • _Why_ did you write the macro? And _why_ did you write it like that rather than using any of C++'s language features that would have let you write it without using a macro? – Jack Aidley Jul 18 '15 at 09:32
  • I explain exactly why in my answer. If you feel there's a better way, perhaps you could expand your answer with an example of an alternative. I'd be interested in your solutions. – Jherico Jul 18 '15 at 16:08
0

Examining the macro definition by looking its implementation is not enough to understand how it is used in the large code bases due to the "copy-paste" nature of macros.

My suggestion is that acquire the preprocessed file and examine where and how the macro is used.

For acquiring the preprocessed file please refer to the below discussion.

How do I see a C/C++ source file after preprocessing in Visual Studio?

Community
  • 1
  • 1
Validus Oculus
  • 2,756
  • 1
  • 25
  • 34
-1

The code is part of a set of example applications for the Oculus Rift, designed so that each example application can basically define a class with a run method and then include the macro so that the class can be executed regardless of platform, and with the Oculus SDK initialized for the lifetime of the program.

i.e.

class DemonstrateTechnique {
  int run() {
    ...  do stuff ...
  }
}

RUN_OVR_APP(DemonstrateTechnique)

Here is an actual example of the macro being used.

The macro isn't really meant to be exemplary of anything. It's merely a convenience macro so that the author (me) didn't have to write a bunch of boilerplate main functions that instantiated a class and initialized an SDK.

The point of the init/shutdown methods inside the macro are to ensure that the Oculus SDK being initialized has a greater scope than the application itself. This is actually critical for versions of the SDK prior to 0.6, because in older versions if you initialized the SDK after you created your OpenGL context, it was impossible to get Direct HMD mode to work.

Jherico
  • 28,584
  • 8
  • 61
  • 87
  • I was a bit confused by the run-method because i couldnt find "int run()" in the Example_13_4_StereoWebcamDemo.cpp. As i am a c++-beginner i wanted to adapt the code, so that i can use the WebcamApp-class in my code and only use that class to render the two images i forward to it on command. – Echo88 Jul 18 '15 at 12:51
  • The run method for most of classes that show an OpenGL window will actually be in a base class `GlfwApp`. That example contains a class which derives from RiftApp, which in turn derives from GlfwApp. – Jherico Jul 18 '15 at 16:05