Fixes
This commit is contained in:
134
items/item.c
134
items/item.c
@@ -117,29 +117,6 @@ void updateItems() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void registerItem(char name[20], SDL_Renderer *renderer) {
|
||||
const char *dot = strchr(name, '.');
|
||||
memcpy(ItemRegistry[itemRegistryIndex].name, name, dot - name);
|
||||
char texturePath[80];
|
||||
snprintf(texturePath, 80, "./assets/items/%s", name);
|
||||
ItemRegistry[itemRegistryIndex].texture[ORIENT_LEFT] = IMG_LoadTexture(renderer, texturePath);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].texture[0], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[ORIENT_LEFT] = ScaleTexture(renderer,
|
||||
ItemRegistry[itemRegistryIndex].texture[ORIENT_LEFT],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].textureOnBelt[ORIENT_LEFT], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].atlasRects[ORIENT_LEFT] = allocate_32x32(
|
||||
ItemRegistry[itemRegistryIndex].texture[ORIENT_LEFT], renderer);
|
||||
ItemRegistry[itemRegistryIndex].atlasRectsOnBelt[ORIENT_LEFT] = allocate_16x16(
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[ORIENT_LEFT], renderer);
|
||||
ItemRegistry[itemRegistryIndex].type = itemRegistryIndex;
|
||||
ItemRegistry[itemRegistryIndex].isTile = false;
|
||||
ItemRegistry[itemRegistryIndex].miscVal = 60;
|
||||
|
||||
itemRegistryIndex++;
|
||||
}
|
||||
|
||||
// easing function: cosine ease‐in‐out
|
||||
//static float ease_in_out(float t) {
|
||||
// if (t < -1.0f) t = -1.0f;
|
||||
@@ -297,7 +274,11 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
|
||||
SDL_RenderFillRect(renderer, &rectA);
|
||||
}
|
||||
//SDL_RenderCopyx(renderer, ItemRegistry[item.type].textureOnBelt[ORIENT_LEFT], NULL, &rect);
|
||||
SDL_RenderCopy(renderer, atlasTexture, &ItemRegistry[item.type].atlasRectsOnBelt[ORIENT_LEFT], &rect);
|
||||
SDL_RenderCopy(renderer, atlasTexture,
|
||||
&ItemRegistry[item.type].beltAnimation.atlasRects[ORIENT_LEFT][
|
||||
(animationStep / ItemRegistry[item.type].beltAnimation.divisor) %
|
||||
ItemRegistry[item.type].beltAnimation.frameCount],
|
||||
&rect);
|
||||
|
||||
if (debugMode) {
|
||||
renderText(renderer, fonts[3], tempStr, rectA.x, rectA.y);
|
||||
@@ -312,29 +293,102 @@ void putItem(int x, int y, ItemType itemType, uint8_t lane) {
|
||||
}
|
||||
|
||||
void loadItems(SDL_Renderer *renderer) {
|
||||
|
||||
// Load tile-based items
|
||||
for (int i = 0; i < tileTypeIndex; i++) {
|
||||
TileTypeReg tile = TileRegistry[i];
|
||||
|
||||
strcpy(ItemRegistry[itemRegistryIndex].name, tile.name);
|
||||
memcpy(ItemRegistry[itemRegistryIndex].texture, tile.textures, sizeof(tile.textures));
|
||||
memcpy(ItemRegistry[itemRegistryIndex].atlasRects, tile.atlasRects, sizeof(tile.atlasRects));
|
||||
for (int a = 0; a < ORIENT_DIRECTION_COUNT; a++) {
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].texture[a], SDL_BLENDMODE_BLEND);
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[a] = ScaleTexture(renderer, tile.textures[a],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
ItemRegistry[itemRegistryIndex].atlasRectsOnBelt[a] = allocate_16x16(
|
||||
ItemRegistry[itemRegistryIndex].textureOnBelt[a], renderer);
|
||||
SDL_SetTextureBlendMode(ItemRegistry[itemRegistryIndex].textureOnBelt[a], SDL_BLENDMODE_BLEND);
|
||||
Item *item = &ItemRegistry[itemRegistryIndex];
|
||||
strcpy(item->name, tile.name);
|
||||
memcpy(&item->animation, &tile.animation, sizeof(tile.animation));
|
||||
|
||||
for (int frame = 0; frame < item->animation.frameCount; frame++) {
|
||||
for (int a = 0; a < ORIENT_DIRECTION_COUNT; a++) {
|
||||
SDL_SetTextureBlendMode(item->animation.textures[a][frame], SDL_BLENDMODE_BLEND);
|
||||
item->beltAnimation.textures[a][frame] = ScaleTexture(renderer, tile.animation.textures[a][frame],
|
||||
TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
item->beltAnimation.atlasRects[a][frame] = allocate_16x16(item->beltAnimation.textures[a][frame],
|
||||
renderer);
|
||||
if (frame + 1 > item->beltAnimation.frameCount) {
|
||||
item->beltAnimation.frameCount = frame + 1;
|
||||
}
|
||||
SDL_SetTextureBlendMode(item->beltAnimation.textures[a][frame], SDL_BLENDMODE_BLEND);
|
||||
}
|
||||
}
|
||||
ItemRegistry[itemRegistryIndex].type = itemRegistryIndex;
|
||||
ItemRegistry[itemRegistryIndex].isTile = true;
|
||||
|
||||
item->type = itemRegistryIndex;
|
||||
item->isTile = true;
|
||||
item->animation.divisor = 1;
|
||||
item->beltAnimation.divisor = 1;
|
||||
|
||||
itemRegistryIndex++;
|
||||
}
|
||||
|
||||
|
||||
// Skip ahead to avoid overlap (tile items use lower indices)
|
||||
itemRegistryIndex = ITEMREGISTRY_SIZE / 2;
|
||||
iterateSortedDir("./assets/items", (DirEntryCallback) registerItem, renderer);
|
||||
|
||||
}
|
||||
// Load sprite-based items with animations
|
||||
DIR *dir = opendir("./assets/items");
|
||||
if (!dir) {
|
||||
perror("Failed to open item asset folder");
|
||||
return;
|
||||
}
|
||||
|
||||
struct dirent *entry;
|
||||
while ((entry = readdir(dir)) != NULL) {
|
||||
if (!strstr(entry->d_name, ".png")) continue;
|
||||
|
||||
int frame, indexItem;
|
||||
char name[64];
|
||||
|
||||
if (sscanf(entry->d_name, "%d-%20[^-]-%d.png", &indexItem, name, &frame) == 3) {
|
||||
// Success: you now have index, fname, and frame
|
||||
} else {
|
||||
fprintf(stderr, "Invalid format: %s\n", entry->d_name);
|
||||
}
|
||||
indexItem += ITEMREGISTRY_SIZE / 2;
|
||||
|
||||
Item *item;
|
||||
item = &ItemRegistry[indexItem];
|
||||
memset(item, 0, sizeof(Item));
|
||||
strcpy(item->name, name);
|
||||
item->type = indexItem;
|
||||
item->isTile = false;
|
||||
item->miscVal = 60;
|
||||
item->animation.divisor = 1;
|
||||
item->beltAnimation.divisor = 1;
|
||||
|
||||
// Load the texture
|
||||
char path[128];
|
||||
snprintf(path, sizeof(path), "./assets/items/%s", entry->d_name);
|
||||
SDL_Texture *tex = IMG_LoadTexture(renderer, path);
|
||||
if (!tex) {
|
||||
fprintf(stderr, "Failed to load texture %s: %s\n", path, IMG_GetError());
|
||||
continue;
|
||||
}
|
||||
|
||||
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
|
||||
SDL_Texture *beltTex = ScaleTexture(renderer, tex, TILE_SIZE / 2, TILE_SIZE / 2);
|
||||
SDL_SetTextureBlendMode(beltTex, SDL_BLENDMODE_BLEND);
|
||||
|
||||
SDL_Rect mainRect = allocate_32x32(tex, renderer);
|
||||
SDL_Rect beltRect = allocate_16x16(beltTex, renderer);
|
||||
// Assign to all orientations
|
||||
for (int o = 0; o < ORIENT_DIRECTION_COUNT; o++) {
|
||||
item->animation.textures[o][frame] = tex;
|
||||
item->beltAnimation.textures[o][frame] = beltTex;
|
||||
item->animation.atlasRects[o][frame] = mainRect;
|
||||
item->beltAnimation.atlasRects[o][frame] = beltRect;
|
||||
if (frame + 1 > item->animation.frameCount) {
|
||||
item->animation.frameCount = frame + 1;
|
||||
item->beltAnimation.frameCount = frame + 1;
|
||||
}
|
||||
|
||||
}
|
||||
item->animation.divisor = 1;
|
||||
if (indexItem + 1 > itemRegistryIndex) {
|
||||
itemRegistryIndex = indexItem + 1;
|
||||
}
|
||||
}
|
||||
|
||||
closedir(dir);
|
||||
}
|
||||
|
@@ -42,10 +42,8 @@ typedef struct Item {
|
||||
ItemType type;
|
||||
char name[20];
|
||||
uint16_t miscVal;
|
||||
SDL_Texture * texture[ORIENT_DIRECTION_COUNT];
|
||||
SDL_Texture * textureOnBelt[ORIENT_DIRECTION_COUNT];
|
||||
SDL_Rect atlasRects[ORIENT_DIRECTION_COUNT];
|
||||
SDL_Rect atlasRectsOnBelt[ORIENT_DIRECTION_COUNT];
|
||||
OrientedAnimation animation;
|
||||
OrientedAnimation beltAnimation;
|
||||
} Item;
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user