Skip to content

Commit

Permalink
add mac Xcode example project.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsiannian committed Mar 14, 2014
1 parent 13ee15c commit 586e355
Show file tree
Hide file tree
Showing 64 changed files with 20,425 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -5,3 +5,5 @@ ej2d.dSYM/*
*.exe
*.dll
*.ilk
mac/example/example.xcodeproj/xcuserdata
mac/example/example.xcodeproj/project.xcworkspace/xcuserdata
606 changes: 606 additions & 0 deletions mac/example/example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDESourceControlProjectFavoriteDictionaryKey</key>
<false/>
<key>IDESourceControlProjectIdentifier</key>
<string>CE4B60CE-5670-433F-B670-07F804E027F5</string>
<key>IDESourceControlProjectName</key>
<string>example</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>7BECAF10-5173-4676-9513-C1C7F21D6682</key>
<string>http://github.com/cloudwu/ejoy2d.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>mac/example/example.xcodeproj/project.xcworkspace</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>7BECAF10-5173-4676-9513-C1C7F21D6682</key>
<string>../../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>http://github.com/cloudwu/ejoy2d.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>7BECAF10-5173-4676-9513-C1C7F21D6682</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>7BECAF10-5173-4676-9513-C1C7F21D6682</string>
<key>IDESourceControlWCCName</key>
<string>ejoy2d</string>
</dict>
</array>
</dict>
</plist>
195 changes: 195 additions & 0 deletions mac/example/example/window.c
@@ -0,0 +1,195 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <GL/glxew.h>
#include <GL/glew.h>

/* include some silly stuff */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "winfw.h"

#define UPDATE_INTERVAL 1 /* 10ms */

void font_init();

struct X_context {
Display *display;
int screen_num;
Window wnd;
};

static GC gc;
static GLXContext g_context = 0;
struct X_context g_X;
/* Used to intercept window closing requests. */
static Atom wm_delete_window;

static uint32_t
_gettime(void) {
uint32_t t;
#if !defined(__APPLE__)
struct timespec ti;
clock_gettime(CLOCK_MONOTONIC, &ti);
t = (uint32_t)(ti.tv_sec & 0xffffff) * 100;
t += ti.tv_nsec / 10000000;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
t = (uint32_t)(tv.tv_sec & 0xffffff) * 100;
t += tv.tv_usec / 10000;
#endif

return t;
}

static void
update_frame() {
ejoy2d_win_frame();
glXSwapBuffers(g_X.display, g_X.wnd);
}

static int
glx_init(struct X_context *X)
{
XVisualInfo *vi;

int attrib[]={
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_DEPTH_SIZE, 1,
GLX_STENCIL_SIZE, 1,
None
};

if (g_context)
return 0;

vi = glXChooseVisual( X->display, X->screen_num, attrib);

if (vi==0) {
return 1;
}

g_context = glXCreateContext( X->display, vi , NULL , True);

if (g_context == 0) {
return 1;
}

if (!glXMakeCurrent(X->display, X->wnd, g_context )) {
g_context=NULL;
return 1;
}

return 0;
}

static void
init_x() {
unsigned long black,white;
Display *dis;
int screen;
static Window win;

dis=XOpenDisplay(NULL);
screen=DefaultScreen(dis);
black=BlackPixel(dis,screen);
white=WhitePixel(dis, screen);


win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
WIDTH, HEIGHT, 5,white, black);

XMapWindow(dis, win);
wm_delete_window = XInternAtom(dis, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dis, win, &wm_delete_window, 1);

XSetStandardProperties(dis,win,"ejoy2d",NULL,None,NULL,0,NULL);
XSelectInput(dis, win,
ExposureMask|KeyPressMask|KeyReleaseMask
|ButtonPressMask|ButtonReleaseMask|ButtonMotionMask);
gc=XCreateGC(dis, win, 0,0);
XSetBackground(dis,gc,white);
XSetForeground(dis,gc,black);
XClearWindow(dis, win);
XMapRaised(dis, win);

g_X.display = dis;
g_X.screen_num = screen;
g_X.wnd = win;

if (glx_init(&g_X)){
printf("glx init failed\n");
exit(1);
}
if ( glewInit() != GLEW_OK ) {
printf("glew init failed");
exit(1);
}
};

static void
close_x() {
Display *dis = g_X.display;
XFreeGC(dis, gc);
XDestroyWindow(dis, g_X.wnd);
XCloseDisplay(dis);
exit(1);
}

