Files
factorygame/util/atlas.c
2025-06-01 22:13:02 +02:00

80 lines
2.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// Created by bruno on 1.6.2025.
//
#include "atlas.h"
#include "util.h"
SDL_Texture *atlasTexture;
int atlasX = 0, atlasY = 0;
int tileIndex = 0; // Which 32x32 tile we're on
int quadrantIndex = 0; // Which 16x16 slot inside that tile
SDL_Rect allocate_16x16(SDL_Texture *srcTexture, SDL_Renderer *renderer) {
SDL_Texture * oldTarget = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, atlasTexture);
int tileX = tileIndex % ATLAS_TILES_PER_ROW;
int tileY = tileIndex / ATLAS_TILES_PER_ROW;
int dx = (quadrantIndex % 2) * QUADRANT_SIZE;
int dy = (quadrantIndex / 2) * QUADRANT_SIZE;
SDL_Rect destRect = {
tileX * TILE_SIZE + dx,
tileY * TILE_SIZE + dy,
QUADRANT_SIZE,
QUADRANT_SIZE
};
SDL_RenderCopy(renderer, srcTexture, NULL, &destRect);
quadrantIndex++;
if (quadrantIndex >= 4) {
tileIndex++;
quadrantIndex = 0;
}
SDL_SetRenderTarget(renderer, oldTarget);
return destRect;
}
SDL_Rect allocate_32x32(SDL_Texture *srcTexture, SDL_Renderer *renderer) {
SDL_Texture * oldTarget = SDL_GetRenderTarget(renderer);
SDL_SetRenderTarget(renderer, atlasTexture);
// If were not at the start of a tile, skip to the next clean one
if (quadrantIndex != 0) {
tileIndex++;
quadrantIndex = 0;
}
int tileX = tileIndex % ATLAS_TILES_PER_ROW;
int tileY = tileIndex / ATLAS_TILES_PER_ROW;
SDL_Rect destRect = {
tileX * TILE_SIZE,
tileY * TILE_SIZE,
TILE_SIZE,
TILE_SIZE
};
SDL_RenderCopy(renderer, srcTexture, NULL, &destRect);
tileIndex++; // Move to next tile
// quadrantIndex stays 0 — new tile is fresh
SDL_SetRenderTarget(renderer, oldTarget);
return destRect;
}
void initAtlas(SDL_Renderer *renderer) {
// Clear atlas with transparent
SDL_SetRenderTarget(renderer, atlasTexture);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
atlasTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
ATLAS_SIZE, ATLAS_SIZE);
}