Paste number 2718: "3d" window warp example

Index of paste annotations: 1

Paste number 2718: "3d" window warp example
Pasted by: xmath
When:4 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+23I
Channel:#macdev
Paste contents:
Raw Source | XML | Display As
/* save as:  warp.c
 * compile with:  gcc -Wall -o warp warp.c -framework Carbon
 */

#include <Carbon/Carbon.h>

OSErr CPSEnableForegroundOperation(ProcessSerialNumber *psn);

typedef struct CGPointWarp CGPointWarp;

struct CGPointWarp {
	CGPoint local;
	CGPoint global;
};

typedef int CGSWindowID;
typedef void *CGSConnectionID;

extern CGSConnectionID _CGSDefaultConnection(void);
extern CGSWindowID GetNativeWindowFromWindowRef(WindowRef);
extern CGError CGSSetWindowWarp(CGSConnectionID, CGSWindowID, int w, int h, CGPointWarp mesh[h][w]);

#define W 2
#define H 31

#define LEFT 100.0
#define TOP 100.0
#define WIDTH 250.0
#define HEIGHT 300.0

#define X1 (LEFT - WIDTH/3)
#define Y1 (TOP + HEIGHT/2)
#define X2 (X1 + WIDTH/2)
#define Y2 (Y1 - HEIGHT/2)
#define X3 (X1 + WIDTH*2/3)
#define Y3 (Y1 + HEIGHT/2)

static inline float bezier(float p1, float p2, float p3, float t) {
	float nt = 1 - t;
	return p1 * nt * nt + 2 * p2 * t * nt + p3 * t * t;
}

int main() {
        ProcessSerialNumber psn;
	WindowRef w;
	Rect r = { TOP, LEFT, TOP + HEIGHT, LEFT + WIDTH };
	CGPointWarp mesh[H][W];
	int i;

	for (i = 0; i < H; i++) {
		float t = (float) i / (H - 1);
		mesh[i][0].local.x = 0;
		mesh[i][0].local.y = t * HEIGHT;
		mesh[i][0].global.x = bezier(X1, X2, X3, t);
		mesh[i][0].global.y = bezier(Y1, Y2, Y3, t);
		mesh[i][1].local.x = WIDTH;
		mesh[i][1].local.y = mesh[i][0].local.y;
		mesh[i][1].global.x = WIDTH + mesh[i][0].global.x;
		mesh[i][1].global.y = mesh[i][0].global.y;
	}

#if 1	/* omit this if using XCode */
	GetCurrentProcess(&psn);
	CPSEnableForegroundOperation(&psn);
	SetFrontProcess(&psn);
#endif

	CreateNewWindow(kDocumentWindowClass, kWindowMetalAttribute
			| kWindowCompositingAttribute
			| kWindowStandardHandlerAttribute
			| kWindowLiveResizeAttribute
			| kWindowStandardDocumentAttributes, &r, &w);
	SetWindowBounds(w, kWindowStructureRgn, &r);

	ShowWindow(w);

	CGSSetWindowWarp(_CGSDefaultConnection(), GetNativeWindowFromWindowRef(w), W, H, mesh);

	RunApplicationEventLoop();

	return 0;
}

Annotations for this paste:

Annotation number 1: add an unwarped window so you can find the resize on the real one
Pasted by: Feanor
When:4 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+23I#1
Paste contents:
Raw Source | Display As
/* save as:  warp.c
 * compile with:  gcc -Wall -o warp warp.c -framework Carbon
 */

#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>

OSErr CPSEnableForegroundOperation(ProcessSerialNumber *psn);

typedef struct CGPointWarp CGPointWarp;

struct CGPointWarp {
	CGPoint local;
	CGPoint global;
};

typedef int CGSWindowID;
typedef void *CGSConnectionID;

extern CGSConnectionID _CGSDefaultConnection(void);
extern CGSWindowID GetNativeWindowFromWindowRef(WindowRef);
extern CGError CGSSetWindowWarp(CGSConnectionID, CGSWindowID, int w, int h, CGPointWarp mesh[h][w]);

#define W 2
#define H 31

#define LEFT 100.0
#define TOP 100.0
#define WIDTH 250.0
#define HEIGHT 300.0

#define X1 (LEFT - WIDTH/3)
#define Y1 (TOP + HEIGHT/2)
#define X2 (X1 + WIDTH/2)
#define Y2 (Y1 - HEIGHT/2)
#define X3 (X1 + WIDTH*2/3)
#define Y3 (Y1 + HEIGHT/2)

static inline float bezier(float p1, float p2, float p3, float t) {
	float nt = 1 - t;
	return p1 * nt * nt + 2 * p2 * t * nt + p3 * t * t;
}

int main() {
        ProcessSerialNumber psn;
	WindowRef w, w2;
	Rect r = { TOP, LEFT, TOP + HEIGHT, LEFT + WIDTH };
	CGPointWarp mesh[H][W];
	int i;

	for (i = 0; i < H; i++) {
		float t = (float) i / (H - 1);
		mesh[i][0].local.x = 0;
		mesh[i][0].local.y = t * HEIGHT;
		mesh[i][0].global.x = bezier(X1, X2, X3, t);
		mesh[i][0].global.y = bezier(Y1, Y2, Y3, t);
		mesh[i][1].local.x = WIDTH;
		mesh[i][1].local.y = mesh[i][0].local.y;
		mesh[i][1].global.x = WIDTH + mesh[i][0].global.x;
		mesh[i][1].global.y = mesh[i][0].global.y;
	}

#if 1	/* omit this if using XCode */
	GetCurrentProcess(&psn);
	CPSEnableForegroundOperation(&psn);
	SetFrontProcess(&psn);
#endif

	CreateNewWindow(kDocumentWindowClass, kWindowMetalAttribute
			| kWindowCompositingAttribute
			| kWindowStandardHandlerAttribute
			| kWindowLiveResizeAttribute
			| kWindowStandardDocumentAttributes, &r, &w);
	SetWindowBounds(w, kWindowStructureRgn, &r);

	
	CreateNewWindow(kDocumentWindowClass, kWindowMetalAttribute
			| kWindowCompositingAttribute
			| kWindowStandardHandlerAttribute
			| kWindowLiveResizeAttribute
			| kWindowStandardDocumentAttributes, &r, &w2);
	SetWindowBounds(w2, kWindowStructureRgn, &r);
	
	ShowWindow(w2);
	ShowWindow(w);

	CGSSetWindowWarp(_CGSDefaultConnection(), GetNativeWindowFromWindowRef(w), W, H, mesh);

	RunApplicationEventLoop();

	return 0;
}

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.