Key blocking

Luminary
Posts: 5,143
Joined: 2002.04
Post: #16
:/
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #17
chuckle...

Miglu, I don't know what your problem is exactly, but I can tell you that using an Objective-C method for a C function pointer isn't going to work out well for you.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #18
I thought that that might be the problem, but changing -(void) separate to void separate produced two other errors, the other of which is "request for member 'separate' in something not a structure or union" for cpSpaceAddCollisionHandler(space, 1, 2, NULL, NULL, NULL, separate, NULL).
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #19
Well the function prototype for it is:

Code:
typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, struct cpSpace *space, void *data);

Which means your function will need to match that. Something like:

Code:
void separate(cpArbiter *arb, struct cpSpace *space, void *data)
{
    ;
}
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #20
Thanks. However, I got "'separate' undeclared (first use in this function)" for cpSpaceAddCollisionHandler(space, 1, 2, NULL, NULL, NULL, separate, NULL). What is the problem?
Compiler song that I made: Every method you call, every object you make, I'll be whinin' at you. Every byte you free, every step I take, I'll be whinin' at you. O can't you see, you will thank me in the end.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #21
Functions have to either be declared or defined above the first place you ever use them.

Try going to the top of your source file and pasting the function prototype there:

Code:
void separate(cpArbiter *arb, struct cpSpace *space, void *data);

[edit] actually *all* symbols have to be declared first before they can be used, and functions are no exception. C does not allow forward declarations.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #22
Thanks. When I do @property(readonly) bool m_keys[NUM_KEY_CODES] I get "bad property declaration". What is the problem?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #23
I doubt properties can be arrays
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #24
How to access a variable that is in the interface section from a C function?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #25
put the implementation of the function inside the @implementation...@end, pass TheClass* self as an argument to the function, use self->ivarname within the function.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #26
I am trying to use cpSpaceAddCollisionHandler to do a cpCollisionSeparateFunc callback and use an array of bools in the callback function. That array is in an object. I tried to pass the array to the function using cpSpaceAddCollisionHandler's data field, but got "incompatible type for argument 8 of 'cpSpaceAddCollisionHandler'". I also can not use a getter method, as I can not pass the object as an argument, as its pointer is defined in the interface. What should be done?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #27
He already explained exactly what to do.

Another way to do it, if all you want is access to the bool array, is when you pass it as a void pointer, you have to cast it to a void pointer, like so:

Code:
function(blah, blah, (void *)myBoolArray);

Then on the other side, when you get passed "the data" back, you cast it back to a bool array, like so:

Code:
bool *myBoolArray = (bool *)data;
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #28
I tried it:
Code:
void separate(cpArbiter *arb, struct cpSpace *space, void *data)
{
    if ((bool *)data[SPACE]){
        //
    }
} error: void value not ignored as it ought to be

cpSpaceAddCollisionHandler(space, 1, 2, NULL, NULL, NULL, separate, (void *)circle.m_keys[NUM_KEY_CODES]); error: accessing unknown 'm_keys' getter method
How to correct this?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #29
You're still not doing that right. Try OSC's suggestion instead so you can stick to Objective-C to help avoid confusion.

Here's a pseudocode example of how you might do that:

Code:
@interface MyClass : NSObject
{
    bool    key[NUM_KEYS];
}
@end

@implementation MyClass

void myCallbackFunc(int someParameter, void *data)
{
    MyClass *myClassInstance = (MyClass *)data;
    [myClassInstance myObjectiveCCallback:(int)someParameter];
}

- (void)myObjectiveCCallback:(int)someParameter
{
    if (key['q'])
        NSLog(@"q is being held down!");
}

- (id)init
{
    self = [super init];
    if (self == NULL)
        return NULL;
    
    someAPICallInit(1, 2, myCallbackFunc, self);
    
    return self;
}

@end

Do you see what is happening here? You call someAPICallInit with your C callback function, and pass self in the data parameter. self is just a pointer to your instance of MyClass, so when the callback is called by the API it passes self back to you in the data parameter. You can cast that back to get access to your instance of MyClass, then simply call the Objective-C version of your callback instead, so you have full access to everything like you normally would as if you never left Objective-C.

Does this make sense? It's a good technique to know, so it's well worth it to understand how it works.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #30
Thanks. I would have solved this problem ages ago if I had known that Objective-C methods can be called in C functions (I deduced it from the fact that class variables can not be used in C functions). How would it be done in C? I want to know it for if I ever need it. So how would the two errors that are shown in my previous post be solved?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  blocking function for timing purposes unknown 6 3,987 Feb 20, 2006 11:03 AM
Last Post: Skorche
  Non-blocking VBL sync? TomorrowPlusX 4 3,928 Feb 28, 2005 02:06 PM
Last Post: arekkusu