iDevGames Forums
ReceiveNextEvent problem - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: ReceiveNextEvent problem (/thread-74.html)

ReceiveNextEvent problem - bigfoot811 - May 6, 2010 04:22 PM

I am writing an application where I use ReceiveNextEvent to manually capture my events. The problem I am having is that when I run two instances of my program (foo) simultaneously. When I do this, the area where the foo instances overlap, flickers.
I tried running foo and a test glut program, which uses the glut event loop, simultaneously. No flickering.
There must be something wrong with how I am using carbon/agl for my application. I have looked at many examples and I cannot see what I am doing wrong.
I would really appreciate if someone sees the problem with my code and could help me.
Here is a code sample where I have stripped out all the application code but the bare minimum. When I run two instances of this, I get the flickering.
#include <OpenGL/OpenGL.h>
#include <Carbon/Carbon.h>
#include <AGL/agl.h>

AGLContext agl_context;
WindowRef agl_window;

void draw() { }

void create(){
    Rect rect;
    rect.left = 10; = 20;
    rect.right = 800;
    rect.bottom = 800;

    CreateNewWindow(kDocumentWindowClass, (kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute) & ~kWindowResizableAttribute, &rect, &agl_window);

    AGLPixelFormat agl_format =  aglChoosePixelFormat(0, 0, agl_attributes);
    agl_context = aglCreateContext(agl_format, 0);
    aglSetWindowRef(agl_context, agl_window);
void main_loop(){
    ProcessSerialNumber psn = {0, kCurrentProcess};
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    for(;; ){
        EventRef event;
        ReceiveNextEvent(0, NULL, 0.01, true, &event);
        SendEventToEventTarget(event, GetEventDispatcherTarget());

        aglSetWindowRef(agl_context, agl_window);
int main(int argc, char *argv[])
    return 0;

ReceiveNextEvent problem - OneSadCookie - May 6, 2010 07:34 PM

You should not be using Carbon for new code. Use Cocoa instead.

ReceiveNextEvent problem - bigfoot811 - May 7, 2010 07:56 AM

My application has a lot of generic C++ and OpenGL code with only a small amount of code to create a container window. On windows, it uses the windows api and on linux it uses X windows. When I wrote the mac code using agl/carbon, it is only about 150 lines of code to write the container window. So I cannot easily rewrite the 1000s of lines of C++ code into Objective C, just to get the less than 200 lines of windowing functionality to work.
That is the reason I was trying to get Carbon to work.
So going forward, if Apple is not going to support Carbon, is there a way for me to write the windowing module in Cocos (ObjectiveC) and link it with the C++ code compiled with gcc?
I am sorry if this seems like a silly question. I have only been learning to program on macs for a few months. So if someone can at least point me in a direction where someone else has solved this same issue, it would be of great help. Thanks!

ReceiveNextEvent problem - AnotherJake - May 7, 2010 10:42 AM

You don't need to re-write any of your existing code in Objective-C for Cocoa. You can look at Cocoa like .NET, where you'd only use .NET for your basic windowing code, then call out to your C++ codebase for your game.

Instead of using Cocoa, you could look into using SDL instead, for your windowing module. Actually, you could use SDL for Windows/Linux/Mac and save yourself some hassle, if it'd suit your needs.

ReceiveNextEvent problem - OneSadCookie - May 7, 2010 07:06 PM

C and ObjC can be freely intermixed in .m files, C++ and ObjC can be freely intermixed in .mm files. There is no problem with doing exactly what you do on the other two platforms, but using Cocoa. The biggest issue is likely to be if you've assumed that the OS works a particular way (eg. having complete control over the event loop, or assuming you can do anything on any thread).

ReceiveNextEvent problem - bigfoot811 - May 8, 2010 08:51 AM

I am using a manual event loop now on windows and linux, so I can control the refresh based on physics, controller state, etc. For what I want to do, it sounds like SDL is the best implementation path for me to use on the mac. And as jake said, it is portable between platforms, which means I can use the same windows/event code rather than implementing in ms windows, xwindows or cocoa depending on the os.
Thanks for your responses.