Jump to content
  • Advertisement
Sign in to follow this  
wlw_wl

Function pointer and (...) mess

This topic is 3698 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to work out a way to enable theoetical user of a module I'm writing to initialize all I need in an easy way. In other words, my functions draw things. To do that, they need some drawing function, like SetPixel, PutPixel, Plot or whatever. And that *whatever* is the problem. I'd like to use a function pointer so that a user just points which function in his library draws a pixel. So, this is what I do: void (*DrawPixel) (...); void wInitialize(HDC __hdc, int width, int height, void (*PutPixelFunction) (...)) { // some code DrawPixel = PutPixelFunction; } so that later, in my functions, I just use DrawPixel(some_hdc, x, y, color); and the user, before calling any of my drawing functions needs to initialize basic things, for example (in VS): wInitialize(hdc, 800, 600, (void (*)(...)) SetPixel); SetPixel is: COLORREF SetPixel (HDC, int, int, COLORREF); It compiles, but crashes where I'm calling DrawPixel(somehdc, x, y, color), saying that I'm "calling a function declared with one calling convention with a function pointer declared with a different calling convention". I'm not entirely sure if whole the approach is correct, but I don't know any other way to do this. Perhaps some work-around with templates? I just want to keep using the module as easy as possible with other libs than just the one in VS. I've searched and read some about function pointers and they seem good if not ideal for this purpose, so what am I doing wrong this time? ;-)

Share this post


Link to post
Share on other sites
Advertisement
Are you programming in C or C++? Why do you think your DrawPixel function needs to be a variadic function?

Share this post


Link to post
Share on other sites
if you are calling "DrawPixel( HDC, int, int, COLORREF )" then the user's function HAS to match that calling convention.
Ant your function pointer should be of the form void(*PutPixelFunction)(HDC, int, int, COLORREF)

Share this post


Link to post
Share on other sites
SetPixel() is not a function that matches the function signature void (...). So trying to call that function as if it had that signature does bad, bad things.

Share this post


Link to post
Share on other sites
If you are working with function pointers, do everyone a favour and make a typedef.

typedef void (*PutPixelFunctionPtr)(HDC, int, int, COLORREF);

void wInitialize(HDC __hdc, int width, int height, PutPixelFunctionPtr pixelFunc)
{
// some code
DrawPixel = pixelFunc;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Are you programming in C or C++? Why do you think your DrawPixel function needs to be a variadic function?


C++,
I know that my functions need x, y and a color to draw a pixel, and a device contex also. Color is not necessarily of COLORREF type, it comes from my custom wMaterial class that has COLORREF field at the moment. I wanted to keep that universal so for example one trying to use it with BCB doesn't have to digg in the .h and .cpp of the module and substitute all drawing functions call. Should I make it "statically" (HDC, int, int, COLORREF) it all needs changing in mentioned BCB, I guess.

Just wanted to give it some flexibility. What would be a better way to do it?

Share this post


Link to post
Share on other sites
Unless there are some hardware/career restrictions that force you to use C, I'd suggest you start using C++ and taking advantage of interface.

Example:

class IPaintable
{
virtual void drawPixel(int x, int y, int color) = 0;
}

Your class then might look like this:

class MyClass
{
IPaintable* paintable;
void setPaintable(IPaintable* ip)
{
paintable = ip;
}

void draw()
{
if ( paintable ) {
paintable->draw_pixel(10, 10, 0xff00ff);
}
}
};

Those who want to use your library, should derive their classes from your IPaintable.

class Win32Draw : public IPaintable
{
void drawPixel(int x, int y, int color)
{
// this class just happens to use Win32 API for all its drawing routines
SetPixel(somehdc, x, y, color);
}
}

class OpenGLDraw : public IPaintable
{
void drawPixel(int x, int y, int color)
{
// do some awesome 3d pixels drawing routines here
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by wlw_wl

Just wanted to give it some flexibility. What would be a better way to do it?


SetPixel(x,y) style is de facto slowest possible approach to do rendering. Almost without exception APIs provide a way to access drawing buffer directly in form of pixels.

Share this post


Link to post
Share on other sites
Quote:

Unless there are some hardware/career restrictions that force you to use C, I'd suggest you start using C++ and taking advantage of interface.


Quote:

SetPixel(x,y) style is de facto slowest possible approach to do rendering. Almost without exception APIs provide a way to access drawing buffer directly in form of pixels.


Both of you please provide some more information.

I was told to use PutPixel-like function, since I was using VS it's SetPixel. If there's a faster way, I'd be pleased to know it.

Also I don't like COLORREF and I'd like to get rid of it, and non-SetPixel approach would free me from it, I guess :)

Share this post


Link to post
Share on other sites
Quote:
Original post by wlw_wl
Quote:

Unless there are some hardware/career restrictions that force you to use C, I'd suggest you start using C++ and taking advantage of interface.


Both of you please provide some more information.

I was told to use PutPixel-like function, since I was using VS it's SetPixel. If there's a faster way, I'd be pleased to know it.


I edited my post to include an example.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!