void initButtons(SDL_Renderer* Renderer, TTF_Font* Font, ButtonMenu* startButton, ButtonMenu* exitButton, VolumeMenu* volumeButton){ SDL_Color insideColor = {255, 0, 0}; SDL_Color outsideColor = {0, 0, 255}; startButton->Rect.x = 120; startButton->Rect.y = 40; startButton->Rect.w = 400; startButton->Rect.h = 160; startButton->mouseInside = loadTextTexture("START", Font, insideColor, Renderer); startButton->mouseOutside = loadTextTexture("START", Font, outsideColor, Renderer); startButton->cur_texture = startButton->mouseOutside; exitButton->Rect.x = 240; exitButton->Rect.y = 378; exitButton->Rect.w = 160; exitButton->Rect.h = 64; exitButton->mouseInside = loadTextTexture("EXIT", Font, insideColor, Renderer); exitButton->mouseOutside = loadTextTexture("EXIT", Font, outsideColor, Renderer); exitButton->cur_texture = exitButton->mouseOutside; volumeButton->Rect.x = 580; volumeButton->Rect.y = 0; volumeButton->Rect.w = 60; volumeButton->Rect.h = 60; volumeButton->mouseInside_volOn = getTextureFromPath("BMPimages/Icons/volumeOnInside.png", Renderer); volumeButton->mouseOutside_volOn = getTextureFromPath("BMPimages/Icons/volumeOnOutside.png", Renderer); volumeButton->mouseInside_volOff = getTextureFromPath("BMPimages/Icons/volumeOffInside.png", Renderer); volumeButton->mouseOutside_volOff = getTextureFromPath("BMPimages/Icons/volumeOffOutside.png", Renderer); volumeButton->cur_texture = volumeButton->mouseOutside_volOn; }
int main(int argc, char* args[]) { Player player1; Player player2; struct Ball ball; struct Vector vel; // Players vars int moveP1, moveP2; player1.score = player2.score = 0; // Keyboard const Uint8* keyState; char buffer[65]; int r = 10; // Color SDL_Color textColor = { 255, 255, 255, 255 }; // Init players positions moveP1 = moveP2 = SCREEN_HEIGHT / 2 - PLAYER_HEIGHT / 2; // Init ball ball.w = ball.h = 10; ball.x = SCREEN_WIDTH / 2 - ball.w / 2; ball.y = SCREEN_HEIGHT / 2 - ball.w / 2; ball.speed = 4; // Init ball velocity vel.x = 1; vel.y = 1; if (initSDL()); { loadFont(); scorePlayer1 = loadTextTexture(_itoa(player1.score, buffer, r), textColor); scorePlayer2 = loadTextTexture(_itoa(player2.score, buffer, r), textColor); // Main loop while (!quit) { // Event loop while (SDL_PollEvent(&e) != 0) { // User presses the close button if (e.type == SDL_QUIT) quit = true; } keyState = SDL_GetKeyboardState(NULL); if (keyState[SDL_SCANCODE_A]) { if ((moveP1 + PLAYER_HEIGHT) < SCREEN_HEIGHT) { moveP1 += PLAYER_SPEED; //printf("Player1: %d\n", moveP1 + PLAYER_HEIGHT); } } if (keyState[SDL_SCANCODE_S]) { if (moveP1 > 10) { moveP1 -= PLAYER_SPEED; //printf("Player1: %d\n", moveP1); } } if (keyState[SDL_SCANCODE_J]) { if ((moveP2 + PLAYER_HEIGHT) < SCREEN_HEIGHT) { moveP2 += PLAYER_SPEED; //printf("Player2: %d\n", moveP2 + PLAYER_HEIGHT); } } if (keyState[SDL_SCANCODE_K]) { if (moveP2 > 10) { moveP2 -= PLAYER_SPEED; //printf("Player2: %d\n", moveP2); } } // Bounce of player1 if ( (ball.x <= (20 + PLAYER_WIDTH)) && ((ball.y >= moveP1) && (ball.y <= moveP1 + PLAYER_HEIGHT)) ) { vel.x *= -1; } // Bounce of player2 else if ( (ball.x + 10 >= (SCREEN_WIDTH - PLAYER_WIDTH - 20)) && ((ball.y >= moveP2) && (ball.y <= moveP2 + PLAYER_HEIGHT)) ) { vel.x *= -1; } // Bounce from top and bottom edges if ( (ball.y + 10 >= SCREEN_HEIGHT) || (ball.y <= 0) ) { vel.y *= -1; } // Player 1 won if (ball.x + ball.w >= SCREEN_WIDTH) { player1.score += 1; scorePlayer1 = loadTextTexture(_itoa(player1.score, buffer, r), textColor); resetBall(&ball, "Player 1 won!\n"); } // Player 2 won if (ball.x + ball.w <= 0) { player2.score += 1; scorePlayer2 = loadTextTexture(_itoa(player2.score, buffer, r), textColor); resetBall(&ball, "Player 2 won!\n"); } ball.x += (int)vel.x * ball.speed; ball.y += (int)vel.y * ball.speed; // Clear screen SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); SDL_RenderClear(renderer); // Draw objects drawPlayers(moveP1, moveP2); drawBall(ball); // Draw text renderTextTexture(scorePlayer1, SCREEN_WIDTH / 4 - scorePlayer1.w / 2, 20); renderTextTexture(scorePlayer2, SCREEN_WIDTH * 3 / 4 - scorePlayer2.w / 2, 20); // Update screen SDL_RenderPresent(renderer); } closeSDL(); } return 0; }