Below is an OpenGL GPU accelerated version of the Flower Wheel.
It compiles and runs using Pelles C and MSVC using BED.
'====================================================
' Flower Wheel by BPLUS using extended QB64 commands
' BCX version by MrBcx June 24, 2024 MIT License
' Modified by MrBcx to use OpenGL GPU Acceleration
'====================================================
' Tested with MSCV and Pelles C
'====================================================
$BCXVERSION "8.0.2"
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
$PRAGMA comment(lib, "opengl32.lib")
$PRAGMA comment(lib, "glu32.lib")
GUI "Flower Wheel", PIXELS
CONST MAXX = 800 ' Experiment with sizes
CONST MAXY = MAXX ' keep it square
SUB FORMLOAD
GLOBAL AS HWND Form1
Form1 = BCX_FORM("Flower Wheel", 0, 0, MAXX, MAXY)
MODSTYLE(Form1, 0, WS_MAXIMIZEBOX | WS_MINIMIZEBOX, FALSE)
CENTER Form1
SHOW Form1
InitOpenGL()
CALL Drawit
END SUB
SUB InitOpenGL()
! PIXELFORMATDESCRIPTOR pfd = {
! sizeof(PIXELFORMATDESCRIPTOR), 1,
! PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
! PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 8, 0,
! PFD_MAIN_PLANE, 0, 0, 0, 0 };
DIM AS HDC hdc = GetDC(Form1)
DIM AS INT pixelFormat = ChoosePixelFormat(hdc, &pfd)
SetPixelFormat(hdc, pixelFormat, &pfd)
DIM AS HGLRC hglrc = wglCreateContext(hdc)
wglMakeCurrent(hdc, hglrc)
glEnable(GL_DEPTH_TEST)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glViewport(0, 0, MAXX, MAXY)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, MAXX, MAXY, 0) ' Adjusted to match the coordinate system used in GDI
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
ReleaseDC(Form1, hdc)
END SUB
SUB CleanupOpenGL()
DIM AS HDC hdc = GetDC(Form1)
DIM AS HGLRC hglrc = wglGetCurrentContext()
wglMakeCurrent(NULL, NULL)
wglDeleteContext(hglrc)
ReleaseDC(Form1, hdc)
END SUB
BEGIN EVENTS
SELECT CASE CBMSG
CASE WM_QUIT, WM_CLOSE, WM_DESTROY
CleanupOpenGL()
END
END SELECT
END EVENTS
SUB Drawit
DIM RAW Theta = 0 AS SINGLE
DIM RAW x = MAXX/2 AS INTEGER
DIM RAW y = MAXY/2 AS INTEGER
DIM RAW z = MAXX/5 AS INTEGER
DO
DIM AS HDC hdc = GetDC(Form1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Theta = Theta + 0.0175 ' PI/180 = 0.0175 (approx)
AnimateFlowers(x, y, z, 0.25, 4, Theta)
SwapBuffers(hdc)
ReleaseDC(Form1, hdc)
DOEVENTS
LOOP
END SUB
SUB AnimateFlowers(x AS LONG, y AS LONG, r AS LONG, a AS SINGLE, n AS LONG, Theta AS SINGLE)
'============================================================================================
' This is a recursive SUB. A recursive SUB/FUNCTION calls itself. See below...
'============================================================================================
DIM RAW AS LONG xx, yy
IF n > 0 THEN
FOR SINGLE t = 0 TO PI * 2 STEP 1.05 ' PI/3 = 1.05 (approx)
DOEVENTS
xx = x + r * COS(t + Theta)
yy = y + r * SIN(t + Theta)
'=====================================================================================
' Draw circles with OpenGL
glColor3f(1, 0, 0)
DrawCircle(xx, yy, r)
glColor3f(0, 1, 0)
DrawCircle(xx, yy, r - 4)
'=====================================================================================
CALL AnimateFlowers(xx, yy, a * r, a, n - 1, -Theta - n * 0.0175) ' 0.0175 = PI/180
'=====================================================================================
NEXT
END IF
END SUB
SUB DrawCircle(cx AS LONG, cy AS LONG, r AS LONG)
'============================================================================================
' Draw a circle with OpenGL
'============================================================================================
glBegin(GL_LINE_LOOP)
FOR SINGLE angle = 0 TO 2 * PI STEP PI / 18
glVertex2f(cx + r * COS(angle), cy + r * SIN(angle))
NEXT
glEnd()
END SUB
I've linked an animation that shows the speed difference between the original and the GL version.
It was recorded on a 2K screen, so it might be large on some monitors.
My GPU is integrated Intel 530 graphics, so nothing fancy but still a big improvement in speed.
https://bcxbasiccoders.com/GL_Speedup.gif