Skip to main content
gamedev.net
🔒 Locked

Function pointer and (...) mess

Started by wlw_wl 9 replies 1.5k views
Original Post
wlw_wl
wlw_wl
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? ;-)
Sneftel
Sneftel
Are you programming in C or C++? Why do you think your DrawPixel function needs to be a variadic function?
KulSeran
KulSeran
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)
SiCrane
SiCrane
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.
rip-off
rip-off
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;}
wlw_wl
wlw_wl
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?
alnite
alnite
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   }}
Antheus
Antheus
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.
wlw_wl
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.


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 :)
alnite
alnite
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.
wlw_wl
wlw_wl
Noticed that just after posting.

I get the idea, it's nice and it's not unknown to me, just didn't think of using this. Thanks very much for enlighting me :-)

Now only the buffer thing... How to access it in WinAPI? I recon it will be some one dimensional char array or something.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.