Sign in to follow this  
metal_kid43

SDL_Mixer

Recommended Posts

I've just downloaded and installed the SDL_Mixer and want to add some simple little sound effects to my pong game. When the ball hits a paddle or a wall, I want to play I want to play an effect. Problem: IT doesn't play. I was wondering where my error is. . . . Thanks! (and yes, phaser.wav is in my folder)
#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"
using namespace std;

const int WINDOW_WIDTH = 720;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "Pong";
float ballXVel = 1;
float ballYVel = 1;

Mix_Chunk *phaser = NULL;

int phaserChannel = -1;


int main(int argc, char *argv[])
{
	int audio_rate = 22050;
    Uint16 audio_format = AUDIO_S16; 
    int audio_channels = 2;
    int audio_buffers = 4096;

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO);
	SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, 
      SDL_HWSURFACE | SDL_DOUBLEBUF );
	SDL_WM_SetCaption(WINDOW_TITLE, 0);
	SDL_Surface* paddle1 = SDL_LoadBMP("paddle1.bmp");
	SDL_Surface* paddle2 = SDL_LoadBMP("paddle2.bmp");
	SDL_Surface* ball = SDL_LoadBMP("ball.bmp");
    SDL_SetColorKey(ball, SDL_SRCCOLORKEY, SDL_MapRGB(ball->format, 
      255, 0, 0)); 
    SDL_Rect paddle1_source;
	paddle1_source.x = 0;
	paddle1_source.y = 0;
	paddle1_source.w = 20;
	paddle1_source.h = 100;
	
	SDL_Rect paddle1_destination;
    paddle1_destination.x = 75;
    paddle1_destination.y = 240;
    paddle1_destination.w = 20;
    paddle1_destination.h = 100;
    
    SDL_Rect paddle2_source;
    paddle2_source.x = 0;
    paddle2_source.y = 0;
    paddle2_source.w = 20;
    paddle2_source.h = 100;
    
    SDL_Rect paddle2_destination;
    paddle2_destination.x = 645;
    paddle2_destination.y = 240;
    paddle2_destination.w = 20;
    paddle2_destination.h = 100;
    
    SDL_Rect ball_source;
    ball_source.x = 0;
    ball_source.y = 0;
    ball_source.w = 25;
    ball_source.h = 25;
    
    SDL_Rect ball_position;
    ball_position.x = WINDOW_WIDTH/2;
    ball_position.y = WINDOW_HEIGHT/2;
    ball_position.w = 25;
    ball_position.h = 25;
    
    phaser = Mix_LoadWAV("phaser.wav");


SDL_Event event;

	bool gameRunning = true;
	bool keysHeld[323] ={false};
	while (gameRunning)
	{
		
        
        if (SDL_PollEvent(&event))
		{
			if (event.type == SDL_QUIT)
			{
				gameRunning = false;
			}
			if (event.type == SDL_KEYDOWN)
			{
				keysHeld[event.key.keysym.sym] = true;
			}
			if (event.type == SDL_KEYUP)
			{
				keysHeld[event.key.keysym.sym] = false;
			}	
		}
		
		
        if (keysHeld[SDLK_ESCAPE])
		{
			gameRunning = false;
		}
        if (keysHeld[SDLK_UP])
        {
            paddle1_destination.y -= 2;
        }
        if (keysHeld[SDLK_DOWN])
        {
            paddle1_destination.y += 2;
            if (paddle1_destination.y + paddle1_destination.h > WINDOW_HEIGHT)
            {
                paddle1_destination.y = 480 - paddle1_destination.h;
              
            }
        }
        if (keysHeld[SDLK_KP2])
        {
            paddle2_destination.y += 2;
            if (paddle2_destination.y + paddle2_destination.h > WINDOW_HEIGHT)
            {
                paddle2_destination.y = 480 - paddle2_destination.h;
               
            }
        }
        if (keysHeld[SDLK_KP8])
        {
            paddle2_destination.y -= 2;
        }
        
        
        ball_position.x += ballXVel;
        ball_position.y += ballYVel;


        
        if (ball_position.x  + ball_position.w >  WINDOW_WIDTH)
        {
             gameRunning = false;
        }
        if (ball_position.x  < 0)
        {
             gameRunning = false;
        }
        if (ball_position.y  + ball_position.h >  WINDOW_HEIGHT)
        {
             ballYVel *= -1;
             Mix_PlayChannel(-1, phaser, 0);

        }
        if (ball_position.y  <0  )
        {
             ballYVel *= -1;
             Mix_PlayChannel(-1, phaser, 0);

        }
        if (ball_position.x   == paddle1_destination.x + paddle1_destination.w && ball_position.y + ball_position.h >= paddle1_destination.y )
        {
             ballXVel *= -1.0;  
             Mix_PlayChannel(-1, phaser, 0);

        }
        if (ball_position.x +ball_position.w  == paddle2_destination.x  && ball_position.y + ball_position.h  >= paddle2_destination.y && ball_position.y <= paddle2_destination.y + paddle2_destination.h)
        {
             ballXVel *= -1.0;
             Mix_PlayChannel(-1, phaser, 0);

        }
        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 0));
        SDL_BlitSurface(paddle1, &paddle1_source, screen, &paddle1_destination);
        SDL_BlitSurface(paddle2, &paddle2_source, screen, &paddle2_destination);
        SDL_BlitSurface(ball, &ball_source, screen, &ball_position);
        
        SDL_Flip(screen);
	    
    }
    
    SDL_FreeSurface(paddle1); 
    SDL_FreeSurface(paddle2);
    SDL_FreeSurface(ball);
    Mix_CloseAudio();
    SDL_Quit();
	return 0;
}


Share this post


Link to post
Share on other sites
Hey, metal_kid43...what was the problem? Do you remember me man? I'm on your Yahoo messenger list. Also, if you need help with programming now, then I will help. I have made some simple few games, so I could help if you need any help.



Chad.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this