mirror of https://git.sr.ht/~kikoodx/sle
114 lines
3.1 KiB
C
114 lines
3.1 KiB
C
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
/* Copyright (C) 2021 KikooDX */
|
|
|
|
#include "conf.h"
|
|
#include "mouse.h"
|
|
#include "options.h"
|
|
#include "scale.h"
|
|
#include "shared_data.h"
|
|
#include "tile_picker/draw.h"
|
|
#include <raylib.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
static void init_mouse(struct Options options);
|
|
static void update_mouse(int *mouse_x, int *mouse_y,
|
|
struct Options options,
|
|
struct SharedData *shared_data);
|
|
|
|
int tile_picker_main(struct Options options,
|
|
struct SharedData *shared_data)
|
|
{
|
|
int mouse_x;
|
|
int mouse_y;
|
|
Texture2D tileset;
|
|
|
|
/* initialize raylib */
|
|
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();
|
|
}
|
|
}
|
|
|
|
/* unload textures */
|
|
UnloadTexture(tileset);
|
|
|
|
CloseWindow();
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
static void init_mouse(struct Options options)
|
|
{
|
|
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) /
|
|
scaling,
|
|
1.0 / (options.tile_height + options.picker_padding) /
|
|
scaling);
|
|
}
|
|
|
|
static void update_mouse(int *mouse_x, int *mouse_y,
|
|
struct Options options,
|
|
struct SharedData *shared_data)
|
|
{
|
|
const bool left_click = IsMouseButtonDown(0);
|
|
const bool right_click = IsMouseButtonDown(1);
|
|
|
|
update_mouse_position(
|
|
mouse_x, mouse_y,
|
|
options.tileset_width *
|
|
(options.tileset_width + options.picker_padding),
|
|
options.tileset_height *
|
|
(options.tileset_height + options.picker_padding));
|
|
|
|
/* set left button tile */
|
|
if (left_click)
|
|
shared_data->selected_tile =
|
|
*mouse_x + *mouse_y * options.tileset_width;
|
|
}
|