~kikoodx/sle

5727f19fb94fd9acff530dccd917e79afc547d74 — KikooDX 3 years ago dc7161c 0.4.0
feature: window resizing (#1)
M CMakeLists.txt => CMakeLists.txt +1 -0
@@ 8,6 8,7 @@ set(SOURCES
	src/main.c
	src/options.c
	src/mouse.c
	src/scale.c
	src/strtoint.c
	src/strtocolor.c
	src/editing_area/main.c

M README => README +2 -0
@@ 67,9 67,11 @@ These flags take integers as argument.
-level-width : used by -create
-level-height : used by -create
-tile-first
-editor-scale
-editor-width
-editor-height
-editor-target-fps
-editor-scale
-editor-off-x
-editor-off-y
-picker-target-fps

M include/conf.h => include/conf.h +3 -1
@@ 20,13 20,15 @@
#define NEW_LEVEL_WIDTH  32
#define NEW_LEVEL_HEIGHT 18

#define EDITOR_SCALE         2
#define EDITOR_WINDOW_WIDTH  512
#define EDITOR_WINDOW_HEIGHT 288
#define EDITOR_TARGET_FPS    60
#define EDITOR_DRAW_OFFSET_X 0
#define EDITOR_DRAW_OFFSET_Y 0

#define PICKER_TARGET_FPS 30
#define PICKER_SCALE      2
#define PICKER_TARGET_FPS 60
#define PICKER_PADDING    4

#define PICKER_WINDOW_WIDTH                                            \

M include/options.h => include/options.h +2 -0
@@ 16,12 16,14 @@ struct Options {
	int tile_first;
	int new_level_width;
	int new_level_height;
	int editor_scale;
	int editor_width;
	int editor_height;
	int editor_target_fps;
	int editor_draw_offset_x;
	int editor_draw_offset_y;
	Color editor_bg_color;
	int picker_scale;
	int picker_target_fps;
	int picker_padding;
	Color picker_bg_color;

A include/scale.h => include/scale.h +11 -0
@@ 0,0 1,11 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#pragma once

#include "options.h"
#include <raylib.h>

float scale_editor(struct Options options);
Vector2 offset_editor(struct Options options);
float scale_picker(struct Options options);
Vector2 offset_picker(struct Options options);

M src/editing_area/main.c => src/editing_area/main.c +33 -7
@@ 7,6 7,7 @@
#include "info.h"
#include "mouse.h"
#include "options.h"
#include "scale.h"
#include "shared_data.h"
#include <raylib.h>
#include <stdbool.h>


@@ 29,13 30,20 @@ int editing_area_main(struct Options options,
	Texture2D tileset;

	/* initialize raylib */
	InitWindow(options.editor_width, options.editor_height,
	InitWindow(options.editor_width * options.editor_scale,
	           options.editor_height * options.editor_scale,
	           "SLE main window");
	SetWindowState(FLAG_WINDOW_RESIZABLE);
	SetTargetFPS(options.editor_target_fps);
	init_mouse(options);
	/* load textures */
	tileset = LoadTexture(options.tileset_path);

	/* render targets used for upscaling */
	const RenderTexture2D rend_target = LoadRenderTexture(
	    options.editor_width, options.editor_height);
	const RenderTexture2D flip_target = LoadRenderTexture(
	    options.editor_width, options.editor_height);

	/* only proceed if tileset is large enough */
	if (tileset.width < options.tile_width ||
	    tileset.height < options.tile_height) {


@@ 56,15 64,29 @@ int editing_area_main(struct Options options,

	while (!WindowShouldClose()) {
		/* update */
		init_mouse(options);
		update_mouse(&mouse_x, &mouse_y, level, shared_data);

		/* draw */
		BeginDrawing();
		BeginTextureMode(rend_target);

		ClearBackground(options.editor_bg_color);
		level_draw(level, options, tileset);
		editor_mouse_draw(options, mouse_x, mouse_y);

		EndTextureMode();

		/* flip texture */
		BeginTextureMode(flip_target);
		DrawTexture(rend_target.texture, 0, 0, WHITE);
		EndTextureMode();

		/* draw upscaled render */
		ClearBackground(BLACK);
		DrawTextureEx(flip_target.texture,
		              offset_editor(options), 0,
		              scale_editor(options), WHITE);
		EndDrawing();
	}



@@ 76,8 98,10 @@ panic:
	/* deinit */
	level_free(&level);

	/* unload textures */
	/* unload raylib stuff */
	UnloadTexture(tileset);
	UnloadRenderTexture(rend_target);
	UnloadRenderTexture(flip_target);
	CloseWindow();

	/* tell to the child process it can end */


@@ 91,10 115,12 @@ panic:

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);
	const float scaling = scale_editor(options);
	const Vector2 off = offset_editor(options);
	SetMouseOffset(-options.editor_draw_offset_x - off.x,
	               -options.editor_draw_offset_y - off.y);
	SetMouseScale(1.0 / scaling / options.tile_width,
	              1.0 / scaling / options.tile_height);
}

static void update_mouse(int *mouse_x, int *mouse_y, struct Level level,

M src/main.c => src/main.c +8 -0
@@ 102,12 102,14 @@ static void process_arguments(int argc, char **argv,
		    {"tile-first", required_argument, 0, 'I'},
		    {"level-width", required_argument, 0, 'i'},
		    {"level-height", required_argument, 0, 'e'},
		    {"editor-scale", required_argument, 0, 's'},
		    {"editor-width", required_argument, 0, 'W'},
		    {"editor-height", required_argument, 0, 'H'},
		    {"editor-fps", required_argument, 0, 'f'},
		    {"editor-off-x", required_argument, 0, 'o'},
		    {"editor-off-y", required_argument, 0, 'O'},
		    {"editor-bg-color", required_argument, 0, 'b'},
		    {"picker-scale", required_argument, 0, 'S'},
		    {"picker-fps", required_argument, 0, 'F'},
		    {"picker-padding", required_argument, 0, 'p'},
		    {"picker-bg-color", required_argument, 0, 'B'},


@@ 146,6 148,9 @@ static void process_arguments(int argc, char **argv,
		case 'e': /* new level height */
			options->new_level_height = int_arg_min(opt, 1);
			break;
		case 's': /* editor scale */
			options->editor_scale = int_arg_min(opt, 1);
			break;
		case 'W': /* editor width */
			options->editor_width = int_arg_min(opt, 1);
			break;


@@ 167,6 172,9 @@ static void process_arguments(int argc, char **argv,
		case 'b': /* editor background color */
			options->editor_bg_color = strtocolor(optarg);
			break;
		case 'S': /* picker scale */
			options->picker_scale = int_arg_min(opt, 1);
			break;
		case 'F': /* picker target FPS */
			options->picker_target_fps =
			    int_arg_min(opt, 1);

M src/options.c => src/options.c +2 -0
@@ 13,12 13,14 @@ struct Options options_defaults(void)
	    .tileset_height = 0,
	    .new_level_width = NEW_LEVEL_WIDTH,
	    .new_level_height = NEW_LEVEL_HEIGHT,
	    .editor_scale = EDITOR_SCALE,
	    .editor_width = EDITOR_WINDOW_WIDTH,
	    .editor_height = EDITOR_WINDOW_HEIGHT,
	    .editor_target_fps = EDITOR_TARGET_FPS,
	    .editor_draw_offset_x = EDITOR_DRAW_OFFSET_X,
	    .editor_draw_offset_y = EDITOR_DRAW_OFFSET_Y,
	    .editor_bg_color = EDITOR_BACKGROUND_COLOR,
	    .picker_scale = PICKER_SCALE,
	    .picker_window_width = 0,
	    .picker_window_height = 0,
	    .picker_target_fps = PICKER_TARGET_FPS,

A src/scale.c => src/scale.c +46 -0
@@ 0,0 1,46 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */

#include "scale.h"
#include "options.h"
#include <raylib.h>

float scale_editor(struct Options options)
{
	const float w_ratio =
	    (float)GetScreenWidth() / options.editor_width;
	const float h_ratio =
	    (float)GetScreenHeight() / options.editor_height;
	return (w_ratio < h_ratio) ? w_ratio : h_ratio;
}

Vector2 offset_editor(struct Options options)
{
	const float scaling = scale_editor(options);
	return (Vector2){
	    ((float)GetScreenWidth() - options.editor_width * scaling) /
		2,
	    ((float)GetScreenHeight() -
	     options.editor_height * scaling) /
		2};
}

float scale_picker(struct Options options)
{
	const float w_ratio =
	    (float)GetScreenWidth() / options.picker_window_width;
	const float h_ratio =
	    (float)GetScreenHeight() / options.picker_window_height;
	return (w_ratio < h_ratio) ? w_ratio : h_ratio;
}

Vector2 offset_picker(struct Options options)
{
	const float scaling = scale_picker(options);
	return (Vector2){((float)GetScreenWidth() -
	                  options.picker_window_width * scaling) /
	                     2,
	                 ((float)GetScreenHeight() -
	                  options.picker_window_height * scaling) /
	                     2};
}

M src/tile_picker/main.c => src/tile_picker/main.c +31 -6
@@ 4,6 4,7 @@
#include "conf.h"
#include "mouse.h"
#include "options.h"
#include "scale.h"
#include "shared_data.h"
#include "tile_picker/draw.h"
#include <raylib.h>


@@ 24,28 25,48 @@ int tile_picker_main(struct Options options,
	Texture2D tileset;

	/* initialize raylib */
	InitWindow(options.picker_window_width,
	           options.picker_window_height,
	InitWindow(options.picker_window_width * options.picker_scale,
	           options.picker_window_height * options.picker_scale,
	           "SLE secondary window");
	SetTargetFPS(options.picker_target_fps);
	init_mouse(options);
	/* load textures */
	tileset = LoadTexture(options.tileset_path);

	/* render targets used for upscaling */
	const RenderTexture2D rend_target = LoadRenderTexture(
	    options.picker_window_width, options.picker_window_height);
	const RenderTexture2D flip_target = LoadRenderTexture(
	    options.picker_window_width, options.picker_window_height);

	/* only proceed if tileset is well loaded */
	if (tileset.width > 0) {
		while (!shared_data->end_child) {
			/* update */
			init_mouse(options);
			update_mouse(&mouse_x, &mouse_y, options,
			             shared_data);

			/* draw */
			BeginDrawing();
			BeginTextureMode(rend_target);

			ClearBackground(options.picker_bg_color);
			tileset_draw(tileset, options,
			             shared_data->selected_tile);

			EndTextureMode();

			/* flip texture */
			BeginTextureMode(flip_target);
			DrawTexture(rend_target.texture, 0, 0, WHITE);
			EndTextureMode();

			/* draw upscaled render */
			ClearBackground(BLACK);
			DrawTextureEx(flip_target.texture,
			              offset_picker(options), 0,
			              scale_picker(options), WHITE);
			EndDrawing();
		}
	}


@@ 60,11 81,15 @@ int tile_picker_main(struct Options options,

static void init_mouse(struct Options options)
{
	SetMouseOffset(-options.picker_padding,
	               -options.picker_padding);
	const float scaling = scale_picker(options);
	const Vector2 off = offset_picker(options);
	SetMouseOffset(-options.picker_padding - off.x,
	               -options.picker_padding - off.y);
	SetMouseScale(
	    1.0 / (options.tile_width + options.picker_padding),
	    1.0 / (options.tile_height + options.picker_padding));
	    1.0 / (options.tile_width + options.picker_padding) /
		scaling,
	    1.0 / (options.tile_height + options.picker_padding) /
		scaling);
}

static void update_mouse(int *mouse_x, int *mouse_y,