Get initial non-working lua parser set up

pull/170/head
James Ball 2023-04-04 16:17:37 +01:00
rodzic ac67c1bec6
commit a4ee966f50
16 zmienionych plików z 134 dodań i 28984 usunięć

Wyświetl plik

@ -14,7 +14,6 @@
#include "audio/VectorCancellingEffect.h"
#include "audio/DistortEffect.h"
#include "audio/SmoothEffect.h"
#include "lua/luaimport.h"
//==============================================================================
OscirenderAudioProcessor::OscirenderAudioProcessor()
@ -39,49 +38,6 @@ OscirenderAudioProcessor::OscirenderAudioProcessor()
allEffects.push_back(std::make_shared<Effect>(std::make_unique<DistortEffect>(true), "Vertical shift", "verticalDistort"));
allEffects.push_back(std::make_shared<Effect>(std::make_unique<DistortEffect>(false), "Horizontal shift", "horizontalDistort"));
allEffects.push_back(std::make_shared<Effect>(std::make_unique<SmoothEffect>(), "Smoothing", "smoothing"));
// initialization
lua_State * L = luaL_newstate();
luaL_openlibs(L);
// execute script
const char* lua_script = "print('Hello World!')";
int load_stat = luaL_loadbuffer(L, lua_script, strlen(lua_script), lua_script);
lua_pcall(L, 0, 0, 0);
const char* lua_function_script =
R"LUA(
function add(x, y)
return x + y
end
)LUA";
// execute script
load_stat = luaL_loadbuffer(L, lua_function_script, strlen(lua_function_script), lua_function_script);
lua_pcall(L, 0, 0, 0);
int a = 5;
int b = 6;
int sum;
// fetch the function
lua_getglobal(L, "add");
// pass two numbers to lua as arguemnt 1 and 2
lua_pushnumber(L, a);
lua_pushnumber(L, b);
// call the function with 2 arguments and receive 1 result
lua_call(L, 2, 1);
// read the restul from the top of the stack and cast to int
sum = (int)lua_tointeger(L, -1);
lua_pop(L, 1);
DBG("Result: " << sum);
// cleanup
lua_close(L);
}
OscirenderAudioProcessor::~OscirenderAudioProcessor()
@ -348,7 +304,7 @@ void OscirenderAudioProcessor::updateFrame() {
}
void OscirenderAudioProcessor::updateLengthIncrement() {
lengthIncrement = max(frameLength / (currentSampleRate / frequency), MIN_LENGTH_INCREMENT);
lengthIncrement = std::max(frameLength / (currentSampleRate / frequency), MIN_LENGTH_INCREMENT);
}
void OscirenderAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
@ -414,8 +370,8 @@ void OscirenderAudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, j
// hard cap on how many times it can be over the length to
// prevent audio stuttering
frameDrawn += min(lengthIncrement, 20 * length);
shapeDrawn += min(lengthIncrement, 20 * length);
frameDrawn += std::min(lengthIncrement, 20 * length);
shapeDrawn += std::min(lengthIncrement, 20 * length);
// Need to skip all shapes that the lengthIncrement draws over.
// This is especially an issue when there are lots of small lines being

Wyświetl plik

@ -0,0 +1,52 @@
#include "LuaParser.h"
#include "luaimport.h"
LuaParser::LuaParser(juce::String script) {
// initialization
L = luaL_newstate();
luaL_openlibs(L);
this->script = script;
}
LuaParser::~LuaParser() {
lua_close(L);
}
std::vector<std::unique_ptr<Shape>> LuaParser::draw() {
std::vector<std::unique_ptr<Shape>> shapes;
for (int i = 0; i < 100; i++) {
lua_pushnumber(L, step);
lua_setglobal(L, "step");
const int ret = luaL_dostring(L, script.toUTF8());
if (ret != 0) {
const char* error = lua_tostring(L, -1);
DBG(error);
lua_pop(L, 1);
} else if (lua_istable(L, -1)) {
// get the first element of the table
lua_pushinteger(L, 1);
lua_gettable(L, -2);
float x = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
// get the second element of the table
lua_pushinteger(L, 2);
lua_gettable(L, -2);
float y = (int)lua_tonumber(L, -1);
lua_pop(L, 1);
shapes.push_back(std::make_unique<Vector2>(x, y));
}
// pop the table
lua_pop(L, 1);
step++;
}
return shapes;
}

