TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-18-2009, 07:09 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default Next class project?

anyone have any idea's what i can do next to create a class for?

I think creating a class for pagination helped get me started on OO and I definitley don't want to end it there..

If anyone has suggestions on what is a good project I can work on please let me know....
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 07:22 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

The best way to learn classes is to find something you want to make, I've found that classes for the sake of making one often teach bad design since there is no real reason you are making it. Objects for for reusable code, which will come up in any sizable project. So find a full project you want to build and you will probably find an object or two you want to make.

For instance, in the last game I made, I had a number of classes that I wrote because there was a clear and present advantage. I have a general game object class with the ability to do a lot of things. Now that I am writing a different one, I am able to literally copy and paste the classes over to use them since they are not dependent on much.

ps. If your interested, heres the code
code.h
Code:
/****************************
Class Snake_timer
Description: A timer than can give the time in MS between a start and stop
Version: 1
Added on initial build
Level: Engine Core
Todo: Complete
*****************************/
class Snake_timer 
{ 
private: 
    //The clock time when the timer started 
    int startTicks; 
    //The ticks stored when the timer was paused 
    int pausedTicks; 
    //The timer status 
    bool paused; 
    bool started; 
public: 
    //Initializes variables 
    Snake_timer(); 
    //The various clock actions 
    void start(); 
    void stop(); 
    void pause(); 
    void unpause(); 
    void reset();
    //Gets the timer's time 
    int get_ticks(); 
    //Checks the status of the timer 
    bool is_started(); 
    bool is_paused(); 
}; 

/****************************
Class Snake_MCoords
Description: Containes mouse X, Y and which button in a single struct
Version: 1
Added on initial build
Level: Engine Core
Todo: Impliment with input class
*****************************/
struct Snake_MCoords
{
    int x,
        y,
        button;
};

/****************************
Class Snake_input
Description: The class for accepting all types of input
Version: 1
Added on initial build
Level: Engine Core
Todo: Add mouse detection abilities
*****************************/
class Snake_input
{
public: 
    Uint8* keys;
    SDL_Event sEvent;

    Snake_MCoords mouse;
    Snake_input();
    void updateMouseStatus();
    bool isKeyDown(SDLKey Key);
    bool hasEvent();

};



/****************************
Class Object_properties
Description: Containes the essental parts of every object. To be directly implimented
                in no higher level than game core
Version: 1
Added on initial build
Level: Engine Core
Todo: Complete
*****************************/
class Object_properties
{
public:
    float xpos,ypos; 
    int   width,height;

    template <typename Object_properties_child>
    bool  IsColiding(Object_properties_child other);
    float DistanceFrom(Object_properties);
    void  
        ChangeXRelitave(float x), 
        ChangeYRelitave(float y), 
        changePositionRelitave(float x,float y);

    void  
        ChangeXAbsolute(float x), 
        ChangeYAbsolute(float y), 
        changePostitionAbsolute(float x,float y);

    void  
        placeOnMap(float x,float y), 
        Redefine(int x, int y, int w, int h);

    void move(int dir);

    SDL_Surface *face;
    void Draw(SDL_Surface *),
         DrawAll(SDL_Surface *screen);

    SDL_Surface 
        * LoadFullImg(const char *), 
        * LoadFullImg(const char *,int r,int g,int b), 
        * LoadPartImg(const char *,int w,int h,int x,int y), 
        * LoadPartImg(const char *,int w,int h,int x,int y,int r,int g,int b);

    int  drawW, drawH, drawStartX, drawStartY;


    Object_properties();
};


/*---------------------
Function name: IsColiding
Purpose: Deductively reaosns if the two rectangles are colding by returning false if any values are out of range. If all values are in range, a colision is mathmatically required.
Paramaters: Object_properties other(The values of the rect we are comparing)
Returns: False if mathmatical impossibility for colision if found, true if none are found (thus is mathmatically required to be coliding)
Version: 1.0
Level: Core 
---------------------*/
template <typename Object_properties_child>
bool Object_properties::IsColiding(Object_properties_child other)
{
    float bottomA = Object_properties::ypos+Object_properties::width;
    float bottomB = other.ypos+other.width;;

    float topA = Object_properties::ypos;
    float topB = other.ypos;

    float rightA = Object_properties::xpos+Object_properties::width;
    float rightB = other.xpos+other.width;

    float leftA = Object_properties::xpos;
    float leftB = other.xpos;

    //If any of the sides from A are outside of B 
    if( bottomA <= topB ) 
    { 
        return false; 
    } 
    if( topA >= bottomB ) 
    {
        return false;
    } 
    if( rightA <= leftB ) 
    { 
        return false; 
    } 
    if( leftA >= rightB ) 
    { 
        return false; 
    }
    return true;
}


