본문 바로가기
[ChatGPT] Sample Code 샘플 코드

[ChatGPT][Raylib + Tiled] Tiled를 활용한 Raylib 맵 만들기 기본 샘플

by byungwoo733 2024. 1. 2.
728x90
반응형
SMALL
#include "raylib.h"
#include "tiled.h" // Tiled loader for raylib (https://github.com/vtdevapps/raylib-tmx)

int main(void)
{
    // Initialization
    const int screenWidth = 800;
    const int screenHeight = 600;

    InitWindow(screenWidth, screenHeight, "raylib + Tiled Sample");

    SetTargetFPS(60); // Set the frames per second

    // Load Tiled map
    TmxMap map = LoadTmx("path/to/your/map.tmx");

    // Main game loop
    while (!WindowShouldClose()) // Detect window close button or ESC key
    {
        // Update

        // Draw
        BeginDrawing();

        ClearBackground(RAYWHITE); // Clear the background

        // Draw Tiled map layers
        for (int i = 0; i < map.numLayers; i++)
        {
            TmxLayer layer = map.layers[i];

            if (layer.visible)
            {
                // Render each tile
                for (int y = 0; y < layer.height; y++)
                {
                    for (int x = 0; x < layer.width; x++)
                    {
                        int tilesetIndex = layer.data[y * layer.width + x];

                        if (tilesetIndex > 0)
                        {
                            // Note: Tiled uses 1-based indexing for tiles, so we subtract 1
                            TmxTileset tileset = map.tilesets[tilesetIndex - 1];
                            Rectangle sourceRect = { tileset.tileWidth * ((tilesetIndex - tileset.firstGid) % (tileset.image.width / tileset.tileWidth)),
                                                     tileset.tileHeight * ((tilesetIndex - tileset.firstGid) / (tileset.image.width / tileset.tileWidth)),
                                                     tileset.tileWidth, tileset.tileHeight };

                            Rectangle destRect = { x * tileset.tileWidth, y * tileset.tileHeight, tileset.tileWidth, tileset.tileHeight };

                            // Draw the tile using raylib
                            DrawTextureRec(tileset.image.texture, sourceRect, destRect, WHITE);
                        }
                    }
                }
            }
        }

        EndDrawing();
    }

    // Unload resources
    UnloadTexture(map.tilesets[0].image.texture);
    UnloadTmx(map);

    // De-Initialization
    CloseWindow(); // Close the window and OpenGL context

    return 0;
}

이 예에서는 JSON 형식(.tmx 파일)으로 저장된 타일 맵이 있다고 가정합니다. "path/to/your/map.tmx"를 타일 맵 파일의 실제 경로로 바꾸십시오. 참고: 이 예제를 사용하려면 Tiled.h 헤더와 tmxlite 라이브러리에 대한 링크를 포함해야 합니다. raylib-tmx 라이브러리는 타일 맵을 raylib에 로드하는 데 사용됩니다. 

여기에서 찾을 수 있습니다: https://github.com/vtdevapps/raylib-tmx

728x90
LIST

댓글