How to structure an application framework
I want to write something that will take care of:
If possible the int main() loop, as in, I w开发者_开发知识库ant the code for the main function located in this file
Some mundane tasks like creating windows, initializing various things like opengl, opencv and what not.
Various "events" (I quote because I what the c++ concept of events is) for things like keyboard and mouse events.
Provide access to pertinent member variables like the hDC.
I would like to put this code in one file that I can just drop into a new project and then have something like an App.cpp that can respond to the Events as it chooses, without requiring it to handle all of them.
My background is in c#, and I am just beginning c++. I fear that I might not have articulated by question well, but any help would be appreciated.
Edit: I am not looking for a prefab solution. I am not too hung up on it being one file. I am interested in the mechanics of how to actually create the framework. I am not at all concerned with cross platform compatibility
Look at Qt
There is no specific design pattern for what you are looking for, but a name: "software framework" - in your case more specifically "application framework".
Frameworks effectively minimize the work its users have to do to setup things by implementing default behaviour for common tasks.
There is no useful way to just drop one file in though. What you usually do is compiling the framework to a (dynamic or static) library, that your application links to - but you still need to include the neccessary header files then.
There's no design pattern for that. What you're looking for is a basic OS abstraction layer to save you the effort of calling the operating system directly and bundles everything in a few C++ classes.
nitializing various things like opengl, opencv and what not
This together with int main() loop
makes me think you're actually looking for a game engine or a basic foundation to make computer games/do graphics-related stuff. If that's true, why not take a look at SDL, SFML, Ogre, whatever suits to your needs? All provide basic abstraction and more, for different purposes.
Also, much of your requirements is already handled by the various OpenGL utilities - namely GLUT and GLFW. If you intend to stick with OpenGL, both will do the job (GLFW is more advanced and provides more control).
I am answering my question here, but I am very interesting in hearing thoughts about this setup. The idea is that I have my framework located in a class in MyApp.cpp (i am ignoring the header files for brevity) The application constructs its main() method initializes an instance of MyApp. Wires up some events (These arent what I would call events in the C# sense of the word because you can only have one "subscriber" here, but in this case it doesnt matter) and then call the Run method on the app instance. The App object is then responsible for constructing whatever mundane stuff like windows. It gets the message pump going and calls the "events" as appropriate. This way the app can be responsible for all the humdrum stuff and hide it away from view.
Main.cpp
#include <iostream>
#include <string>
#include "MyApp.cpp"
using namespace std;
MyApp app;
void DataReceived(string txt)
{
cout << app.SomeSetting << ": " << txt << "\n";
}
int main(void)
{
//Initialize events and what not.
app.OnDataReceived = DataReceived;
app.Run(); //Run the app
cout << "Goodbye\n";
return 0;
}
MyApp.cpp
#include <iostream>
using namespace std;
//Event signatures
typedef void (*DataReceivedEvent)(string);
class MyApp
{
public:
//Events
DataReceivedEvent OnDataReceived;
//Settings
int SomeSetting;
void Run()
{
SomeSetting = 123;
//Main loop
int input;
bool isRunning = true;
while(isRunning)
{
cout << "Enter a command: ";
cin >> input;
switch (input)
{
case 0:
isRunning = false;
break;
case 1:
OnDataReceived("Command 1");
break;
default:
cout << "What???\n";
}
}
}
};
精彩评论