Rocket Lib for LiteC

Posted By: NeoNeper

Rocket Lib for LiteC - 02/06/13 20:28

hello friends!
I would like to make a suggestion for new implementations 3dgs.
A lot of time we are suffering in creating GUI utilities for our projects LITEC

A Wonderful idea, seriously Make LITEC be compatible with some external GUI, such as a libRocket.
http://librocket.com/
Posted By: Quad

Re: Rocket Lib for LiteC - 02/06/13 21:08

Interesting...
Posted By: NeoNeper

Re: Rocket Lib for LiteC - 02/06/13 22:00

YEYE
Posted By: jcl

Re: Rocket Lib for LiteC - 02/11/13 13:51

Looks certainly interesting.
Posted By: HeelX

Re: Rocket Lib for LiteC - 02/11/13 15:45

Originally Posted By: jcl
Looks certainly interesting.
Are you considering to make an implementation for Gamestudio PC & Android? If so, please tell us so that we/I don't have to do this by myself smile
Posted By: jcl

Re: Rocket Lib for LiteC - 02/13/13 15:24

I can not say at the moment, but will tell you when we made a decision.
Posted By: HeelX

Re: Rocket Lib for LiteC - 02/13/13 15:29

Originally Posted By: jcl
I can not say at the moment, but will tell you when we made a decision.
Thanks. I guess I'll see it on the forecast then. Btw, I am wondering what happened to the beta/forecast page... it wasn't updated a long time.
Posted By: Talemon

Re: Rocket Lib for LiteC - 02/13/13 15:42

Hey, I've spent the last 2 days trying to make it work. I've come to a point where everything is supposed to work but nothing is rendered. You can find my code here: https://github.com/talemon/alibrocket

You need to add the assets that come with the librocket samples to your root folder like fonts, rcss, rml and images.
Posted By: HeelX

Re: Rocket Lib for LiteC - 02/13/13 22:31

Hey Talemon, thanks for your effort! I can't test it right now, though frown but I had a look into your source code.

1.) I see no Lite-C demo which tests the thing...?
2.) I see that you use custom way to export RockInit, RockDestroy and RockUpdate... I would use the standard way with DLLFUNC (which might be exactly the same but it is always a good idea to use the standard procedure)
3.) have you tied the RockUpdate function to render_layer?
4.) Is the DLL really loaded and up and running? (check acklog)

Regards,
-Christian
Posted By: Talemon

Re: Rocket Lib for LiteC - 02/14/13 08:01

Hey HeelX,

1)I guess this should do:
Code:
void RockInit(void);
void RockDestroy(void);
void RockUpdate(void);

void main()
{
	level_load(NULL);
}

function on_k_event()
{
	RockInit();
	
	wait(1);
	while(1)
	{
		RockUpdate();
		wait(1);
	}
}



2) Yes, I've used regular win32 style of exporting but both are actually the same:

DLLFUNC is defined as:
Code:
#define DLLFUNC extern "C" __declspec(dllexport)



And my code uses this and also extern "C" at the declarations:
Code:
#define ALIBROCKET_API __declspec(dllexport)



3) I didn't know about these render_ events, I will try to experiment with them.

