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

@@ -8,6 +8,7 @@
#include "tile.h"
#include "../player/player.h"
#include "../items/item.h"
#include "../util/atlas.h"
void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Rect playerRect, SDL_Renderer *renderer) {
@@ -36,8 +37,10 @@ void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Rect player
adjustRect(&dst1, playerRect);
adjustRect(&dst2, playerRect);
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src1, &dst1);
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src2, &dst2);
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src1, &dst1); //TODO CONVERT TO ATLAS
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src2, &dst2); //TODO CONVERT TO ATLAS
// SDL_RenderCopyx(renderer, atlasTexture, &TileRegistry[tileType].atlasRects[dir], NULL);
// SDL_RenderCopyx(renderer, atlasTexture, &TileRegistry[tileType].atlasRects[dir], NULL);
} else {
int offset = scrollFrame % TILE_SIZE;
@@ -56,6 +59,9 @@ void renderBelt(int x, int y, int w, int h, OrientDirection dir, SDL_Rect player
// Rotate to make the belt vertical
// SDL_RenderCopyx(renderer, atlasTexture, &ItemRegistry[item.type].atlasRectsOnBelt[ORIENT_LEFT], NULL);
// SDL_RenderCopyx(renderer, atlasTexture, &ItemRegistry[item.type].atlasRectsOnBelt[ORIENT_LEFT], NULL);
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src1, &dst1);
SDL_RenderCopy(renderer, TileRegistry[tileType].textures[dir], &src2, &dst2);
}

View File

@@ -27,16 +27,19 @@ void updateFurnace(Tile *tile) {
if (tile->miscVal == 0) {
tile->audioCh = getAvailableChannel();
if (tile->audioCh < NUM_SYNTH_VOICES) {
audioData.synthVoices[tile->audioCh].volume = 1;
audioData.synthVoices[tile->audioCh].volume = 255;
audioData.synthVoices[tile->audioCh].phase = 0;
audioData.synthVoices[tile->audioCh].sourceRect.x = TILE_SIZE * tile->x;
audioData.synthVoices[tile->audioCh].sourceRect.y = TILE_SIZE * tile->y;
audioData.synthVoices[tile->audioCh].waveform = WAVE_TRIANGLE;
audioData.synthVoices[tile->audioCh].frequency = 99;
audioData.synthVoices[tile->audioCh].sourceRect.x = TILE_SIZE * tile->rect.x;
audioData.synthVoices[tile->audioCh].sourceRect.y = TILE_SIZE * tile->rect.y;
audioData.synthVoices[tile->audioCh].waveform = WAVE_SINE;
audioData.synthVoices[tile->audioCh].frequency = 200;
}
}
if (tile->audioCh < NUM_SYNTH_VOICES) {
audioData.synthVoices[tile->audioCh].frequency++;
printf("frq: %d\n", ++audioData.synthVoices[tile->audioCh].frequency);
}
if (audioData.synthVoices[tile->audioCh].volume < 255) {
audioData.synthVoices[tile->audioCh].volume++;
}
if (outItem->type == 0 && ++tile->miscVal >= targetOutItem.miscVal) {
if (tile->audioCh < NUM_SYNTH_VOICES) {

View File

@@ -2,15 +2,31 @@
// Created by bruno on 4/24/25.
//
#include <dirent.h>
#include "tile.h"
#include "../player/player.h"
#include "furnace.h"
#include "../util/atlas.h"
#include "../util/font.h"
int scrollFrame = 0;
unsigned long beltFrames = 0;
TileArray neededUpdates;
int add_tile(TileArray *arr, MiniRect t) {
if (arr->activeCount >= MAX_TILES) return 0;
arr->tiles[arr->activeCount] = t;
arr->activeCount++;
return arr->activeCount - 1;
}
void remove_tile(TileArray *arr, int index) {
if (index < 0 || index >= arr->activeCount) return;
arr->activeCount--;
arr->tiles[index] = arr->tiles[arr->activeCount]; // swap with last active
}
SDL_Texture *backgroundTexture;
SDL_Texture *tilesTexture;
SDL_Texture *itemsTexture;
@@ -30,8 +46,8 @@ void generateTestMap() {
for (int y = 0; y < DISPLAY_MAP_HEIGHT; y++) {
for (int x = 0; x < DISPLAY_MAP_WIDTH; x++) {
Tile tile = {0};
tile.x = x;
tile.y = y;
tile.rect.x = x;
tile.rect.y = y;
tileMap[y][x] = tile;
}
}
@@ -66,21 +82,19 @@ void registerTile(char name[20], SDL_Renderer *renderer) {
TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT] = createFlippedTexture(renderer, texture, SDL_FLIP_HORIZONTAL);
TileRegistry[tileTypeIndex].textures[ORIENT_UP] = createRotatedTexture(renderer, texture, 90);
TileRegistry[tileTypeIndex].textures[ORIENT_DOWN] = createRotatedTexture(renderer, texture, 270);
if (tileTypeIndex == 0) {
SDL_SetTextureAlphaMod(TileRegistry[0].textures[ORIENT_LEFT], 64);
SDL_SetTextureAlphaMod(TileRegistry[0].textures[ORIENT_RIGHT], 64);
SDL_SetTextureAlphaMod(TileRegistry[0].textures[ORIENT_UP], 64);
SDL_SetTextureAlphaMod(TileRegistry[0].textures[ORIENT_DOWN], 64);
}
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_LEFT], SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT], SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_UP], SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(TileRegistry[tileTypeIndex].textures[ORIENT_DOWN], SDL_BLENDMODE_BLEND);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_LEFT] = allocate_32x32(TileRegistry[tileTypeIndex].textures[ORIENT_LEFT], renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_RIGHT] = allocate_32x32(TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT], renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_UP] = allocate_32x32(TileRegistry[tileTypeIndex].textures[ORIENT_UP], renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_DOWN] = allocate_32x32(TileRegistry[tileTypeIndex].textures[ORIENT_DOWN], renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_LEFT] = allocate_32x32(
TileRegistry[tileTypeIndex].textures[ORIENT_LEFT], renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_RIGHT] = allocate_32x32(
TileRegistry[tileTypeIndex].textures[ORIENT_RIGHT], renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_UP] = allocate_32x32(TileRegistry[tileTypeIndex].textures[ORIENT_UP],
renderer);
TileRegistry[tileTypeIndex].atlasRects[ORIENT_DOWN] = allocate_32x32(
TileRegistry[tileTypeIndex].textures[ORIENT_DOWN], renderer);
TileRegistry[tileTypeIndex].type = tileTypeIndex;
TileRegistry[tileTypeIndex].breakTime = 15;
@@ -96,7 +110,8 @@ void registerBackgroundTile(char name[20], SDL_Renderer *renderer) {
SDL_Texture *texture = IMG_LoadTexture(renderer, texturePath);
BackgroundTileRegistry[backgroundTileTypeIndex].texture = texture;
SDL_SetTextureBlendMode(BackgroundTileRegistry[backgroundTileTypeIndex].texture, SDL_BLENDMODE_NONE);
BackgroundTileRegistry[backgroundTileTypeIndex].atlasRect = allocate_32x32(BackgroundTileRegistry[backgroundTileTypeIndex].texture, renderer);
BackgroundTileRegistry[backgroundTileTypeIndex].atlasRect = allocate_32x32(
BackgroundTileRegistry[backgroundTileTypeIndex].texture, renderer);
BackgroundTileRegistry[backgroundTileTypeIndex].type = backgroundTileTypeIndex;
@@ -128,6 +143,8 @@ void setupTiles() {
}
}
TileRegistry[TYPE_FURNACE].outputLane[FURNACE_OUTPUT_SLOT] = 1;
TileRegistry[TYPE_FURNACE].needsTicks = true;
TileRegistry[TYPE_BELT].needsTicks = true;
}
uint16_t getBreakTime(int type) {
@@ -186,8 +203,10 @@ void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
BackgroundTile bt = backgroundMap[y][x];
SDL_Texture *tex = BackgroundTileRegistry[bt.type].texture;
if (tex != NULL) {
SDL_RenderCopy(renderer, tex, NULL, &dstRect);
SDL_Rect atlRect = BackgroundTileRegistry[bt.type].atlasRect;
if (atlRect.w != 0 && atlRect.h != 0) {
SDL_RenderCopy(renderer, atlasTexture, &atlRect, &dstRect);
//SDL_RenderCopy(renderer, tex, NULL, &dstRect);
}
}
}
@@ -209,14 +228,21 @@ void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
Tile t = tileMap[y][x];
switch (t.type) {
case TYPE_AIR:
break;
case TYPE_BELT:
renderBelt(x, y, tileSize, tileSize, t.direction, playerRect, renderer);
break;
default: {
SDL_Rect atlRect = TileRegistry[t.type].atlasRects[t.direction];
SDL_Texture *tex = TileRegistry[t.type].textures[t.direction];
if (tex == NULL) tex = TileRegistry[t.type].textures[ORIENT_LEFT];
if (tex != NULL) {
SDL_RenderCopy(renderer, tex, NULL, &dstRect);
if (atlRect.w == 0 || atlRect.h == 0) {
tex = TileRegistry[t.type].textures[ORIENT_LEFT];
atlRect = TileRegistry[t.type].atlasRects[ORIENT_LEFT];
}
if (atlRect.w != 0 && atlRect.h != 0) {
//SDL_RenderCopy(renderer, tex, NULL, &dstRect);
SDL_RenderCopy(renderer, atlasTexture, &atlRect, &dstRect);
}
}
}
@@ -231,6 +257,18 @@ void renderAllTiles(SDL_Renderer *renderer, SDL_Rect playerRect) {
if (x < 0 || x >= MAP_WIDTH) continue;
Tile t = tileMap[y][x];
if (debugMode) {
char locChar[20];
sprintf(locChar, "X:%d\nY:%d", x, y);
SDL_Rect dstRect = {
.x = x * TILE_SIZE,
.y = y * TILE_SIZE,
.w = TILE_SIZE,
.h = TILE_SIZE
};
adjustRect(&dstRect, playerRect);
renderText(renderer, fonts[3], locChar, dstRect.x, dstRect.y);
}
if (t.type == TYPE_BELT || itemViewing) {
for (uint8_t lane = 0; lane < ItemSlotCount; lane++) {
if (t.items[lane].type != 0) {

View File

@@ -7,10 +7,9 @@
#include "../util/util.h"
#include "../items/item.h"
//#include "../items/item.h"
#define MAP_WIDTH 100
#define MAP_HEIGHT 100
#define MAP_WIDTH 1000
#define MAP_HEIGHT 1000
#define DISPLAY_MAP_WIDTH 60
#define DISPLAY_MAP_HEIGHT 31
@@ -20,6 +19,25 @@
#define DISPLAY_WIDTH DISPLAY_MAP_WIDTH * 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
typedef struct {
MiniRect tiles[MAX_TILES];
int activeCount;
} TileArray;
extern TileArray neededUpdates;
void remove_tile(TileArray *arr, int index);
int add_tile(TileArray *arr, MiniRect t);
extern SDL_Texture *tilesTexture;
extern SDL_Texture *itemsTexture;
extern SDL_Texture *backgroundTexture;
@@ -77,6 +95,7 @@ typedef struct TileTypeReg {
bool itemMoves;
bool allowedInItems[ItemSlotCount][ITEMREGISTRY_SIZE];
bool outputLane[ItemSlotCount];
bool needsTicks;
} TileTypeReg;
typedef struct BackgroundTileType {
@@ -106,8 +125,8 @@ typedef struct Tile {
int miscVal;
ItemOnBelt items[ItemSlotCount];
uint16_t audioCh;
int x;
int y;
MiniRect rect;
int neededUpdateIndex;
} Tile;