~kikoodx/sle

b88e37fadffdca79428d632308de474c2eb91041 — KikooDX 17 days ago a3e60cc 0.1.0
Level creation flag & cleanup and improved README
M .clang-format => .clang-format +1 -0
@@ 5,3 5,4 @@ BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 72
AlignConsecutiveMacros: true

M README => README +28 -5
@@ 45,22 45,25 @@ Close/kill the main window. The secondary window will close automaticly
and your modifications to the level file will be writen to disk.

FLAGS
======
=====

Mendatory flags
Mandatory flags
---------------
All these options take a path to a file as argument.
-tileset Tileset image
-level   KBLE file

Optionnal flags
---------------
Optional flags
--------------
These flags are boolean toggles. They take no option.
-verbose
-verbose : print debug informations to the standard output
-create : create an empty level instead of reading from the -level path

All these flags take integers as argument.
-tile-width
-tile-height
-level-width : used by -create
-level-height : used by -create
-editor-width
-editor-height
-editor-target-fps


@@ 69,6 72,26 @@ All these flags take integers as argument.
-picker-target-fps
-picker-padding

Example aliases
---------------
By using the flags, it's very easy to create specific configurations.
Following are various aliases. They assume than sle is in your PATH.

# PICO-8-like configuration
alias sle-p8='sle -tile-width 8 -tile-height 8 -level-width 16 \
  -level-height 16 -editor-width 128 -editor-height 128 \
  -editor-off-x 0 -editor-off-y 0'

# fx-CG50 configuration
alias sle-cg='sle -tile-width 16 -tile-height 16 -level-width 25 \
  -level-height 14 -editor-width 396 -editor-height 224 \
  -editor-off-x -2 -editor-off-y 0'

# fx-9860G configuration
alias sle-fx='sle -tile-width 8 -tile-height 8 -level-width 16 \
  -level-height 8 -editor-width 128 -editor-height 64 \
  -editor-off-x 0 -editor-off-y 0'

DEFAULT CONFIGURATION
=====================
Edit include/conf.h to change default configuration. Read the comments!

M include/conf.h => include/conf.h +9 -5
@@ 4,7 4,7 @@

#include <raylib.h> /* for the color struct */

#define TILE_WIDTH 16
#define TILE_WIDTH  16
#define TILE_HEIGHT 16

#define UNSELECTED_TILE_COLOR                                          \


@@ 12,14 12,18 @@
#define OVERRING_TILE_COLOR                                            \
	(Color) { 255, 255, 255, 80 }

#define EDITOR_WINDOW_WIDTH 396
#define NEW_LEVEL_WIDTH  25
#define NEW_LEVEL_HEIGHT 14

#define EDITOR_WINDOW_WIDTH  396
#define EDITOR_WINDOW_HEIGHT 224
#define EDITOR_TARGET_FPS 60
#define EDITOR_TARGET_FPS    60
#define EDITOR_DRAW_OFFSET_X -2
#define EDITOR_DRAW_OFFSET_Y -8
#define EDITOR_DRAW_OFFSET_Y 0

#define PICKER_TARGET_FPS 30
#define PICKER_PADDING 4
#define PICKER_PADDING    4

#define PICKER_WINDOW_WIDTH                                            \
	((TILE_WIDTH + PICKER_PADDING) * TILESET_WIDTH + PICKER_PADDING)
#define PICKER_WINDOW_HEIGHT                                           \

M include/editing_area/level.h => include/editing_area/level.h +4 -0
@@ 2,6 2,8 @@
/* Copyright (C) 2021 KikooDX */
#pragma once

#include "options.h"

