How to easily incorporate classes from samples into own basic application

Problems building or running the engine, queries about how to use features etc.
Post Reply
professor_oats
Gnoblar
Posts: 8
Joined: Sat Jul 22, 2023 1:27 pm
x 1

How to easily incorporate classes from samples into own basic application

Post by professor_oats »

Ogre Version: 14.2.0
Operating System: Windows 10 Pro
Render System: OpenGL3+

Hello!

I am hammering the tutorials and would like to try out the code from samples inside my own basic application. This is more of a C++ question if any.
I did try to test a sample through this method: https://wiki.ogre3d.org/OgreBites

It gave me error that mWindow was nullptr and I guess part can be that the newer tutorials on OgreBites using ApplicationContext when setting up the application. Currently I have something simple like this:

Code: Select all

class BaseApplication : public ApplicationContext, public FrameListener, public InputListener

{

public:
    BaseApplication();

virtual ~BaseApplication() {
}

void setup();
bool keyPressed(const KeyboardEvent& evt);

private:
    SceneManager* _ScnMgr;
    Root* _Root;
    Camera* _Cam;

};


BaseApplication::BaseApplication() : ApplicationContext ("OGREsamples") {
    _ScnMgr = nullptr;
    _Root = nullptr;
    _Cam = nullptr;
}

void BaseApplication::setup() {

// do not forget to call the base first

ApplicationContext::setup();
addInputListener(this);

// ...

 }
 
 int main(int argc, char** argv)
{
    try
    {
        BaseApplication app;
        app.initApp();
        app.getRoot()->startRendering();
        app.closeApp();
    }
    catch (const std::exception& e)
    {
        std::cerr << "Error occurred during execution: " << e.what() << '\n';
        return 1;
    }

return 0;
}

My question is how I easily could use like the class SinbadCharacterController to set up a body, a cameraman to the entity and using the functions of inputhandling found in the class definitions. When testing other topics I have run into some access violations at times when my own application would try to use elsewhere defined variables in SDK like mTrayManager and mWindow, etc.

For testing I'd like some opinions in how to min-max using the most out of the sample code and run it in cohesion with my BaseApplication app.

One option is to pick in the codebase and grab what is needed and use it in my app, but I believe it can be tedious since the samples are already well written.

Example:
How would I go about if I'd like the InputListener to use the keyPressed functions defined in CharacterSample.h?
How would you go about to setupBody, setupCamera and setupAnimations for sinbad? My guess is that I would go about to let my BaseApplication inherit from SinbadCharacterController class, or use SinbadCharacterController::setupBody() inside my own BaseApplication::setup().

I am in the midst of coding and testing so I will use any input and tips you have. My initiative for this post is that I tend to get access violation at times and still have some confusion how the ApplicationContext sets up the application.

Cheers!

Code: Select all

class _OgreSampleClassExport Sample_Character : public SdkSample
{
public:

Sample_Character()
{
// ...
}

bool frameRenderingQueued(const FrameEvent& evt) override
{
    // let character update animations and camera
    mChara->addTime(evt.timeSinceLastFrame);
    return SdkSample::frameRenderingQueued(evt);
}

bool keyPressed(const KeyboardEvent& evt) override
{
    // relay input events to character controller
    if (!mTrayMgr->isDialogVisible()) mChara->injectKeyDown(evt);
    return SdkSample::keyPressed(evt);
}
// ...

};
professor_oats
Gnoblar
Posts: 8
Joined: Sat Jul 22, 2023 1:27 pm
x 1

Re: How to easily incorporate classes from samples into own basic application

Post by professor_oats »

I think I kind of have the hold of it. I just like to stimulate discussion for brainstorming. You could also share your favorite song or something. :D

Like if I feel I must use predefined functions I can set them as protected in the BaseApplication class and use overrides.

My newbie brain just realised that the functions are mere definitions and what affects the outcome is the different events like "const KeyboardEvent& evt"

professor_oats
Gnoblar
Posts: 8
Joined: Sat Jul 22, 2023 1:27 pm
x 1

Re: How to easily incorporate classes from samples into own basic application

Post by professor_oats »

