removed C++ dependencies and reverted tilemap, engine and game files to C source files
parent
61f9de5256
commit
b9d0d70e5a
|
@ -1,7 +1,7 @@
|
||||||
#include "engine.hpp"
|
#include "engine.h"
|
||||||
#include "corelib/assets.h"
|
#include "corelib/assets.h"
|
||||||
#include "corelib/render.h"
|
#include "corelib/render.h"
|
||||||
#include "SDL2/SDL_image.h"
|
#include "corelib/input.h"
|
||||||
|
|
||||||
int engine_start() {
|
int engine_start() {
|
||||||
init_context();
|
init_context();
|
||||||
|
@ -9,6 +9,7 @@ int engine_start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
int engine_shutdown() {
|
int engine_shutdown() {
|
||||||
|
game_exit();
|
||||||
clean_assets();
|
clean_assets();
|
||||||
close_context();
|
close_context();
|
||||||
exit(0);
|
exit(0);
|
||||||
|
@ -16,23 +17,27 @@ int engine_shutdown() {
|
||||||
|
|
||||||
void handle_events() {
|
void handle_events() {
|
||||||
while(SDL_PollEvent(&g_context.event)) {
|
while(SDL_PollEvent(&g_context.event)) {
|
||||||
|
input_event(g_context.event);
|
||||||
switch(g_context.event.type) {
|
switch(g_context.event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
g_context.running = 0;
|
g_context.running = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int engine_run() {
|
int engine_run() {
|
||||||
SDL_Window* window = SDL_CreateWindow("Tabletop", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 420, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
SDL_Window* window = SDL_CreateWindow("Tabletop", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 420, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
||||||
|
|
||||||
g_context = (context_t){
|
g_context = (context_t){
|
||||||
.window = window,
|
.window = window,
|
||||||
.renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED),
|
.renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED),
|
||||||
.running = 1,
|
.running = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
load_game();
|
load_game();
|
||||||
start_game();
|
start_game();
|
||||||
|
|
||||||
while(g_context.running) {
|
while(g_context.running) {
|
||||||
handle_events();
|
handle_events();
|
||||||
_render_mode = 1;
|
_render_mode = 1;
|
||||||
|
@ -41,7 +46,6 @@ int engine_run() {
|
||||||
update_game();
|
update_game();
|
||||||
swap_buffer();
|
swap_buffer();
|
||||||
}
|
}
|
||||||
engine_shutdown();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
#ifndef _engine_h
|
#ifndef _engine_h
|
||||||
#define _engine_h
|
#define _engine_h
|
||||||
|
|
||||||
#include "SDL2/SDL.h"
|
|
||||||
#include "corelib/context.h"
|
|
||||||
|
|
||||||
/* TO BE DEFINED IN GAME */
|
/* TO BE DEFINED IN GAME */
|
||||||
|
|
||||||
extern void load_game();
|
extern void load_game();
|
||||||
|
extern void game_exit();
|
||||||
extern void start_game();
|
extern void start_game();
|
||||||
extern void update_game();
|
extern void update_game();
|
||||||
extern void update_ui();
|
extern void update_ui();
|
|
@ -0,0 +1,234 @@
|
||||||
|
#include "engine.h"
|
||||||
|
#include "tilemap.h"
|
||||||
|
#include "corelib/input.h"
|
||||||
|
#include "corelib/render.h"
|
||||||
|
#include "corelib/assets.h"
|
||||||
|
#include "corelib/layers.h"
|
||||||
|
#include "SDL2/SDL_mouse.h"
|
||||||
|
#include "SDL2/SDL_scancode.h"
|
||||||
|
#include "ui.h"
|
||||||
|
|
||||||
|
int cursor_tile = 1;
|
||||||
|
int dragging = 0;
|
||||||
|
int drawing = 0;
|
||||||
|
sprite_t cursor;
|
||||||
|
spritesheet_t world_sheet;
|
||||||
|
tileset_t tileset;
|
||||||
|
tilemap_t tilemap = {
|
||||||
|
.width = 0,.height = 0,
|
||||||
|
.x=0,.y=0
|
||||||
|
};
|
||||||
|
|
||||||
|
void on_save_key(int down) {
|
||||||
|
if(down) save_tilemap(&tilemap, "tilemap.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_load_key(int down) {
|
||||||
|
if(down) load_tilemap(&tilemap, "tilemap.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_clear_key(int down) {
|
||||||
|
if(down) {
|
||||||
|
for(int i = 0; i < tilemap.height * tilemap.width; ++i) {
|
||||||
|
tilemap.tiles[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_cycle_tile(int axis) {
|
||||||
|
cursor_tile += axis;
|
||||||
|
if(cursor_tile <= 0) {
|
||||||
|
cursor_tile = 1;
|
||||||
|
}
|
||||||
|
int size = tileset.size;
|
||||||
|
if(cursor_tile >= size) {
|
||||||
|
cursor_tile = size-1;
|
||||||
|
}
|
||||||
|
if(tileset.size >= cursor_tile && 0 < cursor_tile) {
|
||||||
|
cursor = tileset.set[cursor_tile-1].sprite;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_quit_key(int down) {
|
||||||
|
if(down) {
|
||||||
|
g_context.running = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_click(int down) {
|
||||||
|
drawing = down;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_erase(int down) {
|
||||||
|
drawing = -down;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_drag_world(float dx, float dy) {
|
||||||
|
if(dragging) {
|
||||||
|
g_active_view.x -= dx * g_active_view.width;
|
||||||
|
g_active_view.y -= dy * g_active_view.width;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_middle_mouse(int down) {
|
||||||
|
dragging = down;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_debug_frame(int down) {
|
||||||
|
if(down) {
|
||||||
|
d_debug_next_frame = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void on_button_zoom(int axis) {
|
||||||
|
if(axis != 0) {
|
||||||
|
g_active_view.width += axis;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_scroll_zoom(float delta) {
|
||||||
|
g_active_view.width -= delta * 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void load_game() {
|
||||||
|
style.button.button = style.button.active = make_nineslice("button.png", 126, 0.01);
|
||||||
|
world_sheet = make_spritesheet("tileset.png", 189, 189);
|
||||||
|
|
||||||
|
tileset.set = (tile_t*)calloc(21, sizeof(tile_t));
|
||||||
|
tileset.size = 21;
|
||||||
|
tileset.set[0] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 0),.walkable=0};
|
||||||
|
tileset.set[1] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 1),.walkable=0};
|
||||||
|
tileset.set[2] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 2),.walkable=0};
|
||||||
|
|
||||||
|
tileset.set[3] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 10),.walkable=0};
|
||||||
|
tileset.set[4] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 11),.walkable=0};
|
||||||
|
tileset.set[5] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 12),.walkable=0};
|
||||||
|
|
||||||
|
tileset.set[6] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 20),.walkable=0};
|
||||||
|
tileset.set[7] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 21),.walkable=0};
|
||||||
|
tileset.set[8] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 22),.walkable=0};
|
||||||
|
|
||||||
|
tileset.set[9] = (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 30),.walkable=1};
|
||||||
|
tileset.set[10]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 31),.walkable=1};
|
||||||
|
tileset.set[11]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 32),.walkable=1};
|
||||||
|
|
||||||
|
tileset.set[12]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 3),.walkable=1};
|
||||||
|
tileset.set[13]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 4),.walkable=1};
|
||||||
|
tileset.set[14]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 5),.walkable=1};
|
||||||
|
|
||||||
|
tileset.set[15]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 13),.walkable=1};
|
||||||
|
tileset.set[16]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 14),.walkable=1};
|
||||||
|
tileset.set[17]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 15),.walkable=1};
|
||||||
|
|
||||||
|
tileset.set[18]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 23),.walkable=1};
|
||||||
|
tileset.set[19]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 24),.walkable=1};
|
||||||
|
tileset.set[20]= (tile_t){.sprite=sprite_from_spritesheet(&world_sheet, 25),.walkable=1};
|
||||||
|
|
||||||
|
// register calloc'd array to be deleted with free(...)
|
||||||
|
add_arbitrary_asset(tileset.set, &free);
|
||||||
|
|
||||||
|
tilemap.width = tilemap.height = 128;
|
||||||
|
on_cycle_tile(0);
|
||||||
|
|
||||||
|
input_init();
|
||||||
|
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_W, &on_save_key);
|
||||||
|
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_R, &on_load_key);
|
||||||
|
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_C, &on_clear_key);
|
||||||
|
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_Q, &on_quit_key);
|
||||||
|
add_key_listener(SDL_SCANCODE_A, SDL_SCANCODE_D, &on_cycle_tile);
|
||||||
|
add_key_listener(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_TAB, &on_debug_frame);
|
||||||
|
add_key_listener(SDL_SCANCODE_MINUS, SDL_SCANCODE_EQUALS, &on_button_zoom);
|
||||||
|
add_mouse_button_listener(SDL_BUTTON_LMASK, &on_click);
|
||||||
|
add_mouse_button_listener(SDL_BUTTON_RMASK, &on_erase);
|
||||||
|
add_mouse_button_listener(SDL_BUTTON_MMASK, &on_middle_mouse);
|
||||||
|
add_mouse_listener(&on_drag_world);
|
||||||
|
add_scroll_listener(&on_scroll_zoom);
|
||||||
|
}
|
||||||
|
|
||||||
|
void start_game() {
|
||||||
|
g_active_view.width = 21;
|
||||||
|
on_clear_key(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_game() {
|
||||||
|
if(drawing == 1) {
|
||||||
|
drawing = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
float fx, fy;
|
||||||
|
mouse_world_position(&fx, &fy);
|
||||||
|
cursor.x = floor(fx); cursor.y = floor(fy);
|
||||||
|
if(drawing) {
|
||||||
|
int x, y;
|
||||||
|
x = floorf(fx);
|
||||||
|
y = floorf(fy);
|
||||||
|
if(x >= 0 && y >= 0 && x < tilemap.width && y < tilemap.height) {
|
||||||
|
if(drawing > 0) {
|
||||||
|
set_tile(&tilemap, x, y, cursor_tile);
|
||||||
|
} else {
|
||||||
|
set_tile(&tilemap, x, y, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rectshape_t world_outline = {
|
||||||
|
tilemap.x, tilemap.y, (float)tilemap.width, (float)tilemap.height,
|
||||||
|
0.01f, RLAYER_TILEMAP-1, {0,0,0,0}, {255, 255, 255, 255}
|
||||||
|
};
|
||||||
|
rectshape_t cursor_outline = {
|
||||||
|
cursor.x, cursor.y, 1.0f, 1.0f, 0.01f, RLAYER_SPRITES-1,
|
||||||
|
{0, 0, 0, 0}, {255, 255, 255, 255}
|
||||||
|
};
|
||||||
|
|
||||||
|
draw_rect(&world_outline);
|
||||||
|
draw_rect(&cursor_outline);
|
||||||
|
draw_sprite(&cursor);
|
||||||
|
draw_tilemap(&tilemap, &tileset);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_ui() {
|
||||||
|
update_input();
|
||||||
|
|
||||||
|
nineslice_t sliced = style.button.active;
|
||||||
|
sliced.rect = (SDL_FRect){0, 0, 0.1, 0.1};
|
||||||
|
sliced.depth = RLAYER_UI - 20;
|
||||||
|
draw_sliced(&sliced);
|
||||||
|
|
||||||
|
const int offset_selected = cursor_tile - 1;
|
||||||
|
|
||||||
|
rectshape_t selected_tile_rect = (rectshape_t) {
|
||||||
|
offset_selected * 0.015f, 0.f, 0.015f, 0.015f, 0.001f, RLAYER_UI-10,
|
||||||
|
{0,0,0,0}, {255, 255, 255, 255}
|
||||||
|
};
|
||||||
|
draw_rect(&selected_tile_rect);
|
||||||
|
|
||||||
|
rectshape_t shape = (rectshape_t) {
|
||||||
|
0.0f, 0.0f, 1.f, 0.015f, 0, RLAYER_UI,
|
||||||
|
{100,100,100,255}, {0,0,0,0}
|
||||||
|
};
|
||||||
|
draw_rect(&shape);
|
||||||
|
|
||||||
|
float mx, my;
|
||||||
|
mouse_screen_position(&mx, &my);
|
||||||
|
for(int i = 0; i < tileset.size; ++i) {
|
||||||
|
// make a copy of the reference sprite and adjust it to fit the ui
|
||||||
|
sprite_t sprite = tileset.set[i].sprite;
|
||||||
|
sprite.depth = RLAYER_UI-5;
|
||||||
|
sprite.x = i * 0.015f; sprite.y = 0;
|
||||||
|
sprite.sx = 0.015f; sprite.sy = 0.015f;
|
||||||
|
float amx=mx-sprite.x, amy=my-sprite.y;
|
||||||
|
draw_sprite(&sprite);
|
||||||
|
|
||||||
|
// allow user to select tiles with mouse
|
||||||
|
if(amx >= 0 && amy >= 0 && amx < sprite.sx && amy < sprite.sy) {
|
||||||
|
if(drawing == 1) {
|
||||||
|
drawing = 0;
|
||||||
|
cursor_tile = i+1;
|
||||||
|
on_cycle_tile(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void game_exit() {}
|
214
src/game.cc
214
src/game.cc
|
@ -1,214 +0,0 @@
|
||||||
#include "engine.hpp"
|
|
||||||
#include "tilemap.hpp"
|
|
||||||
#include "corelib/input.h"
|
|
||||||
#include "corelib/render.h"
|
|
||||||
#include "corelib/assets.h"
|
|
||||||
#include "corelib/layers.h"
|
|
||||||
#include "SDL2/SDL_mouse.h"
|
|
||||||
#include <SDL2/SDL_scancode.h>
|
|
||||||
|
|
||||||
int cursor_tile = 1;
|
|
||||||
int dragging = 0;
|
|
||||||
int drawing = 0;
|
|
||||||
sprite_t cursor;
|
|
||||||
sprite_t current_tile_display;
|
|
||||||
spritesheet_t world_sheet;
|
|
||||||
|
|
||||||
void on_save_key(int down) {
|
|
||||||
if(down)
|
|
||||||
save_tilemap("tilemap.csv");
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_load_key(int down) {
|
|
||||||
if(down)
|
|
||||||
load_tilemap("tilemap.csv");
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_clear_key(int down) {
|
|
||||||
if(down) {
|
|
||||||
for(int i = 0; i < g_tilemap.height * g_tilemap.width; ++i) {
|
|
||||||
g_tilemap.tiles[i].value = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_cycle_tile(int axis) {
|
|
||||||
cursor_tile += axis;
|
|
||||||
if(cursor_tile <= 0) {
|
|
||||||
cursor_tile = 1;
|
|
||||||
}
|
|
||||||
if(cursor_tile > g_tilemap.tileset.set.size()) {
|
|
||||||
cursor_tile = g_tilemap.tileset.set.size();
|
|
||||||
}
|
|
||||||
if(g_tilemap.tileset.set.size() >= cursor_tile && 0 < cursor_tile) {
|
|
||||||
cursor = g_tilemap.tileset.set.at(cursor_tile-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_quit_key(int down) {
|
|
||||||
if(down) {
|
|
||||||
g_context.running = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_click(int down) {
|
|
||||||
drawing = down;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_erase(int down) {
|
|
||||||
drawing = -down;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_drag_world(float dx, float dy) {
|
|
||||||
if(dragging) {
|
|
||||||
g_active_view.x -= dx * g_active_view.width;
|
|
||||||
g_active_view.y -= dy * g_active_view.width;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_middle_mouse(int down) {
|
|
||||||
dragging = down;
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_debug_frame(int down) {
|
|
||||||
if(down) {
|
|
||||||
d_debug_next_frame = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void load_game() {
|
|
||||||
g_tilemap.width = g_tilemap.height = 128;
|
|
||||||
|
|
||||||
cursor = make_sprite("tilemap.png", 0, 0);
|
|
||||||
|
|
||||||
current_tile_display = make_sprite("tilemap.png", 0, 0);
|
|
||||||
current_tile_display.depth = 0;
|
|
||||||
current_tile_display.sx = 0.15f;
|
|
||||||
current_tile_display.sy = 0.15f;
|
|
||||||
|
|
||||||
world_sheet = make_spritesheet("tilemap.png", 189, 189);
|
|
||||||
|
|
||||||
g_tilemap.tileset.set = {
|
|
||||||
sprite_from_spritesheet(&world_sheet, 0),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 1),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 2),
|
|
||||||
|
|
||||||
sprite_from_spritesheet(&world_sheet, 10),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 11),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 12),
|
|
||||||
|
|
||||||
sprite_from_spritesheet(&world_sheet, 20),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 21),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 22),
|
|
||||||
|
|
||||||
sprite_from_spritesheet(&world_sheet, 30),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 31),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 32),
|
|
||||||
|
|
||||||
sprite_from_spritesheet(&world_sheet, 3),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 4),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 5),
|
|
||||||
|
|
||||||
sprite_from_spritesheet(&world_sheet, 13),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 14),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 15),
|
|
||||||
|
|
||||||
sprite_from_spritesheet(&world_sheet, 23),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 24),
|
|
||||||
sprite_from_spritesheet(&world_sheet, 25),
|
|
||||||
};
|
|
||||||
|
|
||||||
on_cycle_tile(0);
|
|
||||||
|
|
||||||
input_init();
|
|
||||||
add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_W, &on_save_key);
|
|
||||||
add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_R, &on_load_key);
|
|
||||||
add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_C, &on_clear_key);
|
|
||||||
add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_Q, &on_quit_key);
|
|
||||||
add_listener_for(SDL_SCANCODE_A, SDL_SCANCODE_D, &on_cycle_tile);
|
|
||||||
add_listener_for(SDL_SCANCODE_UNKNOWN, SDL_SCANCODE_TAB, &on_debug_frame);
|
|
||||||
add_mouse_button_listener(SDL_BUTTON_LMASK, &on_click);
|
|
||||||
add_mouse_button_listener(SDL_BUTTON_RMASK, &on_erase);
|
|
||||||
add_mouse_button_listener(SDL_BUTTON_MMASK, &on_middle_mouse);
|
|
||||||
add_mouse_listener(0, &on_drag_world);
|
|
||||||
}
|
|
||||||
|
|
||||||
void start_game() {
|
|
||||||
g_active_view.width = 21;
|
|
||||||
on_clear_key(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_game() {
|
|
||||||
if(drawing == 1) drawing = 2;
|
|
||||||
mouse_world_position(&cursor.x, &cursor.y);
|
|
||||||
cursor.x += 0.1f; cursor.y += 0.1f;
|
|
||||||
|
|
||||||
if(drawing) {
|
|
||||||
int x, y;
|
|
||||||
float fx, fy;
|
|
||||||
mouse_world_position(&fx, &fy);
|
|
||||||
x = floorf(fx);
|
|
||||||
y = floorf(fy);
|
|
||||||
if(x >= 0 && y >= 0 && x < g_tilemap.width && y < g_tilemap.height) {
|
|
||||||
if(drawing > 0) {
|
|
||||||
get_tilemap_tile(x, y)->value = cursor_tile;
|
|
||||||
} else {
|
|
||||||
get_tilemap_tile(x, y)->value = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rectshape_t world_outline = {
|
|
||||||
g_tilemap.x, g_tilemap.y, float(g_tilemap.width), float(g_tilemap.height),
|
|
||||||
0.01f, RLAYER_TILEMAP-1, {0,0,0,0}, {255, 255, 255, 255}
|
|
||||||
};
|
|
||||||
rectshape_t cursor_outline = {
|
|
||||||
cursor.x, cursor.y, 1.0f, 1.0f, 0.01f, RLAYER_SPRITES-1,
|
|
||||||
{0, 0, 0, 0}, {255, 255, 255, 255}
|
|
||||||
};
|
|
||||||
|
|
||||||
draw_rect(&world_outline);
|
|
||||||
draw_rect(&cursor_outline);
|
|
||||||
draw_sprite(&cursor);
|
|
||||||
draw_tilemap();
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_ui() {
|
|
||||||
update_input();
|
|
||||||
|
|
||||||
const int offset_selected = cursor_tile - 1;
|
|
||||||
|
|
||||||
rectshape_t selected_tile_rect = (rectshape_t) {
|
|
||||||
offset_selected * 0.015f, 0.f, 0.015f, 0.015f, 0.001f, RLAYER_UI-10,
|
|
||||||
{0,0,0,0}, {255, 255, 255, 255}
|
|
||||||
};
|
|
||||||
draw_rect(&selected_tile_rect);
|
|
||||||
|
|
||||||
rectshape_t shape = (rectshape_t) {
|
|
||||||
0.0f, 0.0f, 1.f, 0.015f, 0, RLAYER_UI,
|
|
||||||
{100,100,100,255}, {0,0,0,0}
|
|
||||||
};
|
|
||||||
draw_rect(&shape);
|
|
||||||
|
|
||||||
float mx, my;
|
|
||||||
mouse_screen_position(&mx, &my);
|
|
||||||
for(int i=0;i<g_tilemap.tileset.set.size();++i) {
|
|
||||||
// make a copy of the reference sprite and adjust it to fit the ui
|
|
||||||
sprite_t sprite = g_tilemap.tileset.set.at(i);
|
|
||||||
sprite.depth = RLAYER_UI-5;
|
|
||||||
sprite.x = i * 0.015f; sprite.y = 0;
|
|
||||||
sprite.sx = 0.015f; sprite.sy = 0.015f;
|
|
||||||
float amx=mx-sprite.x, amy=my-sprite.y;
|
|
||||||
draw_sprite(&sprite);
|
|
||||||
|
|
||||||
// allow user to select tiles with mouse
|
|
||||||
if(amx >= 0 && amy >= 0 && amx < sprite.sx && amy < sprite.sy) {
|
|
||||||
if(drawing == 1) {
|
|
||||||
drawing = 0;
|
|
||||||
cursor_tile = i+1;
|
|
||||||
on_cycle_tile(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +1,18 @@
|
||||||
#include "tilemap.hpp"
|
#include "tilemap.h"
|
||||||
#include "corelib/layers.h"
|
#include "corelib/layers.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "signal.h"
|
#include "signal.h"
|
||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
|
|
||||||
tilemap_t g_tilemap = {
|
void draw_tilemap(const tilemap_t* tilemap, const tileset_t* tileset) {
|
||||||
.width = 0,.height = 0,
|
|
||||||
.x=0,.y=0
|
|
||||||
};
|
|
||||||
|
|
||||||
void draw_tilemap() {
|
|
||||||
sprite_t sprite;
|
sprite_t sprite;
|
||||||
for(int y=0;y<g_tilemap.height;++y) {
|
for(int y=0;y<tilemap->height;++y) {
|
||||||
for(int x=0;x<g_tilemap.width;++x) {
|
for(int x=0;x<tilemap->width;++x) {
|
||||||
tile_t* tile = get_tilemap_tile(x, y);
|
size_t index = get_tile(tilemap, x, y);
|
||||||
if(tile->value <= 0) continue;
|
if(index == 0) continue;
|
||||||
sprite = g_tilemap.tileset.set[tile->value-1];
|
const tile_t tile = tileset->set[index-1];
|
||||||
|
sprite = tile.sprite;
|
||||||
sprite.depth = RLAYER_TILEMAP;
|
sprite.depth = RLAYER_TILEMAP;
|
||||||
sprite.x = x; sprite.y = y;
|
sprite.x = x; sprite.y = y;
|
||||||
draw_sprite(&sprite);
|
draw_sprite(&sprite);
|
||||||
|
@ -24,20 +20,20 @@ void draw_tilemap() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int save_tilemap(const char* file) {
|
int save_tilemap(const tilemap_t* map, const char* file) {
|
||||||
FILE* fs = fopen(file, "w");
|
FILE* fs = fopen(file, "w");
|
||||||
if(fs == NULL) {
|
if(fs == NULL) {
|
||||||
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
|
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
fprintf(fs, "%d,%d,", g_tilemap.width, g_tilemap.height);
|
fprintf(fs, "%d,%d,", map->width, map->height);
|
||||||
for(int i = 0; i < g_tilemap.width * g_tilemap.height; ++i)
|
for(int i = 0; i < map->width * map->height; ++i)
|
||||||
fprintf(fs,"%d,", g_tilemap.tiles[i].value);
|
fprintf(fs,"%zu,", map->tiles[i]);
|
||||||
fclose(fs);
|
fclose(fs);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_tilemap(const char* file) {
|
int load_tilemap(tilemap_t* map, const char* file) {
|
||||||
FILE* fs = fopen(file, "r");
|
FILE* fs = fopen(file, "r");
|
||||||
if(fs == NULL) {
|
if(fs == NULL) {
|
||||||
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
|
fprintf(stderr, "ERROR: Failed to open file %s\n", file);
|
||||||
|
@ -46,7 +42,7 @@ int load_tilemap(const char* file) {
|
||||||
char buf[99];
|
char buf[99];
|
||||||
char* writer = buf;
|
char* writer = buf;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
tile_t* tile = g_tilemap.tiles;
|
size_t* tile = map->tiles;
|
||||||
int entries=0;
|
int entries=0;
|
||||||
while(1) {
|
while(1) {
|
||||||
n = fgetc(fs);
|
n = fgetc(fs);
|
||||||
|
@ -60,12 +56,12 @@ int load_tilemap(const char* file) {
|
||||||
case ',':
|
case ',':
|
||||||
*writer = '\0';
|
*writer = '\0';
|
||||||
if(entries == 0) {
|
if(entries == 0) {
|
||||||
g_tilemap.width = atoi(buf);
|
map->width = atoi(buf);
|
||||||
} else if(entries == 1) {
|
} else if(entries == 1) {
|
||||||
g_tilemap.height = atoi(buf);
|
map->height = atoi(buf);
|
||||||
} else {
|
} else {
|
||||||
if(entries-2 >= TILEMAP_MAX_SIZE) goto end_while;
|
if(entries-2 >= TILEMAP_MAX_SIZE) goto end_while;
|
||||||
tile->value = (short)atoi(buf);
|
*tile = (size_t)atoi(buf);
|
||||||
++tile;
|
++tile;
|
||||||
}
|
}
|
||||||
writer = buf;
|
writer = buf;
|
|
@ -0,0 +1,41 @@
|
||||||
|
#ifndef _tilemap_h
|
||||||
|
#define _tilemap_h
|
||||||
|
|
||||||
|
#include "corelib/render.h"
|
||||||
|
|
||||||
|
#define TILEMAP_MAX_SIZE 16384
|
||||||
|
#define TILESET_MAX_SIZE 255
|
||||||
|
|
||||||
|
typedef struct tile_t {
|
||||||
|
sprite_t sprite;
|
||||||
|
short walkable;
|
||||||
|
} tile_t;
|
||||||
|
|
||||||
|
typedef struct tileset_t {
|
||||||
|
tile_t* set;
|
||||||
|
size_t size;
|
||||||
|
} tileset_t;
|
||||||
|
|
||||||
|
typedef struct tilemap_t {
|
||||||
|
size_t tiles[TILEMAP_MAX_SIZE];
|
||||||
|
int width, height;
|
||||||
|
float x, y;
|
||||||
|
} tilemap_t;
|
||||||
|
|
||||||
|
static inline int get_tilemap_index(const tilemap_t* tilemap, int x, int y) {
|
||||||
|
return x + y * tilemap->width;
|
||||||
|
}
|
||||||
|
static inline size_t get_tile(const tilemap_t* tilemap, int x, int y) {
|
||||||
|
return *(tilemap->tiles + get_tilemap_index(tilemap, x, y));
|
||||||
|
}
|
||||||
|
static inline void set_tile(tilemap_t* tilemap, int x, int y, size_t value) {
|
||||||
|
*(tilemap->tiles + get_tilemap_index(tilemap, x, y)) = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern void draw_tilemap(const tilemap_t* tilemap, const tileset_t* tileset);
|
||||||
|
/*load csv tilemap from file*/
|
||||||
|
extern int load_tilemap(tilemap_t* tilemap, const char* file);
|
||||||
|
/*write csv tilemap to file*/
|
||||||
|
extern int save_tilemap(const tilemap_t* tilemap, const char* file);
|
||||||
|
|
||||||
|
#endif /* _tilemap_h */
|
|
@ -1,39 +0,0 @@
|
||||||
#ifndef _tilemap_h
|
|
||||||
#define _tilemap_h
|
|
||||||
|
|
||||||
#include "vector"
|
|
||||||
#include "corelib/render.h"
|
|
||||||
|
|
||||||
#define TILEMAP_MAX_SIZE 16384
|
|
||||||
|
|
||||||
typedef struct tile_t {
|
|
||||||
short value;
|
|
||||||
} tile_t;
|
|
||||||
|
|
||||||
typedef struct tileset_t {
|
|
||||||
std::vector<sprite_t> set;
|
|
||||||
} tileset_t;
|
|
||||||
|
|
||||||
typedef struct tilemap_t {
|
|
||||||
tile_t tiles[TILEMAP_MAX_SIZE];
|
|
||||||
tileset_t tileset;
|
|
||||||
int width, height;
|
|
||||||
float x, y;
|
|
||||||
} tilemap_t;
|
|
||||||
|
|
||||||
extern tilemap_t g_tilemap;
|
|
||||||
|
|
||||||
static inline int get_tilemap_index(int x, int y) {
|
|
||||||
return x + y * g_tilemap.width;
|
|
||||||
}
|
|
||||||
static inline tile_t* get_tilemap_tile(int x, int y) {
|
|
||||||
return g_tilemap.tiles + get_tilemap_index(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern void draw_tilemap();
|
|
||||||
/*load csv tilemap from file*/
|
|
||||||
extern int load_tilemap(const char* file);
|
|
||||||
/*write csv tilemap to file*/
|
|
||||||
extern int save_tilemap(const char* file);
|
|
||||||
|
|
||||||
#endif /* _tilemap_h */
|
|
Loading…
Reference in New Issue