From 4c0adf1ebd511fbf4409fe4bb81c126bc12bb4ba Mon Sep 17 00:00:00 2001 From: Alexander Mahr Date: Sun, 2 Mar 2025 09:31:31 +0100 Subject: [PATCH] clean up main.c (remove bloat) + Log --- app/src/nativecode/main.c | 374 ++------------------------------------ 1 file changed, 12 insertions(+), 362 deletions(-) diff --git a/app/src/nativecode/main.c b/app/src/nativecode/main.c index f19b2f8..c8ab9d8 100644 --- a/app/src/nativecode/main.c +++ b/app/src/nativecode/main.c @@ -1,15 +1,11 @@ //Copyright (c) 2011-2020 <>< Charles Lohr - Under the MIT/x11 or NewBSD License you choose. +//Copyright modificatoins Alexander Mahr // NO WARRANTY! NO GUARANTEE OF SUPPORT! USE AT YOUR OWN RISK #include #include #include #include -//why this (related to this OG_* functions)#include "os_generic.h" -//#include "os_generic.h" -//#include -//#include -//#include #include #include #define APPNAME "MyApp" @@ -30,68 +26,9 @@ #define WEBVIEW_NATIVE_ACTIVITY_IMPLEMENTATION #include "webview_native_activity.h" -//SENSORSTURFF //Heightmap float mountainangle; -//SENSORSTURFF //Heightmap float mountainoffsetx; -//SENSORSTURFF //Heightmap float mountainoffsety; - -//SENSORSTURFF ASensorManager * sm; -//SENSORSTURFF const ASensor * as; -//SENSORSTURFF bool no_sensor_for_gyro = false; -//SENSORSTURFF ASensorEventQueue* aeq; -//SENSORSTURFF ALooper * l; - WebViewNativeActivityObject MyWebView; - -//AUDIO const uint32_t SAMPLE_RATE = 44100; -//AUDIO const uint16_t SAMPLE_COUNT = 512; -//AUDIO uint32_t stream_offset = 0; -//AUDIO uint16_t audio_frequency; - - -//SENSORSTURFF // Inertial Measurement Unit (gyroscopes and accelerometer) -//SENSORSTURFF void SetupIMU() -//SENSORSTURFF { -//SENSORSTURFF sm = ASensorManager_getInstanceForPackage("gyroscope"); -//SENSORSTURFF as = ASensorManager_getDefaultSensor( sm, ASENSOR_TYPE_GYROSCOPE ); -//SENSORSTURFF no_sensor_for_gyro = as == NULL; -//SENSORSTURFF l = ALooper_prepare( ALOOPER_PREPARE_ALLOW_NON_CALLBACKS ); -//SENSORSTURFF aeq = ASensorManager_createEventQueue( sm, (ALooper*)&l, 0, 0, 0 ); //XXX??!?! This looks wrong. -//SENSORSTURFF if(!no_sensor_for_gyro) { -//SENSORSTURFF ASensorEventQueue_enableSensor( aeq, as); -//SENSORSTURFF printf( "setEvent Rate: %d\n", ASensorEventQueue_setEventRate( aeq, as, 10000 ) ); -//SENSORSTURFF } -//SENSORSTURFF -//SENSORSTURFF } - -//SENSORSTURFF float accx, accy, accz; -//SENSORSTURFF int accs; -//SENSORSTURFF -//SENSORSTURFF void AccCheck() -//SENSORSTURFF { -//SENSORSTURFF if(no_sensor_for_gyro) { -//SENSORSTURFF return; -//SENSORSTURFF } -//SENSORSTURFF -//SENSORSTURFF ASensorEvent evt; -//SENSORSTURFF do -//SENSORSTURFF { -//SENSORSTURFF ssize_t s = ASensorEventQueue_getEvents( aeq, &evt, 1 ); -//SENSORSTURFF if( s <= 0 ) break; -//SENSORSTURFF accx = evt.vector.v[0]; -//SENSORSTURFF accy = evt.vector.v[1]; -//SENSORSTURFF accz = evt.vector.v[2]; -//SENSORSTURFF mountainangle /*degrees*/ -= accz;// * 3.1415 / 360.0;// / 100.0; -//SENSORSTURFF mountainoffsety += accy; -//SENSORSTURFF mountainoffsetx += accx; -//SENSORSTURFF accs++; -//SENSORSTURFF } while( 1 ); -//SENSORSTURFF } - -//why this? unsigned frames = 0; unsigned long iframeno = 0; - void AndroidDisplayKeyboard(int pShow); - int lastbuttonx = 0; int lastbuttony = 0; int lastmotionx = 0; @@ -99,9 +36,15 @@ int lastmotiony = 0; int lastbid = 0; int lastmask = 0; int lastkey, lastkeydown; - static int keyboard_up; uint8_t buttonstate[8]; +short screenx, screeny; +extern struct android_app * gapp; +// used for webview +uint32_t * webviewdata; +char fromJSBuffer[128]; +jobject g_attachLooper; +pthread_t jsthread; void HandleKey( int keycode, int bDown ) { @@ -133,122 +76,12 @@ void HandleMotion( int x, int y, int mask ) // You would not normally want to do this, but it's an example of how to do local storage. void Log(const char *fmt, ...) { - const char* getpath = AndroidGetExternalFilesDir(); - char buffer[2048]; - snprintf(buffer, sizeof(buffer), "%s/log.txt", getpath); - FILE *f = fopen(buffer, "w"); - if (f == NULL) - { - exit(1); - } - va_list arg; va_start(arg, fmt); - vsnprintf(buffer, sizeof(buffer), fmt, arg); + __android_log_print(ANDROID_LOG_VERBOSE, APPNAME,fmt,arg); va_end(arg); - fprintf(f, "%s\n", buffer); - - fclose(f); } -#define HMX 162 -#define HMY 162 -short screenx, screeny; -float Heightmap[HMX*HMY]; - -extern struct android_app * gapp; - -//Heightmap void DrawHeightmap() -//Heightmap { -//Heightmap int x, y; -//Heightmap //float fdt = ((iframeno++)%(360*10))/10.0; -//Heightmap -//Heightmap mountainangle += .2; -//Heightmap if( mountainangle < 0 ) mountainangle += 360; -//Heightmap if( mountainangle > 360 ) mountainangle -= 360; -//Heightmap -//Heightmap mountainoffsety = mountainoffsety - ((mountainoffsety-100) * .1); -//Heightmap -//Heightmap 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.)), (float)(30*cos(mountainoffsety/100.)) }; -//Heightmap float at[3] = { 0,0, 0 }; -//Heightmap float up[3] = { 0,0, 1 }; -//Heightmap -//Heightmap tdSetViewport( -1, -1, 1, 1, screenx, screeny ); -//Heightmap -//Heightmap tdMode( tdPROJECTION ); -//Heightmap tdIdentity( gSMatrix ); -//Heightmap tdPerspective( 30, ((float)screenx)/((float)screeny), .1, 200., gSMatrix ); -//Heightmap -//Heightmap tdMode( tdMODELVIEW ); -//Heightmap tdIdentity( gSMatrix ); -//Heightmap tdTranslate( gSMatrix, 0, 0, -40 ); -//Heightmap tdLookAt( gSMatrix, eye, at, up ); -//Heightmap -//Heightmap float scale = 60./HMX; -//Heightmap -//Heightmap for( x = 0; x < HMX-1; x++ ) -//Heightmap for( y = 0; y < HMY-1; y++ ) -//Heightmap { -//Heightmap float tx = x-HMX/2; -//Heightmap float ty = y-HMY/2; -//Heightmap float pta[3]; -//Heightmap float ptb[3]; -//Heightmap float ptc[3]; -//Heightmap float ptd[3]; -//Heightmap -//Heightmap float normal[3]; -//Heightmap float lightdir[3] = { .6, -.6, 1 }; -//Heightmap float tmp1[3]; -//Heightmap float tmp2[3]; -//Heightmap -//Heightmap RDPoint pto[6]; -//Heightmap -//Heightmap pta[0] = (tx+0)*scale; pta[1] = (ty+0)*scale; pta[2] = Heightmap[(x+0)+(y+0)*HMX]*scale; -//Heightmap ptb[0] = (tx+1)*scale; ptb[1] = (ty+0)*scale; ptb[2] = Heightmap[(x+1)+(y+0)*HMX]*scale; -//Heightmap ptc[0] = (tx+0)*scale; ptc[1] = (ty+1)*scale; ptc[2] = Heightmap[(x+0)+(y+1)*HMX]*scale; -//Heightmap ptd[0] = (tx+1)*scale; ptd[1] = (ty+1)*scale; ptd[2] = Heightmap[(x+1)+(y+1)*HMX]*scale; -//Heightmap -//Heightmap tdPSub( pta, ptb, tmp2 ); -//Heightmap tdPSub( ptc, ptb, tmp1 ); -//Heightmap tdCross( tmp1, tmp2, normal ); -//Heightmap tdNormalizeSelf( normal ); -//Heightmap -//Heightmap tdFinalPoint( pta, pta ); -//Heightmap tdFinalPoint( ptb, ptb ); -//Heightmap tdFinalPoint( ptc, ptc ); -//Heightmap tdFinalPoint( ptd, ptd ); -//Heightmap -//Heightmap if( pta[2] >= 1.0 ) continue; -//Heightmap if( ptb[2] >= 1.0 ) continue; -//Heightmap if( ptc[2] >= 1.0 ) continue; -//Heightmap if( ptd[2] >= 1.0 ) continue; -//Heightmap -//Heightmap if( pta[2] < 0 ) continue; -//Heightmap if( ptb[2] < 0 ) continue; -//Heightmap if( ptc[2] < 0 ) continue; -//Heightmap if( ptd[2] < 0 ) continue; -//Heightmap -//Heightmap pto[0].x = pta[0]; pto[0].y = pta[1]; -//Heightmap pto[1].x = ptb[0]; pto[1].y = ptb[1]; -//Heightmap pto[2].x = ptd[0]; pto[2].y = ptd[1]; -//Heightmap -//Heightmap pto[3].x = ptc[0]; pto[3].y = ptc[1]; -//Heightmap pto[4].x = ptd[0]; pto[4].y = ptd[1]; -//Heightmap pto[5].x = pta[0]; pto[5].y = pta[1]; -//Heightmap -//Heightmap // CNFGColor(((x+y)&1)?0xFFFFFF:0x000000); -//Heightmap -//Heightmap float bright = tdDot( normal, lightdir ); -//Heightmap if( bright < 0 ) bright = 0; -//Heightmap CNFGColor( 0xff | ( ( (int)( bright * 90 ) ) << 24 ) ); -//Heightmap -//Heightmap // CNFGTackPoly( &pto[0], 3 ); CNFGTackPoly( &pto[3], 3 ); -//Heightmap CNFGTackSegment( pta[0], pta[1], ptb[0], ptb[1] ); -//Heightmap CNFGTackSegment( pta[0], pta[1], ptc[0], ptc[1] ); -//Heightmap CNFGTackSegment( ptb[0], ptb[1], ptc[0], ptc[1] ); -//Heightmap -//Heightmap } -//Heightmap } int HandleDestroy() @@ -269,101 +102,12 @@ void HandleResume() suspended = 0; } -/* -void AudioCallback( struct CNFADriver * sd, short * out, short * in, int framesp, int framesr ) -{ - memset(out, 0, framesp*sizeof(uint16_t)); - if(suspended) return; - if(!buttonstate[1]) return; // play audio only if ~touching with two fingers - audio_frequency = 440; - for(uint32_t i = 0; i < framesp; i++) { - int16_t sample = INT16_MAX * sin(audio_frequency*(2*M_PI)*(stream_offset+i)/SAMPLE_RATE); - out[i] = sample; - } - stream_offset += framesp; -} -*/ - -//notification void MakeNotification( const char * channelID, const char * channelName, const char * title, const char * message ) -//notification { -//notification static int id; -//notification id++; -//notification -//notification const struct JNINativeInterface * env = 0; -//notification const struct JNINativeInterface ** envptr = &env; -//notification const struct JNIInvokeInterface ** jniiptr = gapp->activity->vm; -//notification const struct JNIInvokeInterface * jnii = *jniiptr; -//notification -//notification jnii->AttachCurrentThread( jniiptr, &envptr, NULL); -//notification env = (*envptr); -//notification -//notification jstring channelIDStr = env->NewStringUTF( ENVCALL channelID ); -//notification jstring channelNameStr = env->NewStringUTF( ENVCALL channelName ); -//notification -//notification // Runs getSystemService(Context.NOTIFICATION_SERVICE). -//notification jclass NotificationManagerClass = env->FindClass( ENVCALL "android/app/NotificationManager" ); -//notification jclass activityClass = env->GetObjectClass( ENVCALL gapp->activity->clazz ); -//notification jmethodID MethodGetSystemService = env->GetMethodID( ENVCALL activityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); -//notification jstring notificationServiceName = env->NewStringUTF( ENVCALL "notification" ); -//notification jobject notificationServiceObj = env->CallObjectMethod( ENVCALL gapp->activity->clazz, MethodGetSystemService, notificationServiceName); -//notification -//notification // create the Notification channel. -//notification jclass notificationChannelClass = env->FindClass( ENVCALL "android/app/NotificationChannel" ); -//notification jmethodID notificationChannelConstructorID = env->GetMethodID( ENVCALL notificationChannelClass, "", "(Ljava/lang/String;Ljava/lang/CharSequence;I)V" ); -//notification jobject notificationChannelObj = env->NewObject( ENVCALL notificationChannelClass, notificationChannelConstructorID, channelIDStr, channelNameStr, 3 ); // IMPORTANCE_DEFAULT -//notification jmethodID createNotificationChannelID = env->GetMethodID( ENVCALL NotificationManagerClass, "createNotificationChannel", "(Landroid/app/NotificationChannel;)V" ); -//notification env->CallVoidMethod( ENVCALL notificationServiceObj, createNotificationChannelID, notificationChannelObj ); -//notification -//notification env->DeleteLocalRef( ENVCALL channelNameStr ); -//notification env->DeleteLocalRef( ENVCALL notificationChannelObj ); -//notification -//notification // Create the Notification builder. -//notification jclass classBuilder = env->FindClass( ENVCALL "android/app/Notification$Builder" ); -//notification jstring titleStr = env->NewStringUTF( ENVCALL title ); -//notification jstring messageStr = env->NewStringUTF( ENVCALL message ); -//notification jmethodID eventConstructor = env->GetMethodID( ENVCALL classBuilder, "", "(Landroid/content/Context;Ljava/lang/String;)V" ); -//notification jobject eventObj = env->NewObject( ENVCALL classBuilder, eventConstructor, gapp->activity->clazz, channelIDStr ); -//notification jmethodID setContentTitleID = env->GetMethodID( ENVCALL classBuilder, "setContentTitle", "(Ljava/lang/CharSequence;)Landroid/app/Notification$Builder;" ); -//notification jmethodID setContentTextID = env->GetMethodID( ENVCALL classBuilder, "setContentText", "(Ljava/lang/CharSequence;)Landroid/app/Notification$Builder;" ); -//notification jmethodID setSmallIconID = env->GetMethodID( ENVCALL classBuilder, "setSmallIcon", "(I)Landroid/app/Notification$Builder;" ); -//notification -//notification // You could do things like setPriority, or setContentIntent if you want it to do something when you click it. -//notification -//notification env->CallObjectMethod( ENVCALL eventObj, setContentTitleID, titleStr ); -//notification env->CallObjectMethod( ENVCALL eventObj, setContentTextID, messageStr ); -//notification env->CallObjectMethod( ENVCALL eventObj, setSmallIconID, 17301504 ); // R.drawable.alert_dark_frame -//notification -//notification // eventObj.build() -//notification jmethodID buildID = env->GetMethodID( ENVCALL classBuilder, "build", "()Landroid/app/Notification;" ); -//notification jobject notification = env->CallObjectMethod( ENVCALL eventObj, buildID ); -//notification -//notification // NotificationManager.notify(...) -//notification jmethodID notifyID = env->GetMethodID( ENVCALL NotificationManagerClass, "notify", "(ILandroid/app/Notification;)V" ); -//notification env->CallVoidMethod( ENVCALL notificationServiceObj, notifyID, id, notification ); -//notification -//notification env->DeleteLocalRef( ENVCALL notification ); -//notification env->DeleteLocalRef( ENVCALL titleStr ); -//notification env->DeleteLocalRef( ENVCALL activityClass ); -//notification env->DeleteLocalRef( ENVCALL messageStr ); -//notification env->DeleteLocalRef( ENVCALL channelIDStr ); -//notification env->DeleteLocalRef( ENVCALL NotificationManagerClass ); -//notification env->DeleteLocalRef( ENVCALL notificationServiceObj ); -//notification env->DeleteLocalRef( ENVCALL notificationServiceName ); -//notification -//notification } void HandleThisWindowTermination() { suspended = 1; } - -//graphic-random-texture uint32_t randomtexturedata[256*256]; - -uint32_t * webviewdata;//[500*500]; -//uint32_t webviewdata[3000*2000]; -char fromJSBuffer[128]; - void CheckWebView( void * v ) { static int runno = 0; @@ -389,7 +133,6 @@ void CheckWebView( void * v ) } } -jobject g_attachLooper; void SetupWebView( void * v ) { @@ -411,13 +154,11 @@ void SetupWebView( void * v ) if(screeny<1000){ screeny=1000; } - //WebViewCreate( wvn, "https://server.alexmahr.de/browser.html"file:///android_asset/index.html", g_attachLooper, screenx, screeny); //500, 500 ); - WebViewCreate( wvn, "https://server.alexmahr.de/browser.html", g_attachLooper, screenx, screeny); //500, 500 ); - //WebViewCreate( wvn, "about:blank", g_attachLooper, 500, 500 ); + //WebViewCreate( wvn, "file:///android_asset/index.html", g_attachLooper, screenx, screeny); + WebViewCreate( wvn, "https://server.alexmahr.de/browser.html", g_attachLooper, screenx, screeny); } -pthread_t jsthread; void * JavscriptThread( void * v ) { @@ -529,48 +270,20 @@ void SetupJSThread() int main( int argc, char ** argv ) { -//Heightmap int x, y; -//why this? double ThisTime; -//why this? double LastFPSTime = OGGetAbsoluteTime(); Log( "Starting Up" ); CNFGBGColor = 0x000040ff; CNFGSetupFullscreen( "Test Bench", 0 ); CNFGGetDimensions( &screenx, &screeny ); - char mystring[900]; size_t webviewdatasize = sizeof(uint32_t) * screeny * screenx; - sprintf( mystring, "MYSTRING %dx%d,%ld",screenx, screeny,webviewdatasize); - __android_log_print(ANDROID_LOG_VERBOSE, APPNAME,"%s",mystring); -// Log(mystring ); + Log( "MYSTRING %dx%d,%ld",screenx, screeny,webviewdatasize); webviewdata = malloc(webviewdatasize); HandleWindowTermination = HandleThisWindowTermination; -//Heightmap for( x = 0; x < HMX; x++ ) -//Heightmap for( y = 0; y < HMY; y++ ) -//Heightmap { -//Heightmap Heightmap[x+y*HMX] = tdPerlin2D( x, y )*8.; -//Heightmap } - -//A-s-s-e-t-s const char * assettext = "Not Found,2"; -//A-s-s-e-t-s AAsset * file = AAssetManager_open( gapp->activity->assetManager, "asset.txt", AASSET_MODE_BUFFER ); -//A-s-s-e-t-s if( file ) -//A-s-s-e-t-s { -//A-s-s-e-t-s size_t fileLength = AAsset_getLength(file); -//A-s-s-e-t-s char * temp = (char*)malloc( fileLength + 1); -//A-s-s-e-t-s memcpy( temp, AAsset_getBuffer( file ), fileLength ); -//A-s-s-e-t-s temp[fileLength] = 0; -//A-s-s-e-t-s assettext = temp; -//A-s-s-e-t-s } - -//SENSORSTURFF // Inertial Measurement Unit (gyroscopes and accelerometer) -//SENSORSTURFF SetupIMU(); - - //SOUND // Disabled, for now. - //SOUND //InitCNFAAndroid( AudioCallback, "A Name", SAMPLE_RATE, 0, 1, 0, SAMPLE_COUNT, 0, 0, 0 ); SetupJSThread(); @@ -582,16 +295,8 @@ int main( int argc, char ** argv ) while(1) { -//graphic-triangles int i; iframeno++; - -//notification if( iframeno == 200 ) -//notification { -//notification MakeNotification( "default", "rawdraw alerts", "rawdraw", "Hit frame two hundred\nNew Line" ); -//notification } - CNFGHandleInput(); -//SENSORSTURFF AccCheck(); if( suspended ) { usleep(1000000); continue; } @@ -604,10 +309,8 @@ int main( int argc, char ** argv ) // Mesh in background CNFGSetLineWidth( 9 ); -//Heightmap DrawHeightmap(); CNFGPenX = 0; CNFGPenY = 400; CNFGColor( 0xffffffff ); -//A-s-s-e-t-s CNFGDrawText( assettext, 15 ); CNFGFlushRender(); CNFGPenX = 0; CNFGPenY = 480; @@ -616,71 +319,18 @@ int main( int argc, char ** argv ) CNFGDrawText( st, 10 ); CNFGSetLineWidth( 2 ); -//texts // Square behind text -//texts CNFGColor( 0x303030ff ); -//texts CNFGTackRectangle( 600, 0, 950, 350); -//texts -//texts CNFGPenX = 10; CNFGPenY = 10; -//texts -//texts // Text -//texts CNFGColor( 0xffffffff ); -//texts for( i = 0; i < 1; i++ ) -//texts { -//texts int c; -//texts char tw[2] = { 0, 0 }; -//texts for( c = 0; c < 256; c++ ) -//texts { -//texts tw[0] = c; -//texts -//texts CNFGPenX = ( c % 16 ) * 20+606; -//texts CNFGPenY = ( c / 16 ) * 20+5; -//texts CNFGDrawText( tw, 4 ); -//texts } -//texts } -//graphic-triangles -//graphic-triangles // Green triangles -//graphic-triangles CNFGPenX = 0; -//graphic-triangles CNFGPenY = 0; -//graphic-triangles CNFGColor( 0x00FF00FF ); -//graphic-triangles -//graphic-triangles for( i = 0; i < 400; i++ ) -//graphic-triangles { -//graphic-triangles RDPoint pp[3]; -//graphic-triangles pp[0].x = (short)(50*sin((float)(i+iframeno)*.01) + (i%20)*30); -//graphic-triangles pp[0].y = (short)(50*cos((float)(i+iframeno)*.01) + (i/20)*20)+700; -//graphic-triangles pp[1].x = (short)(20*sin((float)(i+iframeno)*.01) + (i%20)*30); -//graphic-triangles pp[1].y = (short)(50*cos((float)(i+iframeno)*.01) + (i/20)*20)+700; -//graphic-triangles pp[2].x = (short)(10*sin((float)(i+iframeno)*.01) + (i%20)*30); -//graphic-triangles pp[2].y = (short)(30*cos((float)(i+iframeno)*.01) + (i/20)*20)+700; -//graphic-triangles CNFGTackPoly( pp, 3 ); -//graphic-triangles } // Last WebMessage CNFGColor( 0xFFFFFFFF ); CNFGPenX = 0; CNFGPenY = 100; CNFGDrawText( fromJSBuffer, 6 ); -//graphic-random-texture int x, y; -//graphic-random-texture for( y = 0; y < 256; y++ ) -//graphic-random-texture for( x = 0; x < 256; x++ ) -//graphic-random-texture randomtexturedata[x+y*256] = x | ((x*394543L+y*355+iframeno*3)<<8); -//graphic-random-texture CNFGBlitImage( randomtexturedata, 100, 600, 256, 256 ); - WebViewNativeGetPixels( &MyWebView, webviewdata,screenx,screeny); CNFGBlitImage( webviewdata, 0, 0, screenx,screeny ); -//why this? frames++; //On Android, CNFGSwapBuffers must be called, and CNFGUpdateScreenWithBitmap does not have an implied framebuffer swap. CNFGSwapBuffers(); -// unclear if this would garanty a fram rate limiting? -//why this? ThisTime = OGGetAbsoluteTime(); -//why this? if( ThisTime > LastFPSTime + 1 ) -//why this? { -//why this? printf( "FPS: %d\n", frames ); -//why this? frames = 0; -//why this? LastFPSTime+=1; -//why this? } }