please can someone help me to correct this code?
i wanted to make an animation of start game that ends just by pressing on the sentence “press HERE to start menu” , but once i move cursor it doesn’t work
ps : i couldn’t upload the full file with font and image cuz im a new user sorry
#include <stdio.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int SCROLL_SPEED = 2;
const int ANIMATION_DURATION = 1000 / 30;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* imageTexture = NULL;
SDL_Rect spriteRect = {0, 0, 0, 0};
Mix_Music* music = NULL;
TTF_Font* menuFont = NULL;
double oldTime = 0.0;
double newTime = 0.0;
double deltaTime = 0.0;
int spriteXDirection = 1;
float spriteXPosition = 0.0f;
float spriteYPosition = 0.0f;
float spriteMovementSpeed = 300.0f;
int running = 1;
int initSDL() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Error: SDL could not initialize! %s\n", SDL_GetError());
return 0;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG) {
printf("Error: SDL_Image could not initialize. %s\n", IMG_GetError());
return 0;
}
if (TTF_Init() == -1) {
printf("Error: SDL_TTF could not initialize! %s\n", TTF_GetError());
return 0;
}
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
printf("Error: SDL_Mixer could not initialize! %s\n", Mix_GetError());
return 0;
}
window = SDL_CreateWindow("SDL Animation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (!window) {
printf("Error: SDL window could not be created! %s\n", SDL_GetError());
return 0;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
printf("Error: SDL renderer could not be created! %s\n", SDL_GetError());
return 0;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
return 1;
}
void renderText(const char* text, int x, int y, SDL_Color color,int animation1) {
if(animation1){
SDL_Surface* surface = TTF_RenderText_Solid(menuFont, text, color);
SDL_Texture* textexture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect textRect = {x, y, surface->w, surface->h};
SDL_RenderCopy(renderer, textexture, NULL, &textRect);
SDL_FreeSurface(surface);
SDL_DestroyTexture(textexture);
}
}
void cleanupSDL() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Mix_Quit();
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
int initAssets() {
SDL_Surface* imageSurface = IMG_Load("imagesdl2.png");
if (!imageSurface) {
printf("Error: failed to load image! %s\n", SDL_GetError());
return 0;
}
imageTexture = SDL_CreateTextureFromSurface(renderer, imageSurface);
SDL_FreeSurface(imageSurface);
if (!imageTexture) {
printf("Error: failed to create SDL texture! %s\n", SDL_GetError());
return 0;
}
int textureWidth = 0;
int textureHeight = 0;
SDL_QueryTexture(imageTexture, NULL, NULL, &textureWidth, &textureHeight);
spriteXPosition = (float)((WINDOW_WIDTH * 0.5f) - (textureWidth * 0.5f));
spriteYPosition = (float)((WINDOW_HEIGHT * 0.5f) - (textureHeight * 0.5f));
spriteRect.x = (int)spriteXPosition;
spriteRect.y = (int)spriteYPosition;
spriteRect.w = textureWidth;
spriteRect.h = textureHeight;
menuFont = TTF_OpenFont("ChrustyRock-ORLA.ttf", 45);
if (!menuFont) {
printf("Error: failed to load font! %s\n", TTF_GetError());
return 0;
}
return 1;
}
void cleanupAssets() {
TTF_CloseFont(menuFont);
menuFont = NULL;
SDL_DestroyTexture(imageTexture);
imageTexture = NULL;
}
void update(int animation1) {
if(animation1){
if (spriteXDirection == 1) {
spriteXPosition += spriteMovementSpeed * (float)deltaTime;
if (spriteXPosition >= (float)(WINDOW_WIDTH - spriteRect.w))
spriteXDirection = 0;
} else {
spriteXPosition -= spriteMovementSpeed * (float)deltaTime;
if (spriteXPosition <= 0.0f)
spriteXDirection = 1;
}
}
spriteRect.x = (int)spriteXPosition;
spriteRect.y = (int)spriteYPosition;
}
void renderanimationtext1(int isMouseOver,int animation1) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
if(animation1){
if (isMouseOver) {
renderText("Press ", 90, 470, (SDL_Color){ 255, 0, 0}, animation1);
renderText(" HERE ", 230, 470, (SDL_Color){ 255, 0, 80}, animation1);
renderText("to start menu", 385, 470, (SDL_Color){ 255, 0, 0}, animation1);
} else {
renderText("Press HERE to start menu", 90, 470, (SDL_Color){255, 0, 0, 80}, animation1);
}
}
isMouseOver = 0;
SDL_RenderCopy(renderer, imageTexture, NULL, &spriteRect);
SDL_RenderPresent(renderer);
}
int audioThread(void* data) {
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
Mix_Music* music = Mix_LoadMUS("initjeuaudio.mp3");
if (music)
Mix_PlayMusic(music, 0);
while (Mix_PlayingMusic()) {
SDL_Delay(1000 / 60);
}
if (music)
Mix_FreeMusic(music);
Mix_CloseAudio();
Mix_Quit();
return 0;
}
typedef struct {
SDL_Rect rect;
SDL_Color color;
char buttontext[50];
TTF_Font* buttonfont;
SDL_bool isClicked;
} Button;
// Fonction pour initialiser un bouton
Button createButton(int x, int y, int width, int height, SDL_Color color, const char* buttontext, TTF_Font* buttonfont) {
Button button;
button.rect.x = x;
button.rect.y = y;
button.rect.w = width;
button.rect.h = height;
button.color = color;
button.isClicked = SDL_FALSE;
strncpy(button.buttontext, buttontext, sizeof(button.buttontext) - 1);
button.buttontext[sizeof(button.buttontext) - 1] = '\0'; // S'assurer que la chaîne est terminée
button.buttonfont = buttonfont;
return button;
}
// Fonction pour vérifier si les coordonnées (x, y) sont à l'intérieur du bouton
SDL_bool pointInsideRect(int x, int y, SDL_Rect* buttonrect,int animation1) {
if(animation1 == 0){
return (x >= buttonrect->x && x < buttonrect->x + buttonrect->w && y >= buttonrect->y && y < buttonrect->y + buttonrect->h);
}
}
// Fonction pour rendre le bouton avec du texte à l'intérieur
void renderButton(SDL_Renderer* renderer, const Button* button) {
// Rendre le rectangle du bouton
SDL_SetRenderDrawColor(renderer, button->color.r, button->color.g, button->color.b, button->color.a);
SDL_RenderFillRect(renderer, &button->rect);
// Rendre le texte à l'intérieur du bouton
SDL_Surface* textSurface = TTF_RenderText_Solid(button->buttonfont, button->buttontext, (SDL_Color){255, 255, 255, 255});
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
int textWidth, textHeight;
SDL_QueryTexture(textTexture, NULL, NULL, &textWidth, &textHeight);
SDL_Rect textRect = {
button->rect.x + (button->rect.w - textWidth) / 2,
button->rect.y + (button->rect.h - textHeight) / 2,
textWidth,
textHeight
};
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
}
int main(int argc, char** argv) {
if (!initSDL())
running = 0;
if (!initAssets())
running = 0;
TTF_Font* buttonfont = TTF_OpenFont("ChrustyRock-ORLA.ttf", 24);
SDL_Thread* thread = SDL_CreateThread(audioThread, "Audio", NULL);
int isMouseOver = 0;
int mouseX;
int mouseY;
int textX;
int textY;
int textWidth;
int textHeight;
int animation1;
Button Button1 = createButton(170, 350, 200, 50, (SDL_Color){255, 0, 0, 255}, "start!", buttonfont);
Button Button2 = createButton(430, 350, 200, 50, (SDL_Color){255, 0, 0, 255}, "Click me!", buttonfont);
Button Button3 = createButton(170, 450 ,200, 50, (SDL_Color){255, 0, 0, 255}, "Click me!", buttonfont);
Button Button4 = createButton(430, 450, 200, 50, (SDL_Color){255, 0, 0, 255}, "Click me!", buttonfont);
SDL_Event event;
while(running){
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = 0;
break;
case SDL_MOUSEMOTION:
mouseX = event.motion.x;
mouseY = event.motion.y;
textX = 70;
textY = 470;
textWidth = 300;
textHeight = 30;
animation1=1;
if (mouseX >= textX && mouseX <= textX + textWidth &&
mouseY >= textY && mouseY <= textY + textHeight) {
isMouseOver = 1;
} else {
isMouseOver = 0;
}
break;
case SDL_MOUSEBUTTONDOWN:
// Vérifier si le clic a eu lieu sur la phrase
if (isMouseOver) {
printf("Clic sur la phrase!\n");
animation1 = 0;
}
}
if(animation1 == 0){
// Ici, vous pouvez afficher votre menu
if (pointInsideRect(mouseX, mouseY, &Button1.rect, animation1)) {
Button1.isClicked = SDL_TRUE;
printf("Button1 clicked!\n");
}
else if (pointInsideRect(mouseX, mouseY, &Button2.rect, animation1)) {
Button2.isClicked = SDL_TRUE;
printf("Button2 clicked!\n");
}
else if (pointInsideRect(mouseX, mouseY, &Button3.rect, animation1)) {
Button3.isClicked = SDL_TRUE;
printf("Button3 clicked!\n");
}
else if (pointInsideRect(mouseX, mouseY, &Button4.rect, animation1)) {
Button4.isClicked = SDL_TRUE;
printf("Button4 clicked!\n");
}
}
break;
}
newTime = (double)SDL_GetTicks();
deltaTime = (newTime - oldTime) * 0.001;
oldTime = newTime;
if(animation1==1){
update(animation1);
renderanimationtext1(isMouseOver,animation1);
SDL_Delay(1);
}
SDL_RenderClear(renderer);
if(animation1 == 0){
renderButton(renderer, &Button1);
renderButton(renderer, &Button2);
renderButton(renderer, &Button3);
renderButton(renderer, &Button4);
SDL_RenderPresent(renderer);
}
SDL_RenderClear(renderer);
}
int threadReturnValue = 0;
SDL_WaitThread(thread, &threadReturnValue);
TTF_CloseFont(buttonfont);
cleanupAssets();
cleanupSDL();
return 0;
}