BASIC Developer & Support Resources > Scripting Languages

CBASIC

<< < (2/3) > >>

John:
This is an example of IUP OpenGL spinning cube in CBASIC.


--- Code: C ---// gcc glcube.c -liup -liupgl -lGL -lGLU -o glcube #include <stdio.h>#include <stdlib.h>#include <iup.h>#include <iupgl.h>#include <GL/gl.h>#include <GL/glu.h>#include "cbasic.h" DIM LOCAL Ihandle AS *canvas;DIM LOCAL float AS t=0; LOCAL SUB polygon(int a, int b, int c, int d)BEGIN_SUB  DIM double AS vertices[][3]={{-1,-1, 1}, {-1, 1, 1}, { 1, 1, 1}, { 1,-1, 1},                              {-1,-1,-1}, {-1, 1,-1}, { 1, 1,-1}, { 1,-1,-1}};  glBegin(GL_POLYGON);  glVertex3dv(vertices[a]);  glVertex3dv(vertices[b]);  glVertex3dv(vertices[c]);  glVertex3dv(vertices[d]);  glEnd();END_SUB LOCAL SUB colorCube(void)  BEGIN_SUB  glColor3f(1,0,0);  glNormal3f(1,0,0);  polygon(2,3,7,6);  glColor3f(0,1,0);  glNormal3f(0,1,0);  polygon(1,2,6,5);  glColor3f(0,0,1);  glNormal3f(0,0,1);  polygon(0,3,2,1);  glColor3f(1,0,1);  glNormal3f(0,-1,0);  polygon(3,0,4,7);  glColor3f(1,1,0);  glNormal3f(0,0,-1);  polygon(4,5,6,7);  glColor3f(0,1,1);  glNormal3f(-1,0,0);  polygon(5,4,0,1);END_SUB LOCAL FUNCTION int repaint_cb(Ihandle *self) BEGIN_FUNCTION  IupGLMakeCurrent(self);  glClearColor(0.3f, 0.3f, 0.3f, 1.0f);  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);  glEnable(GL_DEPTH_TEST);  glMatrixMode(GL_MODELVIEW);  glPushMatrix();  glTranslatef( 0.0f, 0.0f , 0.0f );  glScalef( 1.0f, 1.0f, 1.0f );  glRotatef(t,0,0,1);  colorCube();  glPopMatrix();  IupGLSwapBuffers(self);  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int resize_cb(Ihandle *self, int width, int height)BEGIN_FUNCTION  IupGLMakeCurrent(self);  glViewport(0, 0, width, height);  glMatrixMode(GL_MODELVIEW);  glLoadIdentity();  glMatrixMode(GL_PROJECTION);  glLoadIdentity();  gluPerspective(60,4./3.,1.,15);  gluLookAt(3,3,3, 0,0,0, 0,0,1);  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION FUNCTION int idle_cd(void)BEGIN_FUNCTION  t+=1;  repaint_cb(canvas);  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION FUNCTION int exit_cb(void)BEGIN_FUNCTION  RETURN_FUNCTION(IUP_CLOSE);END_FUNCTION FUNCTION Ihandle* initDialog(void)BEGIN_FUNCTION  DIM Ihandle* AS dialog;  canvas = IupGLCanvas(NULL);  IupSetAttribute(canvas,"RASTERSIZE","640x480");  IupSetAttribute(canvas, "BUFFER","DOUBLE");  IupSetAttribute(canvas, "DEPTH_SIZE", "16");  IupSetCallback(canvas, "ACTION", (Icallback) repaint_cb);    IupSetCallback(canvas, "RESIZE_CB",(Icallback) resize_cb);   dialog = IupDialog(canvas);  IupSetAttribute(dialog, "TITLE", "IUP 3D OpenGL");  IupSetCallback(dialog, "CLOSE_CB", (Icallback) exit_cb);  IupSetFunction("IDLE_ACTION", (Icallback)idle_cd);  RETURN_FUNCTION(dialog);END_FUNCTION MAINBEGIN_FUNCTION  DIM Ihandle* AS dialog;  IupOpen(0, 0);  IupGLCanvasOpen();  dialog = initDialog();  IupShowXY(dialog, IUP_CENTER, IUP_CENTER);  IupMainLoop();  IupClose();END_FUNCTION 

John:
I was able to get my ScriptBasic IDE/Debugger going. I haven't converted it to CBASIC yet but I thought I would share a screen shot.

John:
This is an example of using CBASIC with the Raspberry Pi Pico micro controller.


--- Code: C ---// RPi Pico onboard LED blink #include "pico/stdlib.h"#include "pico/binary_info.h"#include "cbasic.h" CONST uint AS LED_PIN = 25; MAINBEGIN_FUNCTION  bi_decl(bi_program_description("First Blink"));  bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));  gpio_init(LED_PIN);  gpio_set_dir(LED_PIN, GPIO_OUT);  DEF_WHILE(1)  BEGIN_WHILE    gpio_put(LED_PIN, 0);    sleep_ms(250);    gpio_put(LED_PIN, 1);    sleep_ms(1000);  WENDEND_FUNCTION 

My original Pico kit I bought from Vilros didn't have the headers soldered in place. My attempt to install them trashed the board. 😥 I purchased the following as a replacement.

DIYmall ESP32-PICO-KIT

John:
This is an example of the IUP GTK WebBrowser control.


