Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- //#include <OpenGL/gl.h>
- #include <objc/message.h>
- #include <objc/runtime.h>
- typedef struct CMPoint
- {
- double x;
- double y;
- } CMPoint;
- typedef struct CMSize
- {
- double width;
- double height;
- } CMSize;
- typedef struct CMRect
- {
- CMPoint origin;
- CMSize size;
- } CMRect;
- typedef struct AppDel
- {
- Class isa;
- id window; // Will be an NSWindow later.
- } AppDelegate;
- enum
- {
- NSBorderlessWindowMask = 0,
- NSTitledWindowMask = 1 << 0,
- NSClosableWindowMask = 1 << 1,
- NSMiniaturizableWindowMask = 1 << 2,
- NSResizableWindowMask = 1 << 3,
- };
- typedef id(*CMacsSimpleMessage)(id, SEL);
- typedef void(*CMacsVoidMessage)(id, SEL);
- typedef void(*CMacsVoidMessage1)(id, SEL, void *);
- typedef id(*CMacsRectMessage1)(id, SEL, CMRect);
- typedef id(*CMacsWindowInitMessage)(id, SEL, CMRect, int, int, bool);
- CMacsSimpleMessage cmacs_simple_msgSend = (CMacsSimpleMessage)objc_msgSend;
- CMacsVoidMessage cmacs_void_msgSend = (CMacsVoidMessage)objc_msgSend;
- CMacsVoidMessage1 cmacs_void_msgSend1 = (CMacsVoidMessage1)objc_msgSend;
- CMacsRectMessage1 cmacs_rect_msgSend1 = (CMacsRectMessage1)objc_msgSend;
- CMacsWindowInitMessage cmacs_window_init_msgSend = (CMacsWindowInitMessage)objc_msgSend;
- #define GLCALLTYPE
- #define GL_COLOR_BUFFER_BIT 0x00004000
- typedef unsigned int GLbitfield;
- typedef unsigned char GLboolean;
- typedef signed char GLbyte;
- typedef char GLchar;
- typedef float GLclampf;
- typedef unsigned int GLenum;
- typedef int GLfixed;
- typedef float GLfloat;
- typedef unsigned short GLhalf;
- typedef long long GLint64;
- typedef int GLint;
- typedef intptr_t GLintptr;
- typedef short GLshort;
- typedef int GLsizei;
- typedef size_t GLsizeiptr;
- typedef struct __GLsync *GLsync;
- typedef unsigned char GLubyte;
- typedef unsigned long long GLuint64;
- typedef unsigned int GLuint;
- typedef unsigned short GLushort;
- typedef void GLvoid;
- void (GLCALLTYPE* glClearColor) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) = 0;
- void (GLCALLTYPE* glClear) (GLbitfield mask) = 0;
- void (GLCALLTYPE* glFlush) (void) = 0;
- // This is a simple -drawRect implementation for our class. We could have
- // used a NSTextField or something of that sort instead, but I felt that this
- // stuck with the C-based mentality of the application.
- void View_drawRect(id self, SEL _cmd, CMRect rect)
- {
- printf( "rect %f, %f, %f, %f\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height );
- glClearColor(1, 0, 1, 0);
- glClear(GL_COLOR_BUFFER_BIT);
- //drawAnObject();
- glFlush();
- }
- BOOL AppDel_didFinishLaunching(AppDelegate *self, SEL _cmd, id notification) {
- self->window = cmacs_simple_msgSend((id)objc_getClass("NSWindow"), sel_getUid("alloc"));
- /// Create an instance of the window.
- self->window = cmacs_window_init_msgSend(self->window, sel_getUid("initWithContentRect:styleMask:backing:defer:"), (CMRect){0,0,1024,460}, (NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask), 0, false);
- /// Create an instance of our view class.
- ///
- /// Relies on the view having declared a constructor that allocates a class pair for it.
- id view = cmacs_rect_msgSend1(cmacs_simple_msgSend((id)objc_getClass("View"), sel_getUid("alloc")), sel_getUid("initWithFrame:"), (CMRect){ 0, 0, 320, 480 });
- // here we simply add the view to the window.
- cmacs_void_msgSend1(self->window, sel_getUid("setContentView:"), view);
- cmacs_simple_msgSend(self->window, sel_getUid("becomeFirstResponder"));
- // Shows our window in the bottom-left hand corner of the screen.
- cmacs_void_msgSend1(self->window, sel_getUid("makeKeyAndOrderFront:"), self);
- return YES;
- }
- void RunApplication(void)
- {
- // Once again, just like the app delegate, we tell the runtime to
- // create a new class, this time a subclass of 'UIView' and named 'View'.
- Class ViewClass = objc_allocateClassPair((Class)objc_getClass("NSOpenGLView"), "View", 0);
- // and again, we tell the runtime to add a function called -drawRect:
- // to our custom view. Note that there is an error in the type-specification
- // of this method, as I do not know the @encode sequence of 'CGRect' off
- // of the top of my head. As a result, there is a chance that the rect
- // parameter of the method may not get passed properly.
- class_addMethod(ViewClass, sel_getUid("drawRect:"), (IMP) View_drawRect, "v@:");
- // And again, we tell the runtime that this class is now valid to be used.
- // At this point, the application should run and display the screenshot shown below.
- objc_registerClassPair(ViewClass);
- // Create app delegate
- Class AppDelClass = objc_allocateClassPair( (Class) objc_getClass("NSObject"), "AppDelegate", 0 );
- class_addMethod(AppDelClass, sel_getUid( "applicationDidFinishLaunching:" ), (IMP) AppDel_didFinishLaunching, "i@:@" );
- objc_registerClassPair(AppDelClass);
- // Run
- id app = cmacs_simple_msgSend((id)objc_getClass("NSApplication"), sel_getUid("sharedApplication"));
- if (app == NULL)
- {
- fprintf(stderr,"Failed to initialized NSApplication... terminating...\n");
- return;
- }
- id appDelObj = cmacs_simple_msgSend((id)objc_getClass("AppDelegate"), sel_getUid("alloc"));
- appDelObj = cmacs_simple_msgSend(appDelObj, sel_getUid("init"));
- cmacs_void_msgSend1(app, sel_getUid("setDelegate:"), appDelObj);
- cmacs_void_msgSend(app, sel_getUid("run"));
- }
- #include <dlfcn.h>
- #include <stdlib.h>
- #include <string.h>
- void* GLGetProcAddress (const char* name)
- {
- static void* image = NULL;
- if (NULL == image)
- image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
- return (image ? dlsym(image, (const char *)name) : NULL);
- }
- int main( int argc, char** argv )
- {
- glClearColor = GLGetProcAddress( "glClearColor" );
- glClear = GLGetProcAddress( "glClear" );
- glFlush = GLGetProcAddress( "glFlush" );
- printf( "hello world\n" );
- RunApplication();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement