Skip to content

Commit

Permalink
fix level regeneration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpayne-dev committed Jul 24, 2022
1 parent 467d46e commit 486db35
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion MinecraftC/GUI/GenerateLevelScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void GenerateLevelScreenOnButtonClicked(GenerateLevelScreen * screen, Button * b
if (button->id == 3) {
MinecraftSetCurrentScreen(screen->minecraft, screen->generateLevel.parent);
} else {
LevelRegenerate(&screen->minecraft->level, button->id);
MinecraftRegenerateLevel(screen->minecraft, button->id);
MinecraftSetCurrentScreen(screen->minecraft, NULL);
MinecraftGrabMouse(screen->minecraft);
}
Expand Down
1 change: 0 additions & 1 deletion MinecraftC/GameSettings.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,4 @@ String GameSettingsGetSetting(GameSettings * settings, int setting) {
void GameSettingsDestroy(GameSettings * settings) {
StringFree(settings->file);
ListFree(settings->bindings);
free(settings);
}
5 changes: 2 additions & 3 deletions MinecraftC/Level/Level.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ void LevelCreate(Level * level, ProgressBarDisplay * progressBar, int size) {
}

void LevelRegenerate(Level * level, int size) {
free(level->blocks);
free(level->lightBlockers);
LevelGeneratorGenerate(&level->generator, 128 << size, 128 << size, level);
LevelDestroy(level);
LevelCreate(level, level->generator.progressBar, size);
}

void LevelSetData(Level * level, int w, int d, int h, uint8_t * blocks) {
Expand Down
30 changes: 21 additions & 9 deletions MinecraftC/Minecraft.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ void MinecraftCreate(Minecraft * minecraft, int width, int height, bool fullScre
WaterTextureCreate(waterTexture);
TextureManagerRegisterAnimation(&minecraft->textureManager, waterTexture);
FontRendererCreate(&minecraft->font, &minecraft->settings, "Default.png", &minecraft->textureManager);
LevelRendererCreate(&minecraft->levelRenderer, minecraft, &minecraft->textureManager);
glViewport(0, 0, minecraft->frameWidth, minecraft->frameHeight);
LevelCreate(&minecraft->level, &minecraft->progressBar, 1);
PlayerCreate(&minecraft->player, &minecraft->level);
Expand All @@ -90,19 +89,13 @@ void MinecraftCreate(Minecraft * minecraft, int width, int height, bool fullScre
PlayerData * player = &minecraft->player.player;
InputHandlerCreate(&player->input, &minecraft->settings);
for (int i = 0; i < 9; i++) {
if (player->inventory.slots[i] == -1) {
player->inventory.slots[i] = SessionDataAllowedBlocks[i];
}
player->inventory.slots[i] = SessionDataAllowedBlocks[i];
}
LevelRendererCreate(&minecraft->levelRenderer, minecraft, &minecraft->textureManager);
minecraft->levelRenderer.level = &minecraft->level;
LevelRendererCreate(&minecraft->levelRenderer, minecraft, &minecraft->level, &minecraft->textureManager);
LevelSetRenderer(&minecraft->level, &minecraft->levelRenderer);
LevelRendererRefresh(&minecraft->levelRenderer);
ParticleMangerCreate(&minecraft->particleManager, &minecraft->level, &minecraft->textureManager);
minecraft->level.particleEngine = &minecraft->particleManager;
for (int i = 0; i < 2; i++) {
minecraft->particleManager.particles[i] = ListClear(minecraft->particleManager.particles[i]);
}
HUDScreenCreate(&minecraft->hud, minecraft, minecraft->width, minecraft->height);
CheckGLError(minecraft, "Post startup");
}
Expand Down Expand Up @@ -131,6 +124,25 @@ void MinecraftSetCurrentScreen(Minecraft * minecraft, GUIScreen * screen) {
}
}

void MinecraftRegenerateLevel(Minecraft * minecraft, int size) {
LevelRegenerate(&minecraft->level, size);
PlayerCreate(&minecraft->player, &minecraft->level);
EntityResetPosition(&minecraft->player);
PlayerData * player = &minecraft->player.player;
InputHandlerCreate(&player->input, &minecraft->settings);
for (int i = 0; i < 9; i++) {
if (player->inventory.slots[i] == -1) {
player->inventory.slots[i] = SessionDataAllowedBlocks[i];
}
}
LevelRendererDestroy(&minecraft->levelRenderer);
LevelRendererCreate(&minecraft->levelRenderer, minecraft, &minecraft->level, &minecraft->textureManager);
LevelSetRenderer(&minecraft->level, &minecraft->levelRenderer);
LevelRendererRefresh(&minecraft->levelRenderer);
ParticleManagerDestroy(&minecraft->particleManager);
ParticleMangerCreate(&minecraft->particleManager, &minecraft->level, &minecraft->textureManager);
}

static void OnMouseClicked(Minecraft * minecraft, int button) {
PlayerData * player = &minecraft->player.player;

Expand Down
1 change: 1 addition & 0 deletions MinecraftC/Minecraft.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ void MinecraftCreate(Minecraft * minecraft, int width, int height, bool fullScre
void MinecraftSetCurrentScreen(Minecraft * minecraft, GUIScreen * screen);
void MinecraftRun(Minecraft * minecraft);
void MinecraftGrabMouse(Minecraft * minecraft);
void MinecraftRegenerateLevel(Minecraft * minecraft, int size);
void MinecraftPause(Minecraft * minecraft);
void MinecraftDestroy(Minecraft * minecraft);
5 changes: 0 additions & 5 deletions MinecraftC/Render/Chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ void ChunkSetAllDirty(Chunk * chunk) {

void ChunkDispose(Chunk * chunk) {
ChunkSetAllDirty(chunk);
ChunkDestroy(chunk);
}

int ChunkAppendLists(Chunk * chunk, int dataCache[], int count, int pass) {
Expand All @@ -81,10 +80,6 @@ void ChunkClip(Chunk * chunk, Frustum frustum) {
chunk->visible = FrustumContainsBox(frustum, chunk->x, chunk->y, chunk->z, chunk->x + chunk->width, chunk->y + chunk->height, chunk->z + chunk->depth);
}

void ChunkDestroy(Chunk * chunk) {
free(chunk);
}

int ChunkDistanceComparator(const void * a, const void * b) {
Chunk * ca = *(Chunk **)a;
Chunk * cb = *(Chunk **)b;
Expand Down
1 change: 0 additions & 1 deletion MinecraftC/Render/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ void ChunkSetAllDirty(Chunk * chunk);
void ChunkDispose(Chunk * chunk);
int ChunkAppendLists(Chunk * chunk, int dataCache[], int count, int pass);
void ChunkClip(Chunk * chunk, Frustum frustum);
void ChunkDestroy(Chunk * chunk);

int ChunkDistanceComparator(const void * a, const void * b);
int ChunkVisibleDistanceComparator(const void * a, const void * b);
5 changes: 2 additions & 3 deletions MinecraftC/Render/LevelRenderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "../Utilities/Log.h"
#include "../Utilities/OpenGL.h"

void LevelRendererCreate(LevelRenderer * renderer, Minecraft * minecraft, TextureManager * textures) {
void LevelRendererCreate(LevelRenderer * renderer, Minecraft * minecraft, Level * level, TextureManager * textures) {
*renderer = (LevelRenderer) {
.chunks = ListCreate(sizeof(Chunk *)),
.chunkDataCache = malloc(65536 * sizeof(int)),
Expand All @@ -15,6 +15,7 @@ void LevelRendererCreate(LevelRenderer * renderer, Minecraft * minecraft, Textur
.textures = textures,
.listID = glGenLists(2),
.baseListID = glGenLists(4096 << 6 << 1),
.level = level,
};
}

Expand Down Expand Up @@ -171,11 +172,9 @@ void LevelRendererDestroy(LevelRenderer * renderer) {
free(renderer->chunkDataCache);
if (renderer->chunkCache != NULL) {
for (int i = 0; i < renderer->chunkCacheCount; i++) {
ChunkDestroy(renderer->chunkCache[i]);
free(renderer->chunkCache[i]);
}
free(renderer->chunkCache);
}
if (renderer->loadQueue != NULL) { free(renderer->loadQueue); }
free(renderer);
}
2 changes: 1 addition & 1 deletion MinecraftC/Render/LevelRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct LevelRenderer {
float lastLoadX, lastLoadY, lastLoadZ;
} LevelRenderer;

void LevelRendererCreate(LevelRenderer * renderer, struct Minecraft * minecraft, TextureManager * textures);
void LevelRendererCreate(LevelRenderer * renderer, struct Minecraft * minecraft, Level * level, TextureManager * textures);
void LevelRendererRefresh(LevelRenderer * renderer);
int LevelRendererSortChunks(LevelRenderer * renderer, Player * player, int pass);
void LevelRendererQueueChunks(LevelRenderer * renderer, int x0, int y0, int z0, int x1, int y1, int z1);
Expand Down

0 comments on commit 486db35

Please sign in to comment.