-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmdline): support set native window and sdl window to sdl render
Signed-off-by: pingkai <pingkai010@gmail.com>
- Loading branch information
Showing
6 changed files
with
235 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
|
||
#include "nativeWindow.h" | ||
|
||
#ifdef TEST_NATIVE_COCOA | ||
|
||
#include <Cocoa/Cocoa.h> | ||
|
||
static void *CreateWindowCocoa(int w, int h); | ||
static void DestroyWindowCocoa(void *window); | ||
|
||
NativeWindowFactory CocoaWindowFactory = {"cocoa", CreateWindowCocoa, DestroyWindowCocoa}; | ||
|
||
static void *CreateWindowCocoa(int w, int h) | ||
{ | ||
NSAutoreleasePool *pool; | ||
NSWindow *nswindow; | ||
NSRect rect; | ||
unsigned int style; | ||
NSArray *screens = [NSScreen screens]; | ||
|
||
pool = [[NSAutoreleasePool alloc] init]; | ||
|
||
rect.origin.x = 0; | ||
rect.origin.y = 0; | ||
rect.size.width = w; | ||
rect.size.height = h; | ||
rect.origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - rect.origin.y - rect.size.height; | ||
|
||
style = (NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable); | ||
// NSRect viewport = [contentView bounds]; | ||
// if ([contentView respondsToSelector:@selector(convertRectToBacking:)]) { | ||
// viewport = [contentView convertRectToBacking:viewport]; | ||
// } | ||
// rect.size.width = viewport.size.width; | ||
// rect.size.height = viewport.size.height; | ||
|
||
/* Figure out which screen to place this window */ | ||
NSScreen *screen = nil; | ||
for (NSScreen *candidate in screens) { | ||
NSRect screenRect = [candidate frame]; | ||
if (rect.origin.x >= screenRect.origin.x && rect.origin.x < screenRect.origin.x + screenRect.size.width && | ||
rect.origin.y >= screenRect.origin.y && rect.origin.y < screenRect.origin.y + screenRect.size.height) { | ||
screen = candidate; | ||
rect.origin.x -= screenRect.origin.x; | ||
rect.origin.y -= screenRect.origin.y; | ||
} | ||
} | ||
|
||
nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen]; | ||
|
||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200 /* Added in the 10.12.0 SDK. */ | ||
/* By default, don't allow users to make our window tabbed in 10.12 or later */ | ||
if ([nswindow respondsToSelector:@selector(setTabbingMode:)]) { | ||
[nswindow setTabbingMode:NSWindowTabbingModeDisallowed]; | ||
} | ||
#endif | ||
[nswindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary]; | ||
rect = [nswindow contentRectForFrameRect:[nswindow frame]]; | ||
NSView *contentView = [[NSView alloc] initWithFrame:rect]; | ||
#ifdef __clang__ | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
#endif | ||
if ([contentView respondsToSelector:@selector(setWantsBestResolutionOpenGLSurface:)]) { | ||
// BOOL highdpi = (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) != 0; | ||
[contentView setWantsBestResolutionOpenGLSurface:YES]; | ||
} | ||
#ifdef __clang__ | ||
#pragma clang diagnostic pop | ||
#endif | ||
|
||
|
||
[nswindow makeKeyAndOrderFront:nil]; | ||
[nswindow setContentView:contentView]; | ||
[contentView release]; | ||
|
||
[pool release]; | ||
|
||
return nswindow; | ||
} | ||
|
||
static void DestroyWindowCocoa(void *window) | ||
{ | ||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | ||
NSWindow *nswindow = (NSWindow *) window; | ||
|
||
[nswindow close]; | ||
[pool release]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Created by moqi on 2020/6/4. | ||
// | ||
#include "nativeWindow.h" | ||
#ifdef TEST_NATIVE_X11 | ||
|
||
static void *CreateWindowX11(int w, int h); | ||
static void DestroyWindowX11(void *window); | ||
|
||
NativeWindowFactory X11WindowFactory = { | ||
"x11", | ||
CreateWindowX11, | ||
DestroyWindowX11 | ||
}; | ||
|
||
static Display *dpy; | ||
|
||
static void * | ||
CreateWindowX11(int w, int h) | ||
{ | ||
Window window = 0; | ||
|
||
dpy = XOpenDisplay(NULL); | ||
if (dpy) { | ||
window = | ||
XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, w, h, 0, 0, | ||
0); | ||
XMapRaised(dpy, window); | ||
XSync(dpy, False); | ||
} | ||
return (void *) window; | ||
} | ||
|
||
static void | ||
DestroyWindowX11(void *window) | ||
{ | ||
if (dpy) { | ||
XDestroyWindow(dpy, (Window) window); | ||
XCloseDisplay(dpy); | ||
} | ||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Created by moqi on 2020/6/4. | ||
// | ||
|
||
#ifndef CICADAMEDIA_NATIVEWINDOW_H | ||
#define CICADAMEDIA_NATIVEWINDOW_H | ||
#include <SDL2/SDL.h> | ||
#include <SDL2/SDL_syswm.h> | ||
|
||
typedef struct { | ||
const char *tag; | ||
void *(*CreateNativeWindow)(int w, int h); | ||
void (*DestroyNativeWindow)(void *window); | ||
} NativeWindowFactory; | ||
|
||
#ifdef SDL_VIDEO_DRIVER_WINDOWS | ||
#define TEST_NATIVE_WINDOWS | ||
extern NativeWindowFactory WindowsWindowFactory; | ||
#endif | ||
|
||
#ifdef SDL_VIDEO_DRIVER_X11 | ||
#define TEST_NATIVE_X11 | ||
extern NativeWindowFactory X11WindowFactory; | ||
#endif | ||
|
||
#ifdef SDL_VIDEO_DRIVER_COCOA | ||
/* Actually, we don't really do this, since it involves adding Objective C | ||
support to the build system, which is a little tricky. You can uncomment | ||
it manually though and link testnativecocoa.m into the test application. | ||
*/ | ||
#define TEST_NATIVE_COCOA | ||
extern NativeWindowFactory CocoaWindowFactory; | ||
#endif | ||
|
||
static NativeWindowFactory *getNativeFactor() | ||
{ | ||
#ifdef SDL_VIDEO_DRIVER_COCOA | ||
return &CocoaWindowFactory; | ||
#endif | ||
|
||
#ifdef SDL_VIDEO_DRIVER_WINDOWS | ||
return &WindowsWindowFactory; | ||
#endif | ||
#ifdef SDL_VIDEO_DRIVER_X11 | ||
return &X11WindowFactory; | ||
#endif | ||
return NULL; | ||
} | ||
|
||
|
||
#endif//CICADAMEDIA_NATIVEWINDOW_H |