This commit is contained in:
2025-06-11 15:42:06 +02:00
parent 39c31f0042
commit 78bccd6c6f
30 changed files with 67 additions and 21 deletions

View File

Before

Width:  |  Height:  |  Size: 763 B

After

Width:  |  Height:  |  Size: 763 B

View File

Before

Width:  |  Height:  |  Size: 777 B

After

Width:  |  Height:  |  Size: 777 B

View File

Before

Width:  |  Height:  |  Size: 785 B

After

Width:  |  Height:  |  Size: 785 B

View File

Before

Width:  |  Height:  |  Size: 780 B

After

Width:  |  Height:  |  Size: 780 B

View File

Before

Width:  |  Height:  |  Size: 780 B

After

Width:  |  Height:  |  Size: 780 B

View File

Before

Width:  |  Height:  |  Size: 839 B

After

Width:  |  Height:  |  Size: 839 B

View File

Before

Width:  |  Height:  |  Size: 846 B

After

Width:  |  Height:  |  Size: 846 B

View File

Before

Width:  |  Height:  |  Size: 853 B

After

Width:  |  Height:  |  Size: 853 B

View File

Before

Width:  |  Height:  |  Size: 841 B

After

Width:  |  Height:  |  Size: 841 B

View File

Before

Width:  |  Height:  |  Size: 799 B

After

Width:  |  Height:  |  Size: 799 B

View File

Before

Width:  |  Height:  |  Size: 801 B

After

Width:  |  Height:  |  Size: 801 B

View File

Before

Width:  |  Height:  |  Size: 772 B

After

Width:  |  Height:  |  Size: 772 B

View File

Before

Width:  |  Height:  |  Size: 777 B

After

Width:  |  Height:  |  Size: 777 B

View File

Before

Width:  |  Height:  |  Size: 785 B

After

Width:  |  Height:  |  Size: 785 B

View File

Before

Width:  |  Height:  |  Size: 780 B

After

Width:  |  Height:  |  Size: 780 B

View File

Before

Width:  |  Height:  |  Size: 839 B

After

Width:  |  Height:  |  Size: 839 B

View File

Before

Width:  |  Height:  |  Size: 846 B

After

Width:  |  Height:  |  Size: 846 B

View File

Before

Width:  |  Height:  |  Size: 853 B

After

Width:  |  Height:  |  Size: 853 B

View File

Before

Width:  |  Height:  |  Size: 799 B

After

Width:  |  Height:  |  Size: 799 B

View File

Before

Width:  |  Height:  |  Size: 801 B

After

Width:  |  Height:  |  Size: 801 B

View File

Before

Width:  |  Height:  |  Size: 772 B

After

Width:  |  Height:  |  Size: 772 B

View File

