@@ -91,6 +91,9 @@ typedef struct {
9191 float scroll_offset ; /* Accumulated scroll offset */
9292 float last_frame_time ; /* Time of last frame for delta calculation */
9393 int roller_effect ; /* Roller text effect: 0=all, 1=no outline, 2=no outline/glow, 3=color outline */
94+ /* Tunnel effect optimization LUTs */
95+ float * tunnel_distance ; /* Pre-calculated distance from center */
96+ float * tunnel_angle ; /* Pre-calculated angle from center */
9497} DemoContext ;
9598
9699/* Plasma effect - optimized with lower resolution and LUT */
@@ -802,11 +805,16 @@ void render_tunnel(DemoContext *ctx)
802805
803806 for (int y = 0 ; y < HEIGHT ; y ++ ) {
804807 for (int x = 0 ; x < WIDTH ; x ++ ) {
808+ int idx = y * WIDTH + x ;
809+
805810 float dx = x - eye_x ;
806811 float dy = y - eye_y ;
807812
808- float distance = sqrt (dx * dx + dy * dy );
809- float angle = atan2 (dy , dx );
813+ float distance = sqrtf (dx * dx + dy * dy );
814+ if (distance < 1.0f ) distance = 1.0f ; /* Avoid division by zero */
815+
816+ /* Use pre-calculated angle from LUT (reduces atan2 calls) */
817+ float angle = ctx -> tunnel_angle [idx ];
810818
811819 float u = t * 0.5 + 10.0 / distance ;
812820 float v = angle / PI + t * 0.2 ;
@@ -827,7 +835,7 @@ void render_tunnel(DemoContext *ctx)
827835 g = (int )(g * vignette );
828836 b = (int )(b * vignette );
829837
830- ctx -> pixels [y * WIDTH + x ] = 0xFF000000 | (r << 16 ) | (g << 8 ) | b ;
838+ ctx -> pixels [idx ] = 0xFF000000 | (r << 16 ) | (g << 8 ) | b ;
831839 }
832840 }
833841}
@@ -2696,6 +2704,25 @@ int main(int argc, char *argv[])
26962704 ctx .stars [i ].z = (rand () % 10000 ) / 100.0f ;
26972705 }
26982706
2707+ /* Initialize tunnel LUT for optimization */
2708+ ctx .tunnel_distance = malloc (WIDTH * HEIGHT * sizeof (float ));
2709+ ctx .tunnel_angle = malloc (WIDTH * HEIGHT * sizeof (float ));
2710+ if (ctx .tunnel_distance && ctx .tunnel_angle ) {
2711+ float center_x = WIDTH / 2.0f ;
2712+ float center_y = HEIGHT / 2.0f ;
2713+ for (int y = 0 ; y < HEIGHT ; y ++ ) {
2714+ for (int x = 0 ; x < WIDTH ; x ++ ) {
2715+ int idx = y * WIDTH + x ;
2716+ float dx = x - center_x ;
2717+ float dy = y - center_y ;
2718+ ctx .tunnel_distance [idx ] = sqrtf (dx * dx + dy * dy );
2719+ ctx .tunnel_angle [idx ] = atan2f (dy , dx );
2720+ }
2721+ }
2722+ } else {
2723+ fprintf (stderr , "Warning: Failed to allocate tunnel LUT\n" );
2724+ }
2725+
26992726 /* Load and play music from embedded data */
27002727#ifdef HAVE_MUSIC
27012728 if (audio_available ) {
@@ -2853,6 +2880,13 @@ int main(int argc, char *argv[])
28532880 if (ctx .font_outline ) {
28542881 TTF_CloseFont (ctx .font_outline );
28552882 }
2883+ /* Free tunnel LUT */
2884+ if (ctx .tunnel_distance ) {
2885+ free (ctx .tunnel_distance );
2886+ }
2887+ if (ctx .tunnel_angle ) {
2888+ free (ctx .tunnel_angle );
2889+ }
28562890 SDL_DestroyTexture (ctx .texture );
28572891 SDL_DestroyRenderer (ctx .renderer );
28582892 SDL_DestroyWindow (ctx .window );
0 commit comments