mirror of https://git.sr.ht/~kikoodx/sle
124 lines
3.0 KiB
C
124 lines
3.0 KiB
C
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
/* Copyright (C) 2021 KikooDX */
|
|
|
|
#include "editing_area/draw.h"
|
|
#include "editing_area/level.h"
|
|
#include "info.h"
|
|
#include "mouse.h"
|
|
#include "options.h"
|
|
#include "shared_data.h"
|
|
#include <raylib.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/wait.h>
|
|
|
|
static void init_mouse(struct Options options);
|
|
static void update_mouse(int *mouse_x, int *mouse_y, struct Level level,
|
|
struct SharedData *shared_data);
|
|
|
|
int editing_area_main(struct Options options,
|
|
struct SharedData *shared_data)
|
|
{
|
|
int mouse_x;
|
|
int mouse_y;
|
|
int error;
|
|
struct Level level;
|
|
level.data = NULL;
|
|
Texture2D tileset;
|
|
|
|
/* initialize raylib */
|
|
InitWindow(options.editor_width, options.editor_height,
|
|
"SLE main window");
|
|
SetTargetFPS(options.editor_target_fps);
|
|
init_mouse(options);
|
|
/* load textures */
|
|
tileset = LoadTexture(options.tileset_path);
|
|
|
|
/* only proceed if tileset is large enough */
|
|
if (tileset.width < options.tile_width ||
|
|
tileset.height < options.tile_height) {
|
|
fprintf(stderr,
|
|
"ERROR: tileset size is invalid, expected at "
|
|
"least [%d ; %d], got [%d ; %d]\n",
|
|
options.tile_width, options.tile_height,
|
|
tileset.width, tileset.height);
|
|
goto panic;
|
|
}
|
|
|
|
/* load level or create empty one */
|
|
if (options.level_create) {
|
|
if (level_create(&level, options))
|
|
goto panic;
|
|
} else if (level_read(&level, options.level_path))
|
|
goto panic;
|
|
|
|
while (!WindowShouldClose()) {
|
|
/* update */
|
|
update_mouse(&mouse_x, &mouse_y, level, shared_data);
|
|
|
|
/* draw */
|
|
BeginDrawing();
|
|
|
|
ClearBackground(BLACK);
|
|
level_draw(level, options, tileset);
|
|
editor_mouse_draw(options, mouse_x, mouse_y);
|
|
|
|
EndDrawing();
|
|
}
|
|
|
|
/* save level */
|
|
level_write(level, options.level_path);
|
|
|
|
panic:
|
|
|
|
/* deinit */
|
|
level_free(&level);
|
|
|
|
/* unload textures */
|
|
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;
|
|
}
|
|
|
|
static void init_mouse(struct Options options)
|
|
{
|
|
SetMouseOffset(-options.editor_draw_offset_x,
|
|
-options.editor_draw_offset_y);
|
|
SetMouseScale(1.0 / options.tile_width,
|
|
1.0 / options.tile_height);
|
|
}
|
|
|
|
static void update_mouse(int *mouse_x, int *mouse_y, struct Level level,
|
|
struct SharedData *shared_data)
|
|
{
|
|
const bool left_click = IsMouseButtonDown(0);
|
|
const bool right_click = IsMouseButtonDown(1);
|
|
const bool middle_click = IsMouseButtonPressed(2);
|
|
|
|
update_mouse_position(mouse_x, mouse_y, level.width - 1,
|
|
level.height - 1);
|
|
|
|
/* set tile */
|
|
if (left_click) {
|
|
level.data[*mouse_x + *mouse_y * level.width] =
|
|
shared_data->selected_tile;
|
|
}
|
|
/* remove tile */
|
|
if (right_click) {
|
|
level.data[*mouse_x + *mouse_y * level.width] = 0;
|
|
}
|
|
/* get info about tile */
|
|
if (middle_click) {
|
|
INFO_VAR("%d",
|
|
level.data[*mouse_x + *mouse_y * level.width]);
|
|
}
|
|
}
|