Compare commits

...

8 Commits

Author SHA1 Message Date
a471c88ccd Bump to SDL3 2024-11-05 13:27:26 +01:00
1a18c68fb1 Bump to SDL3 2024-11-05 13:26:27 +01:00
f11527202b Bump to SDL3 2024-11-05 13:25:46 +01:00
9f1ccf8736 Bump to SDL3 2024-11-05 13:25:19 +01:00
9625d67f77 Bump to SDL3 2024-11-05 13:23:43 +01:00
ccec0ef5ef Bump to SDL3 2024-11-05 13:22:02 +01:00
947318c448 Bump to SDL3 2024-11-05 13:18:30 +01:00
2e2e546f69 Bump to SDL3 2024-11-05 11:45:21 +01:00
5 changed files with 78 additions and 213 deletions

View File

@@ -6,18 +6,18 @@ set(CMAKE_CXX_STANDARD 17)
# Ajouter le chemin de la bibliothèque SDL2
set(CMAKE_PREFIX_PATH "C:/SDL2")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL3 REQUIRED)
find_package(SDL3_image REQUIRED)
find_package(SDL3_ttf REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS})
include_directories(${SDL3_INCLUDE_DIRS} ${SDL3_IMAGE_INCLUDE_DIRS} ${SDL3_TTF_INCLUDE_DIRS})
add_executable(bloubloulespoissons main.cpp fish.cpp decors.cpp
camera.cpp
camera.h)
# Lier SDL2 et SDL2_image
target_link_libraries(bloubloulespoissons ${SDL2_LIBRARIES} -lSDL2_image -lSDL2_ttf)
target_link_libraries(bloubloulespoissons ${SDL3_LIBRARIES} -lSDL3_image -lSDL3_ttf)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")

View File

@@ -13,8 +13,8 @@ LIBS := -lSDL2 -lSDL2_image -lSDL2_ttf
# Compiler and linker settings
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
CFLAGS := -g -O2 -Wall -mword-relocations $(ARCH) -I$(DEVKITPRO)/libctru/include -I$(INCLUDES)
LDFLAGS := -L$(DEVKITPRO)/libctru/lib $(LIBS)
CFLAGS := -g -O2 -Wall -mword-relocations $(ARCH) -I$(DEVKITPRO)/libctru/include -I$(DEVKITPRO)/portlibs/3ds/include -I$(DEVKITPRO)/portlibs/3ds/include/SDL
LDFLAGS := -L$(DEVKITPRO)/libctru/lib -L$(DEVKITPRO)/portlibs/3ds/lib -lSDL -lSDL_image -lSDL_ttf
# Rules
all: $(BUILD)/$(TARGET).3dsx

View File

@@ -4,7 +4,7 @@
#ifndef DECORS_H
#define DECORS_H
#include <SDL_render.h>
#include <SDL3/SDL_render.h>
#include <ctime>
#include <cstdlib>
#include <vector>

24
fish.h
View File

@@ -1,7 +1,7 @@
#ifndef FISH_H
#define FISH_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <cmath>
#include <iostream>
#include <string>
@@ -13,8 +13,6 @@ const int ENV_HEIGHT = 2160;
const int MARGIN_WIDTH = 100;
const int MARGIN_HEIGHT = 100;
class Fish {
private:
const int VISUAL_RANGE = 40;
@@ -26,28 +24,24 @@ private:
const float MAX_SPEED = 13;
const float MIN_SPEED = 0.6;
const float BIASVALUE = 0.001;
//const float MAXBIAS = 0.01;
float vx, vy;
std::vector<Fish> &school;
int id;
SDL_Texture* texture;
//int cycle_count = 0;
int width, height;
int biasdir = 1;
;
public:
Fish(const int x, const int y, const float vx, const float vy,std::vector<Fish> &school, const int id,const int width,const int height, SDL_Texture* texture, SDL_Renderer* renderer,int biasdir): x(x), y(y), vx(vx), vy(vy), school(school), id(id), width(width),height(height), texture(texture), biasdir(biasdir) {}
Fish(const int x, const int y, const float vx, const float vy, std::vector<Fish> &school, const int id, const int width, const int height, SDL_Texture* texture, SDL_Renderer* renderer, int biasdir)
: x(x), y(y), vx(vx), vy(vy), school(school), id(id), width(width), height(height), texture(texture), biasdir(biasdir) {}
~Fish() = default;
float getX() const { return x; };
float getY() const { return y; };
float getVx() const { return vx; };
float getVy() const { return vy; };
int getId() const { return id; };
float getX() const { return x; }
float getY() const { return y; }
float getVx() const { return vx; }
float getVy() const { return vy; }
int getId() const { return id; }
void draw(SDL_Renderer* renderer);
void drawArrow(SDL_Renderer* renderer, int x, int y, float vx, float vy);

251
main.cpp
View File

@@ -1,6 +1,6 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <iostream>
#include <vector>
#include <thread>
@@ -36,218 +36,120 @@ void handleEvents(int& playerX, int& playerY, int playerSpeed);
void renderScene(int playerX, int playerY, int *fig);
void cleanup();
// Function to draw a gradient background
void drawGradientBackground(SDL_Renderer* renderer) {
for (int y = 0; y < ENV_HEIGHT; y++) {
Uint8 blue = static_cast<Uint8>(255 * (1.0 - static_cast<float>(y) / ENV_HEIGHT));
SDL_SetRenderDrawColor(renderer, 0, 0, blue, 255);
SDL_RenderDrawLine(renderer, 0, y, ENV_WIDTH, y);
for (int y = 0; y < ENV_HEIGHT; ++y) {
SDL_SetRenderDrawColor(renderer, y * 255 / ENV_HEIGHT, y * 255 / ENV_HEIGHT, 255, 255);
SDL_RenderLine(renderer, 0, y, ENV_WIDTH, y);
}
}
// Function to draw a grid background
void drawGridBackground(SDL_Renderer* renderer) {
// Dessiner le fond bleu
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderClear(renderer);
// Couleur des lignes de la grille
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// Dessiner les lignes horizontales et les coordonnées y
for (int y = 0; y < ENV_HEIGHT; y += 50) {
SDL_RenderDrawLine(renderer, 0, y, ENV_WIDTH, y);
// Afficher les coordonnées y
SDL_RenderLine(renderer, 0, y, ENV_WIDTH, y);
std::string yText = std::to_string(y);
SDL_Surface* textSurface = TTF_RenderText_Solid(font, yText.c_str(), {255, 255, 255});
SDL_Surface* textSurface = TTF_RenderText_Solid(font, yText.c_str(), yText.length(), {255, 255, 255});
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {0, y, textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
SDL_RenderTexture(renderer, textTexture, nullptr, &textRect);
SDL_DestroySurface(textSurface);
}
// Dessiner les lignes verticales et les coordonnées x
for (int x = 0; x < ENV_WIDTH; x += 50) {
SDL_RenderDrawLine(renderer, x, 0, x, ENV_HEIGHT);
// Afficher les coordonnées x
SDL_RenderLine(renderer, x, 0, x, ENV_HEIGHT);
std::string xText = std::to_string(x);
SDL_Surface* textSurface = TTF_RenderText_Solid(font, xText.c_str(), {255, 255, 255});
SDL_Surface* textSurface = TTF_RenderText_Solid(font, xText.c_str(), xText.length(), {255, 255, 255});
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {x, 0, textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
}
}
// Function to update a range of fish
void updateFishRange(std::vector<Fish>& school, int start, int end){
while (running) {
std::this_thread::sleep_for(std::chrono::milliseconds(16));
std::lock_guard<std::mutex> lock(mtx);
for (int i = start; i < end; ++i) {
school[i].cycle();
}
SDL_RenderTexture(renderer, textTexture, nullptr, &textRect);
SDL_DestroySurface(textSurface);
}
}
// Function to display FPS
void displayFPS(SDL_Renderer* renderer, TTF_Font* font, int fps) {
SDL_Color color = {255, 255, 255};
std::string fpsText = "FPS: " + std::to_string(fps);
SDL_Color color = {0, 255, 0}; // Vert
SDL_Surface* textSurface = TTF_RenderText_Solid(font, fpsText.c_str(), color);
SDL_Surface* textSurface = TTF_RenderText_Solid(font, fpsText.c_str(), fpsText.length(), color);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect = {windowWidth - textSurface->w - 10, 10, textSurface->w, textSurface->h};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
SDL_Rect textRect = {10, 10, textSurface->w, textSurface->h};
SDL_RenderTexture(renderer, textTexture, nullptr, &textRect);
SDL_DestroySurface(textSurface);
}
// Function to display player coordinates
void displayPlayerCoord(SDL_Renderer* renderer, TTF_Font* font) {
Camera& camera = Camera::getInstance();
int cameraX = camera.getX();
int cameraY = camera.getY();
// Code pour afficher les coordonnées de la caméra
std::string coordText = "Camera: (" + std::to_string(cameraX) + ", " + std::to_string(cameraY) + ")";
std::string coordText2 = "Player: (" + std::to_string(cameraX + playerBaseX) + ", " + std::to_string(cameraY + playerBaseY) + ")";
SDL_Color textColor = {0, 255, 0};
SDL_Surface* textSurface = TTF_RenderText_Solid(font, coordText.c_str(), textColor);
SDL_Surface* textSurface2 = TTF_RenderText_Solid(font, coordText2.c_str(), textColor);
SDL_Color textColor = {255, 255, 255};
std::string coordText = "X: " + std::to_string(playerX);
std::string coordText2 = "Y: " + std::to_string(playerY);
SDL_Surface* textSurface = TTF_RenderText_Solid(font, coordText.c_str(), coordText.length(), textColor);
SDL_Surface* textSurface2 = TTF_RenderText_Solid(font, coordText2.c_str(), coordText2.length(), textColor);
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Texture* textTexture2 = SDL_CreateTextureFromSurface(renderer, textSurface2);
SDL_Rect textRect = {10, 10, textSurface->w, textSurface->h};
SDL_Rect textRect2 = {10, 30, textSurface2->w, textSurface2->h};
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
SDL_RenderCopy(renderer, textTexture2, nullptr, &textRect2);
SDL_FreeSurface(textSurface);
SDL_FreeSurface(textSurface2);
SDL_DestroyTexture(textTexture);
SDL_DestroyTexture(textTexture2);
}
int main(int argc, char* argv[]) {
if (!initSDL()) {
return 1;
}
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, schoolTexture, renderer, rand() % 2 == 0 ? 1 : 0));
}
std::cout << "Thread: " << std::thread::hardware_concurrency() << std::endl;
std::vector<std::thread> threads;
int fishPerThread = school.size() / std::thread::hardware_concurrency();
for (int i = 0; i < school.size(); i += fishPerThread) {
threads.emplace_back(updateFishRange, std::ref(school), i, std::min(i + fishPerThread, static_cast<int>(school.size())));
}
int playerX = windowWidth / 2;
int playerY = windowHeight / 2;
const int playerSpeed = 5;
int fig = 1;
while (running) {
handleEvents(playerX, playerY, playerSpeed);
renderScene(playerX, playerY, &fig);
//std::cout << "Window size: " << windowWidth << "x" << windowHeight << std::endl;
SDL_Delay(10);
}
running = false;
for (auto& thread : threads) {
thread.join();
}
cleanup();
return 0;
SDL_Rect textRect = {10, 30, textSurface->w, textSurface->h};
SDL_Rect textRect2 = {10, 50, textSurface2->w, textSurface2->h};
SDL_RenderTexture(renderer, textTexture, nullptr, &textRect);
SDL_RenderTexture(renderer, textTexture2, nullptr, &textRect2);
SDL_DestroySurface(textSurface);
SDL_DestroySurface(textSurface2);
}
// Function to initialize SDL
bool initSDL() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "Erreur d'initialisation de SDL: " << SDL_GetError() << std::endl;
return false;
}
if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG)) {
std::cerr << "Erreur d'initialisation de SDL_image: " << IMG_GetError() << std::endl;
SDL_Quit();
if (IMG_Init(IMG_INIT_PNG) == 0) {
std::cerr << "Erreur d'initialisation de SDL_image: " << SDL_GetError() << std::endl;
return false;
}
if (TTF_Init() == -1) {
std::cerr << "Erreur d'initialisation de SDL_ttf: " << TTF_GetError() << std::endl;
SDL_Quit();
std::cerr << "Erreur d'initialisation de SDL_ttf: " << SDL_GetError() << std::endl;
return false;
}
font = TTF_OpenFont("../fonts/arial.ttf", 16);
font = TTF_OpenFont("path/to/font.ttf", 24);
if (font == nullptr) {
std::cerr << "Erreur de chargement de la police: " << TTF_GetError() << std::endl;
TTF_Quit();
SDL_Quit();
std::cerr << "Erreur de chargement de la police: " << SDL_GetError() << std::endl;
return false;
}
window = SDL_CreateWindow("BloubBloub les poissons",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
windowWidth, windowHeight,
SDL_WINDOW_SHOWN);
window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cerr << "Erreur de création de la fenêtre: " << SDL_GetError() << std::endl;
SDL_Quit();
return false;
}
SDL_Surface* iconSurface = IMG_Load("../img/mory.png");
if(iconSurface == nullptr) {
std::cerr << "Erreur de chargement de l'icône: " << IMG_GetError() << std::endl;
} else {
SDL_SetWindowIcon(window, iconSurface);
SDL_FreeSurface(iconSurface);
SDL_Surface* iconSurface = IMG_Load("path/to/icon.png");
if (iconSurface == nullptr) {
std::cerr << "Erreur de chargement de l'icône: " << SDL_GetError() << std::endl;
return false;
}
SDL_SetWindowIcon(window, iconSurface);
SDL_DestroySurface(iconSurface);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
std::cerr << "Erreur de création du renderer: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return false;
}
SDL_Surface* schoolSurface = IMG_Load("../img/poasson.png");
SDL_Surface* schoolSurface = IMG_Load("path/to/school.png");
if (schoolSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image de fond: " << SDL_GetError() << std::endl;
return false;
}
schoolTexture = SDL_CreateTextureFromSurface(renderer, schoolSurface);
SDL_FreeSurface(schoolSurface);
SDL_Surface* playerSurface = IMG_Load("../img/player/player-full.png");
playerTexture = SDL_CreateTextureFromSurface(renderer, playerSurface);
SDL_FreeSurface(playerSurface);
SDL_Surface* backgroundSurface = IMG_Load("../img/background.jpg");
if (backgroundSurface == nullptr) {
std::cerr << "Erreur de chargement de l'image de fond: " << IMG_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return false;
}
backgroundTexture = SDL_CreateTextureFromSurface(renderer, backgroundSurface);
SDL_FreeSurface(backgroundSurface);
SDL_DestroySurface(schoolSurface);
return true;
}
// Function to handle events
void handleEvents(int& playerX, int& playerY, const int playerSpeed) {
SDL_Event event;
const Uint8* keystate = SDL_GetKeyboardState(NULL);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
if (event.type == SDL_EVENT_QUIT) {
running = false;
}
}
Camera& camera = Camera::getInstance();
if (keystate[SDL_SCANCODE_W]) {
if (playerY > 0) {
playerY -= playerSpeed;
@@ -277,14 +179,11 @@ void handleEvents(int& playerX, int& playerY, const int playerSpeed) {
if (keystate[SDL_SCANCODE_ESCAPE]) {
running = false;
}
// Ensure player stays within environment bounds
if (playerX < 0) {
playerX = 0;
} else if (playerX > ENV_WIDTH) {
playerX = ENV_WIDTH;
}
if (playerY < 0) {
playerY = 0;
} else if (playerY > ENV_HEIGHT) {
@@ -292,11 +191,11 @@ void handleEvents(int& playerX, int& playerY, const int playerSpeed) {
}
}
// Function to render the scene
void renderScene(int playerX, int playerY, int *fig) {
static Uint32 lastTime = 0;
static int frameCount = 0;
static int fps = 0;
Uint32 currentTime = SDL_GetTicks();
frameCount++;
if (currentTime - lastTime >= 1000) {
@@ -304,28 +203,18 @@ void renderScene(int playerX, int playerY, int *fig) {
frameCount = 0;
lastTime = currentTime;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//drawGridBackground(renderer);
//drawGradientBackground(renderer);
Camera& camera = Camera::getInstance();
SDL_Rect backgroundRect = { -camera.getX(), -camera.getY(), ENV_WIDTH, ENV_HEIGHT };
SDL_RenderCopy(renderer, backgroundTexture, nullptr, &backgroundRect);
SDL_FRect backgroundRect = { -camera.getX(), -camera.getY(), ENV_WIDTH, ENV_HEIGHT };
SDL_RenderTexture(renderer, backgroundTexture, nullptr, &backgroundRect);
rock.draw(renderer);
//reef.draw(renderer);
kelp.draw(renderer);
std::lock_guard<std::mutex> lock(mtx);
for (auto& fish : school) {
fish.draw(renderer);
}
// SDL_Rect playerRect = { playerX, playerY, 75, 75 };
// SDL_RenderCopy(renderer, playerTexture, nullptr, &playerRect);
SDL_Rect playerRect = {0, 0, 513, 600};
SDL_FRect playerRect = {0, 0, 513, 600};
if (*fig < 5 ) {
playerRect = {46, 26, 442, 541};
} else if (*fig < 8) {
@@ -338,27 +227,9 @@ void renderScene(int playerX, int playerY, int *fig) {
*fig = 0;
}
*fig += 1;
SDL_Rect playerPos = {playerX, playerY, 75, 75};
SDL_RenderCopyEx(renderer, playerTexture, &playerRect, &playerPos, 0, nullptr, SDL_FLIP_NONE);
SDL_FRect playerPos = {playerX, playerY, 75, 75};
SDL_RenderTextureRotated(renderer, playerTexture, &playerRect, &playerPos, 0, nullptr, SDL_FLIP_NONE);
displayFPS(renderer, font, fps);
displayPlayerCoord(renderer, font);
SDL_RenderPresent(renderer);
}
void cleanup() {
TTF_CloseFont(font);
TTF_Quit();
SDL_DestroyTexture(schoolTexture);
SDL_DestroyTexture(backgroundTexture);
if (renderer != nullptr) {
SDL_DestroyRenderer(renderer);
}
if (window != nullptr) {
SDL_DestroyWindow(window);
}
IMG_Quit();
SDL_Quit();
}