Преглед изворни кода

Organize implementation

Put Windows implementation to its own C file.
master
TheoryOfNekomata пре 1 година
родитељ
комит
a86aee3d6a
3 измењених фајлова са 20 додато и 186 уклоњено
  1. +4
    -2
      CMakeLists.txt
  2. +1
    -169
      src/main.c
  3. +15
    -15
      src/main.m

+ 4
- 2
CMakeLists.txt Прегледај датотеку

@@ -12,10 +12,12 @@ if (WIN32)
set(PROJECT_ARCH x86)
add_definitions(-DWIN32)
endif ()
add_executable(cerberus WIN32 src/main.c)
add_executable(cerberus WIN32 src/main.h src/main.c)
elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# does not work, use Xcode to build
# TODO use makefile instead of cmake?
add_definitions(-DMACOS)
add_executable(cerberus src/main.c)
add_executable(cerberus src/main.h src/main.m)
include_directories(/usr/include)
link_directories()
elseif(UNIX)


+ 1
- 169
src/main.c Прегледај датотеку

@@ -1,7 +1,4 @@
#define WINDOW_X 0
#define WINDOW_Y 0
#define WINDOW_WIDTH 350
#define WINDOW_HEIGHT 250
#include "main.h"

#ifdef WIN64

@@ -56,169 +53,4 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
return DefWindowProcW(hwnd, msg, wParam, lParam);
}

#elif defined MACOS

//original objective-c
//
//#import <Cocoa/Cocoa.h>
//int main ()
//{
// @autoreleasepool{
// [NSApplication sharedApplication];
// [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// id applicationName = [[NSProcessInfo processInfo] processName];
// id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 120, 120)
// styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
// [window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
// [window setTitle: applicationName];
// [window makeKeyAndOrderFront:nil];
// [NSApp activateIgnoringOtherApps:YES];
// [NSApp run];
// }
// return 0;
//}

// $ clang minimal.c -framework Cocoa -o minimal.app

#include <objc/runtime.h>
#include <objc/message.h>

#include <Carbon/Carbon.h>

#define cls objc_getClass
#define sel sel_getUid
#define msg ((id (*)(id, SEL, ...))objc_msgSend)
#define cls_msg ((id (*)(Class, SEL, ...))objc_msgSend)

// poor man's bindings!
typedef enum NSApplicationActivationPolicy {
NSApplicationActivationPolicyRegular = 0,
NSApplicationActivationPolicyAccessory = 1,
NSApplicationActivationPolicyERROR = 2,
} NSApplicationActivationPolicy;

typedef enum NSWindowStyleMask {
NSWindowStyleMaskBorderless = 0,
NSWindowStyleMaskTitled = 1 << 0,
NSWindowStyleMaskClosable = 1 << 1,
NSWindowStyleMaskMiniaturizable = 1 << 2,
NSWindowStyleMaskResizable = 1 << 3,
} NSWindowStyleMask;

typedef enum NSBackingStoreType {
NSBackingStoreBuffered = 2,
} NSBackingStoreType;

int main(int argc, char *argv[])
{
// id app = [NSApplication sharedApplication];
id app = cls_msg(cls("NSApplication"), sel("sharedApplication"));

// [app setActivationPolicy:NSApplicationActivationPolicyRegular];
msg(app, sel("setActivationPolicy:"), NSApplicationActivationPolicyRegular);

struct CGRect frameRect = {0, 0, 600, 500};

// id window = [[NSWindow alloc] initWithContentRect:frameRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
id window = msg(cls_msg(cls("NSWindow"), sel("alloc")),
sel("initWithContentRect:styleMask:backing:defer:"),
frameRect,
NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable,
NSBackingStoreBuffered,
false);
msg(window, sel("setTitle:"), cls_msg(cls("NSString"), sel("stringWithUTF8String:"), "Pure C App"));

// [window makeKeyAndOrderFront:nil];
msg(window, sel("makeKeyAndOrderFront:"), nil);

// [app activateIgnoringOtherApps:YES];
msg(app, sel("activateIgnoringOtherApps:"), true);

msg(app, sel("run"));
}

//
//#include <objc/runtime.h>
//#include <objc/message.h>
//
//#include <Carbon/Carbon.h>
//
//#define cls objc_getClass
//#define sel sel_getUid
//#define msg ((id (*)(id, SEL, ...))objc_msgSend)
//#define cls_msg ((id (*)(Class, SEL, ...))objc_msgSend)
//
//// poor man's bindings!
//typedef enum NSApplicationActivationPolicy {
// NSApplicationActivationPolicyRegular = 0,
// NSApplicationActivationPolicyAccessory = 1,
// NSApplicationActivationPolicyERROR = 2,
//} NSApplicationActivationPolicy;
//
//typedef enum NSWindowStyleMask {
// NSWindowStyleMaskBorderless = 0,
// NSWindowStyleMaskTitled = 1 << 0,
// NSWindowStyleMaskClosable = 1 << 1,
// NSWindowStyleMaskMiniaturizable = 1 << 2,
// NSWindowStyleMaskResizable = 1 << 3,
//} NSWindowStyleMask;
//
//typedef enum NSBackingStoreType {
// NSBackingStoreBuffered = 2,
//} NSBackingStoreType;
//
//int main(int argc, char *argv[])
//{
// // id app = [NSApplication sharedApplication];
// id app = cls_msg(
// cls("NSApplication"),
// sel("sharedApplication")
// );
//
// // [app setActivationPolicy:NSApplicationActivationPolicyRegular];
// msg(
// app,
// sel("setActivationPolicy:"),
// NSApplicationActivationPolicyRegular
// );
//
// struct CGRect frameRect = {
// WINDOW_X,
// WINDOW_Y,
// WINDOW_WIDTH,
// WINDOW_HEIGHT
// };
//
// id applicationName = msg(
// cls_msg(
// cls("NSProcessInfo"),
// sel("processInfo")
// ),
// sel("processName")
// );
//
// // id window = [[NSWindow alloc] initWithContentRect:frameRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
// id window = msg(
// cls_msg(cls("NSWindow"), sel("alloc")),
// sel("initWithContentRect:styleMask:backing:defer:"),
// frameRect,
// NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable,
// NSBackingStoreBuffered,
// false
// );
//
// printf("%p\n", window);
// msg(window, sel("setTitle:"), applicationName);

// [window makeKeyAndOrderFront:nil];
//msg(window, sel("makeKeyAndOrderFront:"), nil);

// [app activateIgnoringOtherApps:YES];
//msg(app, sel("activateIgnoringOtherApps:"), true);

//msg(app, sel("run"));
//}

// https://stackoverflow.com/questions/30269329/creating-window-application-in-pure-c-on-mac-osx

#endif

+ 15
- 15
src/main.m Прегледај датотеку

@@ -1,19 +1,19 @@
#import <Cocoa/Cocoa.h>
#import "main.h"

int main ()
{
@autoreleasepool{
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
id applicationName = [[NSProcessInfo processInfo] processName];
id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT)
styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO];
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
[window setTitle: applicationName];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
}
return 0;
int main() {
// https://stackoverflow.com/questions/30269329/creating-window-application-in-pure-c-on-mac-osx
@autoreleasepool {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
id applicationName = [[NSProcessInfo processInfo] processName];
id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT)
styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO];
[window cascadeTopLeftFromPoint:NSMakePoint(20, 20)];
[window setTitle:applicationName];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
}
return 0;
}

Loading…
Откажи
Сачувај