/****************************
Class Snake_window
Description: A handler for a window
Version: 1
Added on 1.1
Level: Engine Core
Todo: 
*****************************/
class Snake_window
{
public:
    SDL_Surface *Screen;
    Object_properties bg;
    int w,h,bpp;
    Snake_window(int w, int h, int bpp, Uint32 SVMFlags);
};
code.cpp
Code:
#include <math.h>
#include <SDL.h>
#include <SDL_TTF.h>
#include <vector>
#include "code.h"

/*---------------------
Function name: Object_properties
Purpose: Constructor for class Object_properties
Paramaters: None
Version: 1.0
Level: Core 
---------------------*/
Object_properties::Object_properties()
{
    xpos   = 0;
    ypos   = 0;
    width  = 0;
    height = 0;
}


/*---------------------
Function name: DistanceFrom
Purpose: Extracts the difference in positions (by pixel) using the distance formula
Paramaters: Object Properties dest (the object that we will be comparing the distance to)
Returns: Distance beween own object and object in paramaters. 
Version: 1.0
Level: Core 
---------------------*/
float Object_properties::DistanceFrom(Object_properties dest)
{
    return sqrt((dest.xpos - Object_properties::xpos) + (dest.ypos - Object_properties::ypos));
}


/*---------------------
Function name: ChangeYRelitave
Purpose: Addes the Y position by the number specified
Paramaters: float change (the value we will add)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeYRelitave(float change)
{
    Object_properties::ypos += change;
    return;
}

/*---------------------
Function name: ChangeYRelitave
Purpose: Addes the X position by the number specified
Paramaters: float change (the value we will add)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeXRelitave(float change)
{
    Object_properties::xpos += change;
    return;
}

/*---------------------
Function name: ChangeYRelitave
Purpose: Addes the X and Y position by the number specified
Paramaters: float X(the value we will add to X), float Y(The value we will add to y)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::changePositionRelitave(float X,float Y)
{
    Object_properties::xpos = X;
    Object_properties::ypos = Y;
    return;
}

/*---------------------
Function name: ChangeYAbsolute
Purpose: Changes the Y position to the specified location
Paramaters: float change (the value we assign Y to)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeYAbsolute(float Y)
{
    Object_properties::ypos = Y;
    return;
}

/*---------------------
Function name: ChangeXAbsolute
Purpose: Changes the X position to the specified location
Paramaters: float change (the value we assign X to)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeXAbsolute(float X)
{
    Object_properties::ypos = X;
    return;
}

/*---------------------
Function name: changePostitionAbsolute
Purpose: Changes the X abd Y position to the specified locations
Paramaters: float X(the value we will change xpos to), float Y(The value we will change ypos to)
Returns: Nothing.
Version: 1.0
Level: Core 
NOTE: This object is redefined in snake
---------------------*/
void Object_properties::changePostitionAbsolute(float X,float Y)
{
    Object_properties::xpos = X;
    Object_properties::ypos = Y;
    return;
}

/*---------------------
Function name: LoadFullImg
Purpose: Loads a complete image and sets the width and height to the total of the image
Paramaters: const char *path (the path of the BMP)
Returns: Returns the class' face
Version: 1.0
Level: Core 
NOTE: Works with the following image types:
        BMP, GIF, JPG, PNG, TIF, PNM, XPM, XCF, PCX, LBM 
Wrappers: LoadFullBMP(const char *path,int r,int g,int b)
---------------------*/
SDL_Surface * Object_properties::LoadFullImg(const char *path)
{
    Object_properties::face = IMG_Load(path);
    if(Object_properties::face == NULL)
    {
        throw "Image not found";
    }
    Object_properties::height = Object_properties::face->h;
    Object_properties::drawStartX = 0;
    Object_properties::drawStartY = 0;
    return Object_properties::face;
}

/*---------------------
Function name: LoadFullBMP
Purpose: Loads a complete BMP with Object_properties::LoadFullBMP(const char *path) and adds RGB transparacy
Paramaters: const char *path (the path of the BMP) int r, int g, int b (the R G and B transparacy values)
Returns: The objects face.
Version: 1.0
Level: Core 
NOTE: None
---------------------*/
SDL_Surface * Object_properties::LoadFullImg(const char *path,int r,int g,int b)
{
    Object_properties::LoadFullImg(path);
    SDL_SetColorKey(Object_properties::face, SDL_SRCCOLORKEY, SDL_MapRGB(Object_properties::face->format, r, g, b));
    return Object_properties::face;
}


