Encoding the autotile pattern

Talk about things that are not making games here. But you should also make games!

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
lennyhome
Slime Knight
Posts: 109
Joined: Fri Feb 14, 2020 6:07 am

Encoding the autotile pattern

Post by lennyhome »

I'll dump the Python script that I'm using here for anybody who may want to read it. It's not a full application. It's meant to be read and maybe as a framework for your own image processing scripts.

In short, it converts a double-sized RpgMaker terrain bock into the 8x6 variant of this:
https://opengameart.org/content/seamles ... emplate-ii
using 20x20 tiles instead of 16x16. But it's applicable to any tile size, with some modifications.

More information and the actual pattern are on the other thread:
viewtopic.php?t=8328

Code: Select all

from sdl2 import *
from sdl2.sdlgfx import *

def tile10(x, y):
    return SDL_Rect(x * 10, y * 10, 10, 10)

if __name__ == "__main__":

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0):
        raise Exception()

    window = SDL_CreateWindow(
        b"Terrain maker - by Lenny 2022",
        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
        640, 400, SDL_WINDOW_SHOWN
    )[0]

    screen = SDL_CreateRGBSurface(
            0, 320, 200, 32, 0, 0, 0, 0
        )[0]

    tileset = SDL_LoadBMP(b"ohr_mk.bmp")[0]

    # scale down

    bigpat = SDL_CreateRGBSurface(
            0, 120, 160, 32, 0, 0, 0, 0
        )[0]

    SDL_BlitSurface(
        tileset, SDL_Rect(20, 20, 120, 160),
        bigpat, SDL_Rect(0, 0, 120, 160)
    )

    SDL_BlitSurface(
        bigpat, None,
        screen, SDL_Rect(20, 20, 120, 160)
    )

    smallpat = shrinkSurface(bigpat, 2, 2)[0]

    # re-arrange

    the_table = (
        (
            ((2, 0), (3, 0), (2, 1), (3, 1)),
            ((0, 0), (3, 2), (0, 1), (3, 7)),
            ((2, 2), (3, 2), (4, 1), (3, 3)),
            ((4, 2), (5, 2), (4, 3), (5, 3)),
            ((0, 2), (1, 2), (0, 3), (1, 3)),
            ((2, 2), (3, 2), (2, 3), (3, 3)),
            ((2, 2), (3, 2), (2, 3), (5, 1)),
            ((2, 2), (1, 0), (2, 7), (1, 1)),
        ), (
            ((0, 2), (1, 2), (0, 3), (5, 1)),
            ((2, 2), (3, 2), (4, 1), (5, 1)),
            ((4, 0), (3, 4), (4, 1), (5, 1)),
            ((2, 4), (5, 0), (4, 1), (3, 5)),
            ((4, 0), (3, 4), (2, 5), (3, 5)),
            ((2, 4), (3, 4), (2, 5), (3, 5)),
            ((2, 4), (5, 0), (2, 5), (5, 1)),
            ((4, 2), (5, 2), (4, 1), (5, 3)),
        ), (
            ((0, 4), (5, 0), (0, 5), (1, 5)),
            ((4, 0), (5, 0), (2, 5), (5, 1)),
            ((4, 0), (5, 0), (4, 1), (5, 1)),
            ((4, 0), (3, 4), (4, 1), (3, 5)),
            ((2, 4), (3, 4), (2, 5), (5, 1)),
            ((2, 4), (3, 4), (4, 1), (5, 1)),
            ((4, 4), (5, 4), (4, 1), (5, 5)),
            ((0, 4), (5, 4), (0, 1), (1, 1)),
        ), (
            ((0, 4), (1, 4), (0, 5), (5, 1)),
            ((2, 4), (5, 0), (4, 1), (5, 1)),
            ((4, 0), (5, 4), (4, 1), (5, 5)),
            ((0, 4), (1, 4), (0, 5), (1, 5)),
            ((2, 4), (5, 0), (2, 5), (3, 5)),
            ((4, 0), (5, 0), (2, 5), (3, 5)),
            ((4, 0), (5, 4), (4, 5), (5, 5)),
            ((0, 0), (1, 0), (0, 5), (5, 5)),
        ), (
            ((0, 4), (5, 0), (0, 5), (5, 1)),
            ((4, 0), (5, 0), (2, 7), (3, 7)),
            ((4, 0), (5, 0), (4, 1), (3, 5)),
            ((4, 0), (3, 4), (2, 5), (5, 1)),
            ((2, 4), (3, 4), (4, 1), (3, 5)),
            ((2, 4), (3, 4), (2, 5), (3, 5)),
            ((4, 4), (5, 4), (4, 5), (5, 5)),
            ((0, 4), (5, 4), (0, 5), (5, 5)),
        ), (
            ((0, 6), (5, 0), (0, 7), (1, 7)),
            ((2, 2), (3, 2), (2, 7), (3, 7)),
            ((4, 0), (3, 6), (2, 7), (3, 7)),
            ((2, 6), (5, 6), (4, 7), (5, 7)),
            ((0, 6), (3, 6), (0, 7), (1, 7)),
            ((2, 6), (3, 6), (2, 7), (3, 7)),
            ((2, 6), (5, 0), (2, 7), (3, 7)),
            ((4, 0), (5, 6), (4, 7), (5, 7)),
        )
    )

    for y, line in enumerate(the_table):
        for x, cell in enumerate(line):
            SDL_BlitSurface(
                smallpat, tile10(cell[0][0], cell[0][1]),
                screen, tile10(x * 2 + 16, (y + 4) * 2)
            )
            SDL_BlitSurface(
                smallpat, tile10(cell[1][0], cell[1][1]),
                screen, tile10(x * 2 + 17, (y + 4) * 2)
            )
            SDL_BlitSurface(
                smallpat, tile10(cell[2][0], cell[2][1]),
                screen, tile10(x * 2 + 16, (y + 4) * 2 + 1)
            )
            SDL_BlitSurface(
                smallpat, tile10(cell[3][0], cell[3][1]),
                screen, tile10(x * 2 + 17, (y + 4) * 2 + 1)
            )

    # horizontal ruler

    for x in range(4):
        SDL_FillRect(
            screen, SDL_Rect(x * 40 + 20, 0, 1, 10),
            SDL_MapRGB(screen.format, 128, 128, 128)
        )

    for x in range(3):
        SDL_FillRect(
            screen, SDL_Rect(x * 40 + 40, 0, 1, 7),
            SDL_MapRGB(screen.format, 80, 80, 80)
        )

    # vertical ruler

    for y in range(5):
        SDL_FillRect(
            screen, SDL_Rect(0, y * 40 + 20, 10, 1),
            SDL_MapRGB(screen.format, 128, 128, 128)
        )

    for y in range(4):
        SDL_FillRect(
            screen, SDL_Rect(0, y * 40 + 40, 7, 1),
            SDL_MapRGB(screen.format, 80, 80, 80)
        )

    # auto tile

    for y in range(20):
        for x in range(20):
            if (x + y) & 1:
                SDL_BlitSurface(
                    smallpat, SDL_Rect(20 + x, y, 1, 1),
                    screen, SDL_Rect(160 + x, y, 1, 1),
                )

    # fill tile
            
    SDL_BlitSurface(
        smallpat, SDL_Rect(20, 0, 20, 20),
        screen, SDL_Rect(180, 0, 20, 20),
    )


    # save the pattern

    SDL_SaveBMP(screen, b"ohr_mk.bmp")
    
    # display it

    SDL_BlitScaled(
        screen, None,
        SDL_GetWindowSurface(window), None
    )

    SDL_UpdateWindowSurface(
        window
    )

    # wait for window close

    running = True

    while running:

        event = SDL_Event()

        while SDL_PollEvent(event):
            if event.type == SDL_QUIT:
                running = False

        SDL_Delay(100)

    # cleanup

    SDL_FreeSurface(tileset)
    SDL_FreeSurface(bigpat)
    SDL_FreeSurface(smallpat)
    SDL_FreeSurface(screen)

    SDL_DestroyWindow(window)
    SDL_Quit()
Post Reply