Author Topic: C BASIC: Julia Rings Hint  (Read 3717 times)

Offline Mike Lobanovsky

  • BASIC Developer
  • Posts: 193
    • FBSL
C BASIC: Julia Rings Hint
« on: December 10, 2013, 01:27:46 PM »
John,

Full sources for TinyPTC by Gaffer can be found at SourceForge. That's a complete package including an SDL solution. I'm not a member at the O2 Basic forum nor do I plan to be one, so this hint here may help you proceed with your Julia Rings project there.

The fractal animation is very spectacular and neat indeed. Thanks for the nice find.
Mike
_________________________
(3.6GHz Intel Core i5, 16GB RAM / 2 x GTX 650i SLI-bridged, 2GB VRAM)

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
Re: C BASIC: Julia Rings Hint
« Reply #1 on: December 10, 2013, 02:27:00 PM »
Thanks Mike!

That should get me back up an running again. I haven't seen any other submissions so the fractal top gun prize still stands.  ;D

Update

Got it working and it's runs great. I wonder if this is the first ever 64 bit version?



<a href="http://files.allbasic.info/C_BASIC/juliarings.swf" target="_blank" class="new_win">http://files.allbasic.info/C_BASIC/juliarings.swf</a>

Code: [Select]
/*
   The Lord of the Julia Rings
   The Fellowship of the Julia Rings
   Relsoft
   Rel.Betterwebber.com
   C BASIC version by JRS 12/9/2013
*/

#include <math.h>
#include <tinyptc.h>
#include "cbasic.h"

#define SCR_WIDTH  320  * 1
#define SCR_HEIGHT  240 * 1
#define SCR_SIZE  SCR_WIDTH*SCR_HEIGHT

#define PI  3.141593
#define MAXITER  20
#define MAXSIZE  4

DIM AS static int vpage[SCR_SIZE];
DIM AS static float lx[SCR_WIDTH];
DIM AS static float ly[SCR_WIDTH];

MAIN
BEGIN_FUNCTION
  DIM AS int px , py, i, pixel, red , grn , blu, tmp, i_last;
  DIM AS float p, q, xmin, xmax, ymin, ymax, theta, deltax , deltay, x, y, xsquare, ysquare, ytemp, ty;
  DIM AS float cmag, cmagsq, zmag, drad, drad_L, drad_H, ztot;
  DIM AS int PTR p_buffer;
  DIM AS int PTR p_bufferl;
  DIM AS unsigned int frame;
  xmin = -2.0;
  xmax =  2.0;
  ymin = -1.5;
  ymax =  1.5;
  deltax = (xmax - xmin) / (float)(SCR_WIDTH - 1);
  deltay = (ymax - ymin) / (float)(SCR_HEIGHT - 1);
  FOR (i = 0 TO i < SCR_WIDTH STEP INCR i)
  BEGIN_FOR
    lx[i] = xmin + i * deltax;
  NEXT
  FOR (i = 0 TO i < SCR_HEIGHT STEP INCR i)
  BEGIN_FOR
    ly[i] = ymax - i * deltay;
  NEXT
  IF (NOT ptc_open("C BASIC - Julia Rings",SCR_WIDTH,SCR_HEIGHT)) THEN_DO RETURN_FUNCTION(1);
  frame = 0;
  WHILE (1)
    BEGIN_WHILE
    p_buffer = vpage;
    p_bufferl = AT vpage[SCR_SIZE-1];
    frame = (frame + 1) & 0x7fffffff;
    theta = frame * PI / 180.0f;
    p = cos(theta) * sin(theta * .7);
    q = sin(theta) + sin(theta);
    p = p * .6;
    q = q * .6;
    cmag = sqrt(p *p + q* q);
    cmagsq = (p *p + q* q);
    drad = 0.04;
    drad_L = (cmag - drad);
    drad_L = drad_L * drad_L;
    drad_H = (cmag + drad);
    drad_H = drad_H * drad_H;
    FOR (py = 0 TO py < (SCR_HEIGHT >> 1) STEP INCR py)
    BEGIN_FOR
      ty = ly[py];
      FOR (px = 0 TO px < SCR_WIDTH STEP INCR px)
      BEGIN_FOR
        x = lx[px];
        y = ty;
        xsquare = 0;
        ysquare = 0;
        ztot =0;
        i = 0;
        WHILE (i < MAXITER AND (xsquare + ysquare) < MAXSIZE)
        BEGIN_WHILE
          xsquare = x * x;
          ysquare = y * y;
          ytemp = x * y * 2;
          x = xsquare - ysquare + p;
          y = ytemp + q;
          zmag = (x * x + y * y);
          IF (zmag < drad_H AND zmag > drad_L AND i > 0) THEN
            ztot = ztot + ( 1 - (fabs(zmag - cmagsq) / drad));
            i_last = i;
          END_IF
          INCR i;
          IF (zmag > 4.0) THEN_DO EXIT_WHILE
        WEND
        IF (ztot > 0) THEN
          i = (int)(sqrt(ztot) * 500);
        ELSE
          i = 0;
        END_IF
        IF (i < 256) THEN
          red = i;
        ELSE
          red = 255;
        END_IF
        IF (i < 512 AND i > 255) THEN
          grn = i - 256;
        ELSE_IF (i >= 512) THEN
          grn = 255;
        ELSE
          grn = 0;
        END_IF
        IF (i <= 768 AND i > 511) THEN
          blu = i - 512;
        ELSE_IF (i >= 768) THEN
          blu = 255;
        ELSE
          blu = 0;
        END_IF
        tmp = (int)((red + grn + blu) * 0.33);
        tmp = tmp & 0xFF;
        red = (int)((red + grn + tmp) * 0.33);
        red = red & 0xFF;
        grn = (int)((grn + blu + tmp) * 0.33);
        grn = grn & 0xFF;
        blu = (int)((blu + red + tmp) * 0.33);
        blu = blu & 0xFF;
        SELECT_CASE (i_last MOD 3)
        BEGIN_SELECT
        CASE 1:
          tmp = red;
          red = grn;
          grn = blu;
          blu = tmp;
          END_CASE
        CASE 2:
          tmp = red;
          blu = grn;
          red = blu;
          grn = tmp;
          END_CASE
        END_SELECT
        pixel = red << 16 | grn << 8 | blu;
        PTR p_buffer = pixel;
        PTR p_bufferl = pixel;
        INCR p_buffer;
        DECR p_bufferl;
      NEXT
    NEXT
    ptc_update(vpage);
  WEND
  RETURN_FUNCTION(0);
