Jump to content
Sign in to follow this  
Lillapojkenpåön

bB equivalents in C/C++

Recommended Posts

I looked into C/C++ a couple days ago and realized how similar it was to bB, I know I'm not the only one who only knows bB so I thought I would share my findings since I found it very inspiring, especially considering SpiceC.

 

I will make different posts with commonly used bB code and it's equivalent in C/C++ and have an index to them here in the first post, and hopefully people chime in with their knowledge, corrections or questions.

 


 

Spoiler

 

Online C++ compiler: https://www.tutorialspoint.com/compile_cpp_online.php

Example to compile:

#include <iostream>

using namespace std;

int main()
{
 int _PlayerY[9];
 int _LoopCounter;
 const int _Y_POS[9] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
#define forloop(x) for (_LoopCounter = 0; _LoopCounter < (x); _LoopCounter++)


  forloop(9) {
 _PlayerY[_LoopCounter] = _Y_POS[_LoopCounter];
    cout << _PlayerY[_LoopCounter] << endl;
 }

   
   return 0;
}


 

 

Edited by Lillapojkenpåön
  • Thanks 1

Share this post


Link to post
Share on other sites

Integer variables bB

 

 Dim _Aristocrat = a
 Dim _Bangladesh = b
 Dim _Cola = c
 Dim _Didrick = d

 

 _Aristocrat = 0: _Bangladesh = 0: _Cola = 0: _Didrick = 0

 

 if _Aristocrat > _Bangladesh then _Cola = 25: _Didrick = 1 else _Cola = 50: _Didrick = 0

 

 

 

 bB Comparison Operators:
 rem =   equal to
 rem <> not equal

 rem >   greater than

 rem <   less than

 rem >= greater than or equal to
 rem <= less than or equal to

 

 bB Logical Operators:

 rem &&   AND  

 rem ||     OR   

 rem !      NOT

 

 

Integer variables C/C++

 

 int _Aristocrat;
 int _Bangladesh;
 int _Cola;
 int _Didrick;

 

  _Aristocrat = 0; _Bangladesh = 0; _Cola = 0; _Didrick = 0;

 

 if (_Aristocrat > _Bangladesh) {_Cola = 25; _Didrick = 1;} else {_Cola = 50; _Didrick = 0;}

 

 

 C/C++ Comparison Operators:
 // == equal to
 // !=  not equal

 // >   greater than

 // <   less than

 // >= greater than or equal to
 // <= less than or equal to

 

 C/C++ Logical Operators:

 // &&   AND  

 // ||     OR   

 // !      NOT


 

Spoiler

 

As you can see they are very similar, but C/C++ is usually written like this:


 if (_Aristocrat > _Bangladesh) {

 _Cola = 25;

 _Didrick = 1;

 } else {

 _Cola = 50;

 _Didrick = 0;

 }

 

The compiler ignores whitespace so you can write it anyway you want.

 

//================================================

 

Give one variable many aliases:

 

  int _Aristocrat;

  int * const _Bangladesh = &_Aristocrat;

  int * const _Cola = &_Aristocrat;

  int * const _Didrick = &_Aristocrat;

 

The three nicknames can not be made to point to anything else,
the compiler will not generate additional code to handle _Aristocrat or *_Bangladesh,
neither reserve space for a pointer.

 

Edited by Lillapojkenpåön

Share this post


Link to post
Share on other sites

Boolean bB

 

   def _Aristocrat=a{0}
   def _Bangladesh=a{1}
   def _Cola=a{2}
   def _Didrick=a{3}

 

   _Aristocrat=0
   _Bangladesh=0
   _Cola=1
   _Didrick=1

 

 if _Aristocrat && !_Bangladesh then _Cola = 1: _Didrick = 0
 if !_Aristocrat && _Bangladesh then _Cola = 0: _Didrick = 1

 

 

Boolean C/C++

 

 bool _Aristocrat = false;
 bool _Bangladesh = false;
 bool _Cola = true;
 bool _Didrick = true;

 

 if (_Aristocrat && !_Bangladesh) {_Cola = true; _Didrick = false;}
 if (!_Aristocrat && _Bangladesh) {_Cola = false; _Didrick = true;}

 


 

