Advertisement
Guest User

Untitled

a guest
Jan 30th, 2018
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. //#include <OpenGL/gl.h>
  4.  
  5. #include <objc/message.h>
  6. #include <objc/runtime.h>
  7.  
  8. typedef struct CMPoint
  9.     {
  10.     double x;
  11.     double y;
  12.     } CMPoint;
  13.  
  14. typedef struct CMSize
  15.     {
  16.     double width;
  17.     double height;
  18.     } CMSize;
  19.  
  20. typedef struct CMRect
  21.     {
  22.     CMPoint origin;
  23.     CMSize size;
  24.     } CMRect;
  25.  
  26. typedef struct AppDel
  27.     {
  28.     Class isa;
  29.     id window; // Will be an NSWindow later.
  30.     } AppDelegate;
  31.  
  32. enum
  33.     {
  34.     NSBorderlessWindowMask      = 0,
  35.     NSTitledWindowMask          = 1 << 0,
  36.     NSClosableWindowMask        = 1 << 1,
  37.     NSMiniaturizableWindowMask  = 1 << 2,
  38.     NSResizableWindowMask       = 1 << 3,
  39.     };
  40.  
  41. typedef id(*CMacsSimpleMessage)(id, SEL);
  42. typedef void(*CMacsVoidMessage)(id, SEL);
  43. typedef void(*CMacsVoidMessage1)(id, SEL, void *);
  44. typedef id(*CMacsRectMessage1)(id, SEL, CMRect);
  45. typedef id(*CMacsWindowInitMessage)(id, SEL, CMRect, int, int, bool);
  46.  
  47. CMacsSimpleMessage cmacs_simple_msgSend = (CMacsSimpleMessage)objc_msgSend;
  48. CMacsVoidMessage cmacs_void_msgSend  = (CMacsVoidMessage)objc_msgSend;
  49. CMacsVoidMessage1 cmacs_void_msgSend1 = (CMacsVoidMessage1)objc_msgSend;
  50. CMacsRectMessage1 cmacs_rect_msgSend1 = (CMacsRectMessage1)objc_msgSend;
  51. CMacsWindowInitMessage cmacs_window_init_msgSend = (CMacsWindowInitMessage)objc_msgSend;
  52.  
  53. #define GLCALLTYPE
  54.  
  55. #define GL_COLOR_BUFFER_BIT                                 0x00004000
  56. typedef unsigned int GLbitfield;
  57. typedef unsigned char GLboolean;
  58. typedef signed char GLbyte;
  59. typedef char GLchar;
  60. typedef float GLclampf;
  61. typedef unsigned int GLenum;
  62. typedef int GLfixed;
  63. typedef float GLfloat;
  64. typedef unsigned short GLhalf;
  65. typedef long long GLint64;
  66. typedef int GLint;
  67. typedef intptr_t GLintptr;
  68. typedef short GLshort;
  69. typedef int GLsizei;
  70. typedef size_t GLsizeiptr;
  71. typedef struct __GLsync *GLsync;
  72. typedef unsigned char GLubyte;
  73. typedef unsigned long long GLuint64;
  74. typedef unsigned int GLuint;
  75. typedef unsigned short GLushort;
  76. typedef void GLvoid;
  77.  
  78. void (GLCALLTYPE* glClearColor) (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) = 0;
  79. void (GLCALLTYPE* glClear) (GLbitfield mask) = 0;
  80. void (GLCALLTYPE* glFlush) (void) = 0;
  81.  
  82. // This is a simple -drawRect implementation for our class. We could have
  83. // used a NSTextField  or something of that sort instead, but I felt that this
  84. // stuck with the C-based mentality of the application.
  85. void View_drawRect(id self, SEL _cmd, CMRect rect)
  86.     {
  87.     printf( "rect %f, %f, %f, %f\n", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height );
  88.  
  89.     glClearColor(1, 0, 1, 0);
  90.     glClear(GL_COLOR_BUFFER_BIT);
  91.     //drawAnObject();
  92.     glFlush(); 
  93.     }
  94.  
  95. BOOL AppDel_didFinishLaunching(AppDelegate *self, SEL _cmd, id notification) {
  96.     self->window = cmacs_simple_msgSend((id)objc_getClass("NSWindow"), sel_getUid("alloc"));
  97.    
  98.     /// Create an instance of the window.
  99.     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);
  100.    
  101.     /// Create an instance of our view class.
  102.     ///
  103.     /// Relies on the view having declared a constructor that allocates a class pair for it.
  104.     id view = cmacs_rect_msgSend1(cmacs_simple_msgSend((id)objc_getClass("View"), sel_getUid("alloc")), sel_getUid("initWithFrame:"), (CMRect){ 0, 0, 320, 480 });
  105.    
  106.     // here we simply add the view to the window.
  107.     cmacs_void_msgSend1(self->window, sel_getUid("setContentView:"), view);
  108.     cmacs_simple_msgSend(self->window, sel_getUid("becomeFirstResponder"));
  109.    
  110.     // Shows our window in the bottom-left hand corner of the screen.
  111.     cmacs_void_msgSend1(self->window, sel_getUid("makeKeyAndOrderFront:"), self);
  112.     return YES;
  113. }
  114.  
  115. void RunApplication(void)
  116.     {
  117.     // Once again, just like the app delegate, we tell the runtime to
  118.     // create a new class, this time a subclass of 'UIView' and named 'View'.
  119.     Class ViewClass = objc_allocateClassPair((Class)objc_getClass("NSOpenGLView"), "View", 0);
  120.    
  121.     // and again, we tell the runtime to add a function called -drawRect:
  122.     // to our custom view. Note that there is an error in the type-specification
  123.     // of this method, as I do not know the @encode sequence of 'CGRect' off
  124.     // of the top of my head. As a result, there is a chance that the rect
  125.     // parameter of the method may not get passed properly.
  126.     class_addMethod(ViewClass, sel_getUid("drawRect:"), (IMP) View_drawRect, "v@:");
  127.    
  128.     // And again, we tell the runtime that this class is now valid to be used.
  129.     // At this point, the application should run and display the screenshot shown below.
  130.     objc_registerClassPair(ViewClass);
  131.  
  132.     // Create app delegate
  133.     Class AppDelClass = objc_allocateClassPair( (Class) objc_getClass("NSObject"), "AppDelegate", 0 );
  134.     class_addMethod(AppDelClass, sel_getUid( "applicationDidFinishLaunching:" ), (IMP) AppDel_didFinishLaunching, "i@:@" );
  135.     objc_registerClassPair(AppDelClass);
  136.    
  137.     // Run
  138.     id app = cmacs_simple_msgSend((id)objc_getClass("NSApplication"), sel_getUid("sharedApplication"));
  139.    
  140.     if (app == NULL)
  141.         {
  142.         fprintf(stderr,"Failed to initialized NSApplication...  terminating...\n");
  143.         return;
  144.         }
  145.    
  146.     id appDelObj = cmacs_simple_msgSend((id)objc_getClass("AppDelegate"), sel_getUid("alloc"));
  147.     appDelObj = cmacs_simple_msgSend(appDelObj, sel_getUid("init"));
  148.    
  149.     cmacs_void_msgSend1(app, sel_getUid("setDelegate:"), appDelObj);
  150.     cmacs_void_msgSend(app, sel_getUid("run"));
  151.     }
  152.  
  153.  
  154. #include <dlfcn.h>
  155. #include <stdlib.h>
  156. #include <string.h>
  157.  
  158. void* GLGetProcAddress (const char* name)
  159.     {
  160.     static void* image = NULL;
  161.     if (NULL == image)
  162.         image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
  163.  
  164.     return (image ? dlsym(image, (const char *)name) : NULL);
  165.     }
  166.  
  167.  
  168. int main( int argc, char** argv )
  169.     {
  170.     glClearColor = GLGetProcAddress( "glClearColor" );
  171.     glClear = GLGetProcAddress( "glClear" );
  172.     glFlush = GLGetProcAddress( "glFlush" );
  173.     printf( "hello world\n" );
  174.     RunApplication();
  175.     return 0;
  176.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement