Can I use PureBasic with a static c++ library?

Just starting out? Need help? Post your questions and find answers here.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Can I use PureBasic with a static c++ library?

Post by idle »

If the c++ headers export the functions as extern C which will no doubt aliased with a typedef
Then yes it should be reasonable easy to do.

post a portion of a header if you need any help with the porting
Windows 11, Manjaro, Raspberry Pi OS
Image
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

Here is a tiny portion of the engine header (with permission from Josh Klint, author of Leadwerks):

Code: Select all

//******************************************************************************
//****																		****
//****							Leadwerks Engine 2.51						****
//****							  C/C++ Headers 2.51						****
//****					(c) 1999-2011 Leadwerks Corporation					****
//****						  http://www.leadwerks.com						****
//****																		****
//******************************************************************************

// engine.h

#pragma once
#ifndef LEADWERKS_ENGINE_H
#define LEADWERKS_ENGINE_H

#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

namespace LE
{

extern HMODULE le_hlib;
extern double Pi;

extern int sgn(float num);

typedef float flt;				// floating point type which is used by GPU
typedef flt* FP;				// floating number pointer
typedef byte* BP;				// byte pointer
typedef char* str;				// char pointer
typedef const char* const_str;	// const char pointer

// Unified syntax
typedef BP TEntity;
typedef BP TBody;
typedef BP TBuffer;
typedef BP TCamera;
typedef BP TController;
typedef BP TCorona;
typedef BP TEmitter;
typedef BP TFont;
typedef BP TFramework;
typedef BP TGroup;
typedef BP TJoint;
typedef BP TLayer;
typedef BP TLight;
typedef BP TListener;
typedef BP TMaterial;
typedef BP TMesh;
typedef BP TModel;
typedef BP TPivot;
typedef BP TScript;
typedef BP TShader;
typedef BP TSound;
typedef BP TSource;
typedef BP TSurface;
typedef BP TTerrain;
typedef BP TTire;
typedef BP TTexture;
typedef BP TVehicle;
typedef BP TWorld;
typedef FP TVector;

struct TVec2{union{struct{flt X;flt Y;};flt a[2];};};				// textureUV value
struct TVec3{union{struct{flt X;flt Y;flt Z;};flt a[3];};};			// color, vertex, etc... value
struct TVec4{union{struct{flt X;flt Y;flt Z;flt W;};flt a[4];};};	// color+alpha value
struct TVec6{union{struct{
				flt X0;flt Y0;flt Z0;
				flt X1;flt Y1;flt Z1;};
				flt a[6];};};						// AABB
struct TVec9{union{struct{
				flt A0;flt A1;flt A2;
				flt B0;flt B1;flt B2;
				flt C0;flt C1;flt C2;};
				flt a[9];};};						// 3x3 matrix
struct TVec16{union{struct{
				flt A0;flt A1;flt A2;flt A3;
				flt B0;flt B1;flt B2;flt B3;
				flt C0;flt C1;flt C2;flt C3;
				flt D0;flt D1;flt D2;flt D3;};
				flt a[16];};};						// 4x4 matrix
const flt MAX_DEC = 9999999999.9999999999;

TVec2 Vec2(flt X=0, flt Y=MAX_DEC);
TVec2 Vec2(flt X, flt Y);

TVec3 Vec3(flt X=0, flt Y=MAX_DEC, flt Z=MAX_DEC);
TVec3 Vec3(flt X, flt Y, flt Z);

TVec4 Vec4(flt X=0, flt Y=MAX_DEC, flt Z=MAX_DEC, flt W=MAX_DEC);
TVec4 Vec4(flt X, flt Y, flt Z, flt W);

TVec6 Vec6  (flt X0=0, flt Y0=MAX_DEC, flt Z0=MAX_DEC,
			 flt X1=MAX_DEC, flt Y1=MAX_DEC, flt Z1=MAX_DEC);
TVec6 Vec6  (flt X0, flt Y0, flt Z0,
			 flt X1, flt Y1, flt Z1);

TVec9 Vec9  (flt A0=0, flt A1=MAX_DEC, flt A2=MAX_DEC,
			 flt B0=MAX_DEC, flt B1=MAX_DEC, flt B2=MAX_DEC,
			 flt C0=MAX_DEC, flt C1=MAX_DEC, flt C2=MAX_DEC);
TVec9 Vec9  (flt A0, flt A1, flt A2,
			 flt B0, flt B1, flt B2,
			 flt C0, flt C1, flt C2);

TVec16 Vec16(flt A0=0, flt A1=MAX_DEC, flt A2=MAX_DEC, flt A3=MAX_DEC,
			 flt B0=MAX_DEC, flt B1=MAX_DEC, flt B2=MAX_DEC, flt B3=MAX_DEC,
			 flt C0=MAX_DEC, flt C1=MAX_DEC, flt C2=MAX_DEC, flt C3=MAX_DEC,
			 flt D0=MAX_DEC, flt D1=MAX_DEC, flt D2=MAX_DEC, flt D3=MAX_DEC);
TVec16 Vec16(flt A0, flt A1, flt A2, flt A3,
			 flt B0, flt B1, flt B2, flt B3,
			 flt C0, flt C1, flt C2, flt C3,
			 flt D0, flt D1, flt D2, flt D3);

struct TPick
{
	TEntity entity;
	TEntity surface;
	flt X, Y, Z;
	flt NX, NY, NZ;
	int triangle;
	int vegetationindex;
};

#ifndef LEADWERKS_NO_MATHLIB
#include "mathlib.h"
#endif

typedef int			(_stdcall *TGraphics)				(int, int, int, int, int);
typedef void		(_stdcall *TFlip)					(int);
typedef int			(_stdcall *TGraphicsWidth)			(void);
typedef int			(_stdcall *TGraphicsHeight)			(void);
typedef void		(_stdcall *TEndRender)				(void);
typedef int			(_stdcall *TGraphicsModeExists)		(int, int, int, int);
typedef	int			(_stdcall *TGetGraphicsVendor)		(void);
typedef	int			(_stdcall *TGetShaderModel)			(void);

typedef TBuffer		(_stdcall *TCreateBuffer)			(int, int, int);
typedef TBuffer		(_stdcall *TBackBuffer)				(void);
typedef int			(_stdcall *TBufferWidth)			(TBuffer);
typedef int			(_stdcall *TBufferHeight)			(TBuffer);
typedef void		(_stdcall *TSetBuffer)				(TBuffer);
typedef	int			(_stdcall *TMaxColorBuffers)		(void);
typedef TBuffer		(_stdcall *TGetColorBuffer)			(TBuffer, int);
typedef TBuffer		(_stdcall *TGetDepthBuffer)			(TBuffer);
typedef	TBuffer		(_stdcall *TCreateCustomBuffer)		(byte*, byte*);
typedef	int			(_stdcall *TSaveBuffer)				(TBuffer, const_str, int);
typedef	void		(_stdcall *TClearBuffer)			(int);
typedef	void		(_stdcall *TFreeBuffer)				(TBuffer);
typedef	TBuffer		(_stdcall *TCurrentBuffer)			(void);
typedef	void		(_stdcall *TCopyBuffer)				(TBuffer, TBuffer, int);
typedef	int			(_stdcall *TSetColorBuffer)			(TBuffer, TTexture, int, int);
typedef	int			(_stdcall *TSetDepthBuffer)			(TBuffer, TTexture);

typedef TWorld		(_stdcall *TCreateWorld)			(void);
typedef void		(_stdcall *TSetWorld)				(TWorld);
typedef void		(_stdcall *TSetWorldSize)			(const TVec3&);
typedef void		(_stdcall *TSetWorldCullRange)		(flt, flt, flt);
typedef void		(_stdcall *TClearWorld)				(void);
typedef void		(_stdcall *TFreeWorld)				(TWorld);
typedef TWorld		(_stdcall *TCurrentWorld)			(void);
typedef void		(_stdcall *TUpdateWorld)			(float);
typedef void		(_stdcall *TRenderWorld)			(int);
typedef TEntity		(_stdcall *TLoadScene)				(const_str);

typedef TTerrain	(_stdcall *TCreateTerrain)			(int);
typedef int			(_stdcall *TLoadTerrainHeightmap)	(TTerrain, const_str);
typedef int			(_stdcall *TLoadTerrainAlphamap)	(TTerrain, const_str);
typedef int			(_stdcall *TSaveTerrainHeightmap)	(TTerrain, const_str);
typedef int			(_stdcall *TSaveTerrainAlphamap)	(TTerrain, const_str);
typedef	void		(_stdcall *TTerrainDetail)			(int);
typedef void		(_stdcall *TSetTerrainHeight)		(TTerrain, int, int, flt);
typedef flt			(_stdcall *TTerrainHeight)			(TTerrain, int, int);
typedef void		(_stdcall *TUpdateTerrainNormals)	(TTerrain, int, int, int, int);
typedef void		(_stdcall *TUpdateTerrainNormal)	(TTerrain, int, int);
typedef void		(_stdcall *TUpdateTerrainAlphaMap)	(TTerrain, int, int, int, int);
typedef flt			(_stdcall *TTerrainElevation)		(TTerrain, flt, flt);
typedef void		(_stdcall *TTerrainNormal)			(TTerrain, flt, flt, TVec3*);
typedef void		(_stdcall *TSetTerrainTexture)		(TTerrain, TTexture, int, int);
typedef void		(_stdcall *TSetTerrainTextureScale)	(TTerrain, flt, int);
typedef void		(_stdcall *TSetTerrainTextureRotation)		(TTerrain, flt, int);
typedef void		(_stdcall *TSetTerrainTextureMappingMode)	(TTerrain, int, int);
typedef void		(_stdcall *TBlendTerrainTexture)			(TTerrain, int, int, flt, int);
typedef void		(_stdcall *TSetTerrainTextureConstraints)	(TTerrain, flt, flt, flt, flt, int);
typedef	void		(_stdcall *TSetVegetationShadowMode)		(int);
If you want, I could send you the entire engine.h file - I have permission to do so.

I was just given permission by Josh, the author, to share the entire engine.h file with anyone - it's included with the free demo, so it's no secret.

Mike
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Can I use PureBasic with a static c++ library?

Post by c4s »

karmacomposer wrote:Asking Josh will more than likely not happen since he does not code in PureBasic [...]
I don't know if that helps you out but I just remembered that some years ago he was actually posting on here: http://www.purebasic.fr/english/viewtop ... 16&t=33950
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

Luis,

You seem to know about or have created a utility that allows one to load a .dll in and then define the commands located within that .dll

http://www.purebasic.fr/english/viewtop ... 9&start=90

Would this work for me, since you only need engine.dll to run Leadwerks, which is now, at version 2.5, very robust and very stable and VERY cool!

It's come a very long way.

Mike
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

The following is in c++, but most of it is in Leadwerks. Does anyone know how I can change the c++ commands to PureBasic ones?

Code: Select all

#include "engine.h"

int main(int argc, char** argv)
{
        //Load the engine dll
        Initialize();

        //Create a graphics window
        Graphics(640,480,0,0,GRAPHICS_BACKBUFFER+GRAPHICS_DEPTHBUFFER);

        //Create a world. A world must be created before anything else.
        CreateWorld();

        //Create a camera
        TEntity cam;
        cam=CreateCamera(0);
        MoveEntity(cam,Vec3(0,0,-5));

        //Create a visible mesh
        CreateCube(0);

        //Main loop
        while(!KeyHit(KEY_ESCAPE)) {

                //Update the world
                UpdateWorld(1);
                
                //Render the world
                RenderWorld(RENDER_ALL);
                
                //Swap the buffers
                Flip(1);
        }
        
        //Stop the engine and free the dll from memory
        Terminate();
        return 0;
}
I don't need the include (shown above) since the engine is being loaded by the OpenLibrary command. Mostly I need to change:

int main(int argc, char** argv)
{

and

}

//Stop the engine and free the dll from memory
Terminate();
return 0;

To whatever you would use to initialize PureBasic and end it

I already have the Prototypes and functions in place.

Mike
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

Is there a PureBasic equivalient to VOID in c++?

Mike
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Can I use PureBasic with a static c++ library?

Post by Shield »

Just use Integer and ignore the returning value.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

Almost have an example working. Why does this give a invalid memory access error:

Graphics (1024,768,0,60,BACKBUFFER)

Graphics is the command, then width, height, depth, hz and backbuffer as a flag

Is this a roadblock? Is the engine trying to call up a graphics window and PureBasic does not like it?

Mike
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Can I use PureBasic with a static c++ library?

Post by skywalk »

PB Constants -> #BACKBUFFER
Otherwise, BACKBUFFER is just a variable.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

skywalk wrote:PB Constants -> #BACKBUFFER
Otherwise, BACKBUFFER is just a variable.
Replacing it with #BACKBUFFER just gives me a Constant Not Found error.

Is there a certain syntax to that?

Mike
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

I shortened the Prototype command for this to just (a.l,b.l) - for width and height - and it still gave me an error. In the Leadwerks engine, you can shorten that and it works.

Mike
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Can I use PureBasic with a static c++ library?

Post by applePi »

karmacomposer, i have found on my hard disk this Buggy includefile for using leadwerks in purebasic, the place i have downloaded it before a year is from leadwerks as shown in the attached htm files but now not available. i remember it does not work, but you may find it usefull .
yes leadwerks are very good i have run it using VS 2008 and the examples are great. but since the VS and the alike products resembles the elephant in its size and speed and everything , and purebasic is like a powerful smart Gymnast, so if you succeed in calling leadwerk from purebasic will be great.
the files here:
http://www.mediafire.com/?bo6vz4uwq07qobf
karmacomposer
Enthusiast
Enthusiast
Posts: 113
Joined: Sun Jul 29, 2012 2:51 pm

Re: Can I use PureBasic with a static c++ library?

Post by karmacomposer »

applePi wrote:karmacomposer, i have found on my hard disk this Buggy includefile for using leadwerks in purebasic, the place i have downloaded it before a year is from leadwerks as shown in the attached htm files but now not available. i remember it does not work, but you may find it usefull .
yes leadwerks are very good i have run it using VS 2008 and the examples are great. but since the VS and the alike products resembles the elephant in its size and speed and everything , and purebasic is like a powerful smart Gymnast, so if you succeed in calling leadwerk from purebasic will be great.
the files here:
http://www.mediafire.com/?bo6vz4uwq07qobf
Thank you. I think the engine has gone through so many updates this code simply does not work. I would not even know where to begin. I keep getting memory allocation errors when I try to run any attempt at getting Leadwerks to work with PureBasic.

Mike
Post Reply