Spoiler

Short Hand If...Else (Ternary Operator)
If you have only one statement to execute for if, and one for else, use this.

 

Syntax:
 variable = (condition) ? expressionTrue : expressionFalse;

 

 

  _Cola = (_Bangladesh) ? true : false;
 

same as:

     if (Bangladesh) {_Cola = true;} else {_Cola = false}

 

//===========================================

 

Those examples uses an entire variable as one boolean, here's one way of using the different bits.

 

//create the variable

  unsigned char flags = 0;

 

//and some masks
  const unsigned char _Aristocrat     { 1 << 0 }; // 0000 0001
  const unsigned char _Bangladesh  { 1 << 1 }; // 0000 0010
  const unsigned char _Cola            { 1 << 2 }; // 0000 0100
  const unsigned char _Didrick        { 1 << 3 }; // 0000 1000
  const unsigned char mask4          { 1 << 4 }; // 0001 0000
  const unsigned char mask5          { 1 << 5 }; // 0010 0000
  const unsigned char mask6          { 1 << 6 }; // 0100 0000
  const unsigned char mask7          { 1 << 7 }; // 1000 0000

 

 //Bitwise OR:
  flags |= _Aristocrat; // turn on bit 0
  flags |= (_Aristocrat | _Bangladesh); // turn bits 0 and 1 on at the same time

 

 //Bitwise AND and Bitwise NOT together:
  flags &= ~_Aristocrat; // turn off bit 0
  flags &= ~(_Aristocrat | _Bangladesh); // turn bits 0 and 1 off at the same time

 

 //Bitwise XOR:
  flags ^= _Aristocrat; // flip bit 0
  flags ^= (_Aristocrat | _Bangladesh); // flip bits 0 and 1 at the same time

 

 

 

   (flags & _Bangladesh) ? flags |= _Cola : flags &= ~_Cola;

 

same as:

     if (flags & _Bangladesh) {flags |= _Cola;} else {flags &= ~_Cola;}

 

Hard for me to see what it does so I use aliases:

#define ON |=
#define OFF &= ~
#define FLIP ^=

 

Edited by Lillapojkenpåön

Share this post


Link to post
Share on other sites

For Loop bB

 

 dim _LoopCounter = temp5


   for _LoopCounter = 0 to 9

 player1y[_LoopCounter] = _Y_POS[_LoopCounter]

   next

 

 

 rem put outside of code
   data _Y_POS
   10, 20, 30, 40, 50, 60, 70, 80, 90
end


 

Spoiler

 

 Same as:

 

   _LoopCounter = 0

 

__loop
 player1y[_LoopCounter] = _Y_POS[_LoopCounter]  ; player1y = 10 first, player2y = 20 first loop,
                                                                            ; player3y = 30 second loop etc.

    _LoopCounter = _LoopCounter + 1
    if _LoopCounter < 9 then goto __loop

 

 

 

 

For Loop C/C++


 int _PlayerY[9]; // Create an array of nine integers

 int _LoopCounter;
 const int _Y_POS[9] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
// Read-Only array, same as a data table in bB

 

 

 for (_LoopCounter = 0; _LoopCounter < 9; _LoopCounter++) {

 _PlayerY[_LoopCounter] = _Y_POS[_LoopCounter];
}


 

Spoiler

 

 for (statement 1; statement 2; statement 3) {
  // code block to be executed
}

 

//Statement 1 is executed (one time) before the execution of the code block.
//Statement 2 defines the condition for executing the code block.
//Statement 3 is executed (every time) after the code block has been executed.

 

 

Same as:

 

 _LoopCounter = 0; //Statement 1

 

__loop:
 _PlayerY[_LoopCounter] = _Y_POS[_LoopCounter];  // player1y = 10 first, player2y = 20 first loop,
                                                                             // player3y = 30 second loop etc.

 

 _LoopCounter++; //Statement 3
 if (_LoopCounter < 9) {goto __loop;} //Statement 2

 

