/
jsTransitions.cpp
241 lines (221 loc) · 7.55 KB
/
jsTransitions.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Module jsTransitions.cpp
*
* This module defines the Javascript image transition
* classes as described in jsTransition.h
*
*
* Change History :
*
* $Log: jsTransitions.cpp,v $
* Revision 1.1 2003-03-12 02:57:42 ericn
* -Initial import
*
*
* Copyright Boundary Devices, Inc. 2003
*/
#include "jsTransitions.h"
#include "fbDev.h"
#include "hexDump.h"
#include "js/jscntxt.h"
#include "jsGlobals.h"
#include "jsImage.h"
static unsigned long now_ms()
{
struct timeval now ; gettimeofday( &now, 0 );
return (now.tv_sec*1000)+(now.tv_usec/1000) ;
}
JSBool
jsFadeTick( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval )
{
*rval = JSVAL_FALSE ;
if( 0 == argc )
{
jsval stVal ;
jsval durVal ;
jsval lhVal ;
jsval rhVal ;
jsval xVal, yVal ;
if( JS_GetProperty( cx, obj, "startTick", &stVal )
&&
JS_GetProperty( cx, obj, "duration", &durVal )
&&
JS_GetProperty( cx, obj, "src", &lhVal )
&&
JS_GetProperty( cx, obj, "dest", &rhVal )
&&
JS_GetProperty( cx, obj, "x", &xVal )
&&
JS_GetProperty( cx, obj, "y", &yVal ) )
{
unsigned long const start = JSVAL_TO_INT( stVal );
unsigned long const duration = JSVAL_TO_INT( durVal );
unsigned long const msNow = now_ms();
unsigned long const end = start+duration ;
if( msNow < end )
{
unsigned long const _256ths = ((msNow-start)*256)/duration ;
//printf( "0x%02x /256ths\n", _256ths ); fflush( stdout );
JSObject *const src = JSVAL_TO_OBJECT( lhVal );
JSObject *const dest = JSVAL_TO_OBJECT( rhVal );
jsval widthVal, heightVal, srcPixVal, destPixVal ;
if( JS_GetProperty( cx, src, "width", &widthVal )
&&
JS_GetProperty( cx, src, "height", &heightVal )
&&
JS_GetProperty( cx, src, "pixBuf", &srcPixVal )
&&
JSVAL_IS_STRING( srcPixVal )
&&
JS_GetProperty( cx, dest, "pixBuf", &destPixVal )
&&
JSVAL_IS_STRING( destPixVal )
)
{
int width = JSVAL_TO_INT( widthVal );
int height = JSVAL_TO_INT( heightVal );
JSString *srcPixStr = JSVAL_TO_STRING( srcPixVal );
unsigned short const *const srcPixMap = (unsigned short *)JS_GetStringBytes( srcPixStr );
unsigned const srcLen = JS_GetStringLength( srcPixStr );
JSString *destPixStr = JSVAL_TO_STRING( destPixVal );
unsigned short const *const destPixMap = (unsigned short *)JS_GetStringBytes( destPixStr );
unsigned const destLen = JS_GetStringLength( destPixStr );
if( ( srcLen == destLen )
&&
( srcLen == width * height * sizeof( srcPixMap[0] ) ) )
{
fbDevice_t &fb = getFB();
fb.blend( JSVAL_TO_INT( xVal ), JSVAL_TO_INT( yVal ), width, height, srcPixMap, destPixMap, _256ths );
*rval = JSVAL_TRUE ;
}
else
JS_ReportError( cx, "Invalid pixMap(s)\n" );
}
else
JS_ReportError( cx, "Object not initialized, can't draw\n" );
}
else
{
JSObject *dest = JSVAL_TO_OBJECT( rhVal );
jsval params[2] = { xVal, yVal };
jsImageDraw( cx, dest, 2, params, rval );
} // just draw end item
}
else
JS_ReportError( cx, "getting timeProperties" );
} // need msNow parameter
else
JS_ReportError( cx, "Usage: fade.tick()" );
return JS_TRUE ;
}
static JSFunctionSpec fadeMethods_[] = {
{"tick", jsFadeTick, 3 },
{0}
};
enum jsFade_tinyId {
FADE_SRC,
FADE_DEST,
FADE_DURATION,
FADE_X,
FADE_Y,
FADE_STARTTICK
};
JSClass jsFadeClass_ = {
"fade",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
JSCLASS_NO_OPTIONAL_MEMBERS
};
static JSPropertySpec fadeProperties_[] = {
{0,0,0}
};
static JSBool fade( JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval )
{
*rval = JSVAL_FALSE ;
if( ( 5 == argc )
&&
JSVAL_IS_OBJECT( argv[0] )
&&
JSVAL_IS_OBJECT( argv[1] )
&&
JSVAL_IS_INT( argv[2] )
&&
JSVAL_IS_INT( argv[3] )
&&
JSVAL_IS_INT( argv[4] ) )
{
JSObject *thisObj = JS_NewObject( cx, &jsFadeClass_, NULL, NULL );
if( thisObj )
{
*rval = OBJECT_TO_JSVAL( thisObj ); // root
JSObject *const src = JSVAL_TO_OBJECT( argv[0] );
JSObject *const dest = JSVAL_TO_OBJECT( argv[1] );
jsval lhWidth, lhHeight,
rhWidth, rhHeight ;
if( JS_GetProperty( cx, src, "width", &lhWidth )
&&
JS_GetProperty( cx, src, "height", &lhHeight )
&&
JS_GetProperty( cx, dest, "width", &rhWidth )
&&
JS_GetProperty( cx, dest, "height", &rhHeight ) )
{
if( ( JSVAL_TO_INT( lhWidth ) == JSVAL_TO_INT( rhWidth ) )
&&
( JSVAL_TO_INT( lhHeight ) == JSVAL_TO_INT( rhHeight ) ) )
{
int const duration = JSVAL_TO_INT( argv[2] );
if( 0 < duration )
{
int const x = JSVAL_TO_INT( argv[3] );
int const y = JSVAL_TO_INT( argv[4] );
fbDevice_t &fbDev = getFB();
if( ( 0 <= x ) && ( 0 <= y )
&&
( fbDev.getWidth() > x )
&&
( fbDev.getHeight() > y ) )
{
for( int i = 0 ; i < argc ; i++ )
JS_DefineProperty( cx, thisObj,
fadeProperties_[i].name,
argv[i], 0, 0, JSPROP_ENUMERATE|JSPROP_PERMANENT|JSPROP_READONLY );
unsigned long const tickNow = now_ms();
JS_DefineProperty( cx, thisObj, fadeProperties_[argc].name, INT_TO_JSVAL( tickNow ),
0, 0, JSPROP_ENUMERATE|JSPROP_PERMANENT|JSPROP_READONLY );
}
}
else
JS_ReportError( cx, "duration must be > 0" );
}
else
JS_ReportError( cx, "fade: images must be the same size\n"
"%d/%d, %d/%d\n",
JSVAL_TO_INT( lhWidth ), JSVAL_TO_INT( rhWidth ),
JSVAL_TO_INT( lhHeight ), JSVAL_TO_INT( rhHeight ) );
}
else
JS_ReportError( cx, "fade: getting image sizes" );
}
else
JS_ReportError( cx, "Error allocating fade" );
}
else
JS_ReportError( cx, "Usage : new fade( srcImg, destImg, durationMs );" );
return JS_TRUE ;
}
bool initJSTransitions( JSContext *cx, JSObject *glob )
{
JSObject *rval = JS_InitClass( cx, glob, NULL, &jsFadeClass_,
fade, 1,
fadeProperties_,
fadeMethods_,
0, 0 );
if( rval )
{
return true ;
}
else
return false ;
}