first commit (map tmx -> csv + player)

This commit is contained in:
bgiraudr 2021-07-29 18:33:22 +02:00
commit 61aed97416
20 changed files with 281 additions and 0 deletions

13
.gitignore vendored Normal file
View File

@ -0,0 +1,13 @@
# Build files
/build-fx
/build-cg
/*.g1a
/*.g3a
# Python bytecode
__pycache__/
# Common IDE files
*.sublime-project
*.sublime-workspace
.vscode

42
CMakeLists.txt Normal file
View File

@ -0,0 +1,42 @@
# Configure with [fxsdk build-fx] or [fxsdk build-cg], which provide the
# toolchain file and module path of the fxSDK
cmake_minimum_required(VERSION 3.15)
project(MyAddin)
include(GenerateG1A)
include(GenerateG3A)
include(Fxconv)
include_directories(include)
find_package(Gint 2.1 REQUIRED)
fxconv_declare_converters(assets-cg/converters.py)
add_custom_command(
OUTPUT "${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/testCarte.csv"
COMMENT "Convert tmx map to csv"
COMMAND tiled --export-map csv testCarte.tmx testCarte.csv
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/assets-cg/maps/
DEPENDS assets-cg/maps/testCarte.tmx assets-cg/tileset.png assets-cg/converters.py)
set(SOURCES
src/main.c
src/game.c
src/engine.c
)
set(ASSETS_cg
assets-cg/tileset.png
assets-cg/maps/testCarte.csv
assets-cg/spritesheet.png
)
fxconv_declare_assets(${ASSETS} ${ASSETS_fx} ${ASSETS_cg} WITH_METADATA)
add_executable(myaddin ${SOURCES} ${ASSETS} ${ASSETS_${FXSDK_PLATFORM}})
target_compile_options(myaddin PRIVATE -Wall -Wextra -Os)
target_link_libraries(myaddin Gint::Gint)
if("${FXSDK_PLATFORM_LONG}" STREQUAL fxCG50)
generate_g3a(TARGET myaddin OUTPUT "MyAddin.g3a"
NAME "MyAddin" ICONS assets-cg/icon-uns.png assets-cg/icon-sel.png)
endif()

Binary file not shown.

31
assets-cg/converters.py Normal file
View File

@ -0,0 +1,31 @@
import fxconv
import csv
def convert(input, output, params, target):
if params["custom-type"] == "map":
convert_map(input, output, params, target)
return 0
else:
return 1
def convert_map(input, output, params, target):
with open(input, "r") as csvData:
tiles = []
csvReader = csv.reader(csvData)
for row in csvReader:
tiles.append(row)
w = max(len(i) for i in tiles)
h = len(tiles)
encoded_tiles = bytearray()
for(y, i) in enumerate(tiles):
for(x, j) in enumerate(i):
encoded_tiles += bytearray(fxconv.u16((int)(j)+1))
o = fxconv.ObjectData()
o += fxconv.u32(w) + fxconv.u32(h)
o += fxconv.ref("img_tileset")
o += fxconv.ref(encoded_tiles)
fxconv.elf(o, output, "_" + params["name"], **target)

View File

@ -0,0 +1,8 @@
tileset.png:
type: bopti-image
name: img_tileset
profile:p4
spritesheet.png:
type: bopti-image
name: img_spritesheet

BIN
assets-cg/icon-sel.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

BIN
assets-cg/icon-uns.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -0,0 +1,3 @@
testCarte.csv:
custom-type: map
name: map_1

View File

@ -0,0 +1,16 @@
575,576,577,578,579,-1,-1,-1,-1,397,398,399,79,80,81,343,344,345,346,347,79,80,81,-1,-1,-1
604,605,606,607,608,-1,58,59,60,426,427,428,108,109,110,372,373,374,375,376,108,109,110,-1,-1,-1
633,634,635,636,637,-1,87,88,89,455,456,457,137,138,139,401,402,403,404,405,137,138,139,76,77,77
662,663,664,665,666,-1,116,117,118,484,485,486,-1,-1,-1,430,431,432,433,434,-1,-1,-1,105,106,106
691,692,693,694,695,210,211,212,213,214,215,216,217,-1,-1,459,460,461,462,463,-1,-1,-1,105,106,106
720,721,722,723,724,239,240,241,242,243,244,245,246,162,163,488,489,490,491,492,273,274,275,105,106,106
749,750,251,752,753,592,593,594,595,-1,-1,-1,-1,191,192,517,518,519,520,521,302,303,304,105,106,106
-1,18,251,592,593,594,595,-1,-1,592,593,594,595,220,221,546,251,548,549,550,331,332,333,105,106,106
-1,15,251,14,20,18,13,17,18,18,13,14,18,17,12,20,251,18,676,-1,360,361,362,134,135,135
-1,18,251,251,251,251,251,251,251,251,251,251,251,251,251,251,251,15,705,-1,575,576,577,578,579,-1
-1,13,17,18,13,15,18,19,20,16,20,14,18,18,18,16,18,18,-1,-1,604,605,606,607,608,-1
508,509,510,676,-1,-1,-1,-1,-1,-1,-1,-1,148,149,150,67,68,69,-1,-1,633,634,635,636,637,-1
537,538,539,705,79,80,81,-1,-1,79,80,81,177,178,179,96,97,97,69,-1,662,663,664,665,666,-1
-1,-1,-1,-1,108,109,109,80,80,109,109,110,206,207,208,125,126,126,127,-1,691,692,693,694,695,-1
-1,-1,-1,-1,108,109,109,138,138,109,109,110,235,236,237,-1,-1,-1,-1,-1,720,721,722,723,724,-1
-1,-1,-1,-1,108,109,110,-1,-1,108,109,110,-1,-1,-1,-1,-1,-1,-1,-1,749,750,751,752,753,-1
1 575 576 577 578 579 -1 -1 -1 -1 397 398 399 79 80 81 343 344 345 346 347 79 80 81 -1 -1 -1
2 604 605 606 607 608 -1 58 59 60 426 427 428 108 109 110 372 373 374 375 376 108 109 110 -1 -1 -1
3 633 634 635 636 637 -1 87 88 89 455 456 457 137 138 139 401 402 403 404 405 137 138 139 76 77 77
4 662 663 664 665 666 -1 116 117 118 484 485 486 -1 -1 -1 430 431 432 433 434 -1 -1 -1 105 106 106
5 691 692 693 694 695 210 211 212 213 214 215 216 217 -1 -1 459 460 461 462 463 -1 -1 -1 105 106 106
6 720 721 722 723 724 239 240 241 242 243 244 245 246 162 163 488 489 490 491 492 273 274 275 105 106 106
7 749 750 251 752 753 592 593 594 595 -1 -1 -1 -1 191 192 517 518 519 520 521 302 303 304 105 106 106
8 -1 18 251 592 593 594 595 -1 -1 592 593 594 595 220 221 546 251 548 549 550 331 332 333 105 106 106
9 -1 15 251 14 20 18 13 17 18 18 13 14 18 17 12 20 251 18 676 -1 360 361 362 134 135 135
10 -1 18 251 251 251 251 251 251 251 251 251 251 251 251 251 251 251 15 705 -1 575 576 577 578 579 -1
11 -1 13 17 18 13 15 18 19 20 16 20 14 18 18 18 16 18 18 -1 -1 604 605 606 607 608 -1
12 508 509 510 676 -1 -1 -1 -1 -1 -1 -1 -1 148 149 150 67 68 69 -1 -1 633 634 635 636 637 -1
13 537 538 539 705 79 80 81 -1 -1 79 80 81 177 178 179 96 97 97 69 -1 662 663 664 665 666 -1
14 -1 -1 -1 -1 108 109 109 80 80 109 109 110 206 207 208 125 126 126 127 -1 691 692 693 694 695 -1
15 -1 -1 -1 -1 108 109 109 138 138 109 109 110 235 236 237 -1 -1 -1 -1 -1 720 721 722 723 724 -1
16 -1 -1 -1 -1 108 109 110 -1 -1 108 109 110 -1 -1 -1 -1 -1 -1 -1 -1 749 750 751 752 753 -1

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.7.1" orientation="orthogonal" renderorder="right-down" width="26" height="16" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="1">
<editorsettings>
<export target="testCarte.csv" format="csv"/>
</editorsettings>
<tileset firstgid="1" source="tileset.tsx"/>
<layer id="1" name="1" width="26" height="16">
<data encoding="csv">
576,577,578,579,580,0,0,0,0,398,399,400,80,81,82,344,345,346,347,348,80,81,82,0,0,0,
605,606,607,608,609,0,59,60,61,427,428,429,109,110,111,373,374,375,376,377,109,110,111,0,0,0,
634,635,636,637,638,0,88,89,90,456,457,458,138,139,140,402,403,404,405,406,138,139,140,77,78,78,
663,664,665,666,667,0,117,118,119,485,486,487,0,0,0,431,432,433,434,435,0,0,0,106,107,107,
692,693,694,695,696,211,212,213,214,215,216,217,218,0,0,460,461,462,463,464,0,0,0,106,107,107,
721,722,723,724,725,240,241,242,243,244,245,246,247,163,164,489,490,491,492,493,274,275,276,106,107,107,
750,751,252,753,754,593,594,595,596,0,0,0,0,192,193,518,519,520,521,522,303,304,305,106,107,107,
0,19,252,593,594,595,596,0,0,593,594,595,596,221,222,547,252,549,550,551,332,333,334,106,107,107,
0,16,252,15,21,19,14,18,19,19,14,15,19,18,13,21,252,19,677,0,361,362,363,135,136,136,
0,19,252,252,252,252,252,252,252,252,252,252,252,252,252,252,252,16,706,0,576,577,578,579,580,0,
0,14,18,19,14,16,19,20,21,17,21,15,19,19,19,17,19,19,0,0,605,606,607,608,609,0,
509,510,511,677,0,0,0,0,0,0,0,0,149,150,151,68,69,70,0,0,634,635,636,637,638,0,
538,539,540,706,80,81,82,0,0,80,81,82,178,179,180,97,98,98,70,0,663,664,665,666,667,0,
0,0,0,0,109,110,110,81,81,110,110,111,207,208,209,126,127,127,128,0,692,693,694,695,696,0,
0,0,0,0,109,110,110,139,139,110,110,111,236,237,238,0,0,0,0,0,721,722,723,724,725,0,
0,0,0,0,109,110,111,0,0,109,110,111,0,0,0,0,0,0,0,0,750,751,752,753,754,0
</data>
</layer>
</map>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.5" tiledversion="1.7.1" name="tileset" tilewidth="16" tileheight="16" tilecount="754" columns="29">
<image source="../tileset.png" width="464" height="416"/>
<tile id="18" probability="5"/>
</tileset>

BIN
assets-cg/spritesheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
assets-cg/tileset.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

10
include/engine.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
struct game {
struct map *map;
struct player *player;
};
void engine_draw(struct game const *game);
void engine_draw_player(struct player const *player);
void engine_move(struct player *player, int direction);

4
include/game.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void play();
int get_inputs(void);

13
include/map.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
struct map {
int w, h;
bopti_image_t *tileset;
short *tiles;
};
enum map_state {
TILE_AIR = 0,
TILE_SOLID = 1,
TILE_WALKABLE = 2
};

15
include/player.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
struct player {
int x, y;
int direction;
int frame;
//struct animation const *anim;
};
enum direction {
DIR_DOWN = 0,
DIR_LEFT = 1,
DIR_UP = 2,
DIR_RIGHT = 3
};

39
src/engine.c Normal file
View File

@ -0,0 +1,39 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include "engine.h"
#include "game.h"
#include "map.h"
#include "player.h"
#define TILESET_WIDTH 29
void engine_draw(struct game const *game) {
dclear(C_WHITE);
for(int y = 0; y < game->map->h; y++) {
for(int x = 0; x < game->map->w; x++) {
int tile = game->map->tiles[y * game->map->w + x] - 1;
if(tile != -1) {
int tile_x = tile % TILESET_WIDTH;
int tile_y = tile / TILESET_WIDTH;
//dprint(x * 55, y * 16, C_BLACK,"%d(%d:%d)",tile, tile_x, tile_y);
dsubimage(x * 16, y * 16, game->map->tileset, tile_x * 16, tile_y * 16, 16, 16, DIMAGE_NONE);
}
}
}
engine_draw_player(game->player);
}
void engine_draw_player(struct player const *player) {
extern bopti_image_t img_spritesheet;
dsubimage(12 * 16, 7 * 16 - 5, &img_spritesheet, player->direction * 16, 0, 16, 21, DIMAGE_NONE);
}
void engine_move(struct player *player, int direction) {
int dx = (direction == DIR_RIGHT) - (direction == DIR_LEFT);
int dy = (direction == DIR_DOWN) - (direction == DIR_UP);
player->direction = direction;
player->x += dx;
player->y += dy;
}

46
src/game.c Normal file
View File

@ -0,0 +1,46 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include "game.h"
#include "map.h"
#include "engine.h"
#include "player.h"
extern struct map map_1;
struct map *maps[] = {
&map_1,
};
void play() {
struct player player = {
.x = 2,
.y = 3,
.direction = 0
};
struct game game = {
.map = maps[0],
.player = &player
};
while(!keydown(KEY_7)) {
engine_draw(&game);
dupdate();
int dir = get_inputs();
engine_move(&player, dir);
}
}
int get_inputs(void)
{
while(1)
{
int key = getkey().key;
if(key == KEY_DOWN) return DIR_DOWN;
if(key == KEY_RIGHT) return DIR_RIGHT;
if(key == KEY_UP) return DIR_UP;
if(key == KEY_LEFT) return DIR_LEFT;
}
}

9
src/main.c Normal file
View File

@ -0,0 +1,9 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include "game.h"
int main(void)
{
play();
return 0;
}