C++ Question Problems with C ++ programming

Dany

Addon-Dev F.O.I.
Joined
Apr 7, 2011
Messages
18
Reaction score
0
Points
0
cattur10.jpg

I'm trying to program in C++ this "Stand", by inserting the option "SKIN".
I was able to implement it but I'm having trouble when closing:
Launchpad remains in memory, or it crashes if I add another similar vessel.

This is what I did:
File .Cpp
Code:
#define ORBITER_MODULE
#include "MyVessel.h"

//HINSTANCE g_hDLL;


// --------------------------------------------------------------
// Constructor
// --------------------------------------------------------------
MyVessel::MyVessel (OBJHANDLE hObj, int fmodel)
: VESSEL3 (hObj, fmodel)
{
	int i, j;

	
	visual            = NULL;
	exmesh            = NULL;

	skinpath[0] = '\0';
	for (i = 0; i < 3; i++)
		skin[i] = 0;

}

// --------------------------------------------------------------
// Destructor
// --------------------------------------------------------------
MyVessel::~MyVessel ()
{
	int i, j;
	
	for (i = 0; i < 3; i++)
		if (skin[i]) oapiReleaseTexture (skin[i]);
}



//=========================================================
// Vessel Animations
//=========================================================
void MyVessel::DefineAnimations()
{
	//ANIMATIONCOMPONENT_HANDLE parent;
	//static UINT gear_groups[2] = {5,6};
	//static MGROUP_ROTATE gear (
	//	0,
	//	gear_groups,
	//	2,
	//	_V(0,-1.0,8.5),
	//	_V(1,0,0),
	//	(float)(0.45*PI)
	//);
	//static UINT wheel_groups[2] = {10,11};
	//wheel = new MGROUP_ROTATE (
	//	0,
	//	wheel_groups,
	//	2,
	//	_V(0,-1.0,6.5),
	//	_V(1,0,0),
	//	(float)(2*PI)
	//);
	//anim_gear = CreateAnimation (0.0);
	//parent = AddAnimationComponent (anim_gear, 0, 1, &gear);
	//anim_wheel = CreateAnimation (0.0);
	//AddAnimationComponent (anim_wheel, 0, 1, wheel, parent);
}

// --------------------------------------------------------------
// Apply custom skin to the current mesh instance
// --------------------------------------------------------------
void MyVessel::ApplySkin ()
{
	if (!exmesh) return;
	if (skin[0]) oapiSetTexture (exmesh, 1, skin[0]);
}


//=========================================================
// Vessel Capabilities
//=========================================================
void MyVessel::clbkSetClassCaps (FILEHANDLE cfg)
{
	int i;
	// vessel caps definitions
	SetTouchdownPoints (PB_TDP[0], PB_TDP[1], PB_TDP[2]);
	SetMeshVisibilityMode (AddMesh (exmesh_tpl = oapiLoadMeshGlobal ("Stand")), MESHVIS_EXTERNAL);
}

//=========================================================
// Vessel Load and Save Parameters
//=========================================================
void MyVessel::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{
	char *line; while (oapiReadScenario_nextline (scn, line))
	{
		if (!strnicmp (line, "SKIN", 4))
		{
			sscanf (line+4, "%s", skinpath);
			char fname[256];
			strcpy (fname, "Stand\\Skins\\");
			strcat (fname, skinpath);
			int n = strlen(fname); fname[n++] = '\\';
			strcpy (fname+n, "Stand.dds");  skin[0] = oapiLoadTexture (fname);
		}
		else
		{
			ParseScenarioLineEx (line, vs);
		}
	}
}

void MyVessel::clbkSaveState (FILEHANDLE scn)
{
	char cbuf[256];
	int i;

	VESSEL3::clbkSaveState (scn);

	if (skinpath[0])
		oapiWriteScenario_string (scn, "SKIN", skinpath);
}

