Tilemap2h: Convert .xml or .tmx map file to cpp header

Hi community
I wrote a Python 3 script for converting Pyxel or Tiled map file to cpp header. I’m not familiar to writing programs in Python. I’m really happy to see suggestions.

Usage:
First arg = File input type: tiled .tmx or pyxel .xml [tiled, pyxel]
Second arg = How many tiles used for map [2, 4, 16, 256] Warning! Only pyxel supported at this time
Third arg = Filename of .tmx or .xml file
example usage: tilemap2h.py pyxel 4 tilemap.xml

Edit: For saving memory you can select how many tiles used for map. Also it’s automatically creates readMap function.

Edit2: Example

In working:

Map:

Main.cpp

#include "Pokitto.h"
#include "tilemap.h"
#include "tileset.h"
Pokitto::Core game;

int x = 0, y = 0;

//
void drawMap(int _x, int _y, uint8_t _w = 27, uint8_t _h = 21, uint8_t _sw = 8, uint8_t _sh = 8);

int main () {
  game.begin();
  game.display.load565Palette(sprite_pal);
  while (game.isRunning()) {
    if (game.update()) {
        if(game.buttons.repeat(BTN_UP, 2)){
            y--;
            if (y < 0) y = maxMapy;
        }
		if(game.buttons.repeat(BTN_DOWN, 2)){
			y++;
			if (y > maxMapy) y = 0;
		}
		if(game.buttons.repeat(BTN_LEFT, 2)){
            x--;
            if (x < 0) x = maxMapx;
        }
		if(game.buttons.repeat(BTN_RIGHT, 2)){
			x++;
			if (x > maxMapx) x = 0;
		}
		drawMap(x, y);
		game.display.setCursor(0,0);
		game.display.print(x, 10); game.display.print(" "); game.display.print(y, 10);
    }
  }
  return 1;
}

void drawMap(int _x, int _y, uint8_t _w, uint8_t _h, uint8_t _sw, uint8_t _sh){
    for(uint8_t __x = 0; __x < _w; __x++){
        for(uint8_t __y = 0; __y < _h; __y++){
            game.display.drawBitmap(__x * _sw, __y * _sh, sprites[readMap((__x + _x) % (maxMapx + 1), (__y + _y) % (maxMapy + 1))]);
        }
    }
}

tilemap.h:

/*
	tilemap2h: Convert tilemap .xml or .tmx map file to cpp header file
	Author: Malik Enes Safak
*/
uint8_t maxMapx = 54, maxMapy = 26;

const uint8_t tilemap[27][14] = {
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1010, 0b0, 0b0, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10100000, 0b10000000, 0b0, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1010, 0b0, 0b100000, 0b0, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10100000, 0b0, 0b1000, 0b0, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1010, 0b0, 0b0, 0b10, 0b0, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10100000, 0b0, 0b0, 0b0, 0b10000000, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1010, 0b0, 0b0, 0b0, 0b0, 0b100000, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10100000, 0b0, 0b0, 0b0, 0b0, 0b1000, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b1010, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10, 0b0, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b0, 0b10100000, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10000000, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b1010, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b100000, 0b0},
{0b0, 0b0, 0b0, 0b0, 0b10100000, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1000, 0b0},
{0b0, 0b0, 0b0, 0b1010, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10, 0b0},
{0b0, 0b0, 0b0, 0b10100000, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b10000000},
{0b0, 0b0, 0b1010, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b100000},
{0b0, 0b0, 0b10100000, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1000},
{0b0, 0b1010, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1000},
{0b0, 0b10100000, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1000},
{0b1010, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1000},
{0b10100000, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b0, 0b1000},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100},
{0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010101, 0b1010100}};

uint8_t readMap(uint8_t _x, uint8_t _y){
	if((_x % 4) == 0){
		return (tilemap[_y][int(_x / 4)] >> 6) & 0b00000011;
	}
	if((_x % 4) == 1){
		return (tilemap[_y][int(_x / 4)] >> 4) & 0b00000011;
	}
	if((_x % 4) == 2){
		return (tilemap[_y][int(_x / 4)] >> 2) & 0b00000011;
	}
	if((_x % 4) == 3){
		return tilemap[_y][int(_x / 4)] & 0b00000011;
	}
}

tileset.h (generated with img2pok.exe. Thanks @HomineLudens ):

#include <stdint.h>

//Total colors 4
const uint16_t sprite_pal[] = {
0,63245,17475,13598,
};

//Sprite sheet:8x1
const uint8_t sprites [][18] ={
//[0] cell:0x0
{
8,8,
255,255,
255,255,
255,255,
255,255,
255,255,
255,255,
255,255,
255,255,
},
//[1] cell:1x0
{
8,8,
170,170,
170,170,
170,170,
170,170,
170,170,
170,170,
170,170,
170,170,
},
//[2] cell:2x0
{
8,8,
85,85,
85,85,
85,85,
85,85,
85,85,
85,85,
85,85,
85,85,
}
};
4 Likes

Glad you use it @NullMember