diff --git a/luacmdinterface/CMakeLists.txt b/luacmdinterface/CMakeLists.txt index b3deabd1a..470e5f92c 100644 --- a/luacmdinterface/CMakeLists.txt +++ b/luacmdinterface/CMakeLists.txt @@ -5,20 +5,13 @@ set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") message("***** Lua command line interface *****") -# Cairo -find_package(Cairo REQUIRED) -include_directories(${CAIRO_INCLUDE_DIRS}) - -# Pango -find_package(Pango 1.36 REQUIRED) -include_directories(${PANGO_INCLUDE_DIRS}) -link_directories(${PANGO_LIBRARY_DIRS}) - # Curl find_package(CURL REQUIRED) include_directories(${CURL_INCLUDE_DIRS}) link_directories(${CURL_LIBRARY_DIRS}) +find_package(glfw3 3.3 REQUIRED) + # Eigen 3 find_package(Eigen3 REQUIRED) if( CMAKE_COMPILER_IS_GNUCXX) @@ -32,24 +25,6 @@ find_package(Boost COMPONENTS program_options filesystem system log date_time th FIND_PACKAGE ( Threads REQUIRED ) include_directories(${Boost_INCLUDE_DIRS}) -# GDK-Pixbuf -find_package(GDK-Pixbuf REQUIRED) -include_directories(${GDK-PIXBUF_INCLUDE_DIRS}) - -#GDK -find_package(GDK REQUIRED) -include_directories(${GDK_INCLUDE_DIRS}) - -#GTK -find_package(GTK REQUIRED) -include_directories(${GTK_INCLUDE_DIRS}) - - -#GLib -set(GLIB_FIND_COMPONENTS gobject) -find_package(GLib REQUIRED) -include_directories(${GLIB_INCLUDE_DIRS}) - #Lua find_package(Lua 5.2 REQUIRED) include_directories(${LUA_INCLUDE_DIR}) @@ -83,14 +58,10 @@ set(hdrs add_executable(luacmdinterface ${src} ${hdrs}) target_link_libraries(luacmdinterface ${CMAKE_THREAD_LIBS_INIT} - ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES} ${Boost_LIBRARIES} ${CURL_LIBRARIES} ${APR_LIBRARIES} - ${GLIB_GOBJECT_LIBRARIES} ${GLIB_LIBRARIES} ${LUA_LIBRARIES} - ${GDK-PIXBUF_LIBRARIES} - ${GTK_LIBRARIES} - lcluascript lckernel lcluascript lcviewernoqt ${Boost_LIBRARIES} + lcluascript lckernel lcluascript lcviewernoqt ${Boost_LIBRARIES} glfw ) diff --git a/luacmdinterface/main.cpp b/luacmdinterface/main.cpp index be4f88c5c..9f67b22f7 100644 --- a/luacmdinterface/main.cpp +++ b/luacmdinterface/main.cpp @@ -4,16 +4,16 @@ #include #include -#include #include -#include #include -#include #include #include #include #include #include +#include +#include +#include namespace po = boost::program_options; @@ -25,7 +25,7 @@ static const int DEFAULT_IMAGE_WIDTH = 400; static const int DEFAULT_IMAGE_HEIGHT = 400; static std::string* readBuffer; -static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { +static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void*) { size_t realsize = size * nmemb; readBuffer->append((char*) contents, realsize); return realsize; @@ -39,7 +39,6 @@ std::string loadFile(const std::string& url) { if (curl != nullptr) { curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - /* example.com is redirected, so we tell libcurl to follow redirection */ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); @@ -61,16 +60,6 @@ std::string loadFile(const std::string& url) { } } -std::ofstream* ofile; -cairo_status_t write_func (void* closure, const unsigned char* data, unsigned int length) { - - if (ofile->is_open()) { - ofile->write((const char*) data, length); - } - - return CAIRO_STATUS_SUCCESS; -} - static FILE* openFileDialog(bool isOpening, const char* description, const char* mode) { std::string path; @@ -102,7 +91,7 @@ int main(int argc, char** argv) { ("height,h", po::value(&height), "(optional) Set output image height, example -h 200") ("ifile,i", po::value(&fIn), "(required) Set LUA input file name, example: -i file:myFile.lua") ("ofile,o", po::value(&fOut), "(optional) Set output filename, example -o out.png") - ("otype,t", po::value(&fType), "(optional) output file type, example -t svg"); + ("otype,t", po::value(&fType), "(optional) output file type, example -t tga"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); @@ -130,7 +119,7 @@ int main(int argc, char** argv) { // Create Librecad document auto _storageManager = std::make_shared(); auto _document = std::make_shared(_storageManager); - auto _canvas = std::make_shared(_document); + auto _canvas = std::make_shared(_document); // Add background auto _gradientBackground = std::make_shared(lc::Color(0x90, 0x90, 0x90), @@ -144,24 +133,52 @@ int main(int argc, char** argv) { } std::transform(fType.begin(), fType.end(), fType.begin(), ::tolower); - ofile = new std::ofstream; - ofile->open(fOut); - using namespace CairoPainter; + LcPainter* lcPainter; + if(!glfwInit()) { + LOG_ERROR << "Failed to initialize GLFW"; + return -1; + } - LcPainter* lcPainter = nullptr; - if (fType == "pdf") { - lcPainter = new LcCairoPainter(width, height, &write_func); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + + GLFWwindow* window; + window = glfwCreateWindow(width, height, "LibreCAD", nullptr, nullptr); + if(window == nullptr) { + const char* description; + glfwGetError(&description); + LOG_ERROR << "Failed opening GLFW: " << description << std::endl; + glfwTerminate(); + return -1; } - else if (fType == "svg") { - lcPainter = new LcCairoPainter(width, height, &write_func); + + glfwMakeContextCurrent(window); + + glewExperimental = GL_TRUE; + GLenum err = glewInit(); + + if (err != GLEW_OK) { + LOG_ERROR << "GLEW Error: " << glewGetErrorString(err) << std::endl; + exit(1); } - else { - lcPainter = new LcCairoPainter(width, height, nullptr); + if (!GLEW_VERSION_2_1) { + LOG_ERROR << "OpenGL version 2.1 is not available" << std::endl; + exit(1); } + LOG_INFO << (char*) glGetString(GL_VERSION) << std::endl; + + lcPainter = lc::viewer::createOpenGLPainter(nullptr, width, height); + + lcPainter->create_resources(); + _canvas->setPainter(lcPainter); + // Set device width/height _canvas->newDeviceSize(width, height); + lcPainter->new_device_size(width, height); // Render Lua Code kaguya::State luaState; @@ -191,19 +208,31 @@ int main(int argc, char** argv) { } _canvas->autoScale(*lcPainter); - _canvas->render(*lcPainter, VIEWER_BACKGROUND); - _canvas->render(*lcPainter, VIEWER_DOCUMENT); - _canvas->render(*lcPainter, VIEWER_FOREGROUND); + lcPainter->clear(0,0,0); - if (fType == "png" || (fType != "pdf" && fType != "svg")) { - dynamic_cast*>(lcPainter)->writePNG(fOut); - } - ofile->close(); + _canvas->render(*lcPainter, lc::viewer::VIEWER_BACKGROUND); + _canvas->render(*lcPainter, lc::viewer::VIEWER_DOCUMENT); + _canvas->render(*lcPainter, lc::viewer::VIEWER_FOREGROUND); + + glfwSwapBuffers(window); + + FILE* out = fopen(fOut.c_str(), "wb"); + char* pixel_data = new char[3*width*height]; + short TGAhead[] = { 0, 2, 0, 0, 0, 0, static_cast(width), static_cast(height), 24 }; + + glReadBuffer(GL_FRONT); + glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE, pixel_data); + + fwrite(&TGAhead,sizeof(TGAhead),1,out); + fwrite(pixel_data, 3*width*height, 1, out); + fclose(out); + + delete[] pixel_data; lc::lua::LuaCustomEntityManager::getInstance().removePlugins(); + glfwDestroyWindow(window); delete lcPainter; - delete ofile; delete readBuffer; return 0; } \ No newline at end of file