Results 1 to 9 of 9

Thread: How Can I forward Declare a struct?

  1. #1
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default How Can I forward Declare a struct?

    I decided to write the entire SRL include into a language I know quite well.. C++.. NVM with a little fiddling, I got around it.. Got it forwarded nicely.. Only thing left is the TPA's.. I can't seem to find the source code for TPA's.. like making a TPA in c++ or something that I can translate at least. Problem is how to dynamically allocate structs aka my TPA's..


    Non-Template version (UPDATED this version only.. Now prints points and TPA's but only the first point in the TPA Why?):
    Code:
    #ifndef TYPES_H_INCLUDED
    #define TYPES_H_INCLUDED
    #include <math.h>
    #include <vector>
    #include <assert.h>
    #include <sstream>
    
    using namespace std;
    
    
    /** Definitions **/
    #define LEN(a) (sizeof(a)/sizeof(*a))
    #define HIGH(a) (sizeof(a)/sizeof(*a) - 1)
    
    #define Repeat do{
    #define Until(condition) }while(!(condition));
    
    #define writeln(Output) (cout<<Output<<"\n");
    
    /** Types **/
    
    struct Box;
    struct Point
    {
        int X, Y;
        Point() {X = Y = 0;};                              //Default constructor.
        Point(int X_, int Y_) : X(X_), Y(Y_) {};           //Alternate constructor.
        ~Point(){};                                        //Destructor.
    
        int operator == (const Point &PT) const            //Are the points Equal?
        {
            return ((X == PT.X) && (Y == PT.Y));
        }
    
        int operator != (const Point &PT) const            //Are the points Not equal?
        {
            return ((X != PT.X) && (Y != PT.Y));
        }
    
        Point& operator = (const Point& PT)
        {
            if (this != &PT)
            {
                X = PT.X;
                Y = PT.Y;
            }
            return *this;
        }
    
        Point operator += (Point PT)
        {
            X += PT.X;
            Y += PT.Y;
            return *this;
        }
    
        Point operator -= (Point PT)
        {
            X -= PT.X;
            Y -= PT.Y;
            return *this;
        }
    
        Point MidPointBox (const Box &B);
    
        friend ostream& operator << (ostream& Str, const Point &PT)      //For use with Writeln & cout.
        {
            Str<<"("<<PT.X<<", "<<PT.Y<<")";
            return Str;
        }
    
        friend inline Point operator + (Point P1, Point P2)           //Add two points.
        {
            return Point(P1.X + P2.X, P1.Y + P2.Y);
        }
    
        friend inline Point operator - (Point P1, Point P2)           //Subtract two points.
        {
            return Point(P1.X - P2.X, P1.Y - P2.Y);                 //No absolute value needed since points can have negative values.
        }
    
        friend inline int Distance(Point P1, Point P2)              //Friend is for Inheritance and friendship of a Class/Struct.
        {
            return (hypot(P1.X - P2.X, P1.Y - P2.Y));
        }
    
        friend inline Point Invert(Point PT)                     //Invert a point.
        {
            return Point(-PT.X, -PT.Y);
        }
    };
    
    
    struct Box
    {
        int X1, Y1, X2, Y2;
        Box(){X1 = Y1 = X2 = Y2 = 0;};                                                      //Default Constructor.
        Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) {};    //Alternate Constructor.
        ~Box(){};                                                                           //Destructor.
    
        friend int operator == (const Box &B, const Box &B2)                          //Are the Boxes the same?
        {
            return ((B.X1 == B2.X1) && (B.X2 == B2.X2) && (B.Y1 == B2.Y1) && (B.Y2 == B2.Y2));
        }
    
        friend inline int operator != (const Box &B, const Box &B2)                                               //Are the Boxes Not the same?
        {
            return !(B == B2);
        }
    
        Box operator += (Box B)
        {
            X1 += B.X1;
            Y1 += B.Y1;
            X2 += B.X2;
            Y2 += B.Y2;
            return *this;
        }
    
        Box operator -= (Box B)
        {
            X1 -= B.X1;
            Y1 -= B.Y1;
            X2 -= B.X2;
            Y2 -= B.Y2;
            return *this;
        }
    
        Box PointToBox(Point UpperLeft, Point LowerRight)
        {
            return Box(UpperLeft.X, UpperLeft.Y, LowerRight.X, LowerRight.Y);
        }
    
        Box& operator = (const Box& B)
        {
            if (this != &B)
            {
                X1 = B.X1;
                Y1 = B.Y1;
                X2 = B.X2;
                Y2 = B.Y2;
            }
            return *this;
        }
    
        friend ostream& operator << (ostream& Str, const Box &B)                                  //For use with Writeln & cout.
        {
            Str<<"("<<B.X1<<", "<<B.Y1<<", "<<B.X2<<", "<<B.Y2<<")";
            return Str;
        }
    
        friend inline Box operator + (Box B1, int S)                                              //Increase Box Size.
        {
            return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);
        }
    
        friend inline Box operator - (Box B1, int S)                                              //Decrease Box Size.
        {
            if (((B1.X1 - S) < 0) || ((B1.Y1 - S) < 0) || ((B1.X2 - S) < 0) || ((B1.Y2 - S) < 0))
                return Box(-1, -1, -1, -1);                                                     //Return -1's if its not a valid box.
            else if (((B1.X1 - S) == 0) || ((B1.Y1 - S) == 0) || ((B1.X2 - S) == 0) || ((B1.Y2 - S) == 0))
                return Box(0, 0, 0, 0);                                                         //Returns 0's because it becomes a point.
            else
                return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);                         //Return the resized box.
        }
    };
    
    struct PointArray
    {
        private:
            Point* P;
            int rows;
    
        public:
            Point& operator ()(int I)
            {
                return P[I];                            //Return A Point.
            }
    
            Point& operator[](int I)
            {
                assert(P != 0 && I >= 0 &&  I < rows);  //P <> 0 & I Must be >=0 & I < num of rows. Asserts to terminate the program upon Bad Input.
                return P[I];                            //Return A Point.
            }
    
            const Point& operator[](int I) const
            {
                assert(P != 0 && I >= 0 && I < rows);   //P <> 0 & I Must be >=0 & I < num of rows.
                return P[I];                            //Return A Point.
            }
    
            int Rows() const
            {
                return rows;
            }
    
            operator const Point* () const
            {
                return P;
            }
    
            operator Point* ()
            {
                return P;
            }
    
            Point* Resize(int NewSize);
            PointArray& operator = (const Point& PT)
            {
                if (this[LEN(this)] != &PT)
                {
                    P[LEN(this)] = PT;
                }
                return *this;
            }
    
            friend ostream& operator << (ostream& Str, const PointArray &PA)                                  //For use with Writeln & cout.
            {
                Str<<"[";
                for (int I = 0; I < LEN(PA); I++)
                {
                    Str<<PA[I];
                }
                Str<<"]";
    
                return Str;
            }
    
            PointArray(const PointArray &N);
            PointArray(int I = 2):
                P(I > 0 ? new Point[I] : 0), rows(I) {      //Created a New Point.
            }
            ~PointArray()
            {
                delete[] P;                                 //The new operator was used.. Must delete all instances of P.
                P = 0;
                rows = 0;
            }
    };
    
    Point Point::MidPointBox(const Box& B)
    {
        return Point(((B.X1 + B.X2)/2), ((B.Y1 + B.Y2)/2));
    };
    
    
    #endif //TYPES_H_INCLUDED
    Template Version (Does not need forwarding):
    Code:
    #ifndef TYPEDEFS_H_INCLUDED
    #define TYPEDEFS_H_INCLUDED
    #include <math.h>
    
    template <class T> struct Point;
    template <class T> struct Box;
    template <class T>
    struct Point
    {
        T X, Y;
        Point() {X = Y = 0;};                              //Default constructor.
        Point(int X_, int Y_) : X(X_), Y(Y_) {};           //Alternate constructor.
        ~Point(){};
    
        int operator == (const Point &PT);
        int operator != (const Point &PT){return ((X != PT.X) && (Y != PT.Y));};
        Point& operator = (const Point& PT){if (this != &PT){X = PT.X; Y = PT.Y;} return *this;};
        Point operator += (Point PT){X += PT.X; Y += PT.Y; return *this;};
        Point operator -= (Point PT){X -= PT.X; Y -= PT.Y; return *this;};
        Point MidPointBox (Box<T> B){return Point<T>(((B.X1 + B.Y1)/2), ((B.X2 + B.Y2)/2));};
        friend inline Point<T> operator + (Point<T> P1, Point<T> P2){return Point<T>(P1.X + P2.X, P1.Y + P2.Y);};
        friend inline Point<T> operator - (Point<T> P1, Point<T> P2){return Point<T>(P1.X - P2.X, P1.Y - P2.Y);};
        friend inline T Distance(Point<T> P1, Point<T> P2){return T(hypot(P1.X - P2.X, P1.Y - P2.Y));};
        friend inline Point<T> Invert(Point<T> PT){return Point<T>(-PT.X, -PT.Y);};
    };
    
    template <class T>
    struct Box
    {
        int X1, Y1, X2, Y2;
        Box(){X1 = Y1 = X2 = Y2 = 0;};                                                      //Default Constructor.
        Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) {};    //Alternate Constructor.
        ~Box(){};                                                                           //Destructor.
    
        friend int operator == (const Box<T> &B, const Box<T> &B2){return ((B.X1 == B2.X1) && (B.X2 == B2.X2) && (B.Y1 == B2.Y1) && (B.Y2 == B2.Y2));}
    
        friend inline int operator != (const Box<T> &B, const Box<T> &B2){return !(B == B2);}
        Box operator += (Box B){X1 += B.X1; Y1 += B.Y1; X2 += B.X2; Y2 += B.Y2; return *this;}
        Box operator -= (Box B){X1 -= B.X1; Y1 -= B.Y1; X2 -= B.X2; Y2 -= B.Y2; return *this;}
        Box PointToBox(Point<T> UpperLeft, Point<T> LowerRight){return Box<T>(UpperLeft.X, UpperLeft.Y, LowerRight.X, LowerRight.Y);}
        Box& operator = (const Box& B){if (this != &B){X1 = B.X1; Y1 = B.Y1; X2 = B.X2; Y2 = B.Y2;} return *this;}
        friend inline Box<T> operator + (Box<T> B1, int S){return Box<T>(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);}
        friend inline Box<T> operator - (Box<T> B1, int S)
        {
            if (((B1.X1 - S) < 0) || ((B1.Y1 - S) < 0) || ((B1.X2 - S) < 0) || ((B1.Y2 - S) < 0))
                return Box<T>(-1, -1, -1, -1);
            else if (((B1.X1 - S) == 0) || ((B1.Y1 - S) == 0) || ((B1.X2 - S) == 0) || ((B1.Y2 - S) == 0))
                return Box<T>(0, 0, 0, 0);
            else
                return Box<T>(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);
        }
    };
    
    
    #endif // TYPEDEFS_H_INCLUDED
    I am Ggzz..
    Hackintosher

  2. #2
    Join Date
    Aug 2011
    Posts
    62
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    with some german dance music ofcourse.

  3. #3
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Simba Code:
    TPointArray = array of TPoint;

    PHP Code:
      tpoint tpa[]; 
    Pretty much (iirc?).
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  4. #4
    Join Date
    Nov 2010
    Location
    Australia
    Posts
    1,472
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

    Quote Originally Posted by ggzz View Post
    I decided to write the entire SRL include into a language I know quite well.. C++.. NVM with a little fiddling, I got around it.. Got it forwarded nicely.. Only thing left is the TPA's.. I can't seem to find the source code for TPA's.. like making a TPA in c++ or something that I can translate at least.
    1) Don't try to do it(i tried for like 3-4 weeks, and i didn't get past converting the wizzyplugin to c++).

    2)for the source look for a tpa.pas file in here: https://github.com/MerlijnWajer

    3)Why do you want to convert it anyway? The speed increase isn't even needed unless its for fighting or something. I would suggest you convert the include to a .dll using lazarus(which requires very little change other than copy/paste).

  5. #5
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    problem with the two suggestions.. In c++ there is no such thing as "Array of" like in Pascal.. when defining an array, a size must be put or else use the new keyword which requires that it's delete right after use..

    I was thinking of using a vector instead because that way it fully replicates it the same way as in pascal.. a size is not needed, you can add and remove at anytime you want. Thing is, how can you make a "vector of structs" as a type. In other words vector<Point> TPoint.. Now how in the world would I write a constructor for that?

    Don't worry kingkong I won't give up lol.. I have 2 years of programming class left and the wizziplugin will be easy once u get the other stuff down such as the types and what not. I just decided to do this for fun and see how far I can get.. Besides I see that not a single soul worked on Scar++ so I decided I'll make a Simba++ that is worked on by myself and anyone who wants to do it.. I'd rather c++ cuz it has so much more to offer than pascal.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    I don't know C++ very well, or at all. But is there anything similar to array lists in Java? Pretty simply just dynamically updates the size of an array without having to mess about deleting it each time and such. Are you attempting to make SRL into a plugin?

  8. #8
    Join Date
    Apr 2007
    Posts
    581
    Mentioned
    1 Post(s)
    Quoted
    2 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    I don't know C++ very well, or at all. But is there anything similar to array lists in Java? Pretty simply just dynamically updates the size of an array without having to mess about deleting it each time and such. Are you attempting to make SRL into a plugin?
    Vector in the c++ standard library is similar to ArrayList in Java.

  9. #9
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Richard View Post
    I don't know C++ very well, or at all. But is there anything similar to array lists in Java? Pretty simply just dynamically updates the size of an array without having to mess about deleting it each time and such. Are you attempting to make SRL into a plugin?
    But they are objects :S Those are structs.. how can I create a dynamic array of structs? No If I wanted to make a plugin I'd compile the entire source on Git into a plugin.. Not exactly what I want. I want to make http://villavu.com/forum/showthread.php?t=30214

    But with everything updated.. That one doesn't have TPA's n what not.. I want to do it from the start rather than copy his code..

    And I'm going to update the first post with what I currently have.. Just cannot get the size of an array of structs.

    Edit: Updated the first post.. you can try it if u like.. This is the program I'm using to test it (and it prints out everything perfect just not TPA's):

    Code:
    #include "Types.h"
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        Point P(1, 2);
        Point Q(3, 4);
        Point X(5, 6);
        Point Y(7, 8);
    
        PointArray Z[10];
        Z[0] = P;
        Z[1] = Q;
        Z[2] = X;
        Z[3] = Y;
    
        for (int I = 0; I < LEN(Z); I++)
            writeln(*Z);
    
        cin.get();
    }
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

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

Posting Permissions

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