mirror of
https://github.com/BreizhHardware/bloubloulespoissons.git
synced 2026-01-18 16:47:31 +01:00
Add fish with mory head
This commit is contained in:
@@ -7,13 +7,11 @@ set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_PREFIX_PATH "C:/SDL2")
|
||||
|
||||
find_package(SDL2 REQUIRED)
|
||||
include_directories(${SDL2_INCLUDE_DIRS})
|
||||
find_package(SDL2_image REQUIRED)
|
||||
|
||||
add_executable(bloubloulespoissons main.cpp
|
||||
fish.cpp
|
||||
fish.h
|
||||
decors.cpp
|
||||
decors.h)
|
||||
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})
|
||||
|
||||
# Lier SDL2
|
||||
target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES})
|
||||
add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp)
|
||||
|
||||
# Lier SDL2 et SDL2_image
|
||||
target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES} -lSDL2_image)
|
||||
|
||||
39
fish.cpp
39
fish.cpp
@@ -1,13 +1,24 @@
|
||||
//
|
||||
// Created by BreizhHardware on 13/09/2024.
|
||||
//
|
||||
|
||||
#include "fish.h"
|
||||
|
||||
constexpr int WINDOW_WIDTH = 800;
|
||||
constexpr int WINDOW_HEIGHT = 600;
|
||||
constexpr int ENV_WIDTH = 800;
|
||||
constexpr int ENV_HEIGHT = 600;
|
||||
Fish::Fish(int x, int y, int vx, int vy, int width, int height, SDL_Renderer* renderer, const char* imagePath)
|
||||
: x(x), y(y), vx(vx), vy(vy), width(width), height(height), texture(nullptr) {
|
||||
if(imagePath == nullptr) {
|
||||
return;
|
||||
}
|
||||
SDL_Surface* surface = IMG_Load(imagePath);
|
||||
if (surface) {
|
||||
texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||
SDL_FreeSurface(surface);
|
||||
} else {
|
||||
std::cerr << "Erreur de chargement de l'image: " << IMG_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Fish::~Fish() {
|
||||
if (texture) {
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
}
|
||||
|
||||
void Fish::move() {
|
||||
x += vx;
|
||||
@@ -37,11 +48,13 @@ void Fish::drawArrow(SDL_Renderer* renderer, int x, int y, int vx, int vy) {
|
||||
SDL_RenderDrawLines(renderer, points, 4);
|
||||
}
|
||||
|
||||
|
||||
void Fish::draw(SDL_Renderer* renderer) {
|
||||
SDL_Rect rect = { x, y, width, height };
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Rouge
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
|
||||
drawArrow(renderer, x + width / 2, y + height / 2, vx, vy);
|
||||
if (texture) {
|
||||
SDL_RenderCopy(renderer, texture, nullptr, &rect);
|
||||
} else {
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // Rouge
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
drawArrow(renderer, x + width / 2, y + height / 2, vx, vy); // Dessiner la flèche
|
||||
}
|
||||
21
fish.h
21
fish.h
@@ -1,15 +1,21 @@
|
||||
//
|
||||
// Created by BreizhHardware on 13/09/2024.
|
||||
//
|
||||
|
||||
#ifndef FISH_H
|
||||
#define FISH_H
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
const int ENV_WIDTH = 1600;
|
||||
const int ENV_HEIGHT = 1200;
|
||||
|
||||
|
||||
class Fish {
|
||||
public:
|
||||
Fish(int x, int y, int vx, int vy, int width, int height) : x(x), y(y), vx(vx), vy(vy), width(width), height(height) {}
|
||||
Fish(int x, int y, int vx, int vy, int width, int height, SDL_Renderer* renderer, const char* imagePath);
|
||||
~Fish();
|
||||
|
||||
void move();
|
||||
void draw(SDL_Renderer* renderer);
|
||||
@@ -19,8 +25,7 @@ private:
|
||||
int x, y;
|
||||
int vx, vy;
|
||||
int width, height;
|
||||
SDL_Texture* texture;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //FISH_H
|
||||
#endif //FISH_H
|
||||
BIN
img/mory.png
Normal file
BIN
img/mory.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.8 MiB |
43
main.cpp
43
main.cpp
@@ -1,4 +1,5 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
@@ -7,11 +8,6 @@
|
||||
#include "fish.h"
|
||||
#include "decors.h"
|
||||
|
||||
const int WINDOW_WIDTH = 800;
|
||||
const int WINDOW_HEIGHT = 600;
|
||||
const int ENV_WIDTH = 1600;
|
||||
const int ENV_HEIGHT = 1200;
|
||||
|
||||
std::mutex mtx;
|
||||
|
||||
// Fonction pour dessiner un fond en dégradé
|
||||
@@ -41,6 +37,13 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialiser SDL_image
|
||||
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
|
||||
std::cerr << "Erreur d'initialisation de SDL_image: " << IMG_GetError() << std::endl;
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Créer une fenêtre
|
||||
SDL_Window* window = SDL_CreateWindow("BloubBloub les poissons",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
@@ -53,7 +56,7 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Créer un renderer avec SDL_RENDERER_SOFTWARE
|
||||
// Créer un renderer
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (renderer == nullptr) {
|
||||
std::cerr << "Erreur de création du renderer: " << SDL_GetError() << std::endl;
|
||||
@@ -62,9 +65,32 @@ int main(int argc, char* argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Charger l'image de la texture pour les poissons
|
||||
SDL_Surface* fishSurface = IMG_Load("../img/mory.png");
|
||||
if (fishSurface == nullptr) {
|
||||
std::cerr << "Erreur de chargement de l'image: " << IMG_GetError() << std::endl;
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Texture* fishTexture = SDL_CreateTextureFromSurface(renderer, fishSurface);
|
||||
SDL_FreeSurface(fishSurface);
|
||||
if (fishTexture == nullptr) {
|
||||
std::cerr << "Erreur de création de la texture: " << SDL_GetError() << std::endl;
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::vector<Fish> fishes;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5);
|
||||
for (int i = 0; i < 90; ++i) {
|
||||
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5, renderer, nullptr);
|
||||
}
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
fishes.emplace_back(rand() % ENV_WIDTH, rand() % ENV_HEIGHT, rand() % 3 - 1, rand() % 3 - 1, 10, 5, renderer, "../img/mory.png");
|
||||
}
|
||||
|
||||
std::thread fishThread(updateFish, std::ref(fishes));
|
||||
@@ -135,6 +161,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
fishThread.join();
|
||||
SDL_DestroyTexture(fishTexture);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
Reference in New Issue
Block a user