Add coral to world gen

This commit is contained in:
2024-11-09 19:42:55 +01:00
parent b73f71bffb
commit d1029c745a
5 changed files with 53 additions and 6 deletions

View File

@@ -35,7 +35,20 @@ void Kelp::draw(SDL_Renderer* renderer) const {
}
}
void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&rocks, int envHeight, int envWidth , SDL_Renderer* renderer) {
void Coral::draw(SDL_Renderer* renderer) const {
Camera& camera = Camera::getInstance();
int cameraX = camera.getX();
int cameraY = camera.getY();
SDL_Rect coralRect = { x - cameraX, y - cameraY, size, size };
if (texturesVector[2] != nullptr) {
SDL_RenderCopy(renderer, texturesVector[2], nullptr, &coralRect);
} else {
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderFillRect(renderer, &coralRect);
}
}
void generateProceduralDecorations(std::vector<Kelp>& kelps, std::vector<Rock>& rocks, std::vector<Coral>& corals,int envHeight, int envWidth, SDL_Renderer* renderer) {
std::srand(std::time(0));
//Generate Kelp
@@ -61,4 +74,15 @@ void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&ro
int b = 100 + std::rand() % 156;
rocks.emplace_back(Rock(x, y, size, r, g, b, renderer));
}
int numCoral = 10 + std::rand() % 10;
for (int i = 0; i < numCoral; i++) {
int x = std::rand() % envWidth;
int y = envHeight - (std::rand() % static_cast<int>(envHeight * 0.2));
int size = 250 + std::rand() % 20;
int r = 100 + std::rand() % 156;
int g = 100 + std::rand() % 156;
int b = 100 + std::rand() % 156;
corals.emplace_back(Coral(x, y, size, r, g, b, renderer));
}
}

View File

@@ -38,7 +38,18 @@ private:
Uint8 b;
};
void generateProceduralDecorations(std::vector<Kelp>& kelps, std::vector<Rock>& rocks, int envHeight, int envWidth, SDL_Renderer* renderer);
class Coral {
public:
Coral(int x, int y, int size, Uint8 r, Uint8 g, Uint8 b, SDL_Renderer* renderer) : x(x), y(y), size(size), r(r), g(g), b(b) {};
void draw(SDL_Renderer* renderer) const;
int x;
int y;
private:
int size;
Uint8 r, g, b;
};
void generateProceduralDecorations(std::vector<Kelp>& kelps, std::vector<Rock>& rocks, std::vector<Coral>& corals,int envHeight, int envWidth, SDL_Renderer* renderer);
#endif //DECORS_H

View File

@@ -72,5 +72,12 @@ std::vector<SDL_Texture*> initTexture(SDL_Renderer* renderer) {
}
textures.push_back(SDL_CreateTextureFromSurface(renderer, rockSurface));
SDL_FreeSurface(rockSurface);
//Load the coral texture
SDL_Surface* coralSurface = IMG_Load("../img/coral.png");
if (coralSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image du Coral: " << IMG_GetError() << std::endl;
}
textures.push_back(SDL_CreateTextureFromSurface(renderer, coralSurface));
return textures;
}

BIN
img/coral.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 KiB

View File

@@ -23,7 +23,7 @@ std::vector<Fish> school;
bool initSDL();
void handleQuit();
void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks);
void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks, const std::vector<Coral>& corals);
void cleanup();
void updateFishRange(std::vector<Fish>& school, int start, int end, int id){
@@ -168,7 +168,8 @@ int main(int argc, char* args[]) {
std::vector<Kelp> kelps;
std::vector<Rock> rocks;
generateProceduralDecorations(kelps, rocks, ENV_HEIGHT, ENV_WIDTH, renderer);
std::vector<Coral> corals;
generateProceduralDecorations(kelps, rocks, corals,ENV_HEIGHT, ENV_WIDTH, renderer);
for (int i = 0; i < FISH_NUMBER ; ++i) {
school.emplace_back(Fish(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, 0.1, 0.1, school, i, 50, 50, renderer, rand() % 2 == 0 ? 1 : 0, fishTextures[rand() % fishCount]));
@@ -191,7 +192,7 @@ int main(int argc, char* args[]) {
while (running) {
handleQuit();
renderScene(player, kelps, rocks);
renderScene(player, kelps, rocks, corals);
SDL_Delay(10);
}
running = false;
@@ -219,7 +220,7 @@ void handleQuit() {
}
void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks) {
void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks, const std::vector<Coral>& corals) {
static Uint32 lastTime = 0;
static int frameCount = 0;
static int fps = 0;
@@ -247,6 +248,10 @@ void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vecto
rock.draw(renderer);
}
for (const auto& coral : corals) {
coral.draw(renderer);
}
std::lock_guard<std::mutex> lock(mtx);
for (Fish& fish : school) {
fish.draw(renderer);