Tag: linux

  • FreeBASIC Graphics Counting and ASCII Character Program

    This is a picture that I made with a program I wrote in FreeBASIC. One of the cool things about FreeBASIC is that it has built in graphics as part of its standard library. This means it takes less work to get some text and pictures onto the screen compared to doing it with C and SDL.

    The full source code is included in this post. It is quite large but still smaller than what I usually do with C. Mostly, it serves as a reminder of how I achieved this to inspire me to make a game in FreeBASIC.

    main.bas

    #include "chastelib.bi"
    
    'define screen size variables before the graphics header
    dim shared as integer screen_width=1280,screen_height=720
    #include "chastelib-graphics.bi"
    
    ' Set the screen mode to size I want and 32 bits true color
    ScreenRes 1280, 720, 32
    
    'optionally change font
    
    'width screen_width/8,screen_height\8  'use 8x8 font (default)
    width screen_width\8,screen_height\16 'use 8x16 font
    
    chaste_checker
    
    color &h000000,&hFFFFFF 'set foreground and background text color using hex RGB codes
    
    dim as integer cursor_x=3,cursor_y=2,y_top=7
    
    locate cursor_y,cursor_x
    print "FreeBASIC Graphics Counting and ASCII Character Program"
    
    cursor_y+=2
    locate cursor_y,cursor_x
    print "Written by Chastity White Rose, the Pure Princess of Pixels, Polygons, and Ponies"
    
    
    cursor_x=2
    cursor_y=y_top
    locate cursor_y,cursor_x
    
    ' Keep the window open until the user presses a key
    
    dim as integer a,b,c
    
    radix=16
    
    a=0
    b=strint("100")
    c=32
    
    while a<b
    
    locate cursor_y,cursor_x
    
    radix=2
    int_width=8
    print intstr(a);" ";
    
    radix=16
    int_width=2
    print intstr(a);" ";
    
    radix=10
    int_width=3
    print intstr(a);
    
    'if(a>=32) and (a<=126) then
    print " "+chr(a);
    'endif
    
    'print
    
    a+=1
    cursor_y+=1:
    
    'cursor math to display to the right of last column of numbers
    if (a mod c)=0 then
    cursor_x+=20
    cursor_y=y_top
    end if
    
    wend
    
    sleep
    
    /'
     This is a FreeBASIC program.
    
     compile and run as:
    
     fbc main.bas && ./main
    '/
    

    chastelib.bi

    /'
     global variables to define radix and formatting
     for the intstr function
    '/
    dim shared as integer radix=2
    dim shared as integer int_width=1
    
    /'
     translation of intstr function for FreeBASIC
     by original C programmer Chastity White Rose
    '/
    function intstr(i as uinteger) as string
     dim as string s=""
     dim as integer w=0
     dim as byte c
    
     while i<>0 or w<int_width 
    
      c=i mod radix                  
      i\=radix                     
    
      if c<10 then 
      c+=48
      else
      c+=55
      end if
    
      s=chr(c)+s
    
      w+=1                     
     wend
    
    return s
    end function
    
    /'
     global variable for error detection in strint function
     this variable will be zero if last string was a number
    '/
    dim shared as integer strint_errors=0
    
    /'
     translation of strint function for FreeBASIC
     by original C programmer Chastity White Rose
    '/
    function strint(s as string) as uinteger
    dim as uinteger i=0
    dim as integer x=0,y=len(s)
    dim as byte c
    
    strint_errors = 0 /' clear errors '/
    
    while x<y
    
     /' read digit from string '/
     c=s[x]
    
     /' 0 to 9 '/
     if c >= 48 and c <= 57 then
     c-=48
     /' A to Z '/
     elseif c >= 65 and c <= 90 then
     c-=65
     c+=10
     /' a to z '/
     elseif c >= 97 and c <= 122 then
     c-=97
     c+=10
     /' whitespace '/
     elseif c >= 0 and c <= 32 then
      exit while /' exit correctly at string end '/
     else
      strint_errors+=1
      print "Error: ";chr(s[x]);" is not an alphanumeric character!"
      exit while /' exit at invalid character '/
     end if
    
     if c>=radix then
      strint_errors+=1
      print "Error: ";chr(s[x]);" is not a valid character for radix ";radix
      exit while /' exit at digit wrong for radix '/
     end if
    
     /'multiply by radix then add digit'/
     i*=radix
     i+=c
    
    x+=1
    wend
    
    return i
    end function
    
    

    chastelib-graphics.bi

    dim shared as integer rect_size=8
    
    /'
     this function draws a checkerboard. it is highly optimized because it does not switch colors during the function. it only draws half of the checkerboard squares and leaves the remaining areas the same as the background
    '/
    sub chaste_checker()
     dim as integer x,y,index,index1
     dim as integer rect_x,rect_y,rect_w,rect_h
    
     'draw filled rectangle with the line function
     'fill the whole screen
      line (0,0)-(screen_width,screen_height), &HFF000000,bf
     
     index=0
     rect_x=0
     rect_y=0
     rect_w=rect_size
     rect_h=rect_size
    
     y=0
     while(y<screen_height)
      index1=index
      x=0
      while(x<screen_width)
      if(index=1) then
    
       'draw filled rectangle with the line function
       line (x,y)-(x+rect_w,y+rect_h), &HFFFFFF, bf
    
       end if
       index=index xor 1
       x+=rect_size
      wend
      index=index1 xor 1
      y+=rect_size
      wend
    
    end sub
    
    
    
    ' Notes
    
    'The line function actually can draw rectangles, despite its name
    'https://www.freebasic.net/wiki/KeyPgLinegraphics