ReceiveNextEvent problem

Nibbie
Posts: 3
Joined: 2010.05
Post: #1
Hello,
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.
--------------------------
Code:
#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;
    rect.top = 20;
    rect.right = 800;
    rect.bottom = 800;

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

    GLint agl_attributes[] = {AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 32, AGL_NO_RECOVERY, AGL_NONE, AGL_NONE};
    AGLPixelFormat agl_format =  aglChoosePixelFormat(0, 0, agl_attributes);
    agl_context = aglCreateContext(agl_format, 0);
    aglDestroyPixelFormat(agl_format);
    ShowWindow(agl_window);
    SelectWindow(agl_window);
    aglSetWindowRef(agl_context, agl_window);
}
void main_loop(){
    ProcessSerialNumber psn = {0, kCurrentProcess};
    TransformProcessType(&psn, kProcessTransformToForegroundApplication);
    SetFrontProcess(&psn);
    aglSetCurrentContext(agl_context);
    for(;; ){
        EventRef event;
        ReceiveNextEvent(0, NULL, 0.01, true, &event);
        SendEventToEventTarget(event, GetEventDispatcherTarget());
        ReleaseEvent(event);

        SelectWindow(agl_window);
        aglSetWindowRef(agl_context, agl_window);
        draw();
        aglSwapBuffers(agl_context);
    }
}
int main(int argc, char *argv[])
{
    create();
    main_loop();
    return 0;
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
You should not be using Carbon for new code. Use Cocoa instead.
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2010.05
Post: #3
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!
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #4
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
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).
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2010.05
Post: #6
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.
Quote this message in a reply
Post Reply