4) Yes the I've gotten rid of every error and it loads up fine. I added diag()s to various places and confirmed that they are being called. libRocket calls my DLL's RenderCompiledGeometry function with many triangles(which I've confirmed non-zero with diag_var()s)

P.S: I even used procmon and confirmed that the application is reading and loading the resource files, they are all fine.
Posted By: 3dgs_snake

Re: Rocket Lib for LiteC - 02/14/13 09:00

Hi,



I have also tried librocket (while Panda3D 1.8 has been released). I think that your problem is with render states (Some states used by the engine are [incompatible] with the states required to render librocket). It is possible to make them work together ( I have tried using a naive aproach that caused some slowdown), but you need to identify those states and enable/disable them based on the context you wand to render (librocket/engine). Renderering librocket on a BMAP (and a better input integration) is also something that needs more investigation.

GWEN also is good (More components but with traditional GUI programming - no HTML/CSS - Can be good for tools) (http://code.google.com/p/gwen/) but the last time I tried it (while it was hosted at code.google.com) it had some bugs with tree components. Now it is hosted on github and seems to be actively developed again.
Posted By: Ch40zzC0d3r

Re: Rocket Lib for LiteC - 02/14/13 09:40

This "gwen" looks really good!
I think I'll give it a try laugh
Posted By: HeelX

Re: Rocket Lib for LiteC - 02/14/13 10:43

@Talemon: Yeah, but I see no line that loads something like an example GUI or so?

@3dgs_snake: As we can see from your screenshots, you were successful with implementing RocketLib, so why don't you tell Talemon the necessary render states and what he has to change??
Posted By: Talemon

Re: Rocket Lib for LiteC - 02/14/13 11:07

@HeelX: It's in the DLL as I just wanted it to render something, I was planning to add functions to manage it from lite-c.

Line 62 on:
https://github.com/talemon/alibrocket/blob/master/alibrocket/alibrocket.cpp

@3dgs_snake: well, as he said, it would be nice if you could share what you did to make it work.
Posted By: 3dgs_snake

Re: Rocket Lib for LiteC - 02/14/13 11:07

Like I said, I really don't know. I just saved all render states, reset, apply librocket states (That's what I called naive approach and slow). Also, that was exactly 1 year ago blush . Bellow is the C++ code I used to test the lib (Before creating a plugin, I test it using Win App) if that can help you (I think that only the states that are used by librocket and 3dgs need to be changed).

PS : As I said, it is only a dirty code I used to test if a 3dgs plugin can be created using librocket wink .

Code:
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <string>

#include <var.h>
#include <adll.h>

#include <Rocket\Core.h>
#include <Rocket\Controls.h>
#include <Rocket\Debugger.h>

#include "RenderInterfaceDirectX.h"
#include "Input.h"
#include "InputWin32.h"

// Engine vars
ENGINE_VARS *ev = NULL;

LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

Rocket::Core::Context *context = NULL;             

IDirect3DStateBlock9* pStateBlock = NULL;
IDirect3DStateBlock9* pStateBlock2 = NULL;

// System Interface
class GSSystemInterface : public Rocket::Core::SystemInterface
{
public:
	virtual float GetElapsedTime(); 
};

float GSSystemInterface::GetElapsedTime()
{
	return v(total_ticks) * 0.16;
}

// Load default fonts
void LoadFonts()
{
	Rocket::Core::String font_names[5];
	font_names[0] = "Delicious-Roman.otf";
	font_names[1] = "Delicious-Italic.otf";
	font_names[2] = "Delicious-Bold.otf";
	font_names[3] = "Delicious-BoldItalic.otf";
	font_names[4] = "Comica BD.ttf";

	for (int i = 0; i < sizeof(font_names) / sizeof(Rocket::Core::String); i++)
	{
		Rocket::Core::FontDatabase::LoadFontFace(Rocket::Core::String("assets/") + font_names[i]);
	}
}

// Set renderStates for rocket
void SetRenderStates()
{
	// Get screen size
	VECTOR screen_size;
	vec_set(&screen_size, &v(screen_size));
	
	// Save State block before
	// Save current stateblock
	g_pd3dDevice->CreateStateBlock( D3DSBT_ALL, &pStateBlock2);
	//Restore state block
	pStateBlock->Apply();
	
	// Set up an orthographic projection.
	D3DXMATRIX projection;
	D3DXMatrixOrthoOffCenterLH(&projection, 0, screen_size.x, screen_size.y, 0, -1, 1);
	g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &projection);

	// Switch to clockwise culling instead of counter-clockwise culling; Rocket generates counter-clockwise geometry,
	// so you can either reverse the culling mode when Rocket is rendering, or reverse the indices in the render
	// interface.
	//g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);

	// Enable alpha-blending for Rocket.
	g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_BOTHSRCALPHA);
	g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_BOTHINVSRCALPHA);

	// Set up the texture stage states for the diffuse texture.
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
	g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);

	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
	g_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);

	// Disable lighting for Rocket.
	g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

	//g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, 2);
}

