int main(int argc, char **argv) { SDL_Event e; MyWindow* window = new MyWindow("Handle Key Press Event", 800, 600); window->updateSurface(bmpDefault->getSurface()); while (window->isRunning()) { while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: window->setRunning(false); break; case SDL_KEYDOWN: handleKeyDownEvent(e, window); break; default: break; } } } closeSDL(window->getWindow()); return 0; }
SDLEventReader::~SDLEventReader() { if (sdlIsOpen) { closeSDL(); } }
void VideoPlayer::pause() { if (eventloop == NULL) return; eventloop->exit(); closeSDL(); curState = PausedState; emit stateChanged(curState); }
void SDLEventReader::quit() { if (sdlIsOpen) { closeSDL(); joysticks = 0; } }
// Quit and save preference. void quitLast() { if ( !noSound ) closeSound(); savePreference(); closeBarragemanager(); closeSDL(); SDL_Quit(); exit(1); }
void SDLEventReader::secondaryRefresh() { if (sdlIsOpen) { closeSDL(); } initSDL(); }
void VideoPlayer::stop() { if (eventloop == NULL) return; eventloop->exit(); closeVideo(); closeSDL(); curState = StoppedState; emit stateChanged(curState); }
void SDLEventReader::secondaryRefresh() { disconnect(this, SIGNAL(eventRaised()), this, 0); if (sdlIsOpen) { closeSDL(); } initSDL(); }
void handleInputSDL() { SDL_Event event; // Here we handle all events while (SDL_PollEvent(&event) != 0) { if (event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_q: { closeSDL(); } break; default: break; } } } }
// add "int argc, char *args[]" for cross-compat int main (void) { if (!initSDL()) { fprintf(stderr, "Failed to initialize SDL!\n"); } else { if (!loadMedia()) { fprintf(stderr, "Failed to load media!\n"); } else { mainLoop(); } } closeSDL(); return 0; }
int main( int argc, char* args[] ) { //Quit flag bool quit = false; //Initialize if( initGame() == false ) { return 1; } //Wait for user exit while( quit == false ) { //Clear the screen glClear( GL_COLOR_BUFFER_BIT ); //While there are events to handle while( SDL_PollEvent( &event ) ) { //Handle key presses handleInput(); if( event.type == SDL_QUIT ) { quit = true; } } if (gameStarted && !gameOver && !gameWon) update(); draw(); //Update screen SDL_GL_SwapBuffers(); } //Clean up closeSDL(); return 0; }
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; }