Create blitz games/apps in C++

BlitzMax Forums/BlitzMax Programming/Create blitz games/apps in C++

Would this be of interest to anyone?......

Each Blitz function is registered as a C function, and is then callable from a 'C' DLL. If all the Blitz functions were supported in the same fashion (as the demo code below) then it would be possible to write your game/application completely in C/C++ without having to further touch Blitz.

This might be of interest to people who like the simplicity of drawing graphics with Blitz but who want to program purely in C/C++.


This example draws a line rotationg about a point.

Here's the C code

//Write your game/application here
void ezRunApp()
{
   ezGraphics(1024,768);
   float a = 0;
   while(!ezKeyHit(13) && !ezKeyHit(27)) // Enter or Escape
   {
      ezCls();
      ezDrawLine(320, 240, 320+cos(a)*100, 240+sin(a)*100);
      ezFlip();

      a += c2PI/360;
      if(a == c2PI)
        a=0;
   }
}



Here's the C header

#include <math.h>

const float c2PI = 6.28318531;

#define EZSHARE_API __declspec(dllexport)

//Run App
extern "C"{EZSHARE_API void _cdecl ezRunApp(void);}

//Register prototypes
extern "C"{EZSHARE_API void _cdecl ezRegisterGraphics(int* func);}
extern "C"{EZSHARE_API void _cdecl ezRegisterCls(int* func);}
extern "C"{EZSHARE_API void _cdecl ezRegisterFlip(int* func);}
extern "C"{EZSHARE_API void _cdecl ezRegisterKeyHit(int* func);}
extern "C"{EZSHARE_API void _cdecl ezRegisterDrawLine(int* infunc);}

//Use these to directly call Blitz functions
int (*ezGraphics)(int, int);
void (*ezCls)();
void (*ezFlip)();
int (*ezKeyHit)(int);
void (*ezDrawLine)(float, float, float, float);

//Blitz uses these to register its functions
void ezRegisterGraphics(int* func){ezGraphics = (int (*) (int, int)) func;}
void ezRegisterKeyHit(int* func){ezKeyHit = (int (*) (int)) func;}
void ezRegisterCls(int* func){ezCls = (void (*) ()) func;}
void ezRegisterFlip(int* func){ezFlip = (void (*) ()) func;}
void ezRegisterDrawLine(int* func){ezDrawLine = (void (*) (float,float,float,float)) func;}



Here's the Blitz code

Import pub.win32 

Global ezLib=LoadLibraryA("Adder")
Global ezRegisterGraphics(fp:Byte Ptr)=getprocaddress(ezLib,"ezRegisterGraphics")
Global ezRegisterCls(fp:Byte Ptr)=getprocaddress(ezLib,"ezRegisterCls")
Global ezRegisterDrawLine(fp:Byte Ptr)=getprocaddress(ezLib,"ezRegisterDrawLine")
Global ezRegisterFlip(fp:Byte Ptr)=getprocaddress(ezLib,"ezRegisterFlip")
Global ezRegisterKeyHit(fp:Byte Ptr)=getprocaddress(ezLib,"ezRegisterKeyHit")
Global ezRunApp()=getprocaddress(ezLib,"ezRunApp")

ezRegisterGraphics(ezGraphics)
ezRegisterCls(Cls)
ezRegisterDrawLine(DrawLine)
ezRegisterFlip(Flip)
ezRegisterKeyHit(KeyHit)

Function ezGraphics( a:Int, b:Int )
	Graphics a, b
End Function

' Run the game/app
ezRunApp

End


For me no - Blitz is a much nicer language than C IMO.

If you were to distribute anything like that you would potentially make Blitz commands available to people who haven't purchased it, which is a big no-no.

I'm not distributing it. Just letting anyone who comes from a C/C++ background know that the blitz functions can be called in this way so they can write in their native programming language if they want to. I'm not suggesting that Blitz programmers should code in C/C++ - god no!

Nice sig you got there verfum.
Thats the same specs of my first machine! except for the sound blaster ;)

Haha sorry if I sounded a bit defensive there, verfum. I wasn't accusing. We had a guy try to sell a wrapped version of B3D recently and he actually tried advertising it on here!

No worries.
I wonder why it is possible to call the Blitz functions directly using the function pointer for Cls, Flip, DrawLine, but I couldn't get this to work for Graphics function. Any guesses?

I'd be very interested in working in C++ -> theres alot of OOP stuff in C++ thats very nice (although dangerous) I'd like to be using.

This would be great. Being able to hook into Blitz from Lisp via it's foreign function interface (which is designed with C in mind) would be awesome.

Would this be of interest to anyone?
Can't speak for everyone, but personally no. I haven't programmed in C/C++ for at least 5 years now (except for that course I took at Uni - talk about easy credit).

With all the other wonderful languages that are available right now, I don't feel any rush to either.

I like the idea of using the same technique for Lisp. I'm sure this is not limited to C or C++. There must be other languages that could call the Blitz functions via their pointers.

How about a Blitz Java class implemented using the JNI to Blitz? Would this be possible using the pointer passing?