/*---------------------
Function name: LoadPartBMP
Purpose: Loads part of the BMP and assigns the dimentions accordingly
Paramaters: const char *path (the path of the BMP), int startX (the starting point X), int startY (The starting point Y), int w (the width), int h (the height)
Returns: The objects face.
Version: 1.0
Level: Core 
NOTE: None
---------------------*/
SDL_Surface * Object_properties::LoadPartImg(const char * path, int startX, int startY, int w, int h)
{
    Object_properties::face = IMG_Load(path);
    Object_properties::drawStartX = startX;
    Object_properties::drawStartY = startY;
    Object_properties::width  = w;
    Object_properties::height = h;
    return Object_properties::face;
}

/*---------------------
Function name: LoadPartBMP
Purpose: Loads a part of a BMP then assigns a transparacy to it
Paramaters: const char *path (the path of the BMP), int startX (the starting point X), int startY (The starting point Y), int w (the width), int h (the height), int r, int g, int b (the RGB tansparacy values)
Returns: The objects face.
Version: 1.0
Level: Core 
NOTE: None
---------------------*/
SDL_Surface * Object_properties::LoadPartImg(const char * path, int startX, int startY, int w, int h, int r, int g, int b)
{
    Object_properties::LoadPartImg(path,startX,startY,w,h);
    SDL_SetColorKey(Object_properties::face, SDL_SRCCOLORKEY, SDL_MapRGB(Object_properties::face->format, r, g, b));
    return Object_properties::face;
}

/*---------------------
Function name: placeOnMap
Purpose: Places an object at a given point on the map
Paramaters: float xp, float yp (the x and y points to place the item on)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: Should probably be removed
---------------------*/
void Object_properties::placeOnMap(float xp, float yp)
{
    Object_properties::xpos = xp;
    Object_properties::ypos = yp;
    return;
}

/*---------------------
Function name: Draw
Purpose: Draws the object's face on the screen
Paramaters: SDL_Surface *screen (the screen to draw it on)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Object_properties::Draw(SDL_Surface *screen)
{
    SDL_Rect dest;
        dest.x = (Sint16)Object_properties::xpos;
        dest.y = (Sint16)Object_properties::ypos;
      SDL_Rect dest2;
          dest2.x = Object_properties::drawStartX;
          dest2.y = Object_properties::drawStartY;
          dest2.w = Object_properties::width;
          dest2.h = Object_properties::height;

        SDL_BlitSurface(Object_properties::face, &dest2, screen, &dest);
    return;
}

/*---------------------
Function name: DrawAll
Purpose: Draws the entire image, regardless of the data regarding size
Paramaters: SDL_Surface *screen (the screen to draw it on)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Object_properties::DrawAll(SDL_Surface *screen)
{
    SDL_Rect dest;
        dest.x = (Sint16)Object_properties::xpos;
        dest.y = (Sint16)Object_properties::ypos;

        SDL_BlitSurface(Object_properties::face, NULL, screen, &dest);
    return;
}

/*---------------------
Function name: Redefine
Purpose: Redefines the size data of the image
Paramaters: int startx, int starty, int w, int h (the start x,y of the image, the width and height of the image)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Object_properties::Redefine(int startx, int starty, int w, int h)
{
    Object_properties::drawStartX = startx;
    Object_properties::drawStartY = starty;
    Object_properties::width  = w;
    Object_properties::height = h;
    return;
}

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
  Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  switch (screen->format->BytesPerPixel)
  {
    case 1: // Assuming 8-bpp
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
        *bufp = color;
      }
      break;
    case 2: // Probably 15-bpp or 16-bpp
      {
        Uint16 *bufp;
        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *bufp = color;
      }
      break;
    case 3: // Slow 24-bpp mode, usually not used
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
        {
          bufp[0] = color;
          bufp[1] = color >> 8;
          bufp[2] = color >> 16;
        } else {
          bufp[2] = color;
          bufp[1] = color >> 8;
          bufp[0] = color >> 16;
        }
      }
      break;
    case 4: // Probably 32-bpp
      {
        Uint32 *bufp;
        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
        *bufp = color;
      }
      break;
  }
}

void Slock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    if ( SDL_LockSurface(screen) < 0 )
    {
      return;
    }
  }
}

void Sulock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    SDL_UnlockSurface(screen);
  }
}


Snake_input::Snake_input()
{
    SDL_SetCursor(NULL);
}
/*---------------------
Function name: isKeyDown
Purpose: Detects if a given key is down
Paramaters: SDLKey Key (the key to check for)
Returns: true if key is pressed, false if it is not
Version: 1.0
Level: Core 
NOTE: 
---------------------*/

bool Snake_input::isKeyDown(SDLKey Key)
{
    keys = SDL_GetKeyState(NULL);
    if ( keys[Key] ) 
    {  
        return true;
    }
    return false;
}


bool Snake_input::hasEvent()
{
    if(SDL_PollEvent(&sEvent) == 1)
    {
        return true;
    }
    return false;
}
void Snake_input::updateMouseStatus()
{
    mouse.x = Snake_input::sEvent.motion.x;
    mouse.y = Snake_input::sEvent.motion.y;
}

/*---------------------
Function name: Snake_timer
Purpose: Initilizes the timer
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
Snake_timer::Snake_timer() 
{ 
       //Initialize the variables 
       startTicks = 0; 
       pausedTicks = 0; 
       paused = false; 
       started = false; 
}

/*---------------------
Function name: start
Purpose: Starts the timper
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::start() 
{ 
     //Start the timer 
     started = true; 
     //Unpause the timer 
     paused = false; 
     //Get the current clock time 
     startTicks = SDL_GetTicks(); 
     return;
}

/*---------------------
Function name: stop
Purpose: Stops the timer
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::stop() 
{ 
     //Stop the timer 
     started = false; 
     //Unpause the timer 
     paused = false; 
     return;
}

/*---------------------
Function name: get_ticks
Purpose: Gets the number of MS since the timer has been initilized or reset.
Paramaters: None
Returns: The number of milliseconds it has been since ilitilization or last reset
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
int Snake_timer::get_ticks() 
{ 
    //If the timer is running 
    if( started == true ) 
    { 
        //If the timer is paused 
        if( paused == true ) 
        { 
            //Return the number of ticks when the timer was paused 
            return pausedTicks; 
        } 
        else 
        {
              //Return the current time minus the start time 
              return SDL_GetTicks() - startTicks; 
        } 
    } //If the timer isn't running 
return 0; 
}


/*---------------------
Function name: pause
Purpose: Pauses the timer without reseting it
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::pause() 
{ 
     //If the timer is running and isn't already paused 
     if( ( started == true ) && ( paused == false ) ) 
     { 
         //Pause the timer 
         paused = true; 
         //Calculate the paused ticks 
         pausedTicks = SDL_GetTicks() - startTicks; 
     } 
}

/*---------------------
Function name: pause
Purpose: Starts the timer again
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::unpause() 
{ 
     //If the timer is paused 
     if( paused == true ) 
     { 
         //Unpause the timer 
         paused = false; 
         //Reset the starting ticks 
         startTicks = SDL_GetTicks() - pausedTicks; 
         //Reset the paused ticks
          pausedTicks = 0; 
     } 
}

/*---------------------
Function name: is_started()
Purpose: Tells if the timer has been started
Paramaters: None
Returns: the value of the started variable
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
bool Snake_timer::is_started() 
{ 
     return started; 
} 

/*---------------------
Function name: is_paused()
Purpose: Tells if the timer has been paused
Paramaters: None
Returns: the value of the paused variable
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
bool Snake_timer::is_paused() 
{ 
     return paused; 
}

/*---------------------
Function name: reset()
Purpose: resets the timer
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::reset()
{
    startTicks = SDL_GetTicks();
}

Snake_window::Snake_window(int w, int h, int bpp, Uint32 SVMFlags)
{
    Screen = SDL_SetVideoMode( 800,600, 32, SVMFlags );
}
__________________

Village Idiot is offline  
Reply With Quote
Old 04-18-2009, 07:34 PM   #3 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
The best way to learn classes is to find something you want to make, I've found that classes for the sake of making one often teach bad design since there is no real reason you are making it. Objects for for reusable code, which will come up in any sizable project. So find a full project you want to build and you will probably find an object or two you want to make.

For instance, in the last game I made, I had a number of classes that I wrote because there was a clear and present advantage. I have a general game object class with the ability to do a lot of things. Now that I am writing a different one, I am able to literally copy and paste the classes over to use them since they are not dependent on much.

ps. If your interested, heres the code
code.h
Code:
/****************************
Class Snake_timer
Description: A timer than can give the time in MS between a start and stop
Version: 1
Added on initial build
Level: Engine Core
Todo: Complete
*****************************/
class Snake_timer 
{ 
private: 
    //The clock time when the timer started 
    int startTicks; 
    //The ticks stored when the timer was paused 
    int pausedTicks; 
    //The timer status 
    bool paused; 
    bool started; 
public: 
    //Initializes variables 
    Snake_timer(); 
    //The various clock actions 
    void start(); 
    void stop(); 
    void pause(); 
    void unpause(); 
    void reset();
    //Gets the timer's time 
    int get_ticks(); 
    //Checks the status of the timer 
    bool is_started(); 
    bool is_paused(); 
}; 

/****************************
Class Snake_MCoords
Description: Containes mouse X, Y and which button in a single struct
Version: 1
Added on initial build
Level: Engine Core
Todo: Impliment with input class
*****************************/
struct Snake_MCoords
{
    int x,
        y,
        button;
};

/****************************
Class Snake_input
Description: The class for accepting all types of input
Version: 1
Added on initial build
Level: Engine Core
Todo: Add mouse detection abilities
*****************************/
class Snake_input
{
public: 
    Uint8* keys;
    SDL_Event sEvent;

    Snake_MCoords mouse;
    Snake_input();
    void updateMouseStatus();
    bool isKeyDown(SDLKey Key);
    bool hasEvent();

};



/****************************
Class Object_properties
Description: Containes the essental parts of every object. To be directly implimented
                in no higher level than game core
Version: 1
Added on initial build
Level: Engine Core
Todo: Complete
*****************************/
class Object_properties
{
public:
    float xpos,ypos; 
    int   width,height;

    template <typename Object_properties_child>
    bool  IsColiding(Object_properties_child other);
    float DistanceFrom(Object_properties);
    void  
        ChangeXRelitave(float x), 
        ChangeYRelitave(float y), 
        changePositionRelitave(float x,float y);

    void  
        ChangeXAbsolute(float x), 
        ChangeYAbsolute(float y), 
        changePostitionAbsolute(float x,float y);

    void  
        placeOnMap(float x,float y), 
        Redefine(int x, int y, int w, int h);

    void move(int dir);

    SDL_Surface *face;
    void Draw(SDL_Surface *),
         DrawAll(SDL_Surface *screen);

    SDL_Surface 
        * LoadFullImg(const char *), 
        * LoadFullImg(const char *,int r,int g,int b), 
        * LoadPartImg(const char *,int w,int h,int x,int y), 
        * LoadPartImg(const char *,int w,int h,int x,int y,int r,int g,int b);

    int  drawW, drawH, drawStartX, drawStartY;


    Object_properties();
};


/*---------------------
Function name: IsColiding
Purpose: Deductively reaosns if the two rectangles are colding by returning false if any values are out of range. If all values are in range, a colision is mathmatically required.
Paramaters: Object_properties other(The values of the rect we are comparing)
Returns: False if mathmatical impossibility for colision if found, true if none are found (thus is mathmatically required to be coliding)
Version: 1.0
Level: Core 
---------------------*/
template <typename Object_properties_child>
bool Object_properties::IsColiding(Object_properties_child other)
{
    float bottomA = Object_properties::ypos+Object_properties::width;
    float bottomB = other.ypos+other.width;;

    float topA = Object_properties::ypos;
    float topB = other.ypos;

    float rightA = Object_properties::xpos+Object_properties::width;
    float rightB = other.xpos+other.width;

    float leftA = Object_properties::xpos;
    float leftB = other.xpos;

    //If any of the sides from A are outside of B 
    if( bottomA <= topB ) 
    { 
        return false; 
    } 
    if( topA >= bottomB ) 
    {
        return false;
    } 
    if( rightA <= leftB ) 
    { 
        return false; 
    } 
    if( leftA >= rightB ) 
    { 
        return false; 
    }
    return true;
}


/****************************
Class Snake_window
Description: A handler for a window
Version: 1
Added on 1.1
Level: Engine Core
Todo: 
*****************************/
class Snake_window
{
public:
    SDL_Surface *Screen;
    Object_properties bg;
    int w,h,bpp;
    Snake_window(int w, int h, int bpp, Uint32 SVMFlags);
};
code.cpp
Code:
#include <math.h>
#include <SDL.h>
#include <SDL_TTF.h>
#include <vector>
#include "code.h"

/*---------------------
Function name: Object_properties
Purpose: Constructor for class Object_properties
Paramaters: None
Version: 1.0
Level: Core 
---------------------*/
Object_properties::Object_properties()
{
    xpos   = 0;
    ypos   = 0;
    width  = 0;
    height = 0;
}


/*---------------------
Function name: DistanceFrom
Purpose: Extracts the difference in positions (by pixel) using the distance formula
Paramaters: Object Properties dest (the object that we will be comparing the distance to)
Returns: Distance beween own object and object in paramaters. 
Version: 1.0
Level: Core 
---------------------*/
float Object_properties::DistanceFrom(Object_properties dest)
{
    return sqrt((dest.xpos - Object_properties::xpos) + (dest.ypos - Object_properties::ypos));
}


