Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: SDL Coding Help?

                  
   
  1. #11
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    This code
    #include <fstream>
    #include <SDL.h>
    #include <SDL_mixer.h>
    #include <SDL_image.h>

    /////////// DEFINE FUNCTIONS ///////////
    #define SCR_W 480
    #define SCR_H 272
    #define SCR_BBP 16

    /////////// SDL CONFIGURATION OBJECTS ///////////
    SDL_Joystick *stick = NULL;
    SDL_Event event;

    SDL_Surface *gfx_screen;
    SDL_Surface *gfx_psiko;

    /////////// INITIALIZE THE SDL VIDEO, SET THE SCREEN MODE ///////////
    int InitSDLVideo()
    {
    gfx_screen = SDL_SetVideoMode(SCR_W,SCR_H,SCR_BBP,SDL_SWSURFACE |SDL_DOUBLEBUF);
    if(gfx_screen == NULL)
    {
    fprintf(stderr,"Error in video %s\n", SDL_GetError());
    return 1;
    }
    return 0;
    }

    /////////// INITIALIZE THE SDL AUDIO///////////
    int InitSDLAudio()
    {
    if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 512) < 0)
    {
    fprintf(stderr,"Error in audio %s\n", Mix_GetError());
    return 1;
    }
    return 0;
    }

    /////////// FUNCTION FOR BLITTING IMAGES TO THE SCREEN BUFFER ///////////
    void gfx_apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination )
    {
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;
    SDL_BlitSurface( source, NULL, destination, &offset );
    }

    /////////// FUNCTION FOR FLIPPING THE SCREEN FROM THE BUFFER ///////////
    void gfx_flipscreen()
    {SDL_Flip(gfx_screen);}

    /////////// INITIALIZE THE OBJECT STRUCTURES ///////////
    class Object
    {
    protected:
    int x, y;
    int graphicwidth;
    int graphicheight;
    int height, width;


    public:
    Object();
    ~Object();
    void step();
    void draw();
    void setGraphicHeight(int tempHeigt);
    void setGraphicWidth(int tempWidth);
    int getGraphicHeight();
    int getGraphicWidth();
    void setX(int tempX);
    void setY(int tempY);
    int getX();
    int getY();
    int direction;
    void setGraphic(SDL_Surface *graphic);
    SDL_Surface* loadImage(char *file);
    SDL_Surface *graphic;
    };

    /////////// SETUP THE OBJECTS CONSTRUCTOR ///////////
    Object::Object()
    {
    x = 0;
    y = 0;
    graphic = NULL;
    graphicwidth = 0;
    graphicheight = 0;
    }

    /////////// SETUP THE OBJECTS DESTRUCTOR ///////////
    Object::~Object()
    {
    if (graphic != NULL)
    {SDL_FreeSurface(graphic);graphic = NULL;}
    }

    SDL_Surface* Object::loadImage(char *filename)
    {
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load(filename);
    if( loadedImage != NULL )
    {
    optimizedImage = SDL_DisplayFormat( loadedImage );
    SDL_FreeSurface( loadedImage );
    }
    return optimizedImage;
    }

    /////////// SETUP THE OBJECTS DRAW ///////////
    void Object::draw()
    {gfx_apply_surface(x,y,graphic,gfx_screen);}

    /////////// SET THE GRAPHICS FOR THE OBJECT ///////////
    void Object::setGraphic(SDL_Surface* setgraphic)
    {
    graphic = setgraphic;
    graphicwidth = graphic->w;
    graphicheight = graphic->h;
    }

    /////////// SET X AND Y FOR THE OBJECT ///////////
    int Object::getX()
    {return x;}

    int Object::getY()
    {return y;}

    /////////// SET X AND Y FOR THE OBJECT ///////////
    void Object::setX(int tempX)
    {x = tempX;}

    void Object::setY(int tempY)
    {y = tempY;}

    /////////// SET THE GRAPHIC HEIGHT AND WIDTH ///////////
    void Object::setGraphicHeight(int tempHeight)
    {graphicheight = tempHeight;}

    void Object::setGraphicWidth(int tempWidth)
    {graphicwidth = tempWidth;}

    /////////// GET THE GRAPHIC HEIGHT AND WIDTH ///////////
    int Object::getGraphicHeight()
    {return graphicheight;}

    int Object::getGraphicWidth()
    {return graphicwidth;}

    /////////// COLLISION DETECTION (BOUNDING BOX) ///////////
    int obj_IsCollision(Object *object1, Object *object2)
    {
    int obj_top1, obj_top2;
    int obj_bot1, obj_bot2;
    int obj_rigt1, obj_rigt2;
    int obj_left1, obj_left2;

    obj_left1 = object1->getX();
    obj_left2 = object2->getX();
    obj_top1 = object1->getY();
    obj_top2 = object2->getY();

    obj_rigt1 = object1->getX() + object1->getGraphicWidth();
    obj_rigt2 = object2->getX() + object2->getGraphicWidth();
    obj_bot1 = object1->getY() + object1->getGraphicHeight();
    obj_bot2 = object2->getY() + object2->getGraphicHeight();

    if (obj_bot1 <= obj_top2) return(0);
    if (obj_top1 >= obj_bot2) return(0);
    if (obj_rigt1 <= obj_left2) return(0);
    if (obj_left1 >= obj_rigt2) return(0);

    return 1;
    }

    void gmp_splashscreenrun(char *filename)
    {
    Object SplashScreen;
    SplashScreen.setGraphic(SplashScreen.loadImage(fil ename));
    SplashScreen.setX((SCR_W/2)-(SplashScreen.getGraphicWidth()/2));
    SplashScreen.setY((SCR_H/2)-(SplashScreen.getGraphicHeight()/2));
    for(int i = 0;i<255;i++)
    {
    SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
    SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
    SDL_Delay(10);
    if (i == 254)
    {SDL_Delay(25);}
    SplashScreen.draw();
    gfx_flipscreen();
    }
    for(int i = 255;i>0;i--)
    {
    SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
    SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
    SDL_Delay(10);
    if (i == 1)
    {SDL_Delay(25);}
    SplashScreen.draw();
    gfx_flipscreen();
    }
    }

    /////////// START THE MAIN FUNCTION ///////////
    extern "C" int main(){

    /////////// INITIALIZE THE SDL FUNCTIONS ///////////
    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_J OYSTICK)<0)
    {
    fprintf(stderr, "Unable to Init SDL: %s\n",SDL_GetError());
    exit(1);
    }
    InitSDLVideo();
    InitSDLAudio();
    stick = SDL_JoystickOpen(0);

    gmp_splashscreenrun("psiko.png");

    SDL_Quit();
    return 0;
    }

  2. #12
    DCEmu Regular psiko_scweek's Avatar
    Join Date
    Apr 2006
    Posts
    315
    Rep Power
    71

    Default hrm!?

    i tried that exact code and it crashed on me at the same exact spot. (when calling SDL_FreeSurface)

    could it be my Makefile!? or, perhaps my installation of SDL?

    i copied your code and it did not work. however, if I remove SDL_DOUBLEBUF from the SDL_Video it works.

    im absolutely baffled.

  3. #13
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    What hardware are you running it on?

    Try the flags SDL_HWSURFACE | SDL_DOUBLEBUF

  4. #14
    DCEmu Regular psiko_scweek's Avatar
    Join Date
    Apr 2006
    Posts
    315
    Rep Power
    71

    Default alright!

    Im running it on the PSP. (which i believe is the greatest device ever made) eitherway, ill try that. I removed the SDL_DOUBLEBUF and it started to work, but Ill try your suggestion. And again, thanks a lot! Ill be sure to give credit where it is due whenever I finish my game :-)

  5. #15
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    That figures, I been trying this code on the PC which explains why I am not getting the same errors.

Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •