Testing
This commit is contained in:
109
util/util.c
109
util/util.c
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <dirent.h>
|
||||
#include "util.h"
|
||||
#include "../tiles/tile.h"
|
||||
#include "font.h"
|
||||
//#include "font.h"
|
||||
|
||||
|
||||
@@ -132,7 +134,7 @@ void renderBar(SDL_Renderer *renderer,
|
||||
char barString[20];
|
||||
sprintf(barString, "%d/%d", currentValue, maxValue);
|
||||
|
||||
//renderText(mainRenderer, fonts[3], barString, width / 2, margin);
|
||||
renderText(mainRenderer, fonts[3], barString, width / 2, margin);
|
||||
}
|
||||
|
||||
int cmpstringp(const void *p1, const void *p2) {
|
||||
@@ -178,4 +180,109 @@ void iterateSortedDir(const char *path, DirEntryCallback callback, SDL_Renderer
|
||||
}
|
||||
free(names);
|
||||
}
|
||||
}
|
||||
|
||||
bool checkCollision(SDL_Rect a, SDL_Rect b) {
|
||||
//The sides of the rectangles
|
||||
int leftA, leftB;
|
||||
int rightA, rightB;
|
||||
int topA, topB;
|
||||
int bottomA, bottomB;
|
||||
|
||||
//Calculate the sides of tileRect A
|
||||
leftA = a.x;
|
||||
rightA = a.x + a.w;
|
||||
topA = a.y;
|
||||
bottomA = a.y + a.h;
|
||||
|
||||
//Calculate the sides of tileRect B
|
||||
leftB = b.x;
|
||||
rightB = b.x + b.w;
|
||||
topB = b.y;
|
||||
bottomB = b.y + b.h;
|
||||
|
||||
//If any of the sides from A are outside of B
|
||||
if (bottomA <= topB) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (topA >= bottomB) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rightA <= leftB) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (leftA >= rightB) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//If none of the sides from A are outside B
|
||||
return true;
|
||||
}
|
||||
|
||||
bool canMoveTo(SDL_Rect newRect) {
|
||||
// Round down to get all tiles the rect overlaps
|
||||
int left = newRect.x / TILE_SIZE;
|
||||
int right = (newRect.x + newRect.w - 1) / TILE_SIZE;
|
||||
int top = newRect.y / TILE_SIZE;
|
||||
int bottom = (newRect.y + newRect.h - 1) / TILE_SIZE;
|
||||
|
||||
for (int tx = left; tx <= right; ++tx) {
|
||||
for (int ty = top; ty <= bottom; ++ty) {
|
||||
MiniRect tile = { tx, ty };
|
||||
if (!isWalkable(tile)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool canMoveWithRadius(SDL_Rect centerRect) {
|
||||
// 16px radius — create a square bounding box around center
|
||||
int radius = 16;
|
||||
|
||||
int left = (centerRect.x - radius) / TILE_SIZE;
|
||||
int right = (centerRect.x + radius - 1) / TILE_SIZE;
|
||||
int top = (centerRect.y - radius) / TILE_SIZE;
|
||||
int bottom = (centerRect.y + radius - 1) / TILE_SIZE;
|
||||
|
||||
int x, y;
|
||||
for (x = left; x <= right; x++) {
|
||||
for (y = top; y <= bottom; y++) {
|
||||
MiniRect tile;
|
||||
tile.x = x;
|
||||
tile.y = y;
|
||||
|
||||
if (!isWalkable(tile)) {
|
||||
// Get pixel bounds of tile
|
||||
int tileLeft = x * TILE_SIZE;
|
||||
int tileRight = tileLeft + TILE_SIZE;
|
||||
int tileTop = y * TILE_SIZE;
|
||||
int tileBottom = tileTop + TILE_SIZE;
|
||||
|
||||
// Bounding box of the player
|
||||
int playerLeft = centerRect.x - radius;
|
||||
int playerRight = centerRect.x + radius;
|
||||
int playerTop = centerRect.y - radius;
|
||||
int playerBottom = centerRect.y + radius;
|
||||
|
||||
// AABB collision check
|
||||
if (playerRight > tileLeft && playerLeft < tileRight &&
|
||||
playerBottom > tileTop && playerTop < tileBottom) {
|
||||
return 0; // Collision
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1; // No collisions
|
||||
}
|
||||
|
||||
int compareStrings(const void *a, const void *b) {
|
||||
const char *strA = *(const char **) a;
|
||||
const char *strB = *(const char **) b;
|
||||
return strcmp(strA, strB);
|
||||
}
|
Reference in New Issue
Block a user