开发者

How to correctly return callbacks from C++ engine to obj-c program?

First the situation:

There is C++ written engine implemented in Objective C program. iPhone xcode to be precise. It uses OpenGL to draw everything. It has "renderFrame" method that is called every frame (approximately 50 times per second). User has many buttons that navigate to other Objective C views. These buttons are drawn and handled in C++ engine too. When user presses one of them, the C++ engine sends callback to the program that a button is pressed (so program can change views).

My way of making this work:

Since there is a "renderFrame" method, i made it to return integer value each time. Most of the time it is 0. But when it returns something else, it has a meaning. For example if "renderFrame" returns 2, that means second button is pressed. To handle all these returned values i must build quite huge chunk of IFs.

So my question:

Since i am a fresh programmer i want to know whether this is a good approach or is it something only a junior may come up with. This engine will be ported to many other systems including android and even PC, so i do not know whether i can and if i should implement other kind of callback system. I also would like to know if this can get me performance issues and what would be more professional approach.

P.S. At the moment i have implemented more than 20 callbacks like this and everything works just fine. I can react instantly 开发者_运维百科and precisely each frame.

Thank you for your suggestions.


It's a bad design for a number of reasons. First, what happens if more than one event took place? Two button clicks at once? Do you send 2 + 3? Do you ignore one and hope it wasn't important?

What happens when what you need to send isn't just a single binary fact? What if you want to send what button was clicked and where that happened? Or you need to transmit a number or character?

You should have a callback class, something like MyGraphicsCallbacks with functions like Button1Clicked(int locationX, int locationY), and then calling these functions when their respective events occur.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