Add texture to kelp (bug load player texture)

This commit is contained in:
2024-11-08 09:08:54 +01:00
parent 91cfd31280
commit cd5e0ea05b
5 changed files with 32 additions and 14 deletions

View File

@@ -24,16 +24,34 @@ void Rock::draw(SDL_Renderer* renderer) const{
//std::cout << "Rock drawn at (" << x << ", " << y << ")" << std::endl;
}
void Kelp::draw(SDL_Renderer* renderer) const{
Kelp::Kelp(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), Kelptexture(nullptr) {
Kelptexture = IMG_LoadTexture(renderer, "../img/kelp.png");
if (Kelptexture == nullptr) {
std::cerr << "Failed to load kelp texture: " << SDL_GetError() << std::endl;
}
}
Kelp::~Kelp() {
if (Kelptexture != nullptr) {
SDL_DestroyTexture(Kelptexture);
}
}
void Kelp::draw(SDL_Renderer* renderer) const {
Camera& camera = Camera::getInstance();
int cameraX = camera.getX();
int cameraY = camera.getY();
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_Rect kelpRect = { x - cameraX, y - cameraY, size / 3, size };
SDL_RenderFillRect(renderer, &kelpRect);
if (Kelptexture != nullptr) {
SDL_RenderCopy(renderer, Kelptexture, nullptr, &kelpRect);
} else {
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderFillRect(renderer, &kelpRect);
}
}
void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&rocks, int envHeight, int envWidth) {
void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&rocks, int envHeight, int envWidth , SDL_Renderer* renderer) {
std::srand(std::time(0));
//Generate Kelp
@@ -45,7 +63,7 @@ void generateProceduralDecorations(std::vector<Kelp>&kelps, std::vector<Rock>&ro
Uint8 r = 4;
Uint8 g = 87;
Uint8 b = 0;
kelps.emplace_back(Kelp(x, y, size, r, g, b));
kelps.emplace_back(Kelp(x, y, size, r, g, b, renderer));
}
//Generate Rocks

View File

@@ -5,6 +5,7 @@
#ifndef DECORS_H
#define DECORS_H
#include <SDL_render.h>
#include <SDL2/SDL_image.h>
#include <ctime>
#include <cstdlib>
#include <vector>
@@ -25,19 +26,21 @@ private:
class Kelp {
public:
Kelp(int x, int y, int height, Uint8 r, Uint8 g, Uint8 b) : x(x), y(y), size(height), r(r), g(g), b(b) {};
Kelp(int x, int y, int size, Uint8 r, Uint8 g, Uint8 b, SDL_Renderer* renderer);
void draw(SDL_Renderer* renderer) const;
int x;
int y;
~Kelp();
private:
int size;
int r;
int g;
int b;
Uint8 r;
Uint8 g;
Uint8 b;
SDL_Texture* Kelptexture;
};
void generateProceduralDecorations(std::vector<Kelp>& kelps, std::vector<Rock>& rocks, int envHeight, int envWidth);
void generateProceduralDecorations(std::vector<Kelp>& kelps, std::vector<Rock>& rocks, int envHeight, int envWidth, SDL_Renderer* renderer);
#endif //DECORS_H

BIN
img/kelp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 471 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 48 KiB

View File

@@ -21,9 +21,6 @@ SDL_Texture* playerTexture = nullptr;
SDL_Texture* fishTextures[100]; // Adjust the size as needed
std::vector<Fish> school;
Rock rock(0, 0, 50, 255, 0, 0);
Kelp kelp(500, 500, 100, 4, 87, 0);
bool initSDL();
void handleQuit();
void renderScene(Player player, const std::vector<Kelp>& kelps, const std::vector<Rock>& rocks);
@@ -183,7 +180,7 @@ int main(int argc, char* args[]) {
std::vector<Kelp> kelps;
std::vector<Rock> rocks;
generateProceduralDecorations(kelps, rocks, ENV_HEIGHT, ENV_WIDTH);
generateProceduralDecorations(kelps, rocks, ENV_HEIGHT, ENV_WIDTH, renderer);
for (int i = 0; i < 1000; ++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]));