All pastes #400722 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-18 21:35 UTC
rendered paste body
#include <iostream>
#include "SDL/SDL.h"
#include <string>
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "audiere.h"

using namespace audiere;
using namespace std;

const int SCREEN_WIDTH        = 300;
const int SCREEN_HEIGHT       = 300;
const int SCREEN_BPP          = 32;
const int FRAMES_PER_SECOND   = 20;
const int LEVEL_WIDTH         = 5000;
const int LEVEL_HEIGHT        = 5000;
const int MAN_WIDTH           = 20;
const int MAN_HEIGHT          = 20;
const int CAMERA_WIDTH        = 20;
const int CAMERA_HEIGHT       = 20;
//******************************************************************************
//Required Sdl stuff
//******************************************************************************
    SDL_Event event;
    
//******************************************************************************
//the camera
//******************************************************************************
    SDL_Rect camera = {0,0, SCREEN_WIDTH, SCREEN_HEIGHT};

//******************************************************************************
//main objects
//******************************************************************************
    SDL_Surface *message      = NULL;
    SDL_Surface *background   = NULL;
    SDL_Surface *screen       = NULL;
    SDL_Surface *man          = NULL;
    
//******************************************************************************
//testcode
//******************************************************************************
//******************************************************************************
//Initialize audio
//******************************************************************************
    AudioDevicePtr device(OpenDevice());

//******************************************************************************
//Methods
//******************************************************************************
    SDL_Surface *load_image(string filename )
    {
    SDL_Surface* loadedImage     = NULL;
    SDL_Surface* optimizedImage  = NULL;
    loadedImage = IMG_Load(filename.c_str());
    if(loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);
        SDL_FreeSurface(loadedImage);
    if( optimizedImage != NULL )
    {
        Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF );
        SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey );
    }
    }
        return optimizedImage;
    }
    
    
    void apply_surface (int x, int y, SDL_Surface* source,SDL_Surface* destination,SDL_Rect* clip = NULL)
    {
        SDL_Rect offset;
        offset.x = x;
        offset.y = y;
                                
    
        SDL_BlitSurface( source, NULL, destination, &offset);
    }
//******************************************************************************
//main
//******************************************************************************
    int main(int argc,char* args[]){
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    
    //**************************************************************************
    //load resources
    //**************************************************************************
    OutputStreamPtr sound(OpenSound(device,"media/music/bard.mp3", false));
      
    //**************************************************************************
    //load bitmaps
    //**************************************************************************
    background = load_image( "media/image/background.gif" );
    man        = load_image( "media/image/man.gif" );
    SDL_WM_SetCaption("Direct x",NULL);
    
    //**************************************************************************
    //variables
    //**************************************************************************
    int frame        = 0;
    int scene        = 1;
    int placex       = 128;
    int placey       = 128;
    int placexvel    = 0;
    int placeyvel    = 0;
    float volume     = 2.0;
    bool tempbool    = false;
    bool cont        = true;
    bool up          = false;  
    bool down        = false;
    bool left        = false;
    bool right       = false;
   //***************************************************************************
   //play music
   //***************************************************************************
    sound->setVolume(volume);
    sound->play();
    sound->setRepeat(true);

    
    //**************************************************************************
    //Scene based play
    //**************************************************************************
   
    while(cont){
         //scene 1
        
        if(SDL_Flip(screen)== -1) return 1;
        if( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
           //Set the proper text
                switch( event.key.keysym.sym )
                {
                    case SDLK_UP:      up    = true;placeyvel =  1; break;
                    case SDLK_DOWN:    down  = true;placeyvel = -1; break;
                    case SDLK_LEFT:    left  = true;placexvel = -1; break;
                    case SDLK_RIGHT:   right = true;placexvel = -1; break;    
                    case SDLK_ESCAPE:  cont  = false;break;
                }
             else if(event.type = SDL_KEYUP){
                 switch(event.key.keysym.sym)
                 {
                    case SDLK_UP:      up    = false;placeyvel  = 0;break;
                    case SDLK_DOWN:    down  = false;placeyvel  = 0;break;
                    case SDLK_LEFT:    left  = false;placexvel  = 0;break;
                    case SDLK_RIGHT:   right = false;placexvel  = 0;break;
                 }  
                }
                
                //testcode
                  camera.x =( placex + MAN_WIDTH / 2 ) -  SCREEN_WIDTH / 2;
                  camera.y =( placey + MAN_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;
                  if( camera.x < 0 )
                  {
                   camera.x = 0;    
                  }
                  if( camera.y < 0 )
                  {
                   camera.y = 0;    
                  }
                  if( camera.x > LEVEL_WIDTH - camera.w )
                  {
                   camera.x = LEVEL_WIDTH - camera.w;    
                  }
                  if( camera.y > LEVEL_HEIGHT - camera.h )
                  {
                   camera.y = LEVEL_HEIGHT - camera.h;    
                  }            
                
                //end of test code
                
                  }
                   apply_surface(0,0,background,screen,&camera);
                   apply_surface(placex - camera.x, placey - camera.y, man, screen);
                   SDL_Delay(50);
                   if (!up)    placeyvel -= 1;  
                   if (!down)  placeyvel += 1; 
                   if (!right) placexvel += 1;
                   if (!left)  placexvel -= 1; 
                   placex -= placexvel;
                   placey -= placeyvel;
                 }
           
//******************************************************************************    
//clean-up
//******************************************************************************
//free all bitmaps
   SDL_FreeSurface(man);
   SDL_FreeSurface(background);
   SDL_Quit();
   return 0;
   }