[SOLVED] Panda3d librocket C++ Hello World

I tried to translate this hello world example in python into C++

from panda3d.rocket import *
from direct.directbase import DirectStart

LoadFontFace("assets/Delicious-Roman.otf")

r = RocketRegion.make('pandaRocket', base.win)
r.setActive(1)
context = r.getContext()

context.LoadDocument('data/background.rml').Show()


doc = context.LoadDocument('data/main_menu.rml')
doc.Show()

ih = RocketInputHandler()
base.mouseWatcher.attachNewNode(ih)
r.setInputHandler(ih)

run()
#ifdef HAVE_ROCKET_PYTHON
	#include "Python.h"
#endif

#include "pandaFramework.h"
#include "pandaSystem.h"
#include "rocketRegion.h"
#include "Rocket/Core.h"

//uncomment if you need the debugger
//#include "Rocket/Debugger.h"

int main(int argc, char *argv[]) {
    //open a new window framework
    PandaFramework framework;
    framework.open_framework(argc, argv);
    //set the window title to My Panda3D Window
    framework.set_window_title("My Panda3D Rockettest Window");
    //open the window
    
	#ifdef HAVE_ROCKET_PYTHON
		Py_Initialize();
	#endif 
		
	WindowFramework *window = framework.open_window();

    //Loading fonts is necessary since there's no default fallback
	//if you don't load the fonts then your text will not be visible
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-Roman.otf");
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-Bold.otf");
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-Italic.otf");
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-BoldItalic.otf");
    
	//this creates and enables the Display Region that will display the Rocket UI
	PT(RocketRegion) rocketRegion = RocketRegion::make("PandaRocketRegion",window->get_graphics_output());
    
	//Setting up the input handler for mouse events is necessary for any
	//interaction and without it you cant even control the debugger
	PT(RocketInputHandler) rih = new RocketInputHandler();
	window->get_mouse().attach_new_node(rih);
	rocketRegion->set_input_handler(rih);
	
	//Uncomment the following to start the program with the Rocket Debugger on
	//Rocket::Debugger::Initialise(rocketRegion->get_context());
	//Rocket::Debugger::SetVisible(true);
	
	Rocket::Core::ElementDocument* document = rocketRegion->get_context()->LoadDocument("data/tutorial.rml");
    document->Show();

    //do the main loop, equal to run() in python
    framework.main_loop();
    //close the window framework
	framework.close_framework();
		
	#ifdef HAVE_ROCKET_PYTHON
		Py_Finalize();
	#endif

	return (0);
 }

old post:

edit2: ok got it working basically, there’s still something wrong with the shutdown but this is the basic code to load a rocket interface and enable mouse forwarding from python. You can receive Rocket Events in the same manor as the C++ samples that come with rocket work.

I didn’t find the issue, but it started to work after I deleted some lines:

 int main(int argc, char *argv[]) { 
    //open a new window framework 
    PandaFramework framework; 
    framework.open_framework(argc, argv); 
    //set the window title to My Panda3D Window 
    framework.set_window_title("My Panda3D Window"); 
    //open the window 
    WindowFramework *window = framework.open_window(); 

    //here is room for your own code 
    PT(RocketRegion) rocketRegion = RocketRegion::make("RocketRegionTest",window->get_graphics_output()); 
    Rocket::Core::ElementDocument* document = rocketRegion->get_context()->LoadDocument("data/main_menu.rml"); 
    document->Show(); 
    document = rocketRegion->get_context()->LoadDocument("data/main_menu.rml"); 
    document->Show(); 
     

    PT(RocketInputHandler) rih = new RocketInputHandler(); 
    rocketRegion->set_input_handler(rih); 

    //doesn't crash if I disable the region 
    //rocketRegion->set_active(false); 

    //do the main loop, equal to run() in python 
    framework.main_loop(); 
    //close the window framework 
    framework.close_framework(); 
    return (0); 
 }

Though the buttons don’t react on events, but I assume this is because the Control thingy isn’t activated.

That’s what I feared, could you be so nice as to share your compiler settings with me. Most importantly a list of all the different libraries you link against.

What is frustrating is that if I execute python sourcecode from within the C++ program (with PyRun) it works.

-L/usr/lib/panda3d p3framework panda pandafx pandaexpress p3dtoolconfig p3dtool p3direct p3rocket RocketCore RocketControls angelscript

All of my options. You can forget about angelscript of course.

