Start atlas

This commit is contained in:
2025-06-01 22:13:02 +02:00
parent 96a9a45c20
commit 84805b92cb
64 changed files with 954 additions and 243 deletions

View File

@@ -5,6 +5,9 @@
#include "item.h"
#include "../player/player.h"
#include "../util/font.h"
#include "../tiles/furnace.h"
#include "../tiles/tilecallbacks.h"
#include "../util/atlas.h"
#include <dirent.h>
Item ItemRegistry[ITEMREGISTRY_SIZE];
@@ -22,22 +25,27 @@ void updateItems() {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Tile *t = &tileMap[y][x];
if (t->type != TYPE_BELT) continue;
TileTypeReg tt = TileRegistry[t->type];
if (t->type == TYPE_AIR) continue;
int dir = t->direction;
bool horz = (dir == ORIENT_LEFT || dir == ORIENT_RIGHT);
bool vert = (dir == ORIENT_UP || dir == ORIENT_DOWN);
for (uint8_t lane = 0; lane < 2; lane++) {
for (uint8_t lane = 0; lane < ItemSlotCount; lane++) {
if (!tt.outputLane[lane]) continue;
ItemOnBelt *itm = &t->items[lane];
if (itm->type == 0) continue;
// 1) Advance
itm->offset += speed;
if (tt.itemMoves) {
itm->offset += speed;
// 1) Advance
}
// 2) Time to hop?
if (itm->offset >= 0.5f) {
itm->offset -= 1.0f;
if (itm->offset >= 0.5f || !tt.itemMoves) {
if (tt.itemMoves) {
itm->offset -= 1.0f;
}
// target coords
int nx = x + dirDx[dir];
@@ -45,43 +53,49 @@ void updateItems() {
// bounds & belt?
if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT) {
//itm->type = 0;
itm->offset += 1.0f - speed;
if (tt.itemMoves) {
itm->offset += 1.0f - speed;
}
continue;
}
Tile *next = &tileMap[ny][nx];
if (next->type != TYPE_BELT) {
//itm->type = 0;
itm->offset += 1.0f - speed;
continue;
}
// Decide new lane
TileTypeReg ntt = TileRegistry[next->type];
int newLane = lane;
int newDir = next->direction;
bool nH = (newDir == ORIENT_LEFT || newDir == ORIENT_RIGHT);
bool nV = (newDir == ORIENT_UP || newDir == ORIENT_DOWN);
switch (next->type) {
case TYPE_BELT:
int newDir = next->direction;
bool nH = (newDir == ORIENT_LEFT || newDir == ORIENT_RIGHT);
bool nV = (newDir == ORIENT_UP || newDir == ORIENT_DOWN);
if ((horz && nH) || (vert && nV)) {
// same axis → keep lane
} else if (horz && nV) {
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
newLane = (dir == ORIENT_RIGHT ^ newDir == ORIENT_UP ? 0 : 1);
itm->offset = 0.0f;
} else if (vert && nH) {
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
newLane = (dir == ORIENT_UP ^ newDir == ORIENT_RIGHT ? 1 : 0);
itm->offset = 0.0f;
if ((horz && nH) || (vert && nV)) {
// same axis → keep lane
} else if (horz && nV) {
// came off a horizontal: lane0=top→vertical.left, lane1=bottom→vertical.right
newLane = (dir == ORIENT_RIGHT ^ newDir == ORIENT_UP ? 0 : 1);
itm->offset = 0.0f;
} else if (vert && nH) {
// came off vertical: lane0=left→horizontal.top, lane1=right→horizontal.bottom
newLane = (dir == ORIENT_UP ^ newDir == ORIENT_RIGHT ? 1 : 0);
itm->offset = 0.0f;
}
// (diagonals fall back to same-lane)
// Find a free slot in
break;
default:
itm->offset += 1.0f - speed;
}
// (diagonals fall back to same-lane)
// Find a free slot in
if (next->items[newLane].type == 0) {
if (next->items[newLane].type == 0 && ntt.allowedInItems[newLane][itm->type]) {
// MOVE it
ItemOnBelt moved = *itm;
moved.tileX = nx;
moved.tileY = ny;
if (!ntt.itemMoves) {
moved.offset = 0.5f;
}
next->items[newLane] = moved;
// clear this one
@@ -90,8 +104,15 @@ void updateItems() {
// both slots full → wait at end
itm->offset = epsilon;
}
} else {
}
}
const UpdateTileCallback cb = ItemTileCallbacks[t->type];
if (cb) {
cb(t);
}
}
}
}
@@ -108,7 +129,11 @@ void registerItem(char name[20], SDL_Renderer *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++;
}
@@ -196,7 +221,7 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_LEFT:
xOffset += 0.0f * TILE_SIZE + (TILE_SIZE / 2);
xOffset += 0.0f * TILE_SIZE + (TILE_SIZE);
yOffset += 0.26f * TILE_SIZE;
break;
case ORIENT_LEFT_UP:
@@ -205,14 +230,14 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
break;
case ORIENT_UP:
xOffset += 0.22f * TILE_SIZE; //GOTO HEHREHRHE
yOffset += 0.0f * TILE_SIZE + (TILE_SIZE / 2);
yOffset += 0.0f * TILE_SIZE + (TILE_SIZE);
break;
case ORIENT_RIGHT_UP:
xOffset += 0.0f * TILE_SIZE;
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_RIGHT:
xOffset += 0.0f * TILE_SIZE;
xOffset += 0.0f * TILE_SIZE - (TILE_SIZE / 2);
yOffset += 0.18f * TILE_SIZE; //FIX THIS
break;
case ORIENT_RIGHT_DOWN:
@@ -220,8 +245,8 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
yOffset += 0.0f * TILE_SIZE;
break;
case ORIENT_DOWN:
xOffset += 0.18f * TILE_SIZE + (TILE_SIZE / 4);
yOffset += 0.0f * TILE_SIZE;
xOffset += 0.18f * TILE_SIZE + (TILE_SIZE / 8);
yOffset += 0.0f * TILE_SIZE - (TILE_SIZE / 2);
break;
default:
break;
@@ -275,7 +300,7 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
}
}
void putItem(int x, int y, uint16_t itemType, uint8_t lane) {
void putItem(int x, int y, ItemType itemType, uint8_t lane) {
tileMap[y][x].items[lane].type = itemType;
tileMap[y][x].items[lane].offset = 0;
tileMap[y][x].items[lane].tileX = x;
@@ -283,33 +308,29 @@ void putItem(int x, int y, uint16_t itemType, uint8_t lane) {
}
void loadItems(SDL_Renderer *renderer) {
for (int i = 0; i < tileTypeIndex; i++) {
TileType tile = TileRegistry[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);
}
ItemRegistry[itemRegistryIndex].type = itemRegistryIndex;
ItemRegistry[itemRegistryIndex].isTile = true;
itemRegistryIndex++;
}
itemRegistryIndex = ITEMREGISTRY_SIZE / 2;
iterateSortedDir("./assets/items", (DirEntryCallback) registerItem, renderer);
// DIR *dir = opendir("./assets/items");
// if (dir) {
// struct dirent *entry;
// while ((entry = readdir(dir)) != NULL) {
// if (entry->d_name[0] == '.') {
// continue;
// }
// registerItem(entry->d_name, mainRenderer);
// }
// }
}

View File

@@ -5,23 +5,46 @@
#include <SDL2/SDL.h>
#include "../util/util.h"
#include "../tiles/belt.h"
#include "../tiles/tile.h"
#ifndef FACTORYGAME_ITEM_H
#define FACTORYGAME_ITEM_H
#define ITEMREGISTRY_SIZE 20
#define ITEMREGISTRY_SIZE 128
typedef struct {
typedef enum ItemType {
TYPE_AIR = 0,
TYPE_BLOCK,
TYPE_BELT,
TYPE_FURNACE,
IRON_ORE = ITEMREGISTRY_SIZE / 2,
SILVER_ORE,
GOLD_ORE,
PLATINUM_ORE,
IRON_INGOT,
SILVER_INGOT,
GOLD_INGOT,
PLATINUM_INGOT,
LOG
} ItemType;
typedef struct ItemOnBelt {
float offset;
int tileX, tileY;
uint16_t type;
ItemType type;
} ItemOnBelt;
typedef struct {
uint16_t type;
typedef struct Item {
bool isTile;
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];
} Item;
@@ -39,5 +62,5 @@ extern uint8_t laneTarget;
extern double speed;
void putItem(int x, int y, uint16_t itemType, uint8_t lane);
void putItem(int x, int y, ItemType itemType, uint8_t lane);
#endif //FACTORYGAME_ITEM_H