// Event Handlers Declaration
// Store Original message handler
WNDPROC OriginalHandler = NULL;
// Create a neww handler for rocket
LRESULT CALLBACK AckRocketEventHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	// Process Input
	InputWin32::ProcessWindowsEvent(msg, wParam, lParam);
	// Call OriginalHandler
	return OriginalHandler(hWnd, msg, wParam, lParam);
}

VOID WINAPI ColorFill (D3DXVECTOR4* pOut, const D3DXVECTOR2* pTexCoord, 
const D3DXVECTOR2* pTexelSize, LPVOID pData)
{
    //*pOut = D3DXVECTOR4(pTexCoord->x, pTexCoord->y, pTexCoord->x + pTexCoord->y, 1.0f);
	*pOut = D3DXVECTOR4(pTexCoord->x, pTexCoord->y, pTexCoord->x + pTexCoord->y, 1.0f);
}

// WinMain
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCMd)
{
	// Open engine
	ev = engine_open("");

	// Create Rendering engine
	engine_frame();
	
	//level_load(NULL);
	vec_set((VECTOR *)&v(screen_color), vector(255, 0, 0));
	//level_load(NULL);
	//v(camera).size_x = 100;
	//v(camera).size_y = 100;
	
	// Get DirectX interface
	g_pd3dDevice = (LPDIRECT3DDEVICE9)draw_begin();
	if (g_pd3dDevice == NULL)
	{
		return -1;
	}
	// Save current stateblock
	g_pd3dDevice->CreateStateBlock( D3DSBT_ALL, &pStateBlock );

	// Test level load
	v(logo) = 1;
	//level_load(NULL);
	//ENTITY *ent = ent_create("CUBE.MDL", vector(0, 0, 0), NULL);
	//ent->x += 100;
	//v(fps_max) = 60;
	// Display panel
	PANEL *pan = pan_create("", 0);
	pan->flags = SHOW;
	BMAP *bmp = bmap_createblack((v(screen_size).x * 0.5f), (v(screen_size).y * 0.5f), 32);
	pan->bmap = bmp;
	bmap_fill(bmp, (COLOR *)vector(0, 0, 255), 100);

	level_load("");

	var frames = 0;
	var fps = 0;
	pan_setdigits(pan, 0, 10, 200, "Variable acknex = %5.0f", NULL, 16, &fps);

	// Initialize librocket
	GSSystemInterface sysInterface;
	Rocket::Core::SetSystemInterface(&sysInterface);

	RenderInterfaceDirectX directx_renderer((LPDIRECT3D9)NULL, g_pd3dDevice);
	Rocket::Core::SetRenderInterface(&directx_renderer);
	
	// Init Rocket
	Rocket::Core::Initialise();
	// Init controls
	Rocket::Controls::Initialise();

	// Initialize keymap
	InputWin32::Initialise();
	// Bind event handler
	OriginalHandler = (WNDPROC) *ev->on_message;
	*ev->on_message = (EVENT)AckRocketEventHandler;

	// define a suited vertex struct
	typedef struct VERTEX_FLAT { 
		float x,y,z; 
		float rhw; 
		D3DCOLOR color; 
	} VERTEX_FLAT;
	#define D3DFVF_FLAT (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

	// define the three corner vertices
   VERTEX_FLAT v[3];
   v[0].x = 10.0; v[0].y = 10.0; v[0].color = 0xFFFF00FF; // the red corner
   v[1].x = 310.0; v[1].y = 10.0; v[1].color = 0xFF0000FF; // the blue corner
   v[2].x = 10.0; v[2].y = 310.0; v[2].color = 0xFF00FFFF; // the green corner
   v[0].z = v[1].z = v[2].z = 0.0; // z buffer - paint over everything
   v[0].rhw = v[1].rhw = v[2].rhw = 1.0; // no perspective

	// Engine loop
	while(engine_frame())
	{
		g_pd3dDevice = (LPDIRECT3DDEVICE9)draw_begin();
		SetRenderStates();

		fps = 0.9*fps + 0.1 / v(time_frame); 

		//ent->pan += 5 * v(time_step);

		if (context != NULL)
		{
			context->Update();
			context->Render();

			// Restore stateBlock
			pStateBlock2->Apply();
		}
		else
		{
			// Initialize context
			// CReate the main Rocket context and set it to the input layer
			VECTOR screen_size;
			vec_set(&screen_size, &v(screen_size));
			//(int)screen_size.x, (int)screen_size.y)
			context = Rocket::Core::CreateContext("main", Rocket::Core::Vector2i(screen_size.x, screen_size.y));
			
			/*TCHAR buff[255];
			wsprintf(buff, TEXT("Sizes = %d, %d"), context->GetDimensions().x, context->GetDimensions().y);
			MessageBox(NULL, buff, TEXT("Screen sizes"), MB_OK);*/

			Rocket::Debugger::Initialise(context);
			Rocket::Debugger::SetVisible(true);
			Input::SetContext(context);

			// Load Core fonts
			LoadFonts();
			// Load and show the tutorial document.
			Rocket::Core::ElementDocument* document = context->LoadDocument("hassets/window.rml");
			if (document != NULL)
			{
				document->Show();
				document->RemoveReference();
			}
		}
	}

	// Close acknex engine
	engine_close();

	// Return success
	return 0;
}

