Add some décoration (Reef, Kelp and Rocks)

This commit is contained in:
2024-09-13 16:59:39 +02:00
parent 6159bb9ad8
commit 68ca09008d
4 changed files with 113 additions and 9 deletions

View File

@@ -11,7 +11,9 @@ include_directories(${SDL2_INCLUDE_DIRS})
add_executable(bloubloulespoissons main.cpp
fish.cpp
fish.h)
fish.h
decors.cpp
decors.h)
# Lier SDL2
target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES})

44
decors.cpp Normal file
View File

@@ -0,0 +1,44 @@
//
// Created by BreizhHardware on 13/09/2024.
//
#include "decors.h"
void Rock::draw(SDL_Renderer* renderer) {
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_RenderDrawPoint(renderer, x + dx, y + dy);
}
}
}
}
Reef::Reef(int x, int y) : x(x), y(y) {
std::srand(std::time(0));
int numRocks = 2 + std::rand() % 7;
for (int i = 0; i < numRocks; i++) {
int size = 50 + std::rand() % 51;
int r = 46 + std::rand() % 47;
int g = 45 + std::rand() % 40;
int b = 45 + std::rand() % 26;
rocks.emplace_back(x + std::rand() % 100, y + std::rand() % 100, size, r, g, b);
}
}
void Reef::draw(SDL_Renderer* renderer) {
for (auto& rock : rocks) {
rock.draw(renderer);
}
}
void Kelp::draw(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_Rect kelpRect = { x, y, size / 3, size };
SDL_RenderFillRect(renderer, &kelpRect);
}

53
decors.h Normal file
View File

@@ -0,0 +1,53 @@
//
// Created by BreizhHardware on 13/09/2024.
//
#ifndef DECORS_H
#define DECORS_H
#include <SDL_render.h>
#include <ctime>
#include <stdlib.h>
#include <vector>
class Rock {
public:
Rock(int x, int y, int size, int r, int g, int b) : x(x), y(y), size(size), r(r), g(g), b(b) {}
void draw(SDL_Renderer* renderer);
private:
int x;
int y;
int size;
int r;
int g;
int b;
};
class Reef {
public:
Reef(int x, int y);
void draw(SDL_Renderer* renderer);
private:
int x;
int y;
int size;
std::vector<Rock> rocks;
};
class Kelp {
public:
Kelp(int x, int y, int size, int r, int g, int b) : x(x), y(y), size(size), r(r), g(g), b(b) {}
void draw(SDL_Renderer* renderer);
private:
int x;
int y;
int size;
int r;
int g;
int b;
};
#endif //DECORS_H

View File

@@ -5,11 +5,12 @@
#include <mutex>
#include "fish.h"
#include "decors.h"
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int ENV_WIDTH = 800;
const int ENV_HEIGHT = 600;
const int ENV_WIDTH = 1600;
const int ENV_HEIGHT = 1200;
std::mutex mtx;
@@ -62,12 +63,16 @@ int main(int argc, char* argv[]) {
}
std::vector<Fish> fishes;
for (int i = 0; i < 1000; ++i) {
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5); // Largeur 10, Hauteur 5
for (int i = 0; i < 100; ++i) {
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5);
}
std::thread fishThread(updateFish, std::ref(fishes));
Rock rock(100, 100, 50, 255, 0, 0);
Reef reef(300, 300);
Kelp kelp(500, 500, 100, 4, 87, 0);
bool running = true;
SDL_Event event;
@@ -75,10 +80,6 @@ int main(int argc, char* argv[]) {
int playerY = WINDOW_HEIGHT / 2;
const int playerSpeed = 5;
Uint32 startTime = SDL_GetTicks();
int frameCount = 0;
int fps = 0;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
@@ -121,6 +122,10 @@ int main(int argc, char* argv[]) {
fish.draw(renderer);
}
rock.draw(renderer);
reef.draw(renderer);
kelp.draw(renderer);
// Dessiner le joueur
SDL_Rect playerRect = { playerX - offsetX, playerY - offsetY, 20, 20 };
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Blanc