/*---------------------
Function name: ChangeYRelitave
Purpose: Addes the Y position by the number specified
Paramaters: float change (the value we will add)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeYRelitave(float change)
{
    Object_properties::ypos += change;
    return;
}

/*---------------------
Function name: ChangeYRelitave
Purpose: Addes the X position by the number specified
Paramaters: float change (the value we will add)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeXRelitave(float change)
{
    Object_properties::xpos += change;
    return;
}

/*---------------------
Function name: ChangeYRelitave
Purpose: Addes the X and Y position by the number specified
Paramaters: float X(the value we will add to X), float Y(The value we will add to y)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::changePositionRelitave(float X,float Y)
{
    Object_properties::xpos = X;
    Object_properties::ypos = Y;
    return;
}

/*---------------------
Function name: ChangeYAbsolute
Purpose: Changes the Y position to the specified location
Paramaters: float change (the value we assign Y to)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeYAbsolute(float Y)
{
    Object_properties::ypos = Y;
    return;
}

/*---------------------
Function name: ChangeXAbsolute
Purpose: Changes the X position to the specified location
Paramaters: float change (the value we assign X to)
Returns: Nothing.
Version: 1.0
Level: Core 
---------------------*/
void Object_properties::ChangeXAbsolute(float X)
{
    Object_properties::ypos = X;
    return;
}

/*---------------------
Function name: changePostitionAbsolute
Purpose: Changes the X abd Y position to the specified locations
Paramaters: float X(the value we will change xpos to), float Y(The value we will change ypos to)
Returns: Nothing.
Version: 1.0
Level: Core 
NOTE: This object is redefined in snake
---------------------*/
void Object_properties::changePostitionAbsolute(float X,float Y)
{
    Object_properties::xpos = X;
    Object_properties::ypos = Y;
    return;
}

/*---------------------
Function name: LoadFullImg
Purpose: Loads a complete image and sets the width and height to the total of the image
Paramaters: const char *path (the path of the BMP)
Returns: Returns the class' face
Version: 1.0
Level: Core 
NOTE: Works with the following image types:
        BMP, GIF, JPG, PNG, TIF, PNM, XPM, XCF, PCX, LBM 
Wrappers: LoadFullBMP(const char *path,int r,int g,int b)
---------------------*/
SDL_Surface * Object_properties::LoadFullImg(const char *path)
{
    Object_properties::face = IMG_Load(path);
    if(Object_properties::face == NULL)
    {
        throw "Image not found";
    }
    Object_properties::height = Object_properties::face->h;
    Object_properties::drawStartX = 0;
    Object_properties::drawStartY = 0;
    return Object_properties::face;
}

/*---------------------
Function name: LoadFullBMP
Purpose: Loads a complete BMP with Object_properties::LoadFullBMP(const char *path) and adds RGB transparacy
Paramaters: const char *path (the path of the BMP) int r, int g, int b (the R G and B transparacy values)
Returns: The objects face.
Version: 1.0
Level: Core 
NOTE: None
---------------------*/
SDL_Surface * Object_properties::LoadFullImg(const char *path,int r,int g,int b)
{
    Object_properties::LoadFullImg(path);
    SDL_SetColorKey(Object_properties::face, SDL_SRCCOLORKEY, SDL_MapRGB(Object_properties::face->format, r, g, b));
    return Object_properties::face;
}


/*---------------------
Function name: LoadPartBMP
Purpose: Loads part of the BMP and assigns the dimentions accordingly
Paramaters: const char *path (the path of the BMP), int startX (the starting point X), int startY (The starting point Y), int w (the width), int h (the height)
Returns: The objects face.
Version: 1.0
Level: Core 
NOTE: None
---------------------*/
SDL_Surface * Object_properties::LoadPartImg(const char * path, int startX, int startY, int w, int h)
{
    Object_properties::face = IMG_Load(path);
    Object_properties::drawStartX = startX;
    Object_properties::drawStartY = startY;
    Object_properties::width  = w;
    Object_properties::height = h;
    return Object_properties::face;
}

/*---------------------
Function name: LoadPartBMP
Purpose: Loads a part of a BMP then assigns a transparacy to it
Paramaters: const char *path (the path of the BMP), int startX (the starting point X), int startY (The starting point Y), int w (the width), int h (the height), int r, int g, int b (the RGB tansparacy values)
Returns: The objects face.
Version: 1.0
Level: Core 
NOTE: None
---------------------*/
SDL_Surface * Object_properties::LoadPartImg(const char * path, int startX, int startY, int w, int h, int r, int g, int b)
{
    Object_properties::LoadPartImg(path,startX,startY,w,h);
    SDL_SetColorKey(Object_properties::face, SDL_SRCCOLORKEY, SDL_MapRGB(Object_properties::face->format, r, g, b));
    return Object_properties::face;
}

/*---------------------
Function name: placeOnMap
Purpose: Places an object at a given point on the map
Paramaters: float xp, float yp (the x and y points to place the item on)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: Should probably be removed
---------------------*/
void Object_properties::placeOnMap(float xp, float yp)
{
    Object_properties::xpos = xp;
    Object_properties::ypos = yp;
    return;
}