Posted By: Talemon

Re: Rocket Lib for LiteC - 02/14/13 11:58

I'll take a look at it, thank you very much for such a quick response (:
Posted By: Ch40zzC0d3r

Re: Rocket Lib for LiteC - 02/14/13 21:09

@3dgs_snake
How did you use gwen with lite-c?
Did you create a dll and import gwen sdk?
Posted By: 3dgs_snake

Re: Rocket Lib for LiteC - 02/15/13 05:21

You need to create a gwen lib (static or dynamic) plus the directx-9 renderer lib. You then use them in a 3dgs plugin or C/C++ app. You can look into the DX9 sample to see how.
Posted By: Ch40zzC0d3r

Re: Rocket Lib for LiteC - 02/15/13 15:17

Thanks, I got it working so far, but Ive got some problems.
1. On resizing the window Im crashing but IDK what pointer is invalid
2. I tried hooking GS MessageHandler to handle all the window messages with the gui function:

Code:
typedef LRESULT (CALLBACK *tMessageHandler)(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
tMessageHandler oMessageHandler;

LRESULT CALLBACK hkMessageHandler(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	MessageBoxA(NULL, "LOL", "LOL", MB_OK);

	return oMessageHandler(hwnd, message, wParam, lParam);
}

oMessageHandler = (tMessageHandler)(ev->on_message);
ev->on_message = (EVENT*)hkMessageHandler;



But the MsgBox never pops up.
Please help me frown
Posted By: Ch40zzC0d3r

Re: Rocket Lib for LiteC - 02/16/13 12:41

Just fixed the messagehandler error, but I always crash on changing resolution while drawing the gui, I tried hooking Reset, or checking if device or screen_size chnaged but it seems its crashing before I can stop drawing.
Any Idea how to fix that?
I think I will release the GUI here if I fixed those bugs laugh

EDIT: Fixed that bug!
Simply hook reset and do this before calling the orig function:
Code:
if(pRenderer)
	pRenderer->Release();

© 2024 lite-C Forums