Skip to content

Commit

Permalink
core: Add start_application() to replace the vull::main target
Browse files Browse the repository at this point in the history
It is better for the application to define int main(). Also switch
to the new ArgsParser class.
  • Loading branch information
yeetari committed Apr 6, 2024
1 parent 2c8c0d9 commit dd395cc
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 142 deletions.
1 change: 0 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
vull_module(core xxHash::xxHash Zstd::Zstd)
vull_module(graphics vull::core vull::vulkan)
vull_module(json vull::core)
vull_module(main vull::core)
vull_module(physics vull::core)
vull_module(script vull::core)
vull_module(shaderc vull::core)
Expand Down
11 changes: 11 additions & 0 deletions engine/include/vull/core/application.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <vull/support/function.hh>

namespace vull {

class ArgsParser;

int start_application(int argc, char **argv, ArgsParser &args_parser, Function<void()> start_fn);

} // namespace vull
6 changes: 0 additions & 6 deletions engine/include/vull/core/main.hh

This file was deleted.

3 changes: 1 addition & 2 deletions engine/sources/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target_sources(vull-core PRIVATE
core/application.cc
core/log.cc
ecs/entity.cc
ecs/world.cc
Expand Down Expand Up @@ -40,8 +41,6 @@ target_sources(vull-json PRIVATE
json/parser.cc
json/tree.cc)

target_sources(vull-main PRIVATE core/main.cc)

target_sources(vull-physics PRIVATE
physics/mpr.cc
physics/physics_engine.cc
Expand Down
83 changes: 83 additions & 0 deletions engine/sources/core/application.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <vull/core/application.hh>

#include <vull/core/log.hh>
#include <vull/platform/file.hh>
#include <vull/support/args_parser.hh>
#include <vull/tasklet/scheduler.hh>
#include <vull/vpak/file_system.hh>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

namespace vull {

static int vpak_select(const struct dirent *entry) {
return StringView(static_cast<const char *>(entry->d_name)).ends_with(".vpak") ? 1 : 0;
}

int start_application(int argc, char **argv, ArgsParser &args_parser, Function<void()> start_fn) {
String vpak_directory_path;
args_parser.add_option(vpak_directory_path, "Vpak directory path", "vpak-dir");
if (auto result = args_parser.parse_args(argc, argv); result != ArgsParseResult::Continue) {
return result == ArgsParseResult::ExitSuccess ? EXIT_SUCCESS : EXIT_FAILURE;
}

// Default to directory containing the executable.
if (vpak_directory_path.empty()) {
char *last_slash = argv[0];
for (char *path = argv[0]; *path != '\0'; path++) {
if (*path == '/') {
last_slash = path;
}
}
vpak_directory_path = String::copy_raw(argv[0], static_cast<size_t>(last_slash - argv[0]));
}

vull::open_log();
vull::set_log_colours_enabled(isatty(STDOUT_FILENO) == 1);

struct dirent **entry_list;
int entry_count = scandir(vpak_directory_path.data(), &entry_list, &vpak_select, &alphasort);
if (entry_count < 0) {
vull::error("[main] Failed to scan vpak directory '{}': {}", vpak_directory_path, strerror(errno));
return EXIT_FAILURE;
}

int dir_fd = open(vpak_directory_path.data(), O_DIRECTORY);
if (dir_fd < 0) {
vull::error("[main] Failed to open vpak directory '{}': {}", vpak_directory_path, strerror(errno));
return EXIT_FAILURE;
}

for (int i = 0; i < entry_count; i++) {
const dirent *entry = entry_list[i];

String name(static_cast<const char *>(entry->d_name));
vull::info("[main] Found vpak {}", name);

int fd = openat(dir_fd, name.data(), 0);
if (fd < 0) {
vull::error("[main] Failed to open vpak '{}': {}", name, strerror(errno));
return EXIT_FAILURE;
}

vpak::load_vpak(File::from_fd(fd));
free(entry_list[i]);
}
free(entry_list);
close(dir_fd);

Scheduler scheduler;
scheduler.start([start_fn = vull::move(start_fn)] {
start_fn();
Scheduler::current().stop();
vull::close_log();
});
return EXIT_SUCCESS;
}

} // namespace vull
109 changes: 0 additions & 109 deletions engine/sources/core/main.cc

This file was deleted.

2 changes: 1 addition & 1 deletion sandbox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vull_executable(vull-sandbox vull::core vull::graphics vull::main vull::physics vull::ui vull::x11-window)
vull_executable(vull-sandbox vull::core vull::graphics vull::physics vull::ui vull::x11-window)
target_sources(vull-sandbox PRIVATE free_camera.cc main.cc orbit_camera.cc)
vull_depend_builtin(vull-sandbox)
38 changes: 15 additions & 23 deletions sandbox/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include <vull/container/array.hh>
#include <vull/container/hash_map.hh>
#include <vull/container/vector.hh>
#include <vull/core/application.hh>
#include <vull/core/input.hh>
#include <vull/core/log.hh>
#include <vull/core/main.hh>
#include <vull/core/window.hh>
#include <vull/ecs/world.hh>
#include <vull/graphics/default_renderer.hh>
Expand All @@ -23,6 +23,7 @@
#include <vull/platform/timer.hh>
#include <vull/scene/scene.hh>
#include <vull/support/algorithm.hh>
#include <vull/support/args_parser.hh>
#include <vull/support/function.hh>
#include <vull/support/optional.hh>
#include <vull/support/result.hh>
Expand Down Expand Up @@ -61,28 +62,7 @@

using namespace vull;

void vull_main(Vector<StringView> &&args) {
if (args.size() < 2) {
vull::println("usage: {} [--enable-vvl] <scene-name>", args[0]);
return;
}

StringView scene_name;
bool enable_validation = false;
for (const auto &arg : vull::slice(args, 1u)) {
if (arg == "--enable-vvl") {
enable_validation = true;
} else if (arg[0] == '-') {
vull::println("fatal: unknown option {}", arg);
return;
} else if (scene_name.empty()) {
scene_name = arg;
} else {
vull::println("fatal: unexpected argument {}", arg);
return;
}
}

void sandbox_main(bool enable_validation, StringView scene_name) {
Window window({}, {}, true);
vk::Context context(enable_validation);
auto swapchain = window.create_swapchain(context, vk::SwapchainMode::LowPower);
Expand Down Expand Up @@ -277,3 +257,15 @@ void vull_main(Vector<StringView> &&args) {
}
context.vkDeviceWaitIdle();
}

int main(int argc, char **argv) {
bool enable_validation = false;
String scene_name;

ArgsParser args_parser("vull-sandbox", "Vull Sandbox", "0.1.0");
args_parser.add_flag(enable_validation, "Enable vulkan validation layer", "enable-vvl");
args_parser.add_argument(scene_name, "scene-name", true);
return vull::start_application(argc, argv, args_parser, [&] {
sandbox_main(enable_validation, scene_name);
});
}

0 comments on commit dd395cc

Please sign in to comment.