--- Code: C ---// gcc webbrowser.c -liup -liupweb -o webbrowser #include <stdlib.h>#include <stdio.h>#include <string.h>#include <iup.h>#include <iupweb.h>#include "cbasic.h" LOCAL FUNCTION int navigate_cb(Ihandle* self, char* url)BEGIN_FUNCTION  PRINT("NAVIGATE_CB: %s\n", url);  (void)self;  IF (strstr(url, "download")!=NULL) THEN    RETURN_FUNCTION(IUP_IGNORE);  END_IF  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION                   LOCAL FUNCTION int error_cb(Ihandle* self, char* url)BEGIN_FUNCTION  PRINT("ERROR_CB: %s\n", url);  (void)self;  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int completed_cb(Ihandle* self, char* url)BEGIN_FUNCTION  PRINT("COMPLETED_CB: %s\n", url);  (void)self;  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int newwindow_cb(Ihandle* self, char* url)BEGIN_FUNCTION  PRINT("NEWWINDOW_CB: %s\n", url);  IupSetAttribute(self, "VALUE", url);  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int back_cb(Ihandle* self)BEGIN_FUNCTION  DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");  IupSetAttribute(web, "BACKFORWARD", "-1");  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int forward_cb(Ihandle* self)BEGIN_FUNCTION  DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");  IupSetAttribute(web, "BACKFORWARD", "1");  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int stop_cb(Ihandle* self)BEGIN_FUNCTION  DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");  IupSetAttribute(web, "STOP", NULL);  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int reload_cb(Ihandle* self)BEGIN_FUNCTION  DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");  IupSetAttribute(web, "RELOAD", NULL);  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION LOCAL FUNCTION int load_cb(Ihandle* self)BEGIN_FUNCTION  DIM Ihandle* AS txt  = (Ihandle*)IupGetAttribute(self, "MY_TEXT");  DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");  IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"));  RETURN_FUNCTION(IUP_DEFAULT);END_FUNCTION SUB WebBrowserTest(void)BEGIN_SUB  DIM Ihandle AS *txt, *dlg, *web;  DIM Ihandle AS *btLoad, *btReload, *btBack, *btForward, *btStop;  IupWebBrowserOpen();                web = IupWebBrowser();  dlg = IupDialog(IupVbox(IupHbox(btBack = IupButton("Back", NULL),        btForward = IupButton("Forward", NULL),        txt = IupText(""),        btLoad = IupButton("Load", NULL),        btReload = IupButton("Reload", NULL),        btStop = IupButton("Stop", NULL),        NULL),         web, NULL));  IupSetAttribute(dlg, "TITLE", "IupWebBrowser");  IupSetAttribute(dlg, "MY_TEXT", (char*)txt);  IupSetAttribute(dlg, "MY_WEB", (char*)web);  IupSetAttribute(dlg, "RASTERSIZE", "800x600");  IupSetAttribute(dlg, "MARGIN", "10x10");  IupSetAttribute(dlg, "GAP", "10");  IupSetAttribute(txt, "VALUE", "https://allbasic.info/forum");  IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"));  IupSetAttributeHandle(dlg, "DEFAULTENTER", btLoad);  IupSetAttribute(txt, "EXPAND", "HORIZONTAL");  IupSetCallback(btLoad, "ACTION", (Icallback)load_cb);  IupSetCallback(btReload, "ACTION", (Icallback)reload_cb);  IupSetCallback(btBack, "ACTION", (Icallback)back_cb);  IupSetCallback(btForward, "ACTION", (Icallback)forward_cb);  IupSetCallback(btStop, "ACTION", (Icallback)stop_cb);  IupSetCallback(web, "NEWWINDOW_CB", (Icallback)newwindow_cb);  IupSetCallback(web, "NAVIGATE_CB", (Icallback)navigate_cb);  IupSetCallback(web, "ERROR_CB", (Icallback)error_cb);  IupSetCallback(web, "COMPLETED_CB", (Icallback)completed_cb);  IupShow(dlg);END_SUB MAINBEGIN_FUNCTION  IupOpen(0, 0);  WebBrowserTest();  IupMainLoop();  IupClose();  RETURN_FUNCTION(EXIT_SUCCESS);END_FUNCTION 

jrs@RPi-Dev:~/sb/cbasic$ gcc webbrowser.c -liup -liupweb -o webbrowser
jrs@RPi-Dev:~/sb/cbasic$ ./webbrowser
NAVIGATE_CB: https://allbasic.info/forum
NAVIGATE_CB: https://allbasic.info/forum/
COMPLETED_CB: https://allbasic.info/forum/
HISTORY ITEMS
Current  00: (null)
jrs@RPi-Dev:~/sb/cbasic$

John:
I have used CBASIC extensively building ScriptBasic extension modules. Most of this work was done under Linux. I have decided to expanding CBASIC to work with Windows embedding ScriptBasic as a support library. The following example uses the ScriptBasic REPLACE function from CBASIC. (gcc with libscriba.dll) A Linux example of this can be found in a previous post in this thread. (RPi 4B - Ubuntu 64)


--- Code: C ---// gcc replace.c -IC:\sbgcc\source libscriba.dll -lpthread -o replace #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS ArgData;  DIM unsigned long AS fnsn;  DIM SbData AS FunctionResult;  pProgram = scriba_new(malloc,free);  scriba_SetFileName(pProgram, "sbfunctions");  scriba_LoadSourceProgram(pProgram);  scriba_Run(pProgram, "");  ArgData = scriba_NewSbArgs(pProgram,"s s s i i", "Hello World", "Hello", "Goodbye", 1, 1);  fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace");  scriba_CallArgEx(pProgram, fnsn, AT FunctionResult, 5, ArgData);  PRINT("%s\n", FunctionResult.v.s);  scriba_destroy(pProgram);  RETURN_FUNCTION(0);END_FUNCTION 
sbfunctions

--- Code: Script BASIC ---' sbfunctions - ScriptBasic CBASIC function wrappers FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)  sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)END FUNCTION 

C:\ScriptBASIC\cbasic>replace
Goodbye World

05/08/2021  05:39 PM            45,402 replace.exe


Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version