//=====================================================

 

If you don't want to write so much, you can create preprocessor macros:

 

#define forloop(x) for (int _LoopCounter = 0; _LoopCounter < (x); _LoopCounter++)
// the x will be replaced by what ever is put into the parenthesis

 

 forloop(9) {

 _PlayerY[_LoopCounter] = _Y_POS[_LoopCounter];

}

 

Edited by Lillapojkenpåön

Share this post


Link to post
Share on other sites

on...goto bB

 

 dim _Counter = a


   on _Counter goto __case_1 __case_2 __case_3 __case_4 __case_5 __case_6 __case_7
 goto __break ; if _Counter > 6

 

__case_1
    rem code block
 goto __break
__case_2
    rem code block
 goto __break
__case_3
    rem code block
 goto __break
__case_4
    rem code block
 goto __break
__case_5
    rem code block
 goto __break
__case_6
    rem code block
 goto __break
__case_7
    rem code block

 

__break

 

 

switch C/C++

 

int _Counter;

 

switch (_Counter) {
  case 1:
    // code block
    break;
  case 2:
    // code block
    break;
  case 3:
    // code block
    break;
  case 4:
    // code block
    break;
  case 5:
    // code block
    break;
  case 6:
    // code block
    break;
  case 7:
    // code block
    break;
}


 

Spoiler


Note that in bB you will jump to case 1 when the counter is 0, and case 7 when it's 6, in C/C++ the numbers correspond.

 

 

Share this post


Link to post
Share on other sites

gosub bB

 

 gosub __MySubroutine ; jumps to MySoubroutine

 

 

 rem put outside of code

__MySubroutine

 rem code block

 return


 

Spoiler

 

 temp1 = player1x : temp2 = player1y ; send copies to the subroutine
 gosub __MySubroutine
 player1x = temp1 : player1y = temp2 ; store back the returned result


__MySubroutine
    ; do alot of boundary checking with temp1 and temp2
    ; that determines where the player go next and "move" temp1 and temp2 there

 return

 ; now the code is re-usable because you just pass copies to the subroutine in separate variables,
 ; you could pass copies of player 2's position, or player 3's etc.

 

 That would be very similar to "passing parameters by values" to functions.

 

 

 temp1 = 4
 gosub __MySubroutine

__MySubroutine
    ; if player1x[temp1]...then player1x[temp1]...
    ; if player1y[temp1]...then player1y[temp1]...
    ; if player1height[temp1]...then player1height[temp1]...
    ; if _NUZIZ1[temp1]...then _NUZIZ1[temp1]...

 return

 ; passing 4 as index would check and change player5x, y, height and nusiz
 ; passing 0 as index would check and change player1x, y, height and nusiz

 

 That would be very similar to "passing parameters by reference" to functions.

 

 

 

 

function C/C++

 

void MyFunction(); // function prototype, needed for the compiler if you want to put the function below where it's being called from

                            // you could put the function here and remove the prototype
 

// we jump from the main function to MyFunction

// variables are only locally known to the function they are defined in
// to create a global variable that can be accessed by any function define it here outside of any function


int main()
{


  MyFunction(); // jumps to MyFunction
 
}


void MyFunction() // function declaration
{
  //code block
}


 

Spoiler

 

"passing parameters by values"


int main()
{


  player5x = MyFunction(player5x); // stores a copy of the value of player5x in function variable temp1
                                
 
}


int MyFunction(int temp1) //int means we will return a value in the form of an integer
{
  //do lots of horisontal boundary checking with temp1
  //that determines where the player go next and "move" temp1

  return temp1; //return the value to player5x
}

 

 

 

"passing parameters by reference"


int main()
{


   MyFunction(player5x, player5y); // stores a reference to player5x in function variable temp1
                                                   // stores a reference to player5y in function variable temp2
 
}


void MyFunction(int& temp1, int& temp2) // void means we won't return a value
{
  //using temp1 and temp2 in the function will now be the same as using player5x and player5y
}

 

 

Edited by Lillapojkenpåön

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...