More progress on audio and rendering

This commit is contained in:
2025-06-02 18:04:15 +02:00
parent 84805b92cb
commit 0c3e2aa730
14 changed files with 569 additions and 264 deletions

View File

@@ -6,6 +6,7 @@
#include "player.h"
#include "../tiles/tile.h"
#include "../util/font.h"
#include "../util/atlas.h"
#define HEALTH_MARGIN 4
@@ -18,6 +19,7 @@ SDL_Texture *hudTexture;
SDL_Texture *PlayerTexture;
SDL_Rect PlayerRect;
SDL_Rect playerTextureRect;
SDL_Rect targetItemBGRect;
@@ -28,9 +30,6 @@ SDL_Color breakingBarColor = {128, 128, 0, 255};
void setActivePlayerSlot(Player *plr, ItemType activeSlotIndex) {
activeSlotIndex = activeSlotIndex % itemRegistryIndex;
if (activeSlotIndex <= 0) {
activeSlotIndex = 1;
}
plr->inventory.activeSlotIndex = activeSlotIndex;
}
@@ -46,11 +45,11 @@ void moveActivePlayerSlot(Player *plr, bool up, bool seek) {
// Stop if we found a slot with count > 0
if (!strlen(ItemRegistry[newSlot].name)) continue;
if (newSlot == 0) continue;
if (newSlot == 0) break;
if (seek) {
if (plr->inventory.slotCounts[newSlot] > 0) break;
} else {
break;
break;
}
} while (true);
@@ -104,6 +103,7 @@ void initPlayer(Player *plr) {
SDL_QueryTexture(PlayerTexture, NULL, NULL, &PlayerRect.w, &PlayerRect.h);
PlayerRect.x = (DISPLAY_WIDTH / 2) - (PlayerRect.w / 2);
PlayerRect.y = (DISPLAY_HEIGHT / 2) - (PlayerRect.h / 2);
playerTextureRect = allocate_32x32(PlayerTexture, mainRenderer);
plr->health = 64;
plr->healthIdle = 0;
@@ -112,12 +112,12 @@ void initPlayer(Player *plr) {
plr->rect.w = TILE_SIZE;
plr->rect.h = TILE_SIZE;
for (ItemType ui = 0; ui < tileTypeIndex; ui++) {
plr->inventory.slotCounts[ui] = 64;
for (ItemType ui = 1; ui < tileTypeIndex; ui++) {
plr->inventory.slotCounts[ui] = 65535;
}
for (ItemType ui = ITEMREGISTRY_SIZE / 2; ui < itemRegistryIndex; ui++) {
plr->inventory.slotCounts[ui] = 64;
plr->inventory.slotCounts[ui] = 65535;
}
hudTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, screenRect.w,
@@ -135,12 +135,12 @@ void initPlayer(Player *plr) {
plr->cursor.targetTileRect.h = TILE_SIZE;
targetItemBGRect.w = DISPLAY_WIDTH;
targetItemBGRect.h = TILE_SIZE;
targetItemBGRect.h = TILE_SIZE + fonts[2].size * 2;
targetItemBGRect.x = 0;
targetItemBGRect.y = DISPLAY_HEIGHT - 32;
targetItemBGRect.y = DISPLAY_HEIGHT - TILE_SIZE - fonts[2].size * 2;
targetItemRect.w = TILE_SIZE / 2;
targetItemRect.h = TILE_SIZE / 2;
targetItemRect.w = TILE_SIZE;
targetItemRect.h = TILE_SIZE;
plr->cursor.heldItemRect.w = TILE_SIZE;
plr->cursor.heldItemRect.h = TILE_SIZE;
@@ -164,7 +164,8 @@ void renderPlayer(Player *plr) {
SDL_SetRenderTarget(mainRenderer, entityTexture);
SDL_RenderClear(mainRenderer);
SDL_RenderCopy(mainRenderer, PlayerTexture, NULL, &PlayerRect);
SDL_RenderCopy(mainRenderer, atlasTexture, &playerTextureRect, &PlayerRect);
//SDL_RenderCopy(mainRenderer, PlayerTexture, NULL, &PlayerRect);
SDL_SetRenderTarget(mainRenderer, hudTexture);
SDL_RenderClear(mainRenderer);
@@ -172,29 +173,31 @@ void renderPlayer(Player *plr) {
DrawThickRect(mainRenderer, plr->cursor.targetTileRect, 4);
ItemType itemIndex = plr->inventory.activeSlotIndex;
SDL_Texture *itemTex;
//SDL_Texture *itemTex;
char itemStringCount[6];
if (itemIndex < itemRegistryIndex) {
if (itemIndex < itemRegistryIndex && itemIndex > 0) {
plr->cursor.heldItemRect.x = plr->cursor.windowX;
plr->cursor.heldItemRect.y = plr->cursor.windowY;
itemTex = ItemRegistry[itemIndex].textureOnBelt[plr->cursor.direction];
if (itemTex == NULL) {
itemTex = ItemRegistry[itemIndex].textureOnBelt[ORIENT_LEFT];
//itemTex = ItemRegistry[itemIndex].textureOnBelt[plr->cursor.direction];
SDL_Rect itemAtlasRect = ItemRegistry[itemIndex].atlasRects[plr->cursor.direction];
if (itemAtlasRect.w == 0 || itemAtlasRect.h == 0) {
itemAtlasRect = ItemRegistry[itemIndex].atlasRects[ORIENT_LEFT];
}
if (itemTex != NULL) {
if (itemAtlasRect.w != 0 && itemAtlasRect.h != 0) {
if (plr->inventory.slotCounts[itemIndex] <= 0) {
// Set a red tint (255, 0, 0)
SDL_SetTextureColorMod(itemTex, 128, 128, 255);
SDL_SetTextureAlphaMod(itemTex, 192);
} else {
SDL_SetTextureColorMod(itemTex, 255, 255, 255);
SDL_SetTextureAlphaMod(itemTex, 255);
}
SDL_RenderCopy(mainRenderer, itemTex, NULL,
&plr->cursor.heldItemRect);
// if (plr->inventory.slotCounts[itemIndex] <= 0) {
// // Set a red tint (255, 0, 0)
// SDL_SetTextureColorMod(itemTex, 128, 128, 255);
//
// SDL_SetTextureAlphaMod(itemTex, 192);
// } else {
// SDL_SetTextureColorMod(itemTex, 255, 255, 255);
//
// SDL_SetTextureAlphaMod(itemTex, 255);
// }
// SDL_RenderCopy(mainRenderer, itemTex, NULL,
// &plr->cursor.heldItemRect);
SDL_RenderCopy(mainRenderer, atlasTexture, &itemAtlasRect, &plr->cursor.heldItemRect);
renderText(mainRenderer, fonts[2], ItemRegistry[itemIndex].name, plr->cursor.heldItemRect.x,
plr->cursor.heldItemRect.y - (fonts[2].size * 3 / 2));
}
@@ -215,21 +218,22 @@ void renderPlayer(Player *plr) {
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
SDL_RenderFillRect(mainRenderer, &targetItemBGRect);
targetItemRect.y = DISPLAY_HEIGHT - 30 + TILE_SIZE / 4 + 3;
targetItemRect.y = DISPLAY_HEIGHT - TILE_SIZE;
targetItemRect.x = TILE_SIZE / 4;
for (ItemType i = 1; i < ITEMREGISTRY_SIZE; i++) {
itemTex = ItemRegistry[i].textureOnBelt[plr->cursor.direction];
if (itemTex == NULL) {
itemTex = ItemRegistry[i].textureOnBelt[ORIENT_LEFT];
SDL_SetTextureBlendMode(atlasTexture, SDL_BLENDMODE_ADD);
for (ItemType i = 0; i < ITEMREGISTRY_SIZE; i++) {
SDL_Rect itemAtlasRectd = ItemRegistry[i].atlasRects[plr->cursor.direction];
if (itemAtlasRectd.w == 0 || itemAtlasRectd.h == 0) {
itemAtlasRectd = ItemRegistry[i].atlasRects[ORIENT_LEFT];
}
if (itemTex != NULL) {
if (plr->inventory.slotCounts[i] <= 0) {
// Set a red tint (255, 0, 0)
SDL_SetTextureColorMod(itemTex, 128, 128, 255);
}
SDL_RenderCopy(mainRenderer, itemTex, NULL, &targetItemRect);
SDL_SetTextureColorMod(itemTex, 255, 255, 255);
if (itemAtlasRectd.w != 0 && itemAtlasRectd.h != 0) {
// if (plr->inventory.slotCounts[i] <= 0) {
// // Set a red tint (255, 0, 0)
// SDL_SetTextureColorMod(itemTex, 128, 128, 255);
// }
SDL_RenderCopy(mainRenderer, atlasTexture, &itemAtlasRectd, &targetItemRect);
//SDL_SetTextureColorMod(itemTex, 255, 255, 255);
if (plr->inventory.activeSlotIndex == i) {
SDL_SetRenderDrawColor(mainRenderer, 16, plr->inventory.slotCounts[i] > 0 ? 128 : 16,
@@ -240,9 +244,10 @@ void renderPlayer(Player *plr) {
renderText(mainRenderer, fonts[2], itemStringCount, targetItemRect.x - 3,
targetItemRect.y - (fonts[2].size * 3 / 2));
//targetItemRect.x += (TILE_SIZE / 2) + (TILE_SIZE / 4);
targetItemRect.x += TILE_SIZE;
targetItemRect.x += TILE_SIZE / 2 * 3;
}
}
SDL_SetTextureBlendMode(atlasTexture, SDL_BLENDMODE_BLEND);
SDL_SetRenderTarget(mainRenderer, originalTarget);
}