glsl-basic/src/main.cpp
2023-10-21 19:20:40 +02:00

139 lines
4.1 KiB
C++

#include <boost/log/trivial.hpp>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include "main.hpp"
int main(int argc, char **argv) {
/* declarations */
int result = 0;
/* Parsing CLI Arguments */
po::options_description desc("Available options");
desc.add_options()("help,h", "show help")("verbose,v", "more verbose output")(
"vert", po::value<string>()->required(), "vertex shader file")(
"frag", po::value<string>()->required(), "fragment shader file")(
"base,b", po::value<string>()->required(), "base program");
/* unused for now */
po::positional_options_description pdesc;
// NOTE: if you want to add a positional argument,
// you need to create and option for it as it seems.
/* pdesc.add("vert", 1); */
/* pdesc.add("frag", 1); */
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(desc) // load options
.positional(pdesc) // load positionals
.run(),
vm);
po::notify(vm);
if (vm.count("help") || argc < 3) {
help(argv[0], desc);
return 1;
}
} catch (po::error &e) {
BOOST_LOG_TRIVIAL(fatal) << e.what() << endl << endl;
help(argv[0], desc);
return EXIT_USAGE;
}
// set boost log level to trace on verbose
// TODO: -v should decrease the default log level by 1 but be repeatable.
// (default should be info)
if (vm.count("verbose")) {
boost::log::core::get()->set_filter(boost::log::trivial::severity >=
boost::log::trivial::trace);
BOOST_LOG_TRIVIAL(trace)
<< "verbose flag found, setting log level to trace";
}
/* init graphics stuff */
GLFWwindow *window = initGl();
if (result != 0) {
return EXIT_GL;
}
/* find the requested */
ShaderProgram::Base base =
ShaderProgram::baseFromName(vm["base"].as<string>());
/* setup the shader program */
BOOST_LOG_TRIVIAL(trace) << "Reading shader files";
ifstream fvert(vm["vert"].as<string>());
ifstream ffrag(vm["frag"].as<string>());
if (fvert.fail()) {
BOOST_LOG_TRIVIAL(fatal) << "failed to read vertex shader file";
exit(EXIT_IO);
}
if (ffrag.fail()) {
BOOST_LOG_TRIVIAL(fatal) << "failed to read fragments shader file";
exit(EXIT_IO);
}
string buf;
string vert;
string frag;
while(!fvert.eof()) {
getline(fvert, buf);
vert.append(buf + "\n");
}
while(!ffrag.eof()) {
getline(ffrag, buf);
frag.append(buf + "\n");
}
// get file sizes
BOOST_LOG_TRIVIAL(trace) << "Reading shader file sizes";
BOOST_LOG_TRIVIAL(debug) << "vert file:\n```glsl\n"
<< vert << "```\n"
<< vert.size() << "B";
BOOST_LOG_TRIVIAL(debug) << "frag file:\n```glsl\n"
<< frag << "```\n"
<< frag.size() << "B";
fvert.close();
ffrag.close();
BOOST_LOG_TRIVIAL(trace) << "Creating ShaderProgram";
// TODO: determinde ShaderProgram::Base with a CLI arg
ShaderProgram shaderProgram(base, vert, frag);
/* Run our main program */
BOOST_LOG_TRIVIAL(trace) << "Starting mainWindow";
result = mainWindow(&shaderProgram, window);
BOOST_LOG_TRIVIAL(trace) << "mainWindow stopped, goodbye";
return result;
}
void help(char *prog, po::options_description desc) {
cout << "Usage:" << endl
<< prog
<< " [-vh] [--vert] shaders/vertex.glsl [--frag] shaders/fragment.glsl"
<< endl
<< endl
<< "return values:" << endl
<< "0\tSuccess" << endl
<< "1\tGeneric Failure" << endl
<< "2\tBad arguments" << endl
<< "3\tI/O Error" << endl
<< "4\tOpenGL Error" << endl
<< "5\tShader Source Error" << endl
<< endl
<< endl
<< "base programs:" << endl
<< "Empty\t\t-\tEmpty window with colored background" << endl
<< "Triangle\t-\tA static Triangle" << endl
<< endl
<< endl
<< desc << endl;
// NOTE: constants for the error codes are defined in main.hpp
}