SDL_Surface array of SDL surfaces

Porgram crashes, i think due to this array issue

Page 1 of 1

1 Replies - 3621 Views - Last Post: 27 February 2010 - 04:34 PM Rate Topic: -----

#1 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

SDL_Surface array of SDL surfaces

Posted 27 February 2010 - 04:27 PM

ok there must be a way to declare this array so that i can have can have it be populated with SDL_Surfaces i just dont know how. I think that the problom with my program is somthing to do with the array. you see as soon as i press any of the arrow keys it just stops working or in other words crashes. so if someone could identifie my problom and also explain how to make an array of SDL_surfaces. heres my code

 #include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <fstream>

void MakeWalls();
void updateCordents();
void updateDiscription();
void updateMonster();

int X;
int Y;
int ScaleX;
int ScaleY;
int SX;
int SY;
int i;
int RandDirectMonster;
int RandDirectPath1;
int RandDirectPath2;
int temp;
int MX;
int MY;
int userDirect;
int input;

bool AtFin;

const int DIR_N = 0x01;
const int DIR_S = 0x02;
const int DIR_E = 0x04;
const int DIR_W = 0x08;

SDL_Surface *DiscriptionArray[11][11];
int WallArray[11][11];

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *background = NULL;

/*0       No place to go.
1       North
2       South
3       North and South
4       East
5       North and East
6       South and East
7       North, South and East
8       West
9       North and West
10      South and West
11      North, South and West
12      East and West
13      North, East and West
14      South, East and West
15      North, South, East and West */

SDL_Surface *Zero = NULL;
SDL_Surface *One = NULL;
SDL_Surface *Two = NULL;
SDL_Surface *Three = NULL;
SDL_Surface *Four = NULL;
SDL_Surface *Five = NULL;
SDL_Surface *Six = NULL;
SDL_Surface *Seven = NULL;
SDL_Surface *Eight = NULL;
SDL_Surface *Nine = NULL;
SDL_Surface *Ten = NULL;
SDL_Surface *Eleven = NULL;
SDL_Surface *Twelve = NULL;
SDL_Surface *Thirteen = NULL;
SDL_Surface *Fourteen = NULL;
SDL_Surface *Fifteen = NULL;
SDL_Surface *Cant = NULL;

SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;



SDL_Event event;

TTF_Font *font = NULL;