/*---------------------
Function name: Draw
Purpose: Draws the object's face on the screen
Paramaters: SDL_Surface *screen (the screen to draw it on)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Object_properties::Draw(SDL_Surface *screen)
{
    SDL_Rect dest;
        dest.x = (Sint16)Object_properties::xpos;
        dest.y = (Sint16)Object_properties::ypos;
      SDL_Rect dest2;
          dest2.x = Object_properties::drawStartX;
          dest2.y = Object_properties::drawStartY;
          dest2.w = Object_properties::width;
          dest2.h = Object_properties::height;

        SDL_BlitSurface(Object_properties::face, &dest2, screen, &dest);
    return;
}

/*---------------------
Function name: DrawAll
Purpose: Draws the entire image, regardless of the data regarding size
Paramaters: SDL_Surface *screen (the screen to draw it on)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Object_properties::DrawAll(SDL_Surface *screen)
{
    SDL_Rect dest;
        dest.x = (Sint16)Object_properties::xpos;
        dest.y = (Sint16)Object_properties::ypos;

        SDL_BlitSurface(Object_properties::face, NULL, screen, &dest);
    return;
}

/*---------------------
Function name: Redefine
Purpose: Redefines the size data of the image
Paramaters: int startx, int starty, int w, int h (the start x,y of the image, the width and height of the image)
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Object_properties::Redefine(int startx, int starty, int w, int h)
{
    Object_properties::drawStartX = startx;
    Object_properties::drawStartY = starty;
    Object_properties::width  = w;
    Object_properties::height = h;
    return;
}

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
  Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  switch (screen->format->BytesPerPixel)
  {
    case 1: // Assuming 8-bpp
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
        *bufp = color;
      }
      break;
    case 2: // Probably 15-bpp or 16-bpp
      {
        Uint16 *bufp;
        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *bufp = color;
      }
      break;
    case 3: // Slow 24-bpp mode, usually not used
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
        {
          bufp[0] = color;
          bufp[1] = color >> 8;
          bufp[2] = color >> 16;
        } else {
          bufp[2] = color;
          bufp[1] = color >> 8;
          bufp[0] = color >> 16;
        }
      }
      break;
    case 4: // Probably 32-bpp
      {
        Uint32 *bufp;
        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
        *bufp = color;
      }
      break;
  }
}

void Slock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    if ( SDL_LockSurface(screen) < 0 )
    {
      return;
    }
  }
}

void Sulock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    SDL_UnlockSurface(screen);
  }
}


Snake_input::Snake_input()
{
    SDL_SetCursor(NULL);
}
/*---------------------
Function name: isKeyDown
Purpose: Detects if a given key is down
Paramaters: SDLKey Key (the key to check for)
Returns: true if key is pressed, false if it is not
Version: 1.0
Level: Core 
NOTE: 
---------------------*/

bool Snake_input::isKeyDown(SDLKey Key)
{
    keys = SDL_GetKeyState(NULL);
    if ( keys[Key] ) 
    {  
        return true;
    }
    return false;
}


bool Snake_input::hasEvent()
{
    if(SDL_PollEvent(&sEvent) == 1)
    {
        return true;
    }
    return false;
}
void Snake_input::updateMouseStatus()
{
    mouse.x = Snake_input::sEvent.motion.x;
    mouse.y = Snake_input::sEvent.motion.y;
}

