Shared data between processes

The two windows are now connected!
This commit is contained in:
KikooDX 2021-03-24 12:42:10 +01:00
parent 400e9d91f7
commit 5f661484e7
8 changed files with 71 additions and 14 deletions

View File

@ -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})

View File

@ -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;

View File

@ -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);

9
include/shared_data.h Normal file
View File

@ -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;
};

View File

@ -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);

View File

@ -4,10 +4,14 @@
#include "conf.h"
#include "editing_area/draw.h"
#include "editing_area/level.h"
#include "shared_data.h"
#include <raylib.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/wait.h>
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;
}

View File

@ -2,19 +2,33 @@
/* Copyright (C) 2021 KikooDX */
#include "editing_area/main.h"
#include "shared_data.h"
#include "tile_picker/main.h"
#include <raylib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
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);
}

View File

@ -2,22 +2,24 @@
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "shared_data.h"
#include "tile_picker/draw.h"
#include <raylib.h>
#include <stdlib.h>
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;