openframeworks / openFrameworks

openFrameworks is a community-developed cross platform toolkit for creative coding in C++.
http://openframeworks.cc
Other
9.89k stars 2.55k forks source link

add an example of a single-file project #1779

Open kylemcdonald opened 11 years ago

kylemcdonald commented 11 years ago

my current starting point for prototyping new ideas with OF looks like this:

#include "ofAppGlutWindow.h"
#include "ofMain.h"

class ofApp : public ofBaseApp {
public:
    void setup() {
    }
    void update() {
    }
    void draw() {
    }
};

int main() {
    ofAppGlutWindow window;
    ofSetupOpenGL(&window, 1280, 720, OF_WINDOW);
    ofRunApp(new ofApp());
}

that's a single-file project contained entirely in a main.cpp. i like it because it means i don't have to answer questions like "should this go in the header or not" when i'm only prototyping a small idea.

@bilderbuchi just suggested that it might be helpful to have a minimal example like this in the examples folder to show people how they might construct a single-file example for reporting bugs or suggesting features (for example like https://github.com/openframeworks/openFrameworks/issues/1674#issuecomment-11756116). i think that could be really cool, and i'd be glad to add this if other people are also interested. so this issue is more to gauge interest.

p.s.: as an aside, it would be cool if we had a macro like this:

#define ofStart(appName, width, height, windowMode) \
int main() {\
    ofAppGlutWindow window;\
    ofSetupOpenGL(&window, width, height, windowMode);\
    ofRunApp(new appName());\
}

and ofMain.h included ofAppGlutWindow.h, then a minimal single-file example would look like:

#include "ofMain.h"
class ofApp : public ofBaseApp {
public:
    void setup() {}
    void update() {}
    void draw() {}
};
ofStart(ofApp, 1280, 720, OF_WINDOW);

this idea is from cinder, which uses:

#if defined( CINDER_MAC )
    #define CINDER_APP_BASIC( APP, RENDERER )                               \
    int main( int argc, char * const argv[] ) {                             \
        cinder::app::AppBasic::prepareLaunch();                             \
        cinder::app::AppBasic *app = new APP;                               \
        cinder::app::Renderer *ren = new RENDERER;                          \
        cinder::app::AppBasic::executeLaunch( app, ren, #APP, argc, argv ); \
        cinder::app::AppBasic::cleanupLaunch();                             \
        return 0;                                                           \
    }
...
bakercp commented 11 years ago

Minor change to automatically choose the correct window type rather than forcing ofAppGlutWindow:

#define ofStart(appName, width, height, windowMode) \
int main() {\
    ofSetupOpenGL(width, height, windowMode);\
    ofRunApp(new appName());\
}
julapy commented 11 years ago

yeah that's great - nice and simple. makes it a lot easier to share only a single file.

bakercp commented 11 years ago

I know the broad use of testApp has been discussed before, but perhaps this tiny test example would better be called testApp and all of the normal examples could be called ofApp? Just thinking of ways to reduce the usage of testApp to more appropriate locations. Perhaps testApp needs to be removed completely? On a related note, it seems that project generator should spit out class names that reflect the project folder name for the main class ... (rather than testApp).

bakercp commented 11 years ago

btw it was #744 (turns out @timscaffidi already mentioned a similar idea about project generation scripts outputting class names to match the folder names over in that thread).

bakercp commented 11 years ago

Also @kylemcdonald your example suggests that it might be time to do a global find / replace

ofSetupOpenGL(1024, 768, OF_WINDOW);ofSetupOpenGL(1280, 720, OF_WINDOW);

in all the examples. It's time to go HD! :)

timscaffidi commented 11 years ago

Nice minimal project template, @kylemcdonald . This combined with @elliotwoods idea on the dev list of an IDE-less oF environment could be a great productivity boost for prototyping or "sketches" of ideas. Nice ofStart macro too.

bilderbuchi commented 11 years ago

Thanks for picking this up, Kyle! great to see the resonance, too. The usage of Minimal Working Examples is an established and useful tradition in programming troubleshooting, so if we supply this out of the box it could help in diagnosing problems and raising the quality of bug reports (especially because you can drop it directly into comments or gists without having to pay attention to cpp/h files).

Maybe as a name, minimalApp would be even more appropriate than testApp.

I'm not so sure about the ofStart macro - I guess the real question is if we really want to hide as much details (like main functions) as possible behind "magic" replacement macros. In the context of people trying to learn C++ on the whole, does this not make it more difficult to "release them into the wild once they're grown up"?