/*---------------------
Function name: Snake_timer
Purpose: Initilizes the timer
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
Snake_timer::Snake_timer() 
{ 
       //Initialize the variables 
       startTicks = 0; 
       pausedTicks = 0; 
       paused = false; 
       started = false; 
}

/*---------------------
Function name: start
Purpose: Starts the timper
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::start() 
{ 
     //Start the timer 
     started = true; 
     //Unpause the timer 
     paused = false; 
     //Get the current clock time 
     startTicks = SDL_GetTicks(); 
     return;
}

/*---------------------
Function name: stop
Purpose: Stops the timer
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::stop() 
{ 
     //Stop the timer 
     started = false; 
     //Unpause the timer 
     paused = false; 
     return;
}

/*---------------------
Function name: get_ticks
Purpose: Gets the number of MS since the timer has been initilized or reset.
Paramaters: None
Returns: The number of milliseconds it has been since ilitilization or last reset
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
int Snake_timer::get_ticks() 
{ 
    //If the timer is running 
    if( started == true ) 
    { 
        //If the timer is paused 
        if( paused == true ) 
        { 
            //Return the number of ticks when the timer was paused 
            return pausedTicks; 
        } 
        else 
        {
              //Return the current time minus the start time 
              return SDL_GetTicks() - startTicks; 
        } 
    } //If the timer isn't running 
return 0; 
}


/*---------------------
Function name: pause
Purpose: Pauses the timer without reseting it
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::pause() 
{ 
     //If the timer is running and isn't already paused 
     if( ( started == true ) && ( paused == false ) ) 
     { 
         //Pause the timer 
         paused = true; 
         //Calculate the paused ticks 
         pausedTicks = SDL_GetTicks() - startTicks; 
     } 
}

/*---------------------
Function name: pause
Purpose: Starts the timer again
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::unpause() 
{ 
     //If the timer is paused 
     if( paused == true ) 
     { 
         //Unpause the timer 
         paused = false; 
         //Reset the starting ticks 
         startTicks = SDL_GetTicks() - pausedTicks; 
         //Reset the paused ticks
          pausedTicks = 0; 
     } 
}

/*---------------------
Function name: is_started()
Purpose: Tells if the timer has been started
Paramaters: None
Returns: the value of the started variable
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
bool Snake_timer::is_started() 
{ 
     return started; 
} 

/*---------------------
Function name: is_paused()
Purpose: Tells if the timer has been paused
Paramaters: None
Returns: the value of the paused variable
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
bool Snake_timer::is_paused() 
{ 
     return paused; 
}

/*---------------------
Function name: reset()
Purpose: resets the timer
Paramaters: None
Returns: Nothing
Version: 1.0
Level: Core 
NOTE: 
---------------------*/
void Snake_timer::reset()
{
    startTicks = SDL_GetTicks();
}

Snake_window::Snake_window(int w, int h, int bpp, Uint32 SVMFlags)
{
    Screen = SDL_SetVideoMode( 800,600, 32, SVMFlags );
}

Thanks VI,

Yeah, I know what you mean. The pagination class I just wrote I will be implementing on my site. I think one does learn better when they are writing code for something that they need rather than just an exercise....

I wish I knew C so I could understand the code that you just wrote. When you said "game", I thought you were coding some game in PHP!!! But now I see that it is in C....

Speaking of games, I was thinking about picking up a book on Objective C to see how difficult it is to learn that for programming iPhone apps...
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 07:41 PM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
Speaking of games, I was thinking about picking up a book on Objective C to see how difficult it is to learn that for programming iPhone apps...
The code you read there was C++, not C. C is very similar to C++, the main difference in that C does not have classes. I don't know any Objective C so I can't comment, but C++ is fairly difficult. The syntax is similar to PHP so that won't give you trouble, but things are overall harder. I initially learned form the book C++ Programming Fundementals by Chuck Easttom and learned from various other resources from both books and the internet after that.

Going specifically to iPhone apps, I have no idea. Mac programming is easier to do than Windows programming, but I have only ever programmed for Windows. You will have to learn your language and a graphics library, probably OpenGL.
__________________

Village Idiot is offline  
Reply With Quote
Old 04-18-2009, 07:51 PM   #5 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

[quote=Village Idiot;23209]
Quote:
Originally Posted by allworknoplay View Post
Speaking of games, I was thinking about picking up a book on Objective C to see how difficult it is to learn that for programming iPhone apps...
[/code]

The code you read there was C++, not C. C is very similar to C++, the main difference in that C does not have classes. I don't know any Objective C so I can't comment, but C++ is fairly difficult. The syntax is similar to PHP so that won't give you trouble, but things are overall harder. I initially learned form the book C++ Programming Fundementals by Chuck Easttom and learned from various other resources from both books and the internet after that.

Going specifically to iPhone apps, I have no idea. Mac programming is easier to do than Windows programming, but I have only ever programmed for Windows. You will have to learn your language and a graphics library, probably OpenGL.
Smacks head! Yes you're right...I think I read that C++ isn't true object-oriented but makes use of OO features? You also have to handle memory management yourself right?

Anyways, don't want to get too OT....

Must find my next PHP class project!

PS - the last book I read was "The Object-Oriented Thought Process" by Matt Weisfeld.
allworknoplay is offline  
Reply With Quote
Old 04-18-2009, 08:29 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Build a database class, you'll need it for almost all the future projects, so it's a good thing to have in your pocket!
__________________
Tanax is offline  
Reply With Quote
Old 04-18-2009, 08:33 PM   #7 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Build a database class, you'll need it for almost all the future projects, so it's a good thing to have in your pocket!
Ok...when you say DB class...what exactly is it suppose to do?

I'm a bit confused with what I see out there like PDO?

I don't mind building any class right now, it will only help me, but what is the difference between a DB class and a DB wrapper?
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Freelance Suite: Client & Project Management Software CLCook Show Off 2 09-14-2008 10:50 AM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 07:42 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design