diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ddec9a..4868878 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(FLAGS -Wall -Wextra -Wshadow -Wswitch-default -Wswitch-enum -Wunreachable-code -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Werror-implicit-function-declaration + -Werror -pedantic -std=c90 -Os) add_executable(${PROJECT_NAME} ${SOURCES} ${SOURCES_RAYLIB}) diff --git a/include/conf.h b/include/conf.h index 9fea3a8..c750632 100644 --- a/include/conf.h +++ b/include/conf.h @@ -10,4 +10,5 @@ static const int draw_offset_x = -2; static const int draw_offset_y = -8; static const int tile_width = 16; static const int tile_height = 16; -static const int target_fps = 60; +static const int editor_target_fps = 60; +static const int picker_target_fps = 30; diff --git a/include/editing_area/main.h b/include/editing_area/main.h index c1d85eb..baa33f8 100644 --- a/include/editing_area/main.h +++ b/include/editing_area/main.h @@ -2,4 +2,7 @@ /* Copyright (C) 2021 KikooDX */ #pragma once -int editing_area_main(int argc, char **argv); +#include "shared_data.h" + +int editing_area_main(int argc, char **argv, + struct SharedData *shared_data); diff --git a/include/shared_data.h b/include/shared_data.h new file mode 100644 index 0000000..d5b3c1d --- /dev/null +++ b/include/shared_data.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* Copyright (C) 2021 KikooDX */ +#pragma once + +/* data shared between processes */ +struct SharedData { + int end_child; + int selected_tile; +}; diff --git a/include/tile_picker/main.h b/include/tile_picker/main.h index eb5623d..ed484b6 100644 --- a/include/tile_picker/main.h +++ b/include/tile_picker/main.h @@ -2,4 +2,7 @@ /* Copyright (C) 2021 KikooDX */ #pragma once -int tile_picker_main(int argc, char **argv); +#include "shared_data.h" + +int tile_picker_main(int argc, char **argv, + struct SharedData *shared_data); diff --git a/src/editing_area/main.c b/src/editing_area/main.c index 59ba8b6..a43923c 100644 --- a/src/editing_area/main.c +++ b/src/editing_area/main.c @@ -4,10 +4,14 @@ #include "conf.h" #include "editing_area/draw.h" #include "editing_area/level.h" +#include "shared_data.h" #include +#include #include +#include -int editing_area_main(int argc, char **argv) +int editing_area_main(int argc, char **argv, + struct SharedData *shared_data) { struct Level level; level.data = NULL; @@ -15,11 +19,11 @@ int editing_area_main(int argc, char **argv) /* initialize raylib */ InitWindow(game_window_width, game_window_height, "SLE main window"); - SetTargetFPS(target_fps); + SetTargetFPS(editor_target_fps); /* load textures */ const Texture2D tileset = LoadTexture(argv[1]); - /* only process if tileset is well loaded */ + /* only proceed if tileset is well loaded */ if (tileset.width > 0) { /* load level */ level_read(&level, argv[2]); @@ -45,5 +49,11 @@ int editing_area_main(int argc, char **argv) UnloadTexture(tileset); CloseWindow(); + /* tell to the child process it can end */ + shared_data->end_child = true; + + /* wait for child process to exit */ + wait(NULL); + return EXIT_SUCCESS; } diff --git a/src/main.c b/src/main.c index 8ca4c01..0877886 100644 --- a/src/main.c +++ b/src/main.c @@ -2,19 +2,33 @@ /* Copyright (C) 2021 KikooDX */ #include "editing_area/main.h" +#include "shared_data.h" #include "tile_picker/main.h" +#include +#include #include #include +#include #include #include +static void *create_shared_memory(size_t size); + /* The editor fork to create two windows. One will be the level editor * and the other the tile selection. This is done to allow the user to * organize these panels in the way they want to. */ int main(int argc, char **argv) { + struct SharedData *shared_data = + (struct SharedData *)create_shared_memory( + sizeof(struct SharedData)); + shared_data->end_child = false; + shared_data->selected_tile = 1; pid_t child_process; + /* set log level */ + SetTraceLogLevel(LOG_ERROR); + /* check for argument count */ if (argc != 3) { fprintf(stderr, "ERROR: expected 2 arguments, got %d\n", @@ -22,14 +36,27 @@ int main(int argc, char **argv) return EXIT_FAILURE; }; + /* forking here */ child_process = fork(); if (child_process < 0) { fprintf(stderr, "ERROR: process couldn't fork"); return EXIT_FAILURE; - } else if (child_process == 0) + } else if (child_process == 0) { /* child process, start the tile picker */ - return tile_picker_main(argc, argv); - else + return tile_picker_main(argc, argv, shared_data); + } else { /* main process, start the editing area */ - return editing_area_main(argc, argv); + editing_area_main(argc, argv, shared_data); + return EXIT_SUCCESS; + } +} + +static void *create_shared_memory(size_t size) +{ + /* shared memory will be read & write */ + int protection = PROT_READ | PROT_WRITE; + /* anonymous and shared, only child process will be able to + * access it */ + int visibility = MAP_SHARED | MAP_ANONYMOUS; + return mmap(NULL, size, protection, visibility, -1, 0); } diff --git a/src/tile_picker/main.c b/src/tile_picker/main.c index fdc27a9..3533523 100644 --- a/src/tile_picker/main.c +++ b/src/tile_picker/main.c @@ -2,22 +2,24 @@ /* Copyright (C) 2021 KikooDX */ #include "conf.h" +#include "shared_data.h" #include "tile_picker/draw.h" #include #include -int tile_picker_main(int argc, char **argv) +int tile_picker_main(int argc, char **argv, + struct SharedData *shared_data) { /* initialize raylib */ InitWindow(picker_window_width, picker_window_height, "SLE secondary window"); - SetTargetFPS(target_fps); + SetTargetFPS(picker_target_fps); /* load textures */ const Texture2D tileset = LoadTexture(argv[1]); - /* only process if tileset is well loaded */ + /* only proceed if tileset is well loaded */ if (tileset.width > 0) { - while (!WindowShouldClose()) { + while (!shared_data->end_child) { /* draw */ BeginDrawing(); @@ -30,6 +32,7 @@ int tile_picker_main(int argc, char **argv) /* unload textures */ UnloadTexture(tileset); + CloseWindow(); return EXIT_SUCCESS;