Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
externalized images, renamed .lsproj to .lsmproj
Browse files Browse the repository at this point in the history
  • Loading branch information
aarcangeli committed Jan 19, 2019
1 parent d8b7b32 commit 8e3bcbf
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "libs/jsoncpp"]
path = libs/jsoncpp
url = https://github.com/open-source-parsers/jsoncpp.git
[submodule "libs/pathie-cpp"]
path = libs/pathie-cpp
url = https://github.com/Quintus/pathie-cpp.git
6 changes: 5 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ TODO:
- Multi document window
- Replace ffmpeg with a better media loader

LSM 0.1, 2018-12-27
SNAPSHOT:
- Changed output namespace from CLS to lsm
- Changed document's extension from '.clsproj' to '.lsmproj'

LSM 0.1 (2018-12-27)
- Initial release
1 change: 1 addition & 0 deletions cmake/setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ IF (NOT LSM_VERSION)
ENDIF ()

set(LSM_OUTPUT_DIRECTORY ${ROOT_DIRECTORY}/bin/lightshowmaker-v${LSM_VERSION})
set(CMAKE_INSTALL_PREFIX ${ROOT_DIRECTORY}/intermediates)

if (MINGW)
# remove dependencies to libstdc++-6.dll, libgcc_s_dw2-1.dll, libwinpthread-1.dll
Expand Down
4 changes: 4 additions & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ set(JSONCPP_WITH_CMAKE_PACKAGE OFF CACHE BOOL "" FORCE)
set(JSONCPP_WITH_PKGCONFIG_SUPPORT OFF CACHE BOOL "" FORCE)
add_subdirectory(jsoncpp)

set(CMAKE_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
add_subdirectory(pathie-cpp)
target_include_directories(pathie PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/pathie-cpp/include")

add_subdirectory(glad)
add_subdirectory(ffmpeg)
add_subdirectory(nativefiledialog)
Expand Down
1 change: 1 addition & 0 deletions libs/pathie-cpp
Submodule pathie-cpp added at 0ec727
15 changes: 15 additions & 0 deletions src/core/str-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <string>
#include <algorithm>
#include <codecvt>
#include <string>
#include <locale>

namespace sm {

Expand All @@ -28,6 +31,18 @@ static void removeExtension(std::string &path) {
if (posSlash > 0 && posDot > posSlash) path.resize(posDot);
}

// convert UTF-8 string to wstring
static std::wstring utf8_to_wstring(const std::string &str) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;
return conv1.from_bytes(str);
}

// convert wstring to UTF-8 string
static std::string wstring_to_utf8(const std::wstring &str) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv1;
return conv1.to_bytes(str);
}

}

#endif //STR_UTILS_H
10 changes: 6 additions & 4 deletions src/editor/OutputVideoEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void OutputVideoEditor::openImage(const shared_ptr<model::Project> &proj) const
dec->height = image->height;
dec->posX = 0;
dec->posY = 0;
dec->image = image;
dec->resource.filename = outPath;
append(proj, dec);
} else {
gApp->error("Unable to open image '" + outPath + "'");
Expand Down Expand Up @@ -179,9 +179,11 @@ void OutputVideoEditor::printDecoration(float decAlpha, const std::shared_ptr<mo
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
shared_ptr<media::Image> &im = dec->image;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, im->width, im->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
im->pixels.data());
shared_ptr<media::Image> im = dec->resource.loadAsImage();
if (im) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, im->width, im->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
im->pixels.data());
}
id = (ImTextureID) (size_t) texture;
}

