home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / objects / sprite.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-07-01  |  12.5 KB  |  388 lines

  1. /***************************************************************************
  2.  * sprite.h  -  header for the corresponding cpp file
  3.  *
  4.  * Copyright (C) 2003 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #ifndef SMC_SPRITE_H
  17. #define SMC_SPRITE_H
  18.  
  19. #include "../core/globals.h"
  20. #include "../core/math/rect.h"
  21. #include "../video/video.h"
  22. #include "../core/collision.h"
  23.  
  24. /* *** *** *** *** *** *** *** cEditor_Object_Settings_Item *** *** *** *** *** *** *** *** *** *** */
  25.  
  26. class cEditor_Object_Settings_Item
  27. {
  28. public:
  29.     cEditor_Object_Settings_Item( void );
  30.     ~cEditor_Object_Settings_Item( void );
  31.  
  32.     // name
  33.     CEGUI::Window *window_name;
  34.     // settings
  35.     CEGUI::Window *window_setting;
  36.     // if set start new row
  37.     bool advance_row;
  38. };
  39.  
  40. /* *** *** *** *** *** *** *** cCollidingSprite *** *** *** *** *** *** *** *** *** *** */
  41.  
  42. class cCollidingSprite
  43. {
  44. public:
  45.     cCollidingSprite( void );
  46.     virtual ~cCollidingSprite( void );
  47.  
  48.     // Parses the collision data
  49.     void Parse_Collisions( void );
  50.     // Handle collision data
  51.     void Handle_Collisions( void );
  52.     /* Add a collision to the collision list
  53.      * returns true if successful
  54.      *
  55.      * base : base/origin object
  56.      * col : colliding object ( only needed if the collision direction is needed )
  57.      * col_id : colliding object identifier
  58.      * col_type : collision object type
  59.      * add_if_new : add the collision only if the object doesn't already collide ( if not added it's deleted )
  60.     */
  61.     bool Collision_Add( cSprite *base, cSprite *col, unsigned int col_id = 0, ObjectCollisionType col_type = CO_NOTHING, bool add_if_new = 0 );
  62.     /* Add a collision object to the collision list
  63.      * returns true if successful
  64.      * add_if_new : add the collision only if the collision object doesn't already collide
  65.      */
  66.     bool Collision_Add( cObjectCollision *collision, bool add_if_new = 0 );
  67.     // Delete the given collision from the list
  68.     void Collision_Delete( cObjectCollision *collision );
  69.     // Delete the last collision
  70.     void Collision_Delete_last( void );
  71.     /* Check if a collision is in the given direction 
  72.      * and returns the collision object number else -1
  73.      */
  74.     int Collision_Check_Direction( ObjectDirection dir );
  75.     // Returns the first added collision
  76.     cObjectCollision *Collision_Get_first( void );
  77.     /* Returns the last added collision
  78.      * if only_blocking is set only movement blocking collisions are returned
  79.     */
  80.     cObjectCollision *Collision_Get_last( bool only_blocking = 0 );
  81.     // Clear the Collision list
  82.     void Collisions_Clear( void );
  83.  
  84.     // default collision handler
  85.     virtual void Handle_Collision( cObjectCollision *collision );
  86.     // collision from player
  87.     virtual void Handle_Collision_Player( cObjectCollision *collision );
  88.     // collision from an enemy
  89.     virtual void Handle_Collision_Enemy( cObjectCollision *collision );
  90.     // collision with massive
  91.     virtual void Handle_Collision_Massive( cObjectCollision *collision );
  92.     // collision from a box
  93.     virtual void Handle_Collision_Box( ObjectDirection cdirection, GL_rect *r2 );
  94.  
  95.     // object collision list
  96.     ObjectCollisionList collisions;
  97. };
  98.  
  99. /* *** *** *** *** *** *** *** cSprite *** *** *** *** *** *** *** *** *** *** */
  100.  
  101. class cSprite : public cCollidingSprite
  102. {
  103. public:
  104.     // if del_img is set the given image will be deleted on change or class deletion
  105.     cSprite( GL_Surface *new_image = NULL, float x = 0, float y = 0, bool del_img = 0 );
  106.     // create from stream
  107.     cSprite( CEGUI::XMLAttributes &attributes );
  108.     // destructor
  109.     virtual ~cSprite( void );
  110.  
  111.     // Init defaults
  112.     virtual void Init( void );
  113.     // copy this sprite
  114.     virtual cSprite *Copy( void );
  115.  
  116.     // create from stream
  117.     virtual void Create_from_Stream( CEGUI::XMLAttributes &attributes );
  118.     // save to stream
  119.     virtual void Save_to_Stream( ofstream &file );
  120.  
  121.     // load from savegame
  122.     virtual void Load_from_Savegame( cSave_Level_Object *save_object );
  123.     // save to savegame
  124.     virtual cSave_Level_Object *Save_to_Savegame( void );
  125.  
  126.     /* Sets the image for drawing
  127.      * if new_start_image is set the default start_image will be set to the given image
  128.      * if del_img is set the given image will be deleted
  129.     */
  130.     virtual void Set_Image( GL_Surface *new_image, bool new_start_image = 0, bool del_img = 0 );
  131.  
  132.     // Set the sprite type
  133.     void Set_Sprite_Type( SpriteType ntype );
  134.     // Returns the sprite type as string
  135.     string Get_Sprite_Type_String( void );
  136.  
  137.     /* Set if the camera should be ignored
  138.      * default : disabled
  139.     */
  140.     void Set_Ignore_Camera( bool enable = 0 );
  141.  
  142.     // Sets the Position
  143.     void Set_Pos( float x, float y, bool new_startpos = 0 );
  144.     void Set_Pos_X( float x, bool new_startpos = 0 );
  145.     void Set_Pos_Y( float y, bool new_startpos = 0 );
  146.     // Set if visible
  147.     void Set_Visible( bool enabled );
  148.     /* Set the shadow
  149.      * if position is set to 0 the shadow is disabled
  150.     */
  151.     void Set_Shadow( Color shadow, float pos );
  152.     /* Set the shadow position
  153.      * if set to 0 the shadow is disabled
  154.     */
  155.     void Set_Shadow_Pos( float pos );
  156.     // Set the shadow color
  157.     void Set_Shadow_Color( Color shadow );
  158.     // Set image color
  159.     void Set_Color( Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha = 255 );
  160.     void Set_Color( Color col );
  161.     /* Set a Color Combination ( GL_ADD, GL_MODULATE or GL_REPLACE )
  162.      * Addition ( adds white to color )
  163.      * 1.0 is the maximum and the given color will be white
  164.      * 0.0 is the minimum and the color will have the default color
  165.      * Modulation ( adds black to color )
  166.      * 1.0 is the maximum and the color will have the default color
  167.      * 0.0 is the minimum and the given color will be black
  168.      * Replace ( replaces color value )
  169.      * 1.0 is the maximum and the given color has maximum value
  170.      * 0.0 is the minimum and the given color has minimum value
  171.     */
  172.     void Set_Color_Combine( float red, float green, float blue, GLint com_type );
  173.  
  174.     /* Set if rotation affects the collision rect
  175.      * only supports 90░ steps currently
  176.      * if enabled col_pos, col_rect and rect must be reset manually before changing rotation
  177.     */
  178.     void Set_Rotation_affects_Rect( bool enable = 0 );
  179.     /* Set the rect rotation
  180.      * does not reset col_pos, col_rect and rect before rotation
  181.      * default : disabled
  182.     */
  183.     void Update_Rect_Rotation( void );
  184.     // Set the X rect rotation
  185.     void Update_Rect_Rotation_X( void );
  186.     // Set the Y rect rotation
  187.     void Update_Rect_Rotation_Y( void );
  188.     // Set the Z rect rotation
  189.     void Update_Rect_Rotation_Z( void );
  190.  
  191.     // Set the X rotation
  192.     void Set_Rotation_X( float rot, bool new_start_rot = 0 );
  193.     // Set the Y rotation
  194.     void Set_Rotation_Y( float rot, bool new_start_rot = 0 );
  195.     // Set the Z rotation
  196.     void Set_Rotation_Z( float rot, bool new_start_rot = 0 );
  197.     // Set the rotation
  198.     void Set_Rotation( float x, float y, float z, bool new_start_rot = 0 );
  199.  
  200.     // Add X rotation
  201.     void Add_Rotation_X( float rot );
  202.     // Add Y rotation
  203.     void Add_Rotation_Y( float rot );
  204.     // Add Z rotation
  205.     void Add_Rotation_Z( float rot );
  206.     // Add rotation
  207.     void Add_Rotation( float x, float y, float z );
  208.  
  209.     /* Set if scale affects the collision rect
  210.      * if enabled previous scale is always undone from rect before setting the new value
  211.      * default : disabled
  212.     */
  213.     void Set_Scale_affects_Rect( bool enable = 0 );
  214.     /* Set which directions of the image get scaled
  215.      * if all are set scaling is centered and if all are not set scaling is disabled
  216.      * todo : if enabled with scale_affects_rect it should also scale the rect position
  217.     */
  218.     void Set_Scale_Directions( bool up = 0, bool down = 1, bool left = 0, bool right = 1 );
  219.     // Set the scale
  220.     void Set_Scale_X( const float scale, const bool new_startscale = 0 );
  221.     void Set_Scale_Y( const float scale, const bool new_startscale = 0 );
  222.     void Set_Scale( const float scale, const bool new_startscale = 0 );
  223.  
  224.     // Add scale
  225.     void Add_Scale_X( const float val );
  226.     void Add_Scale_Y( const float val );
  227.     void Add_Scale( const float val );
  228.  
  229.     // Set this sprite on top of the given one
  230.     void Set_on_Top( const cSprite *sprite, bool optimize_hor_pos = 1 );
  231.  
  232.     /* Move this object
  233.      * real : if set the speedfactor is not used
  234.     */
  235.     virtual void Move( float move_x, float move_y, const bool real = 0 );
  236.  
  237.     // default collision and movement handling
  238.     virtual void Collide_Move( void );
  239.  
  240.     // Update the position rect values
  241.     void Update_Position_Rect( void );
  242.     // default update
  243.     virtual void Update( void );
  244.     // update drawing validation
  245.     virtual void Update_Valid_Draw( void );
  246.     // update updating validation
  247.     virtual void Update_Valid_Update( void );
  248.  
  249.     /* Draw
  250.     * if request is NULL automatically creates the request
  251.     */
  252.     virtual void Draw( cSurfaceRequest *request = NULL );
  253.     /* only draws the image
  254.      * no position nor debug updates
  255.     */
  256.     void Draw_Image( cSurfaceRequest *request = NULL );
  257.  
  258.     /* Set the massive type
  259.      * should be called after setting the new array
  260.     */
  261.     virtual void Set_Massive_Type( MassiveType mtype );
  262.  
  263.     // Check if this sprite is on top of the given object
  264.     bool Is_on_Top( const cSprite *obj );
  265.  
  266.     // if the sprite is visible on the screen
  267.     bool Is_Visible_on_Screen( void );
  268.     // if the Object is in player range
  269.     bool is_Player_range( void );
  270.     // if update is valid for the current state
  271.     virtual bool Is_Update_Valid( void );
  272.     // if draw is valid for the current state and position
  273.     virtual bool Is_Draw_Valid( void );
  274.  
  275.     // deletes this sprite on the next frame start
  276.     virtual void Destroy( void );
  277.  
  278.     // editor add window object
  279.     void Editor_Add( const CEGUI::String name, const CEGUI::String tooltip, CEGUI::Window *window_setting, float obj_width, float obj_height = 28, bool advance_row = 1 );
  280.     // editor activation
  281.     virtual void Editor_Activate( void );
  282.     // editor deactivation
  283.     virtual void Editor_Deactivate( void );
  284.     // editor init
  285.     virtual void Editor_Init( void );
  286.     // editor position update
  287.     virtual void Editor_Position_Update( void );
  288.     // editor state update
  289.     virtual void Editor_State_Update( void );
  290.  
  291.     // current image used for drawing
  292.     GL_Surface *image;
  293.     // editor and first image
  294.     GL_Surface *start_image;
  295.  
  296.     // complete image rect
  297.     GL_rect rect;
  298.     // editor and first image rect
  299.     GL_rect start_rect;
  300.     // collision rect
  301.     GL_rect col_rect;
  302.     // collision start point
  303.     GL_point col_pos;
  304.  
  305.     // current position
  306.     float posx, posy, posz;
  307.     // start position
  308.     float startposx, startposy;
  309.     /* editor z position
  310.      * will be only used if not 0
  311.     */
  312.     float editor_posz;
  313.  
  314.     // if set rotation not only affects the image but also the rectangle
  315.     bool rotation_affects_rect;
  316.     // editor and start rotation
  317.     float start_rotx, start_roty, start_rotz;
  318.     // rotation
  319.     float rotx, roty, rotz;
  320.     // if set scale not only affects the image but also the rectangle
  321.     bool scale_affects_rect;
  322.     /* which parts of the image get scaled
  323.      * if all are set scaling is centered
  324.     */
  325.     bool scale_up, scale_down, scale_left, scale_right;
  326.     // editor and start scale
  327.     float start_scalex, start_scaley;
  328.     // scale
  329.     float scalex, scaley;
  330.  
  331.     // color
  332.     Color color;
  333.     // combine type
  334.     GLint combine_type;
  335.     // combine color
  336.     float combine_col[3];
  337.  
  338.     // sprite type
  339.     SpriteType type;
  340.     // sprite array type
  341.     ArrayType sprite_array;
  342.     // massive collision type
  343.     MassiveType massivetype;
  344.     // sprite visible name
  345.     string name;
  346.     // sprite editor tags
  347.     string editor_tags;
  348.  
  349.     // disable the camera
  350.     bool no_camera;
  351.     // is it visible
  352.     bool visible;
  353.     // if spawned it shouldn't be saved
  354.     bool spawned;
  355.     // range to the player to get updates
  356.     unsigned int player_range;
  357.     // can be used as ground object
  358.     bool can_be_ground;
  359.  
  360.     // delete the given image when it gets unloaded
  361.     bool delete_image;
  362.     // if true delete this sprite on the next frame
  363.     bool destroy;
  364.     // shadow position
  365.     float shadow_pos;
  366.     // shadow color
  367.     Color shadow_color;
  368.  
  369.     // if drawing is valid
  370.     bool valid_draw;
  371.     // if updating is valid
  372.     bool valid_update;
  373.  
  374.     // editor active window list
  375.     typedef vector<cEditor_Object_Settings_Item *> Editor_Object_Settings_List;
  376.     Editor_Object_Settings_List editor_windows;
  377.     // width for all name windows based on largest name text width
  378.     float editor_window_name_width;
  379. };
  380.  
  381. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  382.  
  383. typedef vector<cSprite *> SpriteList;
  384.  
  385. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  386.  
  387. #endif
  388.