beefviper

xge::Game::initVariables()

Sep 17th, 2020
897
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void xge::Game::initVariables(void)
  2. {
  3.     // TODO: Write code to parse the rest of the text fields
  4.     //      -- exprtk lib will parse text variables and expressions
  5.  
  6.     randomNumber<float> randomNumberFloat;
  7.     randomRange<float> randomRangeFloat;
  8.  
  9.     exprtk::symbol_table<float> symbolTable;
  10.     symbolTable.add_function("random.number", randomNumberFloat);
  11.     symbolTable.add_function("random.range", randomRangeFloat);
  12.     symbolTable.add_constant("screen.left", 0);
  13.     symbolTable.add_constant("screen.right", windowDesc.width);
  14.     symbolTable.add_constant("screen.width.center", windowDesc.width / 2);
  15.     symbolTable.add_constant("screen.height.center", windowDesc.height / 2);
  16.     for (auto variable : variables)
  17.     {
  18.         symbolTable.add_constant(variable.first, variable.second);
  19.     }
  20.  
  21.     exprtk::expression<float> expression;
  22.     expression.register_symbol_table(symbolTable);
  23.     exprtk::parser<float> parser;
  24.     std::string expression_str;
  25.  
  26.     for (auto &object : objects)
  27.     {
  28.         expression_str = object.sposition.x;
  29.         parser.compile(expression_str, expression);
  30.         object.position.x = expression.value();
  31.  
  32.         expression_str = object.sposition.y;
  33.         parser.compile(expression_str, expression);
  34.         object.position.y = expression.value();
  35.  
  36.         expression_str = object.svelocity.x;
  37.         parser.compile(expression_str, expression);
  38.         object.velocity.x = expression.value();
  39.  
  40.         expression_str = object.svelocity.y;
  41.         parser.compile(expression_str, expression);
  42.         object.velocity.y = expression.value();
  43.     }
  44. }
  45.  
RAW Paste Data