Thank you very much, still doesn’t work for me but I guess I’ll try it with GCC and see if that makes a difference. If someone who compiles with VS2008 might try it’d be very helpful.

I compiled it under Ubuntu as well and I get a segmentation fault there in the do_cull() function too.

The very very very weird thing is that if I initialize python from C++ it magically works (not the input just the display) is there some easy way to find out what’s the difference between what gets loaded?

#include <Python.h>
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "rocketRegion.h"
#include "Rocket/Core.h"

int main(int argc, char *argv[]) {
    //open a new window framework
    PandaFramework framework;
    framework.open_framework(argc, argv);
    //set the window title to My Panda3D Window
    framework.set_window_title("My Panda3D Window");
    //open the window
    WindowFramework *window = framework.open_window();

    //here is room for your own code
    //Loaded the font, otherwise text wont show up
    Rocket::Core::FontDatabase::LoadFontFace("assets/Delicious-Roman.otf");
    PT(RocketRegion) rocketRegion = RocketRegion::make("RocketRegionTest",window->get_graphics_output());
    Rocket::Core::ElementDocument* document = rocketRegion->get_context()->LoadDocument("data/background.rml");
    document->Show();
    document = rocketRegion->get_context()->LoadDocument("data/main_menu.rml");
    document->Show();

    PT(RocketInputHandler) rih = new RocketInputHandler();
    rocketRegion->set_input_handler(rih);

    //doesn't crash if I disable the region
    //rocketRegion->set_active(false);

    //MAGIC HERE, crashes if I don't run the python code
    Py_Initialize();

    //do the main loop, equal to run() in python
    framework.main_loop();
    //close the window framework
    framework.close_framework();

    Py_Finalize();
    return (0);
 }

Well, if libRocket was compiled with Python bindings, that function calls into Python to lock the GIL. Either make sure that Python is initialised properly (Py_Initialize, etc) or compile Panda3D without HAVE_ROCKET_PYTHON.

Well that’s awkward. So I either need to initialize python or keep 2 compiled versions of libp3rocket around if I want to use it for both Python and C++? Would’ve loved an assert() in init_rocket() but I’m the last person to complain about FOSS (not like I can’t propose a patch).

Mystery #1 solved, then I’ll try to solve input next and will update the opening post once I have a fully functional version.

ok, I got it working to my temporary satisfaction. I still have problems getting a clean shutdown but that’s something that I might or might not be dealing with later on.

#ifdef HAVE_ROCKET_PYTHON
	#include "Python.h"
#endif

#include "pandaFramework.h"
#include "pandaSystem.h"
#include "rocketRegion.h"
#include "Rocket/Core.h"

//uncomment if you need the debugger
//#include "Rocket/Debugger.h"

int main(int argc, char *argv[]) {
    //open a new window framework
    PandaFramework framework;
    framework.open_framework(argc, argv);
    //set the window title to My Panda3D Window
    framework.set_window_title("My Panda3D Rockettest Window");
    //open the window
    
	#ifdef HAVE_ROCKET_PYTHON
		Py_Initialize();
	#endif 
		
	WindowFramework *window = framework.open_window();

    //Loading fonts is necessary since there's no default fallback
	//if you don't load the fonts then your text will not be visible
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-Roman.otf");
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-Bold.otf");
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-Italic.otf");
    Rocket::Core::FontDatabase::LoadFontFace("data/Delicious-BoldItalic.otf");
    
	//this creates and enables the Display Region that will display the Rocket UI
	PT(RocketRegion) rocketRegion = RocketRegion::make("PandaRocketRegion",window->get_graphics_output());
    
	//Setting up the input handler for mouse events is necessary for any
	//interaction and without it you cant even control the debugger
	PT(RocketInputHandler) rih = new RocketInputHandler();
	window->get_mouse().attach_new_node(rih);
	rocketRegion->set_input_handler(rih);
	
	//Uncomment the following to start the program with the Rocket Debugger on
	//Rocket::Debugger::Initialise(rocketRegion->get_context());
	//Rocket::Debugger::SetVisible(true);
	
	Rocket::Core::ElementDocument* document = rocketRegion->get_context()->LoadDocument("data/tutorial.rml");
    document->Show();

    //do the main loop, equal to run() in python
    framework.main_loop();
    //close the window framework
	framework.close_framework();
		
	#ifdef HAVE_ROCKET_PYTHON
		Py_Finalize();
	#endif

	return (0);
 }