Game Project Hi so I’m trying to create a main menu, but the buttons seem to behave weirdly, they are not opening the things they are assigned to any advice?
// Define constants for button dimensions, spacing, and offset
const int BUTTON_WIDTH = 400;
const int BUTTON_HEIGHT = 400;
const int BUTTON_SPACING = -290;
const int NUM_BUTTONS = 4;
// Get the dimensions of the window
int windowWidth, windowHeight;
SDL_GetRendererOutputSize(Game::renderer, &windowWidth, &windowHeight);
// Calculate button positions
int totalButtonHeight = NUM_BUTTONS * BUTTON_HEIGHT + (NUM_BUTTONS - 1) * BUTTON_SPACING;
int startY = (windowHeight - totalButtonHeight) / 2 + 150;
int startX = (windowWidth - BUTTON_WIDTH) / 2 + 10;
// Define buttons using the static renderer
Button buttons[NUM_BUTTONS] = {
Button(startX, startY, BUTTON_WIDTH, BUTTON_HEIGHT, "assets/start button.png", "assets/clicked sta2.png", "assets/clicked sta2.png", Game::renderer), // Start Button (Index 0)
Button(startX, startY + (BUTTON_HEIGHT + BUTTON_SPACING), BUTTON_WIDTH, BUTTON_HEIGHT, "assets/tutorial button.png", "assets/clicked tutorial2.png", "assets/clicked tutorial2.png", Game::renderer), // Tutorial Button (Index 1)
Button(startX, startY + 2 * (BUTTON_HEIGHT + BUTTON_SPACING), BUTTON_WIDTH, BUTTON_HEIGHT, "assets/option button.png", "assets/option_clicked.png", "assets/option_clicked.png", Game::renderer), // Options Button (Index 2)
Button(startX, startY + 3 * (BUTTON_HEIGHT + BUTTON_SPACING), BUTTON_WIDTH, BUTTON_HEIGHT, "assets/about develper button.png", "assets/clicked ab de 2.png", "assets/clicked ab de 2.png", Game::renderer) // About Developer Button (Index 3)
};
// Main game loop
while (gameState != EXIT) {
frameStart = SDL_GetTicks();
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
gameState = EXIT;
}
// Handle returning to the menu from TUTORIAL or OPTIONS
if ((gameState == TUTORIAL || gameState == OPTIONS) && event.type == SDL_KEYDOWN) {
gameState = MENU;
}
if (gameState == ABOUT && event.type == SDL_KEYDOWN) {
gameState = MENU;
}
if (gameState == MENU && event.type == SDL_MOUSEBUTTONDOWN) {
int x, y;
SDL_GetMouseState(&x, &y);
for (int i = 0; i < NUM_BUTTONS; ++i) {
if (buttons[i].isClicked(x, y)) {
switch (i) {
case 0: // START button pressed
gameState = PLAYING;
Mix_HaltMusic(); // Stop background music
if (combatMusic) {
Mix_PlayMusic(combatMusic, -1); // Start combat music
}
if (game->startCombat()) { // Start combat logic
gameState = EXIT; // Exit if combat ends prematurely
}
break;
case 1: // TUTORIAL button pressed
gameState = TUTORIAL;
break;
case 2: // OPTIONS button pressed
gameState = OPTIONS;
break;
case 3: // ABOUT DEVELOPER button pressed
gameState = ABOUT;
break;
}
}
}
}
}
// Render based on game state
SDL_SetRenderDrawColor(Game::renderer, 0, 0, 0, 255);
SDL_RenderClear(Game::renderer);
switch (gameState) {
case MENU:
SDL_RenderCopy(Game::renderer, menuBackdropTexture, nullptr, nullptr);
for (int i = 0; i < NUM_BUTTONS; ++i) {
buttons[i].render(Game::renderer);
}
break;
case TUTORIAL: {
const std::string tutorialMessage =
"Welcome to the tutorial!\n\n"
"In this game, you will embark on an epic quest to reclaim your honor. Use the mouse to interact with menus and buttons. During combat, click on dice to select them and press 'Attack' to deal damage to the enemy.\n\n"
"Press any key to return to the menu.";
renderText(Game::renderer, font, tutorialMessage, whiteColor, (windowWidth - TEXT_WRAP_WIDTH) / 2, 100, TEXT_WRAP_WIDTH);
break;
}
case OPTIONS: {
const std::string optionsMessage =
"Options Menu:\n\n"
"Here you can configure game settings such as audio volume, resolution, and key bindings.\n\n"
"Press any key to return to the menu.";
renderText(Game::renderer, font, optionsMessage, whiteColor, (windowWidth - TEXT_WRAP_WIDTH) / 2, 100, TEXT_WRAP_WIDTH);
break;
}
case ABOUT: {
const std::string aboutMessage =
"Knight Revenger is a story-driven RPG built using SDL in C++, where you play as a wrongfully exiled Knight on a quest to reclaim your honor and uncover the truth behind your expulsion. Featuring challenging gameplay, memorable characters, and immersive visuals and music, the game explores themes of redemption, identity, and justice in a richly crafted world.\n\n"
"This project is brought to you by a diverse team of students, blending creativity and collaboration to deliver an unforgettable gaming experience.\n\n"
"Thank you for supporting our journey!";
const std::string returnMessage = "Press any key to return to menu";
renderText(Game::renderer, font, aboutMessage, whiteColor, (windowWidth - TEXT_WRAP_WIDTH) / 2, 100, TEXT_WRAP_WIDTH);
renderText(Game::renderer, font, returnMessage, whiteColor, (windowWidth - 300) / 2, windowHeight - 50);
break;
}
case PLAYING:
game->handleEvents();
game->update();
game->render();
break;
}
SDL_RenderPresent(Game::renderer);
// Frame timing
frameTime = SDL_GetTicks() - frameStart;
if (FRAME_DELAY > frameTime) {
SDL_Delay(FRAME_DELAY - frameTime);
}
}