Lol.

It was as simple as this:

Character = new SinbadCharacterController(Cam);

Now I just have to get the keyevents and lighting right.

rpgplayerrobin
Gnoll
Posts: 619
Joined: Wed Mar 18, 2009 3:03 am
x 355

Re: How to easily incorporate classes from samples into own basic application

Post by rpgplayerrobin »

If you get access violation and other crashes, I would run in debug to see why it happens.
I would also make sure that Ogre is built in a way so that you can actually debug Ogre code as well, which should be automatic if I remember correctly when building Ogre from source. Just try debugging into an Ogre function such as when creating an entity or a node to see if it works with its debug information. That will solve a lot of these access violation issues since you can then see why it crashes and then solve it in your user code (because that is most likely where the issues are).

One option is to pick in the codebase and grab what is needed and use it in my app, but I believe it can be tedious since the samples are already well written.

If you only want to learn Ogre a bit and not use it for anything bigger or for a long term project, I would only do code in the built Ogre source.
But if you really want your own project and have full control over all of it, I would really recommend only taking what you actually need. This can be a pain for sure, but then you have 100% control over your entire project in the end.

To do that, simply press F10 in Visual Studio to see where the application starts and then go through all lines (with F11 to go into functions and F10 to step one line) until it actually shows up something on the screen. That way you can take the code needed to do the same in your own application.
You may even take whole .cpp/.h files and then use them in your own application, but I can guarantee that you will want to make changes later on, and that will be a pain if you don't have your own code.

professor_oats
Gnoblar
Posts: 8
Joined: Sat Jul 22, 2023 1:27 pm
x 1

Re: How to easily incorporate classes from samples into own basic application

Post by professor_oats »

Thanks for a great reply. My cmake has trouble building the source (could be the Cmake-GUI crap) so I had to add some things manually (SDL2 mainly). I kind of working with release now in the beginning but will rebuild with debug options as I get further. Really good advices. Often the access violations so far has been that I use uninitialised objects or nullptr in function calls.

I am in for a long term relationship here so ... :roll:

To get this sample running I simply created an instance of the SinbadCharacterController class in the setup like this:

Code: Select all

_Character = new SinbadCharacterController(_Cam);

(I kind of tried update as I went but moderator has to accept posts)

I then made some helper functions to make the _Character pointer point to the class SinbadCharacterController class function properly:

Code: Select all

void BaseApplication::createFramelistener() {
    _FrameListener = new MyFrameListener(_Character);
    getRoot()->addFrameListener(_FrameListener);
}

bool BaseApplication::keyPressed(const KeyboardEvent& evt) {

if (evt.keysym.sym == SDLK_ESCAPE)
{
    delete _Character;
    _Character = nullptr;
    getRoot()->queueEndRendering();
}

if (_Character) {
    _Character->injectKeyDown(evt);
}

return true;
}

bool BaseApplication::keyReleased(const KeyboardEvent& evt) {

if (_Character) {
    _Character->injectKeyUp(evt);
}

return true;
}

bool BaseApplication::mouseMoved(const MouseMotionEvent& evt) {

if (_Character) {
    _Character->injectMouseMove(evt);
}


return true;
}

bool BaseApplication::mouseWheelRolled(const MouseWheelEvent& evt) {

if (_Character) {
    _Character->injectMouseWheel(evt);
}

return true;

}

Now I will tinker with the lighting and camera possibilities.
Image

rpgplayerrobin
Gnoll
Posts: 619
Joined: Wed Mar 18, 2009 3:03 am
x 355

Re: How to easily incorporate classes from samples into own basic application

Post by rpgplayerrobin »

I am also using CMake GUI to build Ogre from source.

Here is my guide to do it: viewtopic.php?p=554037#p554037

paroj
OGRE Team Member
OGRE Team Member
Posts: 1995
Joined: Sun Mar 30, 2014 2:51 pm
x 1075
Contact:

Re: How to easily incorporate classes from samples into own basic application

Post by paroj »

the samples could indeed use some code overhaul. I took your post as an impulse to port the character sample to the InputListenerChain, which requires less boilerplate code.

Post Reply