Expand Down
8 changes: 4 additions & 4 deletions src/editor/ProjectWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void ProjectWindow::showMenu() {
gApp->open(std::make_shared<model::Project>());
}
if (ImGui::MenuItem("Open", "Ctrl+O")) {
std::string outPath = gApp->getPath("lsproj", false);
std::string outPath = gApp->getPath("lsmproj", false);
if (!outPath.empty()) {
if (gApp->load(outPath)) {
gApp->saveLastDirectory(outPath);
Expand Down Expand Up @@ -172,10 +172,10 @@ void ProjectWindow::showMenu() {
}

void ProjectWindow::saveAs() const {
std::string outPath = gApp->getPath("lsproj", true);
std::string outPath = gApp->getPath("lsmproj", true);
if (!outPath.empty()) {
if (!endsWith(outPath, ".lsproj")) {
outPath += ".lsproj";
if (!endsWith(outPath, ".lsmproj")) {
outPath += ".lsmproj";
}
if (gApp->save(outPath)) {
gApp->saveLastDirectory(outPath);
Expand Down
73 changes: 46 additions & 27 deletions src/lightshowmaker/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ Application::Application() {

bool Application::init() {

home = Pathie::Path::exe().parent();
iniPath = home / (Pathie::Path::exe().sub_ext(".ini").basename().str());
iniPathStr = iniPath.str();
autoSavePath = home / "autosave/autosave.lsmproj";

// Initialize imgui
IMGUI_CHECKVERSION();
ctx = ImGui::CreateContext();
loader::installConfigLoader(ctx, this);
ImGuiIO &io = ImGui::GetIO();
io.IniFilename = iniPath.c_str();
io.IniFilename = iniPathStr.c_str();
ImGui::LoadIniSettingsFromDisk(io.IniFilename);
if (windowWidth <= 100) windowWidth = 1280;
if (windowHeight <= 100) windowHeight = 720;
Expand Down Expand Up @@ -80,7 +85,7 @@ bool Application::init() {
glClearDepth(1);

// restore previous project
load(autoSavePath);
load(autoSavePath.str(), true);
if (!proj) {
open(std::make_shared<model::Project>());
proj->canvas.makeGroup();
Expand Down Expand Up @@ -171,7 +176,7 @@ int Application::runLoop() {
player.update(proj->canvas);
}

//save(autoSavePath);
save(autoSavePath.str(), true);

return 0;
}
Expand Down Expand Up @@ -234,34 +239,48 @@ void Application::asyncCommand(const std::string &name, bool mergeable, const st
commands.push_back(AppCommand{name, mergeable, fn});
}

void Application::setAppHome(std::string path) {
removeExtension(path);

home = std::move(path);
iniPath = home + ".ini";
autoSavePath = home + "-autosave.lsproj";
}

bool Application::save(std::string filename) {
if (proj) {
std::string data = serializeObject(proj);
std::ofstream file(filename);
file << data;
exportIno(filename);
return true;
bool Application::save(std::string filename, bool quiet) {
try {
printf("Saving %s\n", filename.c_str());
if (proj) {
std::ofstream file(filename);
if (!file.good()) return false;
std::string data = serializeObject(proj, filename);
file << data;
exportIno(filename);
return true;
}
return false;
} catch (const char *errorStr) {
if (!quiet) error("Cannot open '" + filename + "'\nCause: " + errorStr);
return false;
} catch (...) {
if (!quiet) error("Cannot open '" + filename + "'");
return false;
}
return false;
}

bool Application::load(std::string filename) {
std::ifstream file(filename);
if (!file.good()) return false;
std::string data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
bool Application::load(std::string filename, bool quiet) {
try {
printf("Loading %s\n", filename.c_str());
std::ifstream file(filename);
if (!file.good()) {
if (!quiet) error("Cannot open '" + filename + "'\nCause: " + strerror(errno));
return false;
}
std::string data((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

std::shared_ptr<model::Project> proj = deserializeObject<model::Project>(data);
open(proj);
this->filename = filename;
return true;
std::shared_ptr<model::Project> proj = deserializeObject<model::Project>(data, filename);
open(proj);
this->filename = filename;
return true;
} catch (const char *errorStr) {
if (!quiet) error("Cannot open '" + filename + "'\nCause: " + errorStr);
return false;
} catch (...) {
if (!quiet) error("Cannot open '" + filename + "'");
return false;
}
}

std::string Application::getPath(const std::string &pathes, bool isSave) {
Expand Down
14 changes: 7 additions & 7 deletions src/lightshowmaker/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Player.h"
#include "GlobalHotKey.h"
#include "Selection.h"
#include <path.hpp>

namespace sm {

Expand All @@ -38,8 +39,6 @@ class Application {
void stopMerging();
void asyncCommand(const std::string &name, bool mergeable, const std::function<void()> &fn);

void setAppHome(std::string home);

std::string getPath(const std::string &pathes, bool isSave);
void saveLastDirectory(std::string path);

Expand Down Expand Up @@ -69,15 +68,16 @@ class Application {
void applyTheme();

SelectionManager selection;
std::string home;
std::string iniPath;
std::string autoSavePath;
Pathie::Path home;
Pathie::Path iniPath;
Pathie::Path autoSavePath;
std::string iniPathStr;

public:
void error(const std::string &errorMsg) { projectWindow.showError(errorMsg); }

bool save(std::string filename);
bool load(std::string filename);
bool save(std::string filename, bool quiet = false);
bool load(std::string filename, bool quiet = false);

Player &getPlayer() { return player; };
GlobalHotKey &getHotKey() { return hotKey; };
Expand Down
2 changes: 0 additions & 2 deletions src/lightshowmaker/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ int main(int argc, char **argv) {

Application &app = *new Application;

app.setAppHome(argv[0]);

// todo: parse command line

if (!app.init()) {
Expand Down
5 changes: 4 additions & 1 deletion src/media/ImageLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ shared_ptr<Image> ImageLoader::decodeImage(const vector<uint8_t> &vector) {

fmt_ctx = avformat_alloc_context();
fmt_ctx->pb = btc;
avformat_open_input(&fmt_ctx, "dummyFilename", nullptr, nullptr);
if (avformat_open_input(&fmt_ctx, "dummyFilename", nullptr, nullptr) < 0) {
printf("Warning: Cannot open format a codec for stream '%s'\n", filename.c_str());
return nullptr;
}

shared_ptr<Image> ptr = readFromCtx();

Expand Down
2 changes: 1 addition & 1 deletion src/media/ImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Pixel {
};

struct Image {
uint32_t width, height;
uint32_t width = 0, height = 0;
std::vector<Pixel> pixels;
};

Expand Down
2 changes: 1 addition & 1 deletion src/model/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file(GLOB_RECURSE SOURCES *.cpp)
add_library(model ${SOURCES})
target_include_directories(model PUBLIC .)
target_link_libraries(model core media jsoncpp_lib_static)
target_link_libraries(model core media jsoncpp_lib_static pathie)
44 changes: 27 additions & 17 deletions src/model/Decoration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#define DECORATION_H

#include "Serialization.h"
#include "ImageLoader.h"
#include "ExternalResource.h"
#include "fstream"
#include "memory"

namespace sm {
Expand All @@ -18,12 +19,12 @@ class Decoration {
Decoration();

DecorationType type;
ExternalResource resource;

// box
float posX, posY;

// image
std::shared_ptr<media::Image> image;
float width, height;
float ratio = -1;

Expand All @@ -32,7 +33,7 @@ class Decoration {
float size;

// volatile - hold texture
void* textureId = nullptr;
void *textureId = nullptr;

// volatile
bool isSelected = false;
Expand All @@ -45,27 +46,36 @@ class Decoration {
ser.serialize("height", height);
ser.serialize("ratio", ratio);

if (type == IMAGE) {
// legacy image serializer
if (type == IMAGE && ser.DESERIALIZING && ser.hasKey("png")) {
std::vector<uint8_t> bytes;
if (ser.SERIALIZING) {
if (!image) image = std::make_shared<media::Image>();
bytes = media::encodeImage(image);
if (bytes.empty()) {
throw "Cannot encode image";
}
}
ser.serializeBinary("png", bytes);
if (ser.DESERIALIZING) {
image = media::decodeImage(bytes);
if (!image) {
throw "Cannot decode image";
}
}
// std::shared_ptr<media::Image> image = media::decodeImage(bytes);
// if (!image) {
// throw "Cannot decode image";
// }
// save on disk
Pathie::Path assetDir = ser.getBasePath().join("assets");
Pathie::Path outputPath;
int i = 0;
do {
outputPath = assetDir.join("decoration-" + std::to_string(i) + ".png");
i++;
} while (outputPath.exists());
resource.filename = outputPath;
outputPath.parent().mktree();
std::ofstream file;
file.open(outputPath.str(), std::ios_base::binary);
file.write((char *) bytes.data(), bytes.size());
file.close();
}

if (type == LIGHT) {
ser.serialize("color", color);
ser.serialize("size", size);
}

ser.serialize("resource", resource);
}
};

Expand Down
Loading

0 comments on commit 8e3bcbf

Please sign in to comment.