@@ -25,8 +25,9 @@ bool putOntoNext(ItemOnBelt *itm, int nx, int ny, Tile *next, TileTypeReg *ntt,
if (next->items[newLane].type == 0 && (*ntt).allowedInItems[newLane][itm->type]) { if (next->items[newLane].type == 0 && (*ntt).allowedInItems[newLane][itm->type]) {
// MOVE it // MOVE it
ItemOnBelt moved = *itm; ItemOnBelt moved = *itm;
moved.tileX = nx; moved.prevTile = moved.tile;
moved.tileY = ny; moved.tile.x = nx;
moved.tile.y = ny;
if (!(*ntt).itemMoves) { if (!(*ntt).itemMoves) {
moved.offset = 0.5f; moved.offset = 0.5f;
} }
@@ -47,6 +48,12 @@ OrientDirection rotateMainDirection(OrientDirection dir, int steps) {
// The main directions indices array // The main directions indices array
int mainDirs[] = {1, 3, 5, 7}; int mainDirs[] = {1, 3, 5, 7};
if (steps == -1) {
mainDirs[1] = 7;
mainDirs[3] = 3;
mainDirs[0] = 5;
mainDirs[2] = 1;
}
int count = 4; int count = 4;
// Find index of dir in mainDirs // Find index of dir in mainDirs
@@ -153,6 +160,10 @@ void updateItems() {
if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT) if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT)
continue; continue;
if (nx == itm->prevTile.x && ny == itm->prevTile.y) {
continue;
}
Tile *next = &tileMap[ny][nx]; Tile *next = &tileMap[ny][nx];
TileTypeReg ntt = TileRegistry[next->type]; TileTypeReg ntt = TileRegistry[next->type];
@@ -181,6 +192,9 @@ void updateItems() {
continue; continue;
} }
Tile *next = &tileMap[ny][nx]; Tile *next = &tileMap[ny][nx];
if (nx == itm->prevTile.x && ny == itm->prevTile.y) {
continue;
}
TileTypeReg ntt = TileRegistry[next->type]; TileTypeReg ntt = TileRegistry[next->type];
int newLane = lane; int newLane = lane;
switch (next->type) { switch (next->type) {
@@ -252,11 +266,11 @@ uint8_t laneTarget = 0;
void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect playerRect) { void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect playerRect) {
SDL_Rect rect = {0}; SDL_Rect rect = {0};
rect.x = item.tileX * TILE_SIZE; rect.x = item.tile.x * TILE_SIZE;
rect.y = item.tileY * TILE_SIZE; rect.y = item.tile.y * TILE_SIZE;
// get raw direction code // get raw direction code
int dir = tileMap[item.tileY][item.tileX].direction; int dir = tileMap[item.tile.y][item.tile.x].direction;
//--- 1) build a unit vector (dxf, dyf) for this orientation //--- 1) build a unit vector (dxf, dyf) for this orientation
float dxf = 0.0f, dyf = 0.0f; float dxf = 0.0f, dyf = 0.0f;
@@ -370,13 +384,13 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
char tempStr[50]; char tempStr[50];
SDL_Rect rectA = {0}; SDL_Rect rectA = {0};
if (debugMode) { if (debugMode) {
SDL_Rect tileArea = {item.tileX * TILE_SIZE, item.tileY * TILE_SIZE, SDL_Rect tileArea = {item.tile.x * TILE_SIZE, item.tile.y * TILE_SIZE,
TILE_SIZE, TILE_SIZE}; TILE_SIZE, TILE_SIZE};
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 32);
adjustRect(&tileArea, playerRect); adjustRect(&tileArea, playerRect);
SDL_RenderFillRect(renderer, &tileArea); SDL_RenderFillRect(renderer, &tileArea);
rectA.x = item.tileX * TILE_SIZE; rectA.x = item.tile.x * TILE_SIZE;
rectA.y = item.tileY * TILE_SIZE; rectA.y = item.tile.y * TILE_SIZE;
rectA.w = TILE_SIZE; rectA.w = TILE_SIZE;
rectA.h = TILE_SIZE; rectA.h = TILE_SIZE;
sprintf(tempStr, "L%d\n%f\n%f\n%f", lane, item.offset, xOffset, yOffset); sprintf(tempStr, "L%d\n%f\n%f\n%f", lane, item.offset, xOffset, yOffset);
@@ -404,8 +418,9 @@ void renderItem(ItemOnBelt item, SDL_Renderer *renderer, int lane, SDL_Rect play
void putItem(int x, int y, ItemType 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].type = itemType;
tileMap[y][x].items[lane].offset = 0; tileMap[y][x].items[lane].offset = 0;
tileMap[y][x].items[lane].tileX = x; tileMap[y][x].items[lane].tile.x = x;
tileMap[y][x].items[lane].tileY = y; tileMap[y][x].items[lane].tile.y = y;
tileMap[y][x].items[lane].prevTile = tileMap[y][x].items[lane].tile;
} }
void loadItems(SDL_Renderer *renderer) { void loadItems(SDL_Renderer *renderer) {

View File

@@ -40,7 +40,8 @@ typedef enum ItemType {
typedef struct ItemOnBelt { typedef struct ItemOnBelt {
float offset; float offset;
int tileX, tileY; MiniRect tile;
MiniRect prevTile;
ItemType type; ItemType type;
} ItemOnBelt; } ItemOnBelt;

2
main.c
View File

@@ -244,7 +244,7 @@ int render() {
renderPlayer(&player); renderPlayer(&player);
if (!renderAtlas) { if (renderAtlas == 0) {
SDL_RenderCopy(mainRenderer, backgroundTexture, &screenRect, &screenRect); SDL_RenderCopy(mainRenderer, backgroundTexture, &screenRect, &screenRect);
SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect); SDL_RenderCopy(mainRenderer, tilesTexture, &screenRect, &screenRect);
SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect); SDL_RenderCopy(mainRenderer, itemsTexture, &screenRect, &screenRect);

View File

@@ -7,6 +7,7 @@
#include "../tiles/tile.h" #include "../tiles/tile.h"
#include "../util/font.h" #include "../util/font.h"
#include "../util/atlas.h" #include "../util/atlas.h"
#include "../entity/entity.h"
#define HEALTH_MARGIN 4 #define HEALTH_MARGIN 4
@@ -22,6 +23,7 @@ SDL_Rect PlayerRect;
SDL_Rect playerTextureRect; SDL_Rect playerTextureRect;
SDL_Rect targetItemBGRect; SDL_Rect targetItemBGRect;
SDL_Rect waveInfoBGRect;
SDL_Rect targetItemRect; SDL_Rect targetItemRect;
@@ -142,6 +144,11 @@ void initPlayer(Player *plr) {
targetItemBGRect.x = 0; targetItemBGRect.x = 0;
targetItemBGRect.y = DISPLAY_HEIGHT - TILE_SIZE - fonts[2].size * 2; targetItemBGRect.y = DISPLAY_HEIGHT - TILE_SIZE - fonts[2].size * 2;
waveInfoBGRect.y = 0;
waveInfoBGRect.x = 0;
waveInfoBGRect.w = 380;
waveInfoBGRect.h = 80;
targetItemRect.w = TILE_SIZE; targetItemRect.w = TILE_SIZE;
targetItemRect.h = TILE_SIZE; targetItemRect.h = TILE_SIZE;
@@ -249,6 +256,18 @@ void renderPlayer(Player *plr) {
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
SDL_RenderFillRect(mainRenderer, &targetItemBGRect); SDL_RenderFillRect(mainRenderer, &targetItemBGRect);
SDL_RenderFillRect(mainRenderer, &waveInfoBGRect);
char hudStr[50];
char waveStr[30];
if (entities.activeCount > 0) {
snprintf(waveStr, 30, "Remaining enemies: %d", entities.activeCount);
} else {
snprintf(waveStr, 30, "Next wave in: %dm %ds", waveInfo.waveTimer / 60, waveInfo.waveTimer % 60);
}
snprintf(hudStr, 30, "Wave: %d\n%s\n", waveInfo.waveCounter, waveStr);
renderText(mainRenderer, fonts[1], hudStr, 0,0);
targetItemRect.y = DISPLAY_HEIGHT - TILE_SIZE; targetItemRect.y = DISPLAY_HEIGHT - TILE_SIZE;
targetItemRect.x = TILE_SIZE / 4; targetItemRect.x = TILE_SIZE / 4;

View File

@@ -20,11 +20,6 @@
#define DISPLAY_WIDTH DISPLAY_MAP_WIDTH * TILE_SIZE #define DISPLAY_WIDTH DISPLAY_MAP_WIDTH * TILE_SIZE
#define DISPLAY_HEIGHT DISPLAY_MAP_HEIGHT * TILE_SIZE #define DISPLAY_HEIGHT DISPLAY_MAP_HEIGHT * TILE_SIZE
typedef struct MiniRect {
int x;
int y;
} MiniRect;
#define MAX_TILES MAP_WIDTH * MAP_HEIGHT #define MAX_TILES MAP_WIDTH * MAP_HEIGHT

View File

@@ -5,8 +5,8 @@
#ifndef FACTORYGAME_ATLAS_H #ifndef FACTORYGAME_ATLAS_H
#define FACTORYGAME_ATLAS_H #define FACTORYGAME_ATLAS_H
#define ATLAS_SIZE 1024 #define ATLAS_SIZE 2048
#define MAX_RECTS 2048 #define MAX_RECTS 4096
#define TILE_SIZE 32 #define TILE_SIZE 32
#define QUADRANT_SIZE 16 #define QUADRANT_SIZE 16
#define ATLAS_TILES_PER_ROW (ATLAS_SIZE / TILE_SIZE) #define ATLAS_TILES_PER_ROW (ATLAS_SIZE / TILE_SIZE)

View File

@@ -90,8 +90,9 @@ void updateMachine(Tile *tile,
in1->type = TYPE_AIR; in1->type = TYPE_AIR;
if (in2->type != TYPE_AIR) in2->type = TYPE_AIR; if (in2->type != TYPE_AIR) in2->type = TYPE_AIR;
out->type = result; out->type = result;
out->tileX = tile->rect.x; out->prevTile = out->tile;
out->tileY = tile->rect.y; out->tile.x = tile->rect.x;
out->tile.y = tile->rect.y;
out->offset = -0.5f; out->offset = -0.5f;
} }
} }

View File

@@ -13,6 +13,8 @@
SDL_Window *window = NULL; SDL_Window *window = NULL;
volatile bool running = true; volatile bool running = true;
WaveInfo waveInfo;
const char OrientStrings[ORIENT_DIRECTION_COUNT][10] = { const char OrientStrings[ORIENT_DIRECTION_COUNT][10] = {
"LEFT_DOWN", "LEFT_DOWN",
"LEFT", "LEFT",

View File

@@ -44,6 +44,19 @@ typedef struct Animation {
} Animation; } Animation;
typedef struct MiniRect {
int x;
int y;
} MiniRect;
typedef struct WaveInfo {
int waveCounter;
int waveTimer;
} WaveInfo;
extern WaveInfo waveInfo;
typedef struct OrientedAnimation { typedef struct OrientedAnimation {
SDL_Rect atlasRects[ORIENT_DIRECTION_COUNT][TILE_SIZE * 2]; SDL_Rect atlasRects[ORIENT_DIRECTION_COUNT][TILE_SIZE * 2];
unsigned char frameCount; unsigned char frameCount;