SDL_Color textColor = { 0, 0, 0 };

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if ( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if ( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if ( screen == NULL )
    {
        return false;
    }

    //Initialize SDL_ttf
    if ( TTF_Init() == -1 )
    {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption( "Maze", NULL );

    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the background image
    background = load_image( "background.png" );

    //Open the font
    font = TTF_OpenFont( "lazy.ttf", 72 );

    //If there was a problem in loading the background
    if ( background == NULL )
    {
        return false;
    }

    //If there was an error in loading the font
    if ( font == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surfaces
    SDL_FreeSurface( background );
    SDL_FreeSurface( Zero );
    SDL_FreeSurface( One );
    SDL_FreeSurface( Two );
    SDL_FreeSurface( Four );
    SDL_FreeSurface( Five );
    SDL_FreeSurface( Six );
    SDL_FreeSurface( Seven );
    SDL_FreeSurface( Eight );
    SDL_FreeSurface( Nine );
    SDL_FreeSurface( Ten );
    SDL_FreeSurface( Eleven );
    SDL_FreeSurface( Twelve );
    SDL_FreeSurface( Thirteen );
    SDL_FreeSurface( Fourteen );
    SDL_FreeSurface( Fifteen );

    //Close the font
    TTF_CloseFont( font );

    //Quit SDL_ttf
    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}
/*0       No place to go.
1       North
2       South
3       North and South
4       East
5       North and East
6       South and East
7       North, South and East
8       West
9       North and West
10      South and West
11      North, South and West
12      East and West
13      North, East and West
14      South, East and West
15      North, South, East and West */

void MakeWalls()
{
    // later this algorith will be more complex and randomized but with safty messures to ensure nothing bad happens and that you can follow the path
    for (SX=0;SX<ScaleX;SX++)
    {
        for (SY=0;SY<ScaleY;SY++)
        {
            if (SX == 0 && SY == 0)
            {
                WallArray[SX][SY] = 9;
                DiscriptionArray[SX][SY] = Nine;
            }
            if (SX == 0 && SY != 0)
            {
                if ( SY != ScaleY-1)
                {
                    WallArray[SX][SY] = 11;
                    DiscriptionArray[SX][SY] = Eleven;
                }
            }
            if (SX == 0 && SY == ScaleY-1)
            {
                WallArray[SX][SY] = 10;
                DiscriptionArray[SX][SY] = Ten;
            }
            if (SX == ScaleX-1 && SY == ScaleY-1)
            {
                WallArray[SX][SY] = 6;
                DiscriptionArray[SX][SY] = Six;
            }
            if (SX != 0 && SY == ScaleY-1)
            {
                if (SX != ScaleX-1)
                {
                    WallArray[SX][SY] = 14;
                    DiscriptionArray[SX][SY] = Fourteen;
                }
            }
            if (SX == ScaleX-1 && SY != ScaleY-1)
            {
                if (SY != 0)
                {
                    WallArray[SX][SY] = 7;
                    DiscriptionArray[SX][SY] = Seven;
                }
            }
            if (SX != 0 && SY == 0)
            {
                if (SX != ScaleX-1)
                {
                    WallArray[SX][SY] = 13;
                    DiscriptionArray[SX][SY] = Thirteen;
                }
            }
            if (SX == ScaleX-1 && SY == 0)
            {
                WallArray[SX][SY] = 5;
                DiscriptionArray[SX][SY] = Five;
            }
            if (SX != 0 && SX != ScaleX-1)
            {
                if (SY != 0 && SY != ScaleY-1)
                {
                    WallArray[SX][SY] = 15;
                    DiscriptionArray[SX][SY] = Fifteen;
                }
            }
        }
    }
}
void updateMonster()
{

    RandDirectMonster =  1 + rand()%(4 + 1 - 1);
    //cout<<RandDirectMonster<<endl;
    temp = WallArray[MX][MY];
    if (RandDirectMonster==1)
    {
        if ( temp == 1 || temp == 3 || temp == 5 || temp == 7 || temp == 9 || temp == 11 || temp == 13 || temp == 15)
        {
            //cout<<"1";
            MY=MY+1;
        }
        else
        {
            updateMonster();
        }
    }
    if (RandDirectMonster==2)
    {
        if ( temp == 2 || temp == 3 || temp == 6 || temp == 7 || temp == 10 || temp == 11 || temp == 14 || temp == 15)
        {
            //cout<<"2";
            MY=MY-1;
        }
        else
        {
            updateMonster();
        }
    }
    if (RandDirectMonster==3)
    {
        if ( temp == 8 || temp == 9 || temp == 10 || temp == 11 || temp == 12 || temp == 13 || temp == 14 || temp == 15)
        {
            //cout<<"3";
            MX=MX+1;
        }
        else
        {
            updateMonster();
        }
    }
    if (RandDirectMonster==4)
    {
        if ( temp == 4 || temp == 5 || temp == 6 || temp == 7 || temp == 12 || temp == 13 || temp == 14 || temp == 15)
        {
            //cout<<"4";
            MX=MX-1;
        }
        else
        {
            updateMonster();
        }
    }

}
inline void updateCordents()
{

}
int main( int argc, char* args[] )
{
    srand(time(0));
    MakeWalls();
    X = 0;
    Y = 0;
    MX = ScaleX - 1;
    MY = ScaleY - 1;


    //Quit flag
    bool quit = false;

    //Initialize
    if ( init() == false )
    {
        return 1;
    }

    //Load the files
    if ( load_files() == false )
    {
        return 1;
    }
    Zero = TTF_RenderText_Solid( font, "No place to go", textColor );
    One = TTF_RenderText_Solid( font, "North.", textColor );
    Two = TTF_RenderText_Solid( font, "South", textColor );
    Three = TTF_RenderText_Solid( font, "North and South", textColor );
    Four = TTF_RenderText_Solid( font, "East.", textColor );
    Five = TTF_RenderText_Solid( font, "North and East.", textColor );
    Six = TTF_RenderText_Solid( font, "South and East", textColor );
    Seven = TTF_RenderText_Solid( font, "North, South, and East", textColor );
    Eight = TTF_RenderText_Solid( font, "West.", textColor );
    Nine = TTF_RenderText_Solid( font, "North and West.", textColor );
    Ten = TTF_RenderText_Solid( font, "South and West", textColor );
    Eleven = TTF_RenderText_Solid( font, "North, South and West", textColor );
    Twelve = TTF_RenderText_Solid( font, "East and West.", textColor );
    Thirteen = TTF_RenderText_Solid( font, "North, East and West.", textColor );
    Fourteen = TTF_RenderText_Solid( font, "South, East and West", textColor );
    Fifteen = TTF_RenderText_Solid( font, "North, South, East and West", textColor );
    Cant = TTF_RenderText_Solid( font, "You cant go that way.", textColor );

    apply_surface( 0, 0, background, screen );

    while ( quit == false )
    {

        //If there's an event to handle
        if ( SDL_PollEvent( &event ) )
        {
            //If a key was pressed
            if ( event.type == SDL_KEYDOWN )
            {
                //Set the proper message surface
                switch ( event.key.keysym.sym )
                {
                    temp = WallArray[X][Y];
                case SDLK_UP:
                    if ( temp == 1 || temp == 3 || temp == 5 || temp == 7 || temp == 9 || temp == 11 || temp == 13 || temp == 15)
                    {
                        Y=Y+1;
                        message = DiscriptionArray[X][Y];
                    }
                    else
                    {
                        message = Cant;
                    }
                    break;
                case SDLK_DOWN:
                    if ( temp == 2 || temp == 3 || temp == 6 || temp == 7 || temp == 10 || temp == 11 || temp == 14 || temp == 15)
                    {
                        Y=Y-1;
                        message = DiscriptionArray[X][Y];
                    }
                    else
                    {
                        message = Cant;
                    }
                    break;
                case SDLK_LEFT:
                    if ( temp == 8 || temp == 9 || temp == 10 || temp == 11 || temp == 12 || temp == 13 || temp == 14 || temp == 15)
                    {
                        X=X+1;
                        message = DiscriptionArray[X][Y];
                    }
                    else
                    {
                        message = Cant;
                    }
                    break;
                case SDLK_RIGHT:
                    if ( temp == 4 || temp == 5 || temp == 6 || temp == 7 || temp == 12 || temp == 13 || temp == 14 || temp == 15)
                    {
                        X=X-1;
                        message = DiscriptionArray[X][Y];
                    }
                    else
                    {
                        message = Cant;
                    }
                    break;
                }
            }

            //If the user has Xed out the window
            else if ( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
        if ( message != NULL )
        {
            updateCordents();
            updateMonster();
            //Apply the background to the screen
            apply_surface( 0, 0, background, screen );

            //Apply the message centered on the screen
            apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );

            //Null the surface pointer
            message = NULL;
        }

        //Update the screen
        if ( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }
    }

    //Clean up
    clean_up();

    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: SDL_Surface array of SDL surfaces

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: SDL_Surface array of SDL surfaces

Posted 27 February 2010 - 04:34 PM

opps no i figured out why it was crashing. i didn't declar two ScaleX and ScaleY for my MakeWalls function. but now it just always tells me that i cant move so i dont know what to do now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1