Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 132 additions & 1 deletion src/macosx/osxgl.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* See readme.txt for copyright information.
*/


#include "allegro5/allegro.h"
#include "allegro5/allegro_opengl.h"
#include "allegro5/internal/aintern.h"
Expand All @@ -27,6 +26,7 @@
#include "allegro5/platform/aintosx.h"
#include "./osxgl.h"
#include "allegro5/allegro_osx.h"

#ifndef ALLEGRO_MACOSX
#error something is wrong with the makefile
#endif
Expand Down Expand Up @@ -65,6 +65,12 @@
#define MINIMUM_WIDTH 48
#define MINIMUM_HEIGHT 48


/* by MAREK */
static char **cfileURLs;
int cfileURLs_n=0;
/* */ //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;
Expand Down Expand Up @@ -212,6 +218,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;
/* */ //by MAREK
-(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr;
-(ALLEGRO_DISPLAY*) allegroDisplay;
-(void) reshape;
Expand Down Expand Up @@ -294,6 +303,114 @@ void _al_osx_mouse_was_installed(BOOL install) {

@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 *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

//freeing filenames
if (cfileURLs_n>0)
{
for (int ni=0; ni<cfileURLs_n; ni++) free(cfileURLs[ni]);
free(cfileURLs);
cfileURLs_n=0;
}

cfileURLs=malloc(fileURLs.count * sizeof(char*));

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);
//alocating memory for filename
cfileURLs[n]=malloc(strlen(cfileURL)+1);
//copying
strncpy(cfileURLs[n], cfileURL, strlen(cfileURL)+1);
//pointer will be unique and not destructed until next drop series
event.drop.text = cfileURLs[n];
event.drop.is_file = true;
event.drop.row = n;
if ((n+1)<(int)fileURLs.count) event.drop.is_complete = false;
else event.drop.is_complete = true;
_al_event_source_emit_event(es, &event);
_al_event_source_unlock(es);
n++;
}

return YES;
}
return NO;
}

- (void)concludeDragOperation:(id<NSDraggingInfo>)sender {
// Optional: Any cleanup after drop
}

/**/ //by MAREK

-(void) prepareOpenGL
{
[super prepareOpenGL];
Expand Down Expand Up @@ -1646,6 +1763,10 @@ static void init_halt_events(ALLEGRO_DISPLAY_OSX_WIN *dpy)

return;
}

//////
//view->registerForDraggedTypes:@[NSPasteboardTypeFileURL]];
//////
dpy->view = view;
/* Hook up the view to its display */
[view setAllegroDisplay: &dpy->parent];
Expand Down Expand Up @@ -1876,6 +1997,16 @@ static void destroy_display(ALLEGRO_DISPLAY* d)
al_free(d->vertex_cache);
al_free(d);
[pool drain];

/* by MAREK */
//freeing filenames
if (cfileURLs_n>0)
{
for (int ni=0; ni<cfileURLs_n; ni++) free(cfileURLs[ni]);
free(cfileURLs);
cfileURLs_n=0;
}
/* */ //by MAREK
}

/* create_display:
Expand Down