// --------------------------------------------------------------
// Create DG visual
// --------------------------------------------------------------
void MyVessel::clbkVisualCreated (VISHANDLE vis, int refcount)
{
	visual = vis;
	exmesh = GetDevMesh (vis, 0);
	ApplySkin();
}

// --------------------------------------------------------------
// Destroy DG visual
// --------------------------------------------------------------
void MyVessel::clbkVisualDestroyed (VISHANDLE vis, int refcount)
{
	visual = NULL;
	exmesh = NULL;
}


//=========================================================
// Animation Template
//=========================================================
void MyVessel::Timestep (double simt)
{
	//if (gear_status == CLOSING || gear_status == OPENING)
	//{
	//	double da = oapiGetSimStep() * gear_speed;
	//	if (gear_status == CLOSING)
	//	{
	//		if (gear_proc > 0.0)
	//			gear_proc = max (0.0, gear_proc-da);
	//		else
	//			gear_status = CLOSED;
	//	}
	//	else
	//	{
	//		// door opening
	//		if (gear_proc < 1.0)
	//			gear_proc = min (1.0, gear_proc+da);
	//		else
	//			gear_status = OPEN;
	//	}
	//	SetAnimation (anim_gear, gear_proc);
	//}
}

//=========================================================
// Load and Delete Module Stuff
//=========================================================
DLLCLBK void InitModule (HINSTANCE hModule)
{
	//g_hDLL = hModule;
	// perform global module initialisation here
}
DLLCLBK void ExitModule (HINSTANCE hModule)
{
	// perform module cleanup here
}

//=========================================================
// Load and Delete Vessel Stuff
//=========================================================
DLLCLBK VESSEL *ovcInit (OBJHANDLE hvessel, int flightmodel)
{
	return new MyVessel (hvessel, flightmodel);
}
DLLCLBK void ovcExit (VESSEL *vessel)
{
	if (vessel)
		delete (MyVessel*)vessel;
}
File .h
Code:
#pragma once
#include "Orbitersdk.h"


const VECTOR3 PB_TDP[3] = {{0,-48.6,20},{-20,-48.6,-20},{20,-48.6,-20}}; // touchdown points [m]


class MyVessel : public VESSEL3 {

public:
	MyVessel (OBJHANDLE hObj, int fmodel);
	~MyVessel ();

	void DefineAnimations();
	void clbkSetClassCaps (FILEHANDLE cfg);
	void clbkLoadStateEx (FILEHANDLE scn, void *vs);
	void clbkSaveState (FILEHANDLE scn);
	void Timestep (double simt);
	void clbkVisualCreated (VISHANDLE vis, int refcount);
	void clbkVisualDestroyed (VISHANDLE vis, int refcount);

	MESHHANDLE exmesh_tpl;          // vessel mesh: global template
	DEVMESHHANDLE exmesh;           // vessel mesh: instance

private:
	void ApplySkin();                            // apply custom skin
	VISHANDLE visual;                            // handle to DG visual representation
	char skinpath[32];                           // skin directory, if applicable
	SURFHANDLE skin[1];                          // custom skin textures, if applicable
};

Could some C++ expert help me understand if there is an error or something missing?
This is the second attempt to program in C++, the first one failed, have mercy.
Sorry for my english, Thank you. :)
 
Dont have any experience with that particular feature youre trying to add, but what is it? I cant quite figure it out from the pic
 
Does it work adding by scenario editor or is one generally the limit? I can't find anything obvious or trivial in your code why it should not work, but the use of DEVMESHHANDLE could have some oddities.
 
The option "Skin" is used to change the texture file directly into the scenario.

The scenario editor works. Only if I add another ship crashes after a while.
When I close Orbiter is as follows:
 
I see you have skin declared in the header as:
Code:
SURFHANDLE skin[1];
Shouldn't the array have 3 elements?
 
Yes, now it works:thumbup:
I was convinced it was the number of textures
Now the full. A Beta will be released soon.
Thank you.:cheers:
 