Wyświetl plik

@ -0,0 +1,17 @@
#pragma once
#include "../shape/Vector2.h"
#include <JuceHeader.h>
#include "../shape/Shape.h"
struct lua_State;
class LuaParser {
public:
LuaParser(juce::String script);
~LuaParser();
std::vector<std::unique_ptr<Shape>> draw();
private:
long step = 1;
lua_State* L;
juce::String script;
};

Plik diff jest za duży Load Diff

Wyświetl plik

@ -1,6 +1,6 @@
#include "FileParser.h"
#include "../shape/Line.h"
#include "../shape/Arc.h"
#include "../shape/CircleArc.h"
#include <numbers>
FileParser::FileParser() {}
@ -9,6 +9,8 @@ void FileParser::parse(juce::String extension, std::unique_ptr<juce::InputStream
object = nullptr;
camera = nullptr;
svg = nullptr;
text = nullptr;
lua = nullptr;
if (extension == ".obj") {
object = std::make_shared<WorldObject>(stream->readEntireStreamAsString().toStdString());
@ -18,14 +20,17 @@ void FileParser::parse(juce::String extension, std::unique_ptr<juce::InputStream
svg = std::make_shared<SvgParser>(stream->readEntireStreamAsString());
} else if (extension == ".txt") {
text = std::make_shared<TextParser>(stream->readEntireStreamAsString(), juce::Font(1.0f));
} else if (extension == ".lua") {
lua = std::make_shared<LuaParser>(stream->readEntireStreamAsString());
}
}
std::vector<std::unique_ptr<Shape>> FileParser::next() {
std::vector<std::unique_ptr<Shape>> FileParser::nextFrame() {
auto tempObject = object;
auto tempCamera = camera;
auto tempSvg = svg;
auto tempText = text;
auto tempLua = lua;
if (tempObject != nullptr && tempCamera != nullptr) {
return tempCamera->draw(*tempObject);
@ -33,9 +38,11 @@ std::vector<std::unique_ptr<Shape>> FileParser::next() {
return tempSvg->draw();
} else if (tempText != nullptr) {
return tempText->draw();
} else if (tempLua != nullptr) {
return tempLua->draw();
}
auto tempShapes = std::vector<std::unique_ptr<Shape>>();
tempShapes.push_back(std::make_unique<Arc>(0, 0, 0.5, 0.5, std::numbers::pi / 4.0, 2 * std::numbers::pi));
tempShapes.push_back(std::make_unique<CircleArc>(0, 0, 0.5, 0.5, std::numbers::pi / 4.0, 2 * std::numbers::pi));
return tempShapes;
}

Wyświetl plik

@ -6,13 +6,14 @@
#include "../obj/Camera.h"
#include "../svg/SvgParser.h"
#include "../txt/TextParser.h"
#include "../lua/LuaParser.h"
class FileParser : public FrameSource {
public:
FileParser();
void parse(juce::String extension, std::unique_ptr<juce::InputStream>) override;
std::vector<std::unique_ptr<Shape>> next() override;
std::vector<std::unique_ptr<Shape>> nextFrame() override;
bool isActive() override;
void disable() override;
void enable() override;
@ -24,4 +25,5 @@ private:
std::shared_ptr<Camera> camera;
std::shared_ptr<SvgParser> svg;
std::shared_ptr<TextParser> text;
std::shared_ptr<LuaParser> lua;
};

Wyświetl plik

@ -9,7 +9,7 @@ FrameProducer::~FrameProducer() {
void FrameProducer::run() {
while (!threadShouldExit() && frameSource->isActive()) {
frameConsumer.addFrame(frameSource->next(), sourceFileIndex);
frameConsumer.addFrame(frameSource->nextFrame(), sourceFileIndex);
}
}

Wyświetl plik

@ -8,7 +8,7 @@
class FrameSource {
public:
virtual void parse(juce::String extension, std::unique_ptr<juce::InputStream>) = 0;
virtual std::vector<std::unique_ptr<Shape>> next() = 0;
virtual std::vector<std::unique_ptr<Shape>> nextFrame() = 0;
virtual bool isActive() = 0;
virtual void disable() = 0;
virtual void enable() = 0;

Wyświetl plik

@ -1,10 +1,10 @@
#include "Arc.h"
#include "CircleArc.h"
#include <numbers>
#include "Line.h"
Arc::Arc(double x, double y, double radiusX, double radiusY, double startAngle, double endAngle) : x(x), y(y), radiusX(radiusX), radiusY(radiusY), startAngle(startAngle), endAngle(endAngle) {}
CircleArc::CircleArc(double x, double y, double radiusX, double radiusY, double startAngle, double endAngle) : x(x), y(y), radiusX(radiusX), radiusY(radiusY), startAngle(startAngle), endAngle(endAngle) {}
Vector2 Arc::nextVector(double drawingProgress) {
Vector2 CircleArc::nextVector(double drawingProgress) {
// scale between start and end angle in the positive direction
double angle = startAngle + endAngle * drawingProgress;
return Vector2(
@ -13,7 +13,7 @@ Vector2 Arc::nextVector(double drawingProgress) {
);
}
void Arc::rotate(double theta) {
void CircleArc::rotate(double theta) {
double cosTheta = std::cos(theta);
double sinTheta = std::sin(theta);
@ -41,19 +41,19 @@ void Arc::rotate(double theta) {
endAngle = newEndAngle;
}
void Arc::scale(double x, double y) {
void CircleArc::scale(double x, double y) {
this->x *= x;
this->y *= y;
this->radiusX *= x;
this->radiusY *= y;
}
void Arc::translate(double x, double y) {
void CircleArc::translate(double x, double y) {
this->x += x;
this->y += y;
}
double Arc::length() {
double CircleArc::length() {
if (len < 0) {
len = 0;
double angle = startAngle;
@ -67,10 +67,10 @@ double Arc::length() {
return len;
}
std::unique_ptr<Shape> Arc::clone() {
return std::make_unique<Arc>(x, y, radiusX, radiusY, startAngle, endAngle);
std::unique_ptr<Shape> CircleArc::clone() {
return std::make_unique<CircleArc>(x, y, radiusX, radiusY, startAngle, endAngle);
}
std::string Arc::type() {
std::string CircleArc::type() {
return std::string("Arc");
}

Wyświetl plik

@ -3,9 +3,9 @@
#include "Shape.h"
#include "Vector2.h"
class Arc : public Shape {
class CircleArc : public Shape {
public:
Arc(double x, double y, double radiusX, double radiusY, double startAngle, double endAngle);
CircleArc(double x, double y, double radiusX, double radiusY, double startAngle, double endAngle);
Vector2 nextVector(double drawingProgress) override;
void rotate(double theta) override;

Wyświetl plik

@ -1,5 +1,6 @@
#include "Shape.h"
#include "Line.h"
#include "Vector2.h"
double Shape::totalLength(std::vector<std::unique_ptr<Shape>>& shapes) {
double length = 0.0;

Wyświetl plik

@ -4,9 +4,9 @@
#include <cstdlib>
#include <vector>
#include <memory>
#include "Vector2.h"
#include <string>
class Vector2;
class Shape {
public:
virtual Vector2 nextVector(double drawingProgress) = 0;

Wyświetl plik

@ -43,3 +43,17 @@ double Vector2::length() {
double Vector2::magnitude() {
return sqrt(x * x + y * y);
}
std::unique_ptr<Shape> Vector2::clone() {
return std::unique_ptr<Shape>();
}
std::string Vector2::type() {
return std::string();
}
Vector2& Vector2::operator=(const Vector2& other) {
x = other.x;
y = other.y;
return *this;
}

Wyświetl plik

@ -1,20 +1,27 @@
#pragma once
#include "Shape.h"
#include <cmath>
#include <string>
class Vector2 {
class Vector2 : public Shape {
public:
Vector2(double x, double y);
Vector2(double val);
Vector2();
Vector2 nextVector(double drawingProgress);
void rotate(double theta);
void scale(double x, double y);
void translate(double x, double y);
Vector2 nextVector(double drawingProgress) override;
void rotate(double theta) override;
void scale(double x, double y) override;
void translate(double x, double y) override;
void reflectRelativeToVector(double x, double y);
double length();
double length() override;
double magnitude();
std::unique_ptr<Shape> clone() override;
std::string type() override;
// copy assignment operator
Vector2& operator=(const Vector2& other);
double x, y;

Wyświetl plik

@ -1,7 +1,7 @@
#include "EllipticalArcTo.h"
#include "../shape/Line.h"
#include <numbers>
#include "../shape/Arc.h"
#include "../shape/CircleArc.h"
std::vector<std::unique_ptr<Shape>> EllipticalArcTo::absolute(SvgState& state, std::vector<float>& args) {
return parseEllipticalArc(state, args, true);
@ -113,7 +113,7 @@ void EllipticalArcTo::createArc(std::vector<std::unique_ptr<Shape>>& shapes, Vec
angleExtent = std::fmod(angleExtent, 2 * std::numbers::pi);
angleStart = std::fmod(angleStart, 2 * std::numbers::pi);
auto arc = std::make_unique<Arc>(cx, cy, rx, ry, angleStart, angleExtent);
auto arc = std::make_unique<CircleArc>(cx, cy, rx, ry, angleStart, angleExtent);
arc->translate(-cx, -cy);
arc->rotate(theta);
arc->translate(cx, cy);

Wyświetl plik

@ -65,6 +65,8 @@
<FILE id="BlOdIr" name="luaimport.cpp" compile="1" resource="0" file="Source/lua/luaimport.cpp"/>
<FILE id="XUJtiC" name="luaimport.h" compile="0" resource="0" file="Source/lua/luaimport.h"/>
<FILE id="NeC0ti" name="lualib.h" compile="0" resource="0" file="Source/lua/lualib.h"/>
<FILE id="ggEnCt" name="LuaParser.cpp" compile="1" resource="0" file="Source/lua/LuaParser.cpp"/>
<FILE id="BzW4g3" name="LuaParser.h" compile="0" resource="0" file="Source/lua/LuaParser.h"/>
<FILE id="IKwQ2M" name="lundump.c" compile="1" resource="0" file="Source/lua/lundump.c"/>
<FILE id="tuKfAR" name="lundump.h" compile="0" resource="0" file="Source/lua/lundump.h"/>
<FILE id="qkRiRs" name="lutf8lib.c" compile="1" resource="0" file="Source/lua/lutf8lib.c"/>
@ -73,7 +75,6 @@
<FILE id="dy37RJ" name="lzio.c" compile="1" resource="0" file="Source/lua/lzio.c"/>
<FILE id="vYPcZP" name="lzio.h" compile="0" resource="0" file="Source/lua/lzio.h"/>
<FILE id="ktpcF1" name="onelua.c" compile="1" resource="0" file="Source/lua/onelua.c"/>
<FILE id="RykUkb" name="sol.hpp" compile="0" resource="0" file="Source/lua/sol.hpp"/>
</GROUP>
<GROUP id="{CD81913A-7F0E-5898-DA77-5EBEB369DEB1}" name="components">
<FILE id="poPVxL" name="DraggableListBox.cpp" compile="1" resource="0"
@ -154,8 +155,8 @@
<FILE id="CdKZCg" name="Matching.h" compile="0" resource="0" file="Source/chinese_postman/Matching.h"/>
</GROUP>
<GROUP id="{92CEA658-C82C-9CEB-15EB-945EF6B6B5C8}" name="shape">
<FILE id="b9eg7J" name="Arc.cpp" compile="1" resource="0" file="Source/shape/Arc.cpp"/>
<FILE id="KIQgS0" name="Arc.h" compile="0" resource="0" file="Source/shape/Arc.h"/>
<FILE id="iglTFG" name="CircleArc.cpp" compile="1" resource="0" file="Source/shape/CircleArc.cpp"/>
<FILE id="T3S8Sg" name="CircleArc.h" compile="0" resource="0" file="Source/shape/CircleArc.h"/>
<FILE id="G5fbub" name="Shape.cpp" compile="1" resource="0" file="Source/shape/Shape.cpp"/>
<FILE id="Dbvew2" name="CubicBezierCurve.cpp" compile="1" resource="0"
file="Source/shape/CubicBezierCurve.cpp"/>