END_FUNCTION
« Last Edit: December 10, 2013, 09:56:30 PM by John »

Offline Mike Lobanovsky

  • BASIC Developer
  • Posts: 193
    • FBSL
Re: C BASIC: Julia Rings Hint
« Reply #2 on: December 10, 2013, 05:10:31 PM »
Nice job John,

But I'm afraid there was something like that done in QB64.

OTOH the following is certainly the first one done in FBSL:



and the source and executable are here in case anybody is interested.
Mike
_________________________
(3.6GHz Intel Core i5, 16GB RAM / 2 x GTX 650i SLI-bridged, 2GB VRAM)

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
Re: C BASIC: Julia Rings Hint
« Reply #3 on: December 10, 2013, 07:20:14 PM »
I tried yours under Wine and it ran smoothly and produce gorgeous fractals. The C BASIC version seems to be running too fast to appreciate the fractals being generated.  I think allowing the screen to be resized and adjust speed would be two nice improvements. Full screen works for me under Ubuntu.

FBSL

<a href="http://files.allbasic.info/FBSL/juliarings_fbsl.swf" target="_blank" class="new_win">http://files.allbasic.info/FBSL/juliarings_fbsl.swf</a>
« Last Edit: December 10, 2013, 11:46:45 PM by John »

Offline Mike Lobanovsky

  • BASIC Developer
  • Posts: 193
    • FBSL
Re: C BASIC: Julia Rings Hint
« Reply #4 on: December 11, 2013, 04:31:34 AM »
Thanks John,

The main window is resizable in Windows also by dragging any corner or side so I guess it should work the same in Wine too. And yes, the original implementation relies on a tight events loop and overloads the CPU. That's why I decided to move it to interpreted BASIC to slow down the phase change a bit.

Alternatively, a hi-res timer adjustable within 30 to 60FPS may be used to control the animation speed. The use of timer will also bring down the CPU load to about 2 to 5 per cent.
Mike
_________________________
(3.6GHz Intel Core i5, 16GB RAM / 2 x GTX 650i SLI-bridged, 2GB VRAM)

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
Re: C BASIC: Julia Rings Hint
« Reply #5 on: December 11, 2013, 09:26:58 AM »
There was no extra effort to do this right with the C BASIC version. You did a nice job with the FBSL version and in my mind wins the top gun prize.  :)

As Mike is leaving the podium after accepting his prize, a last minute development snatched the first place honors and Mike ended up taking second prize. A fair fight none the less.
« Last Edit: December 11, 2013, 04:47:53 PM by John »

Offline Mike Lobanovsky

  • BASIC Developer
  • Posts: 193
    • FBSL
Re: C BASIC: Julia Rings Hint
« Reply #6 on: December 12, 2013, 07:14:45 AM »
Yes John,

Charles' OpenGL is certainly a killer though I'd suggest using "glCopyTexSubImage2D" to update the texture instead of re-creating it from scratch in every frame. This would bring down the number of OpenGL state changes dramatically and increase the FPS rate accordingly.

Nonetheless his first prize is clearly a well-deserved decision. :)
Mike
_________________________
(3.6GHz Intel Core i5, 16GB RAM / 2 x GTX 650i SLI-bridged, 2GB VRAM)

Offline John

  • Forum Support / SB Dev
  • Posts: 1765
    • ScriptBasic Open Source Project
Re: C BASIC: Julia Rings Hint
« Reply #7 on: December 12, 2013, 12:14:49 PM »
I think you and Charles did a great job of enhancing the Julia Rings by Relsoft example and I thank both of you for the effort.


Offline Mike Lobanovsky

  • BASIC Developer
  • Posts: 193
    • FBSL
Re: C BASIC: Julia Rings Hint
« Reply #8 on: January 06, 2014, 02:26:47 AM »
I always hated that spasmodic white blotch on my screen. Now enjoy this hors concours submission of pure GDI beauty with extra coloration, random placement, and more pleasing resolution and speed. Fixes are minimal and can be easily ported anywhere.

Mike
_________________________
(3.6GHz Intel Core i5, 16GB RAM / 2 x GTX 650i SLI-bridged, 2GB VRAM)