I was convinced it was the number of textures
If it is only meant to have one texture per skin then why are you doing this?:

In vessel constructor:
Code:
	for (i = 0; i < 3; i++)
		skin[i] = 0;

In vessel destructor:
Code:
	for (i = 0; i < 3; i++)
		if (skin[i]) oapiReleaseTexture (skin[i]);
 
I copied from DG, not a programmer.
To make things right, I should change (i = 0; i < 1; i++)?
Or change everything.
 
In such case `skin` doesn't need to be an array at all. Proposed changes (stricken red - removed, green - added):

The .h file:
Code:
#pragma once
#include "Orbitersdk.h"


const VECTOR3 PB_TDP[3] = {{0,-48.6,20},{-20,-48.6,-20},{20,-48.6,-20}}; // touchdown points [m]


class MyVessel : public VESSEL3 {

public:
	MyVessel (OBJHANDLE hObj, int fmodel);
	~MyVessel ();

	void DefineAnimations();
	void clbkSetClassCaps (FILEHANDLE cfg);
	void clbkLoadStateEx (FILEHANDLE scn, void *vs);
	void clbkSaveState (FILEHANDLE scn);
	void Timestep (double simt);
	void clbkVisualCreated (VISHANDLE vis, int refcount);
	void clbkVisualDestroyed (VISHANDLE vis, int refcount);