typedef unsigned int Tile;
struct Level {
	int width;


@@ 11,6 13,8 @@ struct Level {

/* return -1 if error */
int level_read(struct Level *, char *path);
/* return -1 if error */
int level_create(struct Level *, struct Options options);
void level_free(struct Level *);
/* return -1 if error */
int level_write(struct Level level, char *path);

M include/options.h => include/options.h +3 -0
@@ 11,6 11,8 @@ struct Options {
	/* optionnal arguments, default values in conf.h */
	int tile_width;
	int tile_height;
	int new_level_width;
	int new_level_height;
	int editor_width;
	int editor_height;
	int editor_target_fps;


@@ 23,6 25,7 @@ struct Options {
	int tileset_height;
	int picker_window_width;
	int picker_window_height;
	int level_create;
};

struct Options options_defaults(void);

M src/editing_area/level.c => src/editing_area/level.c +39 -3
@@ 3,6 3,7 @@

#include "editing_area/level.h"
#include "info.h"
#include "options.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


@@ 64,12 65,12 @@ int level_read(struct Level *level, char *path)

	/* allocate memory for data */
	level_size = level->width * level->height;
	if (level->data == NULL) {
	if (level->data == NULL)
		level->data = malloc(level_size * sizeof(Tile));
	} else {
	else
		level->data =
		    realloc(level->data, level_size * sizeof(Tile));
	}

	/* check for allocation failure */
	if (level->data == NULL) {
		fprintf(stderr, "ERROR: memory allocation failure\n");


@@ 88,6 89,41 @@ int level_read(struct Level *level, char *path)
	return 0;
}

int level_create(struct Level *level, struct Options options)
{
	int level_size;
	int i;

	/* set width */
	level->width = options.new_level_width;
	if (level->width == (Tile)-1)
		return -1;
	/* set height */
	level->height = options.new_level_height;
	if (level->height == (Tile)-1)
		return -1;

	/* allocate memory for data */
	level_size = level->width * level->height;
	if (level->data == NULL)
		level->data = malloc(level_size * sizeof(Tile));
	else
		level->data =
		    realloc(level->data, level_size * sizeof(Tile));

	/* check for allocation failure */
	if (level->data == NULL) {
		fprintf(stderr, "ERROR: memory allocation failure\n");
		return -1;
	}

	/* file the level with zeros */
	for (i = 0; i < level_size; i += 1)
		level->data[i] = 0;

	return 0;
}

void level_free(struct Level *level) { free(level->data); }

int level_write(struct Level level, char *path)

M src/editing_area/main.c => src/editing_area/main.c +5 -2
@@ 46,8 46,11 @@ int editing_area_main(struct Options options,
		goto panic;
	}

	/* load level */
	if (level_read(&level, options.level_path))
	/* 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()) {

M src/main.c => src/main.c +12 -2
@@ 2,6 2,7 @@
/* Copyright (C) 2021 KikooDX */

#include "editing_area/main.h"
#include "info.h"
#include "options.h"
#include "shared_data.h"
#include "strtoint.h"


@@ 40,7 41,7 @@ int main(int argc, char **argv)
	shared_data->end_child = false;
	shared_data->selected_tile = 1;

	/* set log level */
	/* set log level for raylib */
	SetTraceLogLevel(LOG_ERROR);

	/* process arguments */


@@ 90,12 91,15 @@ static void process_arguments(int argc, char **argv,
{
	int opt;
	while (1) {
		static struct option long_options[] = {
		const struct option long_options[] = {
		    {"verbose", no_argument, &verbose, 1},
		    {"create", no_argument, &options->level_create, 1},
		    {"level", required_argument, 0, 'l'},
		    {"tileset", required_argument, 0, 't'},
		    {"tile-width", required_argument, 0, 'w'},
		    {"tile-height", required_argument, 0, 'h'},
		    {"level-width", required_argument, 0, 'i'},
		    {"level-height", required_argument, 0, 'e'},
		    {"editor-width", required_argument, 0, 'W'},
		    {"editor-height", required_argument, 0, 'H'},
		    {"editor-fps", required_argument, 0, 'f'},


@@ 129,6 133,12 @@ static void process_arguments(int argc, char **argv,
		case 'h': /* tile height */
			options->tile_height = int_arg_min(opt, 1);
			break;
		case 'i': /* new level width */
			options->new_level_width = int_arg_min(opt, 1);
			break;
		case 'e': /* new level height */
			options->new_level_height = int_arg_min(opt, 1);
			break;
		case 'W': /* editor width */
			options->editor_width = int_arg_min(opt, 1);
			break;

M src/options.c => src/options.c +3 -0
@@ 10,6 10,8 @@ struct Options options_defaults(void)
	    .tile_height = TILE_HEIGHT,
	    .tileset_width = 0,
	    .tileset_height = 0,
	    .new_level_width = NEW_LEVEL_WIDTH,
	    .new_level_height = NEW_LEVEL_HEIGHT,
	    .editor_width = EDITOR_WINDOW_WIDTH,
	    .editor_height = EDITOR_WINDOW_HEIGHT,
	    .editor_target_fps = EDITOR_TARGET_FPS,


@@ 19,5 21,6 @@ struct Options options_defaults(void)
	    .picker_window_height = 0,
	    .picker_target_fps = PICKER_TARGET_FPS,
	    .picker_padding = PICKER_PADDING,
	    .level_create = 0,
	};
}