Function pointer and (...) mess

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

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 on other sites
Are you programming in C or C++? Why do you think your DrawPixel function needs to be a variadic function?

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 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 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 on other sites
Quote:
 Original post by SneftelAre 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 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 on other sites
Quote:
 Original post by wlw_wlJust 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 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.

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 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.

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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 13
• 14
• 9
• 9
• 9
• Forum Statistics

• Total Topics
632927
• Total Posts
3009243
• Who's Online (See full list)

There are no registered users currently online

×