forked from cnlohr/rawdrawandroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
357 lines (282 loc) · 8.24 KB
/
test.c
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//Copyright (c) 2011-2020 <>< Charles Lohr - Under the MIT/x11 or NewBSD License you choose.
// NO WARRANTY! NO GUARANTEE OF SUPPORT! USE AT YOUR OWN RISK
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "os_generic.h"
#include <GLES3/gl3.h>
#include <asset_manager.h>
#include <asset_manager_jni.h>
#include <android_native_app_glue.h>
#include <android/sensor.h>
#include "CNFGAndroid.h"
#define CNFG_IMPLEMENTATION
#define CNFG3D
#include "CNFG.h"
float mountainangle;
float mountainoffsetx;
float mountainoffsety;
ASensorManager * sm;
const ASensor * as;
bool no_sensor_for_gyro = false;
ASensorEventQueue* aeq;
ALooper * l;
void SetupIMU()
{
sm = ASensorManager_getInstance();
as = ASensorManager_getDefaultSensor( sm, ASENSOR_TYPE_GYROSCOPE );
no_sensor_for_gyro = as == NULL;
l = ALooper_prepare( ALOOPER_PREPARE_ALLOW_NON_CALLBACKS );
aeq = ASensorManager_createEventQueue( sm, (ALooper*)&l, 0, 0, 0 ); //XXX??!?! This looks wrong.
if(!no_sensor_for_gyro) {
ASensorEventQueue_enableSensor( aeq, as);
printf( "setEvent Rate: %d\n", ASensorEventQueue_setEventRate( aeq, as, 10000 ) );
}
}
float accx, accy, accz;
int accs;
void AccCheck()
{
if(no_sensor_for_gyro) {
return;
}
ASensorEvent evt;
do
{
ssize_t s = ASensorEventQueue_getEvents( aeq, &evt, 1 );
if( s <= 0 ) break;
accx = evt.vector.v[0];
accy = evt.vector.v[1];
accz = evt.vector.v[2];
mountainangle /*degrees*/ -= accz;// * 3.1415 / 360.0;// / 100.0;
mountainoffsety += accy;
mountainoffsetx += accx;
accs++;
} while( 1 );
}
unsigned frames = 0;
unsigned long iframeno = 0;
void AndroidDisplayKeyboard(int pShow);
int lastbuttonx = 0;
int lastbuttony = 0;
int lastmotionx = 0;
int lastmotiony = 0;
int lastbid = 0;
int lastmask = 0;
int lastkey, lastkeydown;
static int keyboard_up;
void HandleKey( int keycode, int bDown )
{
lastkey = keycode;
lastkeydown = bDown;
if( keycode == 10 && !bDown ) { keyboard_up = 0; AndroidDisplayKeyboard( keyboard_up ); }
if( keycode == 4 ) { AndroidSendToBack( 1 ); } //Handle Physical Back Button.
}
void HandleButton( int x, int y, int button, int bDown )
{
lastbid = button;
lastbuttonx = x;
lastbuttony = y;
if( bDown ) { keyboard_up = !keyboard_up; AndroidDisplayKeyboard( keyboard_up ); }
}
void HandleMotion( int x, int y, int mask )
{
lastmask = mask;
lastmotionx = x;
lastmotiony = y;
}
#define HMX 162
#define HMY 162
short screenx, screeny;
float Heightmap[HMX*HMY];
extern struct android_app * gapp;
void DrawHeightmap()
{
int x, y;
//float fdt = ((iframeno++)%(360*10))/10.0;
mountainangle += .2;
if( mountainangle < 0 ) mountainangle += 360;
if( mountainangle > 360 ) mountainangle -= 360;
mountainoffsety = mountainoffsety - ((mountainoffsety-100) * .1);
float eye[3] = { (float)sin(mountainangle*(3.14159/180.0))*30*sin(mountainoffsety/100.), (float)cos(mountainangle*(3.14159/180.0))*30*sin(mountainoffsety/100.), 30*cos(mountainoffsety/100.) };
float at[3] = { 0,0, 0 };
float up[3] = { 0,0, 1 };
tdSetViewport( -1, -1, 1, 1, screenx, screeny );
tdMode( tdPROJECTION );
tdIdentity( gSMatrix );
tdPerspective( 30, ((float)screenx)/((float)screeny), .1, 200., gSMatrix );
tdMode( tdMODELVIEW );
tdIdentity( gSMatrix );
tdTranslate( gSMatrix, 0, 0, -40 );
tdLookAt( gSMatrix, eye, at, up );
float scale = 60./HMX;
for( x = 0; x < HMX-1; x++ )
for( y = 0; y < HMY-1; y++ )
{
float tx = x-HMX/2;
float ty = y-HMY/2;
float pta[3];
float ptb[3];
float ptc[3];
float ptd[3];
float normal[3];
float lightdir[3] = { .6, -.6, 1 };
float tmp1[3];
float tmp2[3];
RDPoint pto[6];
pta[0] = (tx+0)*scale; pta[1] = (ty+0)*scale; pta[2] = Heightmap[(x+0)+(y+0)*HMX]*scale;
ptb[0] = (tx+1)*scale; ptb[1] = (ty+0)*scale; ptb[2] = Heightmap[(x+1)+(y+0)*HMX]*scale;
ptc[0] = (tx+0)*scale; ptc[1] = (ty+1)*scale; ptc[2] = Heightmap[(x+0)+(y+1)*HMX]*scale;
ptd[0] = (tx+1)*scale; ptd[1] = (ty+1)*scale; ptd[2] = Heightmap[(x+1)+(y+1)*HMX]*scale;
tdPSub( pta, ptb, tmp2 );
tdPSub( ptc, ptb, tmp1 );
tdCross( tmp1, tmp2, normal );
tdNormalizeSelf( normal );
tdFinalPoint( pta, pta );
tdFinalPoint( ptb, ptb );
tdFinalPoint( ptc, ptc );
tdFinalPoint( ptd, ptd );
if( pta[2] >= 1.0 ) continue;
if( ptb[2] >= 1.0 ) continue;
if( ptc[2] >= 1.0 ) continue;
if( ptd[2] >= 1.0 ) continue;
if( pta[2] < 0 ) continue;
if( ptb[2] < 0 ) continue;
if( ptc[2] < 0 ) continue;
if( ptd[2] < 0 ) continue;
pto[0].x = pta[0]; pto[0].y = pta[1];
pto[1].x = ptb[0]; pto[1].y = ptb[1];
pto[2].x = ptd[0]; pto[2].y = ptd[1];
pto[3].x = ptc[0]; pto[3].y = ptc[1];
pto[4].x = ptd[0]; pto[4].y = ptd[1];
pto[5].x = pta[0]; pto[5].y = pta[1];
// CNFGColor(((x+y)&1)?0xFFFFFF:0x000000);
float bright = tdDot( normal, lightdir );
if( bright < 0 ) bright = 0;
CNFGColor( 0xff | ( ( (int)( bright * 90 ) ) << 24 ) );
// CNFGTackPoly( &pto[0], 3 ); CNFGTackPoly( &pto[3], 3 );
CNFGTackSegment( pta[0], pta[1], ptb[0], ptb[1] );
CNFGTackSegment( pta[0], pta[1], ptc[0], ptc[1] );
CNFGTackSegment( ptb[0], ptb[1], ptc[0], ptc[1] );
}
}
void HandleDestroy()
{
printf( "Destroying\n" );
exit(10);
}
volatile int suspended;
void HandleSuspend()
{
suspended = 1;
}
void HandleResume()
{
suspended = 0;
}
uint32_t randomtexturedata[256*256];
int main()
{
int x, y;
double ThisTime;
double LastFPSTime = OGGetAbsoluteTime();
int linesegs = 0;
CNFGBGColor = 0x000040ff;
CNFGSetupFullscreen( "Test Bench", 0 );
//CNFGSetup( "Test Bench", 0, 0 );
for( x = 0; x < HMX; x++ )
for( y = 0; y < HMY; y++ )
{
Heightmap[x+y*HMX] = tdPerlin2D( x, y )*8.;
}
const char * assettext = "Not Found";
AAsset * file = AAssetManager_open( gapp->activity->assetManager, "asset.txt", AASSET_MODE_BUFFER );
if( file )
{
size_t fileLength = AAsset_getLength(file);
char * temp = malloc( fileLength + 1);
memcpy( temp, AAsset_getBuffer( file ), fileLength );
temp[fileLength] = 0;
assettext = temp;
}
SetupIMU();
while(1)
{
int i, pos;
iframeno++;
CNFGHandleInput();
AccCheck();
if( suspended ) { usleep(50000); continue; }
CNFGClearFrame();
CNFGColor( 0xFFFFFFFF );
CNFGGetDimensions( &screenx, &screeny );
// Mesh in background
CNFGSetLineWidth( 9 );
DrawHeightmap();
CNFGPenX = 0; CNFGPenY = 400;
CNFGColor( 0xffffffff );
CNFGDrawText( assettext, 15 );
CNFGFlushRender();
CNFGPenX = 0; CNFGPenY = 480;
char st[50];
sprintf( st, "%dx%d %d %d %d %d %d %d\n%d %d\n%5.2f %5.2f %5.2f %d", screenx, screeny, lastbuttonx, lastbuttony, lastmotionx, lastmotiony, lastkey, lastkeydown, lastbid, lastmask, accx, accy, accz, accs );
CNFGDrawText( st, 10 );
CNFGSetLineWidth( 2 );
/* CNFGTackSegment( pto[0].x, pto[0].y, pto[1].x, pto[1].y );
CNFGTackSegment( pto[1].x, pto[1].y, pto[2].x, pto[2].y );
CNFGTackSegment( pto[2].x, pto[2].y, pto[0].x, pto[0].y );
*/
// Square behind text
CNFGColor( 0x303030ff );
CNFGTackRectangle( 600, 0, 950, 350);
CNFGPenX = 10; CNFGPenY = 10;
// Text
pos = 0;
CNFGColor( 0xffffffff );
for( i = 0; i < 1; i++ )
{
int c;
char tw[2] = { 0, 0 };
for( c = 0; c < 256; c++ )
{
tw[0] = c;
CNFGPenX = ( c % 16 ) * 20+606;
CNFGPenY = ( c / 16 ) * 20+5;
CNFGDrawText( tw, 4 );
}
}
// Green triangles
CNFGPenX = 0;
CNFGPenY = 0;
for( i = 0; i < 400; i++ )
{
RDPoint pp[3];
CNFGColor( 0x00FF00FF );
pp[0].x = (short)(50*sin((float)(i+iframeno)*.01) + (i%20)*30);
pp[0].y = (short)(50*cos((float)(i+iframeno)*.01) + (i/20)*20)+700;
pp[1].x = (short)(20*sin((float)(i+iframeno)*.01) + (i%20)*30);
pp[1].y = (short)(50*cos((float)(i+iframeno)*.01) + (i/20)*20)+700;
pp[2].x = (short)(10*sin((float)(i+iframeno)*.01) + (i%20)*30);
pp[2].y = (short)(30*cos((float)(i+iframeno)*.01) + (i/20)*20)+700;
CNFGTackPoly( pp, 3 );
}
int x, y;
for( y = 0; y < 256; y++ )
for( x = 0; x < 256; x++ )
randomtexturedata[x+y*256] = x | ((x*394543L+y*355+iframeno)<<8);
CNFGBlitImage( randomtexturedata, 100, 600, 256, 256 );
frames++;
//On Android, CNFGSwapBuffers must be called, and CNFGUpdateScreenWithBitmap does not have an implied framebuffer swap.
CNFGSwapBuffers();
ThisTime = OGGetAbsoluteTime();
if( ThisTime > LastFPSTime + 1 )
{
printf( "FPS: %d\n", frames );
frames = 0;
linesegs = 0;
LastFPSTime+=1;
}
}
return(0);
}