Results 1 to 21 of 21

Thread: C++: how convert a user class to const POINT*?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    C++: how convert a user class to const POINT*?

    the Polygon() API function accept the const POINT* on 2nd parameter.
    heres my class:
    Code:
    class position
    {
        public:
        float X;
        float Y;
        float Z;
        float perespective;
        float foco;
        position(float posx=0, float posy=0, float posz=0,float FocalDistance=300)
        {
            X = posx;
            Y = posy;
            Z = posz;
            foco = FocalDistance;
        }
        operator POINT()
        {
            RECT WindowSize;
            GetClientRect(GetConsoleWindow(), & WindowSize);
            perespective = foco /(Z+foco);
            POINT pos;
            pos.x=trunc(X * perespective);
            pos.y=trunc(Y * perespective);
            return pos;
        }
    };
    heres my vector:
    Code:
    vector<position> Plane={ {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0}};
    now how can i convert the Plane vector using the class overload(i think these is the right word\name) operator?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    How do you call the Polygon() API - and what errors are you getting? operator POINT() is a cast function that will return a type POINT when position type is used but POINT is required.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    Code:
    vector<position> DrawPlane={ {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0}};
    Polygon(HDCConsole,(POINT*) DrawPlane,DrawPlane.size());
    "error: invalid cast from type 'std::vector<position>' to type 'POINT*' {aka 'tagPOINT*'}"
    so what i'm doing wrong?
    if the 'position' isn't an array, only the vector, that's why i added the 'operator POINT'... but did i miss something?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    Ah. Polygon() for it's second parameter requires a pointer to an array of POINT. DrawPlane is a std::vector of position. You can't cast a std::vector of position to a memory pointer of POINT. You also can't take the address of the data stored within the vector and cast that address to a pointer to POINT. You're going to have to create a new array of type POINT, initialise it with the data from DrawPlane and then pass the address of that array to Polygon.

    Something like (NOT tried):

    Code:
    const std::vector<position> DrawPlane{ {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0}};
    
    std::vector<POINT> mypoints(DrawPlane.size());
    
    for (size_t i {}; i < DrawPlane.size(); ++i)
        mypoints[i] = (POINT)DrawPlane[i];
    
    Polygon(HDCConsole, mypoints.data(), DrawPlane.size());
    Last edited by 2kaud; Sep 4th, 2022 at 06:23 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    I'm sorry, but the POINT operator, on position class, don't resolve that?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    This test code compiles OK with VS2022 (with just some conversion warnings):

    Code:
    #include <vector>
    #include <Windows.h>
    #include <cmath>
    
    class position
    {
    public:
    	float X;
    	float Y;
    	float Z;
    	float perespective;
    	float foco;
    	position(float posx = 0, float posy = 0, float posz = 0, float FocalDistance = 300) {
    		X = posx;
    		Y = posy;
    		Z = posz;
    		foco = FocalDistance;
    	}
    
    	operator POINT() {
    		RECT WindowSize;
    		GetClientRect(GetConsoleWindow(), &WindowSize);
    		perespective = foco / (Z + foco);
    		POINT pos;
    		pos.x = std::trunc(X * perespective);
    		pos.y = std::trunc(Y * perespective);
    		return pos;
    	}
    };
    
    int main() {
    	HDC HDCConsole {};    // FOR COMPILE TEST ONLY
    
    	std::vector<position> DrawPlane { {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0} };
    	std::vector<POINT> mypoints(DrawPlane.size());
    
    	for (size_t i {}; i < DrawPlane.size(); ++i)
    		mypoints[i] = (POINT)DrawPlane[i];
    
    	Polygon(HDCConsole, mypoints.data(), DrawPlane.size());
    }
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    you using the 'for' for convert the 'DrawPlane' to 'mypoints'... so what is doing the 'operator POINT()'?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  8. #8
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    The (POINT) cast calls the POINT() function to perform the cast:

    Code:
    mypoints[i] = (POINT)DrawPlane[i];
    Also, some simplified code that enables DrawPlane to be const (the problem with my first untested code above) and with no compiler warnings:

    Code:
    #include <vector>
    #include <Windows.h>
    #include <cmath>
    
    class position {
    public:
    	float X;
    	float Y;
    	float Z;
    	float foco;
    
    	position(float posx = 0, float posy = 0, float posz = 0, float FocalDistance = 300) :
    		X(posx), Y(posy), Z(posz), foco(FocalDistance) {}
    
    	operator POINT() const {
    		const auto perespective { foco / (Z + foco) };
    
    		return POINT { (LONG)std::trunc(X * perespective), (LONG)std::trunc(Y * perespective) };
    	}
    };
    
    int main() {
    	HDC HDCConsole {};    // FOR COMPILE TEST ONLY
    
    	const std::vector<position> DrawPlane { {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0} };
    	std::vector<POINT> mypoints(DrawPlane.size());
    
    	for (size_t i {}; i < DrawPlane.size(); ++i)
    		mypoints[i] = (POINT)DrawPlane[i];
    
    	Polygon(HDCConsole, mypoints.data(), (int)DrawPlane.size());
    }
    Last edited by 2kaud; Sep 4th, 2022 at 06:46 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  9. #9

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    i'm sorry, but the problem remains
    Code:
    operator POINT() const
    	{
    		const float perespective = foco / (Z + foco);
    
    		return POINT( (LONG)std::trunc(X * perespective), (LONG)std::trunc(Y * perespective) );
    	}
    "error: no matching function for call to 'tagPOINT::tagPOINT(LONG, LONG)'"

    and on Polygon():
    Code:
    vector<position> Plane={ {-470, 255,0}, {-470, 255,1000}, {470, 255,1000},{470, 255,0}};
    vector<position> DrawPlane = Plane;//double for draw what is showed and not change the plane size ;)
    Polygon(HDCConsole,(POINT) DrawPlane,DrawPlane.size());
    "error: no matching function for call to 'tagPOINT::tagPOINT(std::vector<position>&)'"
    VB6 2D Sprite control

    To live is difficult, but we do it.

  10. #10
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    The code as posted above as post #8 compiles OK with MS VS2022 as C++20.

    What compiler are you using? If VS2022, have you set the Language Standard to C++20 (or latest)?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  11. #11

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    i'm using Coode Blocks with GNU compiler(don't have C++20):
    Attachment 185666
    VB6 2D Sprite control

    To live is difficult, but we do it.

  12. #12
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    What version of the GNU compiler? It seems that you're using an old version. I'd suggest upgrading to the current.

    However, as I only use VS I can't help with issues with other compilers. Sorry.

    Does my code in post #6 compile OK?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  13. #13
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    I'd also suggest using the latest MS VS compiler. VS2022 community is free to install/use:
    https://visualstudio.microsoft.com/f...eloper-offers/

    PS. Note that if you do install VS, then C/C++ isn't installed by default. You have to choose to install under Desktop Development with C++
    Last edited by 2kaud; Sep 4th, 2022 at 07:04 AM.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  14. #14

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    yes the code, on #6, works fine.. i added 2 lines and get the Console HDC and the polygon, now, it's drawed on center of window:
    Code:
    #include <vector>
    #include <Windows.h>
    #include <cmath>
    #include <iostream>
    
    class position
    {
    public:
    	float X;
    	float Y;
    	float Z;
    	float perespective;
    	float foco;
    	position(float posx = 0, float posy = 0, float posz = 0, float FocalDistance = 300) {
    		X = posx;
    		Y = posy;
    		Z = posz;
    		foco = FocalDistance;
    	}
    
    	operator POINT() {
    		RECT WindowSize;
    		GetClientRect(GetConsoleWindow(), &WindowSize);
    		perespective = foco / (Z + foco);
    		POINT pos;
    		pos.x = std::trunc(X * perespective)+ WindowSize.right/2;//added the coordenate\window center ;)
    		pos.y = std::trunc(Y * perespective)+ WindowSize.bottom/2;//added the coordenate\window center ;)
    		return pos;
    	}
    };
    
    int main() {
    	HDC HDCConsole =GetWindowDC(GetConsoleWindow());    // FOR COMPILE TEST ONLY... i added the 2 functions for get the HDC
    
    	std::vector<position> DrawPlane { {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0} };
    	std::vector<POINT> mypoints(DrawPlane.size());
    
    	for (size_t i {}; i < DrawPlane.size(); ++i)
    		mypoints[i] = (POINT)DrawPlane[i];
    
    	Polygon(HDCConsole, mypoints.data(), DrawPlane.size());
    	std::cin.get();//for see the result
    }
    works fine.... but you created a new vector<POINT>... instead use the original
    VB6 2D Sprite control

    To live is difficult, but we do it.

  15. #15

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    i need use like these:
    Code:
    Polygon(HDCConsole, DrawPlane.data(), DrawPlane.size());
    VB6 2D Sprite control

    To live is difficult, but we do it.

  16. #16
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    As #6 compiles OK and #8 doesn't either you have a quite old compiler, or you need to inform the compiler to compile as C++17/++20 using an appropriate option. eg –std=c++17 or -std=c++20 if supported by your compiler version. Most compilers (including VS) don't default to the current/latest standard but to some previous standard.

    You can't use the original DrawPlane as that is effectively an array of position - whilst Polygon() requires an array of POINT. You can't convert an array of one type to another. You have to create a new array of the required type. (POINT) casts one value of type position to one value of type POINT - not an array.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  17. #17
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    Code:
    Polygon(HDCConsole, DrawPlane.data(), DrawPlane.size());
    You can't. See my post #16.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  18. #18

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    i'm sorry but what means '-std=c++2a?(i know these linker command using several tests)
    i'm sorry but theres no way for do:
    Code:
    int main()
    {
    	HDC HDCConsole =GetWindowDC(GetConsoleWindow());    // FOR COMPILE TEST ONLY... i added the 2 functions for get the HDC
    
    	std::vector<position> DrawPlane { {-100, 50,0}, {-100, 50,1000}, {100, 50,1000},{100, 50,0} };
    	
    	Polygon(HDCConsole,DrawPlane.data(), DrawPlane.size());//here is what i need to do
    	std::cin.get();//for see the result
    }
    on position class we have the 'POINT' operator... but seems that we need more
    i need upgrade the 'position' class and then use it on Polygon\object3d future class
    Last edited by joaquim; Sep 4th, 2022 at 08:11 AM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  19. #19

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    2kaud: ok i read it. but what is for 'operator POINT const'?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  20. #20
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    801

    Re: C++: how convert a user class to const POINT*?

    const is used as operator POINT() doesn't now change any class member variables and marks the function as const so that it can be used with a const class instance. In my previous #8 post I marked DrawPlane as const vector as it's values are not changed - hence POINT() also needs to be marked const and why I moved perespective to inside the function rather than having it as a member function. If POINT() isn't marked as const then DrawPlane vector can't be marked as const which it should be here as it's value isn't changed. It's good practice to mark all variables const if they are not changed after their initialisation.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++23 Compiler: Microsoft VS2022 (17.4.2)

  21. #21

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,575

    Re: C++: how convert a user class to const POINT*?

    so i must re-invent the position class for do the things more simple in just 1 class
    these class position must do have:
    - 3D position;
    - 3D rotation;
    - perspective.
    so the position class is like a 3D vector


    and the Object3D:
    - have the position class(maybe like a parent class);
    - and the texture;
    - and the draw.
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

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



Click Here to Expand Forum to Full Width