int
main(int argc, char *argv[]) {
XEvent event;
uint32_t timestamp = 0;
KeySym keysym;
char keychar[255];
init_x();
font_init();

ejoy2d_win_init(argc, argv);

for (;;) {
while(XPending(g_X.display) > 0) {
XNextEvent(g_X.display, &event);
if (XFilterEvent(&event,None))
continue;
switch (event.type) {
case Expose:
if (event.xexpose.count==0)
update_frame();
break;
case KeyPress:
XLookupString(&event.xkey, keychar, 255, &keysym, 0);
if (keychar[0] == 'q' || keychar[0] == 'Q') {
close_x();
}
break;
case ButtonPress:
ejoy2d_win_touch(event.xbutton.x, event.xbutton.y, TOUCH_BEGIN);
break;
case ButtonRelease:
ejoy2d_win_touch(event.xbutton.x,event.xbutton.y,TOUCH_END);
break;
case MotionNotify:
ejoy2d_win_touch(event.xbutton.x,event.xbutton.y,TOUCH_MOVE);
break;
case ClientMessage:
if ((Atom)event.xclient.data.l[0] == wm_delete_window) {
close_x();
}
break;
}
}

uint32_t old = timestamp;
timestamp= _gettime();
if (timestamp - old >= UPDATE_INTERVAL) {
ejoy2d_win_update();
update_frame();
}
else
usleep(1000);
}
}
139 changes: 139 additions & 0 deletions mac/example/example/winfont.c
@@ -0,0 +1,139 @@
#include "label.h"
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

FT_Library library;

#ifdef __APPLE__
static const char* TTFONT = "/System/Library/Fonts/STHeiti Light.ttc";
#else
static const char* TTFONT = "/usr/share/fonts/wenquanyi/wqy-zenhei/wqy-zenhei.ttc";
#endif

#define _fault(errcode, msg) __fault(errcode, msg, __FILE__, __LINE__)

static void __fault(int errcode, const char * msg, const char *file, int line) {
if (errcode)
printf("err(%d): %s, error occured in file %s, line %d\n\n\t have a look at fterrdef.h\n",errcode, msg, file, line);
else
printf("err: %s, occured in file %s, line %d\n", msg, file, line);
exit(1);
}
struct bitmap {
void * buf;
int w;
int h;
int pitch;
};

/* x,y is offset of target */
static void
cpy_bitmap(struct bitmap * target, struct bitmap * src, int x, int y) {
unsigned char * sbuf = src->buf;
unsigned char * tbuf = target->buf;

int x0,y0,x1,y1;
x0 = x > 0 ? x : 0;
y0 = y > 0 ? y : 0;
x1 = (x + src->w > target->w) ? target->w : (x+src->w);
y1 = (y + src->h > target->h) ? target->h : (y+src->h);

if (x1 <= x0 || y1 <= y0)
return;
if (x0 >= target->w || y0 >= target->h)
return;

int w = x1 - x0;
int h = y1 - y0;

tbuf += y0 * target->pitch + x0;
sbuf += (y0 - y)*src->pitch + x0 - x;
int i,j;
for (i=0;i<h;i++) {
for (j=0;j<w;j++)
tbuf[j] = sbuf[j];
sbuf += src->pitch;
tbuf += target->pitch;
}
}

void
font_create(int font_size, struct font_context *ctx) {
FT_Face face;
int err = FT_New_Face(library, TTFONT, 0, &face);
if (err) {
if (err == 1)
_fault(err, "set your own vector font resource path");
else
_fault(err, "new face failed");
}

err = FT_Set_Pixel_Sizes(face,0,font_size);

if (err)
_fault(err, "set char size failed");

ctx->font = face;
ctx->ascent = face->size->metrics.ascender >>6;
ctx->h = face->size->metrics.height >> 6;
}

void
font_release(struct font_context *ctx) {
FT_Done_Face(ctx->font);
}

void
font_size(const char *str, int unicode, struct font_context *ctx) {
FT_Face face = ctx->font;
FT_UInt glyph_index = FT_Get_Char_Index(face, unicode);
if (!glyph_index) {
printf("cannot find glyph %d\n", unicode);
exit(1);
}
FT_Load_Glyph(face, glyph_index, FT_LOAD_NO_BITMAP);

int err = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
if (err)
_fault(err, "render failed");

FT_GlyphSlot slot = face->glyph;

ctx->w = slot->advance.x >> 6;
}


void
font_glyph(const char * str, int unicode, void * buffer, struct font_context *ctx) {
FT_Face face = ctx->font;
FT_GlyphSlot slot = face->glyph;
FT_Bitmap *bitmap = &(slot->bitmap);

int offx = slot->bitmap_left;
int offy = ctx->ascent - slot->bitmap_top;

struct bitmap src;
struct bitmap target;
src.buf = bitmap->buffer;
src.pitch = bitmap->pitch;
src.w = bitmap->width;
src.h = bitmap->rows;
target.buf = buffer;
target.w = target.pitch = ctx->w;
target.h = ctx->h;

cpy_bitmap(&target, &src, offx, offy);
}

void
font_init() {
if (FT_Init_FreeType(&library)) {
printf("font init failed");
}
}

0 comments on commit 586e355

Please sign in to comment.