Fixes
This commit is contained in:
234
tiles/tile.c
234
tiles/tile.c
@@ -2,6 +2,7 @@
|
||||
// Created by bruno on 4/24/25.
|
||||
//
|
||||
|
||||
#include <dirent.h>
|
||||
#include "tile.h"
|
||||
#include "../player/player.h"
|
||||
#include "furnace.h"
|
||||
@@ -73,57 +74,189 @@ void generateTestMap() {
|
||||
}
|
||||
|
||||
|
||||
void registerTile(char name[20], SDL_Renderer *renderer) {
|
||||
const char *dot = strchr(name, '.');
|
||||
memcpy(TileRegistry[tileTypeIndex].name, name, dot - name);
|
||||
void registerTile(char fname[20], SDL_Renderer *renderer) {
|
||||
|
||||
char name[21];
|
||||
|
||||
// Load animation frames
|
||||
int frame = 0;
|
||||
int indexTile = 0;
|
||||
char texturePath[80];
|
||||
snprintf(texturePath, 80, "./assets/tiles/%s", name);
|
||||
|
||||
if (sscanf(fname, "%d_%20[^_]_%d.png", &indexTile, name, &frame) == 3) {
|
||||
// Success: you now have index, fname, and frame
|
||||
} else {
|
||||
fprintf(stderr, "Invalid format: %s\n", fname);
|
||||
}
|
||||
strcpy(TileRegistry[indexTile].name, name);
|
||||
snprintf(texturePath, sizeof(texturePath), "./assets/tiles/%s", fname);
|
||||
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, texturePath);
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_LEFT] = texture;
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT] = createFlippedTexture(renderer, texture, SDL_FLIP_HORIZONTAL);
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_UP] = createRotatedTexture(renderer, texture, 90);
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_DOWN] = createRotatedTexture(renderer, texture, 270);
|
||||
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_LEFT], SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT], SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_UP], SDL_BLENDMODE_BLEND);
|
||||
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_DOWN], SDL_BLENDMODE_BLEND);
|
||||
if (!texture) {
|
||||
// Stop loading frames if the first one fails, or after all valid ones are added
|
||||
if (frame == 0) {
|
||||
fprintf(stderr, "Failed to load tile texture %s: %s\n", texturePath, IMG_GetError());
|
||||
}
|
||||
}
|
||||
|
||||
TileRegistry[tileTypeIndex].atlasRects[ORIENT_LEFT] = allocate_32x32(
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_LEFT], renderer);
|
||||
TileRegistry[tileTypeIndex].atlasRects[ORIENT_RIGHT] = allocate_32x32(
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT], renderer);
|
||||
TileRegistry[tileTypeIndex].atlasRects[ORIENT_UP] = allocate_32x32(TileRegistry[tileTypeIndex].textures[ORIENT_UP],
|
||||
renderer);
|
||||
TileRegistry[tileTypeIndex].atlasRects[ORIENT_DOWN] = allocate_32x32(
|
||||
TileRegistry[tileTypeIndex].textures[ORIENT_DOWN], renderer);
|
||||
for (int o = 0; o < ORIENT_DIRECTION_COUNT; o++) {
|
||||
SDL_Texture *textures[ORIENT_DIRECTION_COUNT] = {
|
||||
NULL,
|
||||
texture,
|
||||
NULL,
|
||||
createFlippedTexture(renderer, texture, SDL_FLIP_HORIZONTAL),
|
||||
NULL,
|
||||
createRotatedTexture(renderer, texture, 90),
|
||||
NULL,
|
||||
createRotatedTexture(renderer, texture, 270)
|
||||
};
|
||||
|
||||
TileRegistry[tileTypeIndex].type = tileTypeIndex;
|
||||
TileRegistry[tileTypeIndex].breakTime = 15;
|
||||
printf("Bound %s to %d orient %s\n", fname, indexTile, OrientStrings[o]);
|
||||
TileRegistry[indexTile].animation.textures[o][frame] = textures[o];
|
||||
SDL_SetTextureBlendMode(textures[o], SDL_BLENDMODE_BLEND);
|
||||
TileRegistry[indexTile].animation.atlasRects[o][frame] = allocate_32x32(textures[o], renderer);
|
||||
}
|
||||
|
||||
tileTypeIndex++;
|
||||
TileRegistry[indexTile].type = indexTile;
|
||||
TileRegistry[indexTile].animation.frameCount = frame + 1;
|
||||
TileRegistry[indexTile].animation.divisor = 1;
|
||||
|
||||
TileRegistry[indexTile].type = tileTypeIndex;
|
||||
TileRegistry[indexTile].breakTime = 15;
|
||||
|
||||
if (indexTile + 1 > tileTypeIndex) {
|
||||
tileTypeIndex = indexTile + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void registerBackgroundTile(char name[20], SDL_Renderer *renderer) {
|
||||
const char *dot = strchr(name, '.');
|
||||
memcpy(BackgroundTileRegistry[backgroundTileTypeIndex].name, name, dot - name);
|
||||
|
||||
void registerBackgroundTile(char fname[20], SDL_Renderer *renderer) {
|
||||
char name[21];
|
||||
|
||||
// Load animation frames
|
||||
int frame = 0;
|
||||
int indexBgTile = 0;
|
||||
char texturePath[80];
|
||||
snprintf(texturePath, 80, "./assets/backgrounds/%s", name);
|
||||
|
||||
if (sscanf(fname, "%d_%20[^_]_%d.png", &indexBgTile, name, &frame) == 3) {
|
||||
// Success: you now have index, fname, and frame
|
||||
} else {
|
||||
fprintf(stderr, "Invalid format: %s\n", fname);
|
||||
}
|
||||
strcpy(BackgroundTileRegistry[indexBgTile].name, name);
|
||||
snprintf(texturePath, sizeof(texturePath), "./assets/backgrounds/%s", fname);
|
||||
SDL_Texture *texture = IMG_LoadTexture(renderer, texturePath);
|
||||
BackgroundTileRegistry[backgroundTileTypeIndex].texture = texture;
|
||||
SDL_SetTextureBlendMode(BackgroundTileRegistry[backgroundTileTypeIndex].texture, SDL_BLENDMODE_NONE);
|
||||
BackgroundTileRegistry[backgroundTileTypeIndex].atlasRect = allocate_32x32(
|
||||
BackgroundTileRegistry[backgroundTileTypeIndex].texture, renderer);
|
||||
if (!texture) {
|
||||
if (frame == 0) {
|
||||
fprintf(stderr, "Failed to load background texture %s: %s\n", texturePath, IMG_GetError());
|
||||
}
|
||||
}
|
||||
|
||||
BackgroundTileRegistry[backgroundTileTypeIndex].type = backgroundTileTypeIndex;
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
|
||||
|
||||
backgroundTileTypeIndex++;
|
||||
printf("Bound %s to %d\n", fname, indexBgTile);
|
||||
|
||||
BackgroundTileRegistry[indexBgTile].animation.textures[frame] = texture;
|
||||
BackgroundTileRegistry[indexBgTile].animation.atlasRects[frame] = allocate_32x32(texture, renderer);
|
||||
|
||||
BackgroundTileRegistry[indexBgTile].type = indexBgTile;
|
||||
BackgroundTileRegistry[indexBgTile].animation.frameCount = frame + 1;
|
||||
BackgroundTileRegistry[indexBgTile].animation.divisor = 1;
|
||||
|
||||
if (indexBgTile + 1 > backgroundTileTypeIndex) {
|
||||
backgroundTileTypeIndex = indexBgTile + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int compareStrings(const void *a, const void *b) {
|
||||
const char *strA = *(const char **) a;
|
||||
const char *strB = *(const char **) b;
|
||||
return strcmp(strA, strB);
|
||||
}
|
||||
|
||||
void loadTiles(SDL_Renderer *renderer) {
|
||||
iterateSortedDir("./assets/tiles", (DirEntryCallback) registerTile, renderer);
|
||||
iterateSortedDir("./assets/backgrounds", (DirEntryCallback) registerBackgroundTile, renderer);
|
||||
DIR *dir = opendir("./assets/tiles");
|
||||
if (!dir) {
|
||||
perror("Failed to open tiles directory");
|
||||
return;
|
||||
}
|
||||
|
||||
char *baseNames[MAX_BASE_NAMES];
|
||||
int baseNameCount = 0;
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir))) {
|
||||
char *dot = strrchr(entry->d_name, '.');
|
||||
if (!dot || strcmp(dot, ".png") != 0) continue;
|
||||
|
||||
// Check if baseName already stored
|
||||
int found = 0;
|
||||
for (int i = 0; i < baseNameCount; ++i) {
|
||||
if (strcmp(baseNames[i], entry->d_name) == 0) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found && baseNameCount < MAX_BASE_NAMES) {
|
||||
baseNames[baseNameCount++] = strdup(entry->d_name); // Only store base, not full file name
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
qsort(baseNames, baseNameCount, sizeof(char *), compareStrings);
|
||||
|
||||
// Call registerTile on each base name
|
||||
for (int i = 0; i < baseNameCount; ++i) {
|
||||
char fileName[64];
|
||||
snprintf(fileName, sizeof(fileName), "%s", baseNames[i]);
|
||||
registerTile(fileName, renderer);
|
||||
free(baseNames[i]);
|
||||
}
|
||||
generateBeltFrames(renderer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loadBackgroundTiles(SDL_Renderer *renderer) {
|
||||
DIR *dir = opendir("./assets/backgrounds");
|
||||
if (!dir) {
|
||||
perror("Failed to open backgrounds directory");
|
||||
return;
|
||||
}
|
||||
|
||||
char *baseNames[MAX_BASE_NAMES];
|
||||
int baseNameCount = 0;
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir))) {
|
||||
char *dot = strrchr(entry->d_name, '.');
|
||||
if (!dot || strcmp(dot, ".png") != 0) continue;
|
||||
|
||||
// Check if baseName already stored
|
||||
int found = 0;
|
||||
for (int i = 0; i < baseNameCount; ++i) {
|
||||
if (strcmp(baseNames[i], entry->d_name) == 0) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found && baseNameCount < MAX_BASE_NAMES) {
|
||||
baseNames[baseNameCount++] = strdup(entry->d_name); // Only store base, not full file name
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
qsort(baseNames, baseNameCount, sizeof(char *), compareStrings);
|
||||
|
||||
// Call registerBackgroundTile on each base name
|
||||
for (int i = 0; i < baseNameCount; ++i) {
|
||||
char fileName[64];
|
||||
snprintf(fileName, sizeof(fileName), "%s", baseNames[i]);
|
||||
registerBackgroundTile(fileName, renderer);
|
||||
free(baseNames[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void setupTiles() {
|
||||
@@ -144,10 +277,19 @@ void setupTiles() {
|
||||
}
|
||||
}
|
||||
TileRegistry[TYPE_FURNACE].outputLane[FURNACE_OUTPUT_SLOT] = 1;
|
||||
TileRegistry[TYPE_FURNACE].startFrame = 1;
|
||||
TileRegistry[TYPE_FURNACE].needsTicks = true;
|
||||
TileRegistry[TYPE_FURNACE].animation.divisor = 8;
|
||||
TileRegistry[TYPE_BELT].needsTicks = true;
|
||||
TileRegistry[TYPE_MINER].needsTicks = true;
|
||||
TileRegistry[TYPE_MINER].outputLane[MINER_OUTPUT_SLOT] = 1;
|
||||
TileRegistry[TYPE_MINER].startFrame = 1;
|
||||
|
||||
BackgroundTileRegistry[BGType_WATER_DEEP].animation.divisor = 16;
|
||||
BackgroundTileRegistry[BGType_WATER_SHALLOW].animation.divisor = 12;
|
||||
BackgroundTileRegistry[BGType_GRASS_FLOWER0].animation.divisor = 16;
|
||||
BackgroundTileRegistry[BGType_GRASS_FLOWER1].animation.divisor = 16;
|
||||
BackgroundTileRegistry[BGType_GRASS_FLOWER2].animation.divisor = 16;
|
||||
}
|
||||
|
||||
uint16_t getBreakTime(int type) {
|
||||
@@ -205,8 +347,11 @@ void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
|
||||
adjustRect(&dstRect, playerRect);
|
||||
|
||||
BackgroundTile bt = backgroundMap[y][x];
|
||||
SDL_Texture *tex = BackgroundTileRegistry[bt.type].texture;
|
||||
SDL_Rect atlRect = BackgroundTileRegistry[bt.type].atlasRect;
|
||||
SDL_Texture *tex = BackgroundTileRegistry[bt.type].animation.textures[animationStep %
|
||||
BackgroundTileRegistry[bt.type].animation.frameCount];
|
||||
SDL_Rect atlRect = BackgroundTileRegistry[bt.type].animation.atlasRects[
|
||||
(animationStep / BackgroundTileRegistry[bt.type].animation.divisor) %
|
||||
BackgroundTileRegistry[bt.type].animation.frameCount];
|
||||
if (atlRect.w != 0 && atlRect.h != 0) {
|
||||
SDL_RenderCopy(renderer, atlasTexture, &atlRect, &dstRect);
|
||||
//SDL_RenderCopy(renderer, tex, NULL, &dstRect);
|
||||
@@ -237,11 +382,18 @@ void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
|
||||
renderBelt(x, y, tileSize, tileSize, t.direction, playerRect, renderer);
|
||||
break;
|
||||
default: {
|
||||
SDL_Rect atlRect = TileRegistry[t.type].atlasRects[t.direction];
|
||||
SDL_Texture *tex = TileRegistry[t.type].textures[t.direction];
|
||||
char animationFrame = ((animationStep / TileRegistry[t.type].animation.divisor) %
|
||||
(TileRegistry[t.type].animation.frameCount -
|
||||
TileRegistry[t.type].startFrame)) + TileRegistry[t.type].startFrame;
|
||||
if (t.fixedFrame > 0) {
|
||||
animationFrame = t.fixedFrame - 1;
|
||||
}
|
||||
SDL_Rect atlRect = TileRegistry[t.type].animation.atlasRects[t.direction][animationFrame];
|
||||
SDL_Texture *tex = TileRegistry[t.type].animation.textures[t.direction][animationFrame];
|
||||
if (atlRect.w == 0 || atlRect.h == 0) {
|
||||
tex = TileRegistry[t.type].textures[ORIENT_LEFT];
|
||||
atlRect = TileRegistry[t.type].atlasRects[ORIENT_LEFT];
|
||||
tex = TileRegistry[t.type].animation.textures[ORIENT_LEFT][animationFrame];
|
||||
atlRect = TileRegistry[t.type].animation.atlasRects[ORIENT_LEFT][
|
||||
animationFrame];
|
||||
}
|
||||
if (atlRect.w != 0 && atlRect.h != 0) {
|
||||
//SDL_RenderCopy(renderer, tex, NULL, &dstRect);
|
||||
|
Reference in New Issue
Block a user