-
Notifications
You must be signed in to change notification settings - Fork 309
Update osxgl.m #1664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Update osxgl.m #1664
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -65,6 +65,10 @@ | |||
#define MINIMUM_WIDTH 48 | ||||
#define MINIMUM_HEIGHT 48 | ||||
|
||||
/* by MAREK */ | ||||
#define FILE_URL_SIZE 254 | ||||
/* end by MAREK */ | ||||
|
||||
/* Unsigned integer; data type only avaliable for OS X >= 10.5 */ | ||||
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050 | ||||
typedef unsigned int NSUInteger; | ||||
|
@@ -212,6 +216,9 @@ @interface ALOpenGLView : NSOpenGLView | |||
/* This is passed onto the event functions so we know where the event came from */ | ||||
ALLEGRO_DISPLAY* dpy_ptr; | ||||
} | ||||
/* by MAREK */ | ||||
- (id)initWithFrame:(NSRect)frameRect; | ||||
/* end by MAREK */ | ||||
-(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr; | ||||
-(ALLEGRO_DISPLAY*) allegroDisplay; | ||||
-(void) reshape; | ||||
|
@@ -292,8 +299,119 @@ void _al_osx_mouse_was_installed(BOOL install) { | |||
}); | ||||
} | ||||
|
||||
/* by MAREK */ | ||||
@implementation NSString (Char) | ||||
|
||||
/* | ||||
* Convert a NSString to a char pointer | ||||
*/ | ||||
-(char *)toChar{ | ||||
|
||||
const char* strUtf8 = [self UTF8String]; | ||||
size_t len = strlen(strUtf8) + 1; | ||||
char *toChar = malloc(len); | ||||
memcpy(toChar, strUtf8, len); | ||||
return toChar; | ||||
} | ||||
@end | ||||
/* end by MAREK */ | ||||
|
||||
@implementation ALOpenGLView | ||||
|
||||
/*by MAREK*/ | ||||
- (id)initWithFrame:(NSRect)frameRect | ||||
{ | ||||
self = [super initWithFrame:frameRect]; | ||||
if (self) { | ||||
// Register for file URL drops (e.g., images) | ||||
[self registerForDraggedTypes:@[NSPasteboardTypeFileURL]]; | ||||
// Or other types like NSStringPboardType, NSFileContentsPboardType, etc. | ||||
} | ||||
|
||||
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | ||||
|
||||
|
||||
// NSString *logPath = @"allegro5.log"; | ||||
// freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr); | ||||
|
||||
return self; | ||||
} | ||||
|
||||
// Drag-and-drop methods | ||||
|
||||
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { | ||||
NSPasteboard *pasteboard = [sender draggingPasteboard]; | ||||
if ([[pasteboard types] containsObject:NSPasteboardTypeFileURL]) { | ||||
return NSDragOperationCopy; // Accept copy operation for files | ||||
} | ||||
return NSDragOperationNone; | ||||
} | ||||
|
||||
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender { | ||||
return YES; // Prepare to accept the drop | ||||
} | ||||
|
||||
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { | ||||
NSPasteboard *pasteboard = [sender draggingPasteboard]; | ||||
NSArray *classes = @[NSURL.class]; | ||||
NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey: @YES}; | ||||
|
||||
int n=0; | ||||
|
||||
NSArray<NSURL *> *fileURLs = [pasteboard readObjectsForClasses:classes options:options]; | ||||
if (fileURLs.count > 0) | ||||
{ | ||||
// Handle multiple dropped files here | ||||
|
||||
for (NSURL *fileURL in fileURLs) | ||||
{ | ||||
NSString *filePath = [fileURL path]; | ||||
unsigned long ul=[filePath length]; | ||||
NSLog(@"Dropped file: %@ len:%d", filePath, (int)ul); | ||||
const char *cfileURL= [filePath cStringUsingEncoding:NSUTF8StringEncoding]; | ||||
|
||||
// Process the file: e.g., load as texture, model, or data into OpenGL | ||||
// Example: If it's an image, use NSImage to load and upload to GL texture | ||||
// NSImage *image = [[NSImage alloc] initWithContentsOfURL:fileURL]; | ||||
// Then convert to OpenGL texture... | ||||
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr; | ||||
NSWindow *window = dpy->win; | ||||
|
||||
NSPoint mousePos = [window mouseLocationOutsideOfEventStream]; | ||||
// mousePos.x and mousePos.y now contain the cursor's coordinates relative to the window's bottom-left corner. | ||||
|
||||
NSRect rc = [window frame]; | ||||
NSRect content = [window contentRectForFrameRect: rc]; | ||||
content = [self convertRectToBacking: content]; | ||||
ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es; | ||||
|
||||
_al_event_source_lock(es); | ||||
ALLEGRO_EVENT event; | ||||
|
||||
event.drop.type = ALLEGRO_EVENT_DROP; | ||||
event.drop.timestamp = al_get_time(); | ||||
event.drop.x = (int)mousePos.x; | ||||
event.drop.y = (int)mousePos.y; | ||||
NSLog(@"Event with file (%d/%d) : %@", n+1, (int)fileURLs.count, filePath); | ||||
event.drop.text = (char*) cfileURL; | ||||
|
al_free(event.drop.text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, that's right, so in next my commit i'm allocating memory, freeing and allocating again what is needed to keep pointers working, to free them completely when display is closing. Yes, it can be replaced with al_malloc and al_free, actually just to save one or 3 lines of code, so instead of freeing those variables when display is closing, rather doing nothing because allegro would do that when its architecture is closing. I used malloc because i'm new in allegro 5, i come from the fading world of allegro4. Just let me know if I should change it again, so change the file in my fork, then pull commit to main branch, or you guys would do that as should be in official branch. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sorry, no, is not used anymore, there was another concept in my mind to send 1 single message and receive the event as a bunch of file names separated by \r\n so exactly like MacOS is doing, but eventually i changed minds and did at exactly like xdnd function for X11 in Allegro 5 is doing, so there is single message for each file name. So that definition is not used anymore. Remove it please.