	MESHHANDLE exmesh_tpl;          // vessel mesh: global template
	DEVMESHHANDLE exmesh;           // vessel mesh: instance

private:
	void ApplySkin();                            // apply custom skin
	VISHANDLE visual;                            // handle to DG visual representation
	char skinpath[32];                           // skin directory, if applicable
	SURFHANDLE skin[color=#c00][s][PLAIN][1][/PLAIN][/s][/color];                          // custom skin texture[color=#c00][s]s[/s][/color], if applicable
};

The .cpp file:
Code:
#define ORBITER_MODULE
#include "MyVessel.h"

//HINSTANCE g_hDLL;


// --------------------------------------------------------------
// Constructor
// --------------------------------------------------------------
MyVessel::MyVessel (OBJHANDLE hObj, int fmodel)
: VESSEL3 (hObj, fmodel)
{
	int i, j;

	
	visual            = NULL;
	exmesh            = NULL;

	skinpath[0] = '\0';
	[color=#c00][s]for (i = 0; i < 3; i++)[/s][/color]
		skin[color=#c00][s][PLAIN][i][/PLAIN][/s][/color] = [color=#c00][s]0[/s][/color][color=#0a0]NULL[/color];

}

// --------------------------------------------------------------
// Destructor
// --------------------------------------------------------------
MyVessel::~MyVessel ()
{
	int i, j;
	
	[color=#c00][s]for (i = 0; i < 3; i++)[/s][/color]
		if (skin[color=#c00][s][PLAIN][i][/PLAIN][/s][/color]) oapiReleaseTexture (skin[color=#c00][s][PLAIN][i][/PLAIN][/s][/color]);
}



//=========================================================
// Vessel Animations
//=========================================================
void MyVessel::DefineAnimations()
{
	//ANIMATIONCOMPONENT_HANDLE parent;
	//static UINT gear_groups[2] = {5,6};
	//static MGROUP_ROTATE gear (
	//	0,
	//	gear_groups,
	//	2,
	//	_V(0,-1.0,8.5),
	//	_V(1,0,0),
	//	(float)(0.45*PI)
	//);
	//static UINT wheel_groups[2] = {10,11};
	//wheel = new MGROUP_ROTATE (
	//	0,
	//	wheel_groups,
	//	2,
	//	_V(0,-1.0,6.5),
	//	_V(1,0,0),
	//	(float)(2*PI)
	//);
	//anim_gear = CreateAnimation (0.0);
	//parent = AddAnimationComponent (anim_gear, 0, 1, &gear);
	//anim_wheel = CreateAnimation (0.0);
	//AddAnimationComponent (anim_wheel, 0, 1, wheel, parent);
}

// --------------------------------------------------------------
// Apply custom skin to the current mesh instance
// --------------------------------------------------------------
void MyVessel::ApplySkin ()
{
	if (!exmesh) return;
	if (skin[color=#c00][s][PLAIN][0][/PLAIN][/s][/color]) oapiSetTexture (exmesh, 1, skin[color=#c00][s][PLAIN][0][/PLAIN][/s][/color]);
}


//=========================================================
// Vessel Capabilities
//=========================================================
void MyVessel::clbkSetClassCaps (FILEHANDLE cfg)
{
	int i;
	// vessel caps definitions
	SetTouchdownPoints (PB_TDP[0], PB_TDP[1], PB_TDP[2]);
	SetMeshVisibilityMode (AddMesh (exmesh_tpl = oapiLoadMeshGlobal ("Stand")), MESHVIS_EXTERNAL);
}

//=========================================================
// Vessel Load and Save Parameters
//=========================================================
void MyVessel::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{
	char *line; while (oapiReadScenario_nextline (scn, line))
	{
		if (!strnicmp (line, "SKIN", 4))
		{
			sscanf (line+4, "%s", skinpath);
			char fname[256];
			strcpy (fname, "Stand\\Skins\\");
			strcat (fname, skinpath);
			int n = strlen(fname); fname[n++] = '\\';
			strcpy (fname+n, "Stand.dds");  skin[color=#c00][s][PLAIN][0][/PLAIN][/s][/color] = oapiLoadTexture (fname);
		}
		else
		{
			ParseScenarioLineEx (line, vs);
		}
	}
}

void MyVessel::clbkSaveState (FILEHANDLE scn)
{
	char cbuf[256];
	int i;

	VESSEL3::clbkSaveState (scn);

	if (skinpath[0])
		oapiWriteScenario_string (scn, "SKIN", skinpath);
}

// --------------------------------------------------------------
// Create DG visual
// --------------------------------------------------------------
void MyVessel::clbkVisualCreated (VISHANDLE vis, int refcount)
{
	visual = vis;
	exmesh = GetDevMesh (vis, 0);
	ApplySkin();
}

// --------------------------------------------------------------
// Destroy DG visual
// --------------------------------------------------------------
void MyVessel::clbkVisualDestroyed (VISHANDLE vis, int refcount)
{
	visual = NULL;
	exmesh = NULL;
}


//=========================================================
// Animation Template
//=========================================================
void MyVessel::Timestep (double simt)
{
	//if (gear_status == CLOSING || gear_status == OPENING)
	//{
	//	double da = oapiGetSimStep() * gear_speed;
	//	if (gear_status == CLOSING)
	//	{
	//		if (gear_proc > 0.0)
	//			gear_proc = max (0.0, gear_proc-da);
	//		else
	//			gear_status = CLOSED;
	//	}
	//	else
	//	{
	//		// door opening
	//		if (gear_proc < 1.0)
	//			gear_proc = min (1.0, gear_proc+da);
	//		else
	//			gear_status = OPEN;
	//	}
	//	SetAnimation (anim_gear, gear_proc);
	//}
}

//=========================================================
// Load and Delete Module Stuff
//=========================================================
DLLCLBK void InitModule (HINSTANCE hModule)
{
	//g_hDLL = hModule;
	// perform global module initialisation here
}
DLLCLBK void ExitModule (HINSTANCE hModule)
{
	// perform module cleanup here
}

//=========================================================
// Load and Delete Vessel Stuff
//=========================================================
DLLCLBK VESSEL *ovcInit (OBJHANDLE hvessel, int flightmodel)
{
	return new MyVessel (hvessel, flightmodel);
}
DLLCLBK void ovcExit (VESSEL *vessel)
{
	if (vessel)
		delete (MyVessel*)vessel;
}
 
Ok, now I understand.
Thanks for your interest and especially for the explanation.
:cheers:
 
Back
Top