aboutsummaryrefslogtreecommitdiff
path: root/contrib/xclasses/readme.txt
blob: 1bd49d689a989917a5713a4f0f910683ed2955a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
XClasses is a simple IDL written in Python.
You declare your classes, methods, and members as Python objects,
and XClasses will generate the .c, .h, and .f files for you.
Not only do the Forth classes line up with their C counterparts
exactly, but all non-static methods (virtual and non-virtual)
are *automatically* thunked.  In other words, any method
declared in XClasses and implemented in C can be called from
the matching Ficl class, and the C method will be automatically
called with the correct arguments.  XClasses handles floating-point
arguments too!

Known limitations:
	* All arguments must be one cell wide.  (That means
	  only single-precision floats, too.)



To use:
	* Declare all your classes in a .py script
	* Include "xclasses.h" everywhere you need your classes
	* Include xclasses.cpp in your project somewhere
	* Call
		"xclasses.f" included
	  from your Ficl initialization script

And you're mostly done!

Simply including xclasses.f is not enough, though.  You must
explicitly instantiate your classes.  This is to allow you a
chance to add your own methods to the class.  For a class
named "myCallback", it would look like this:

	declare-myCallback
		end-myCallback

You also have to define the "init" function for the class.
Most of the time this will work fine:

	declare-myCallback
		use-default-init
		end-myCallback


The "default" init function calls the super class's init
function, then sets all data members to 0.  If this isn't
what you want, roll your own.  The init function takes
the usual 2-cell "this" pointer as its argument:

	declare-myCallback
		: init ( 2:this ) ...
			;
		end-myCallback

For a do-nothing init function, you'll want this:

	declare-myCallback
		: init 2drop ;
		end-myCallback


Here's a random example from the simple game I'm working on:

-----------------------------------------------------------------
skinStream = xMethod("stream", "void").setVirtual(1)
gjeSkin.addMethod(skinStream)

##
## gjeTexture
##
##
gjeTexture = xClass("gjeTexture")
gjeTexture.setParent(gjeSkin)
gjeTexture.addMethod(skinStream)
gjeTexture.addMethod(xMethod("clear", "void"))
gjeTexture.addMember(xVariable("texture", "LPDIRECT3DTEXTURE8"))
gjeTexture.addMember(xVariable("name", "char *"))
gjeTexture.addMember(xVariable("variation", "int"))
gjeTexture.addMember(xVariable("material", "D3DMATERIAL8 *"))

-----------------------------------------------------------------

I sure hope that's enough to get you started.



Random notes:
* XClasses doesn't deal well with struct-packing issues.  It assumes
  pretty much everything will be 4-byte aligned.  This can bite you
  if you add a 64-bit int... the C compiler may align it for you,
  and XClasses won't know about it.  (This could be fixed in a future
  release... are you volunteering?)  For now, it's best to declare
  your classes such that 64-bit ints are naturally 8-byte aligned.

* If you don't want to have to declare each class in turn,
  you can add something like this to the end of your Python script:
-----
def declare(name):
	xAddFiclFooter("\t\"" + name + ".constant\" \" sfind swap drop 0= [if] declare-" + name + " use-default-init end-" + name + " [endif] \" evaluate")


xAddFiclFooter(": xclassesDeclare")
for c in classes:
	declare(c.name)
xAddFiclFooter("\t;")
-----
  and then call "xclassesDeclare" from your Ficl script just after
  including "xclasses.f".


--lch
larry@hastings.org