Procedural truc ta race

This commit is contained in:
2024-11-08 09:46:10 +01:00
parent b9d6599a33
commit a4c33e757d
6 changed files with 26 additions and 23 deletions

View File

@@ -11,17 +11,12 @@ void Rock::draw(SDL_Renderer* renderer) const{
Camera& camera = Camera::getInstance();
int cameraX = camera.getX();
int cameraY = camera.getY();
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
for (int w = 0; w < size * 2; w++) {
for (int h = 0; h < size * 2; h++) {
int dx = size - w;
int dy = size - h;
if ((dx * dx + dy * dy) <= (size * size)) {
SDL_Rect rect = { x + dx - cameraX, y + dy - cameraY, 1, 1 };
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_Rect rockRect = { x - cameraX, y - cameraY, size, size };
if (texturesVector[1] != nullptr) {
SDL_RenderCopy(renderer, texturesVector[1], nullptr, &rockRect);
} else {
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderFillRect(renderer, &rockRect);
}
//std::cout << "Rock drawn at (" << x << ", " << y << ")" << std::endl;
}
@@ -47,8 +42,8 @@ void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&ro
int numKelps = 10 + std::rand() % 20;
for (int i = 0; i < numKelps; i++) {
int x = std::rand() % envWidth;
int y = envHeight - (std::rand() % (envHeight / 10));
int size = 50 + std::rand() % 100;
int y = envHeight - (std::rand() % static_cast<int>(envHeight * 0.2));
int size = 250 + std::rand() % 100;
Uint8 r = 4;
Uint8 g = 87;
Uint8 b = 0;
@@ -56,14 +51,14 @@ void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&ro
}
//Generate Rocks
int numRocks = 10 + std::rand() % 20;
int numRocks = 10 + std::rand() % 10;
for (int i = 0; i < numRocks; i++) {
int x = std::rand() % envWidth;
int y = envHeight - (std::rand() % (envHeight / 10));
int size = 10 + std::rand() % 20;
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;
rocks.emplace_back(Rock(x, y, size, r, g, b));
rocks.emplace_back(Rock(x, y, size, r, g, b, renderer));
}
}

View File

@@ -13,15 +13,15 @@
class Rock {
public:
Rock(int x, int y, int size, Uint8 r, Uint8 g, Uint8 b) : x(x), y(y), size(size), r(r), g(g), b(b) {};
Rock(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;
int r;
int g;
int b;
Uint8 r;
Uint8 g;
Uint8 b;
};
class Kelp {

12
env.cpp
View File

@@ -18,7 +18,7 @@ int fishCount = 0;
std::vector<SDL_Texture*> texturesVector;
bool initEnvironment(SDL_Renderer* renderer) {
SDL_Surface* backgroundSurface = IMG_Load("../img/background.jpg");
SDL_Surface* backgroundSurface = IMG_Load("../img/background.png");
if (backgroundSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image de fond: " << IMG_GetError() << std::endl;
return false;
@@ -56,12 +56,20 @@ bool initEnvironment(SDL_Renderer* renderer) {
std::vector<SDL_Texture*> initTexture(SDL_Renderer* renderer) {
std::vector<SDL_Texture*> textures;
// Load the Kelp texture
//Load the Kelp texture
SDL_Surface* kelpSurface = IMG_Load("../img/kelp.png");
if (kelpSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image du Kelp: " << IMG_GetError() << std::endl;
}
textures.push_back(SDL_CreateTextureFromSurface(renderer, kelpSurface));
SDL_FreeSurface(kelpSurface);
//Load the rock texture
SDL_Surface* rockSurface = IMG_Load("../img/rock.png");
if (rockSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image du Rock: " << IMG_GetError() << std::endl;
}
textures.push_back(SDL_CreateTextureFromSurface(renderer, rockSurface));
SDL_FreeSurface(rockSurface);
return textures;
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 MiB

BIN
img/background.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 MiB

BIN
img/rock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB