Skip to content

Commit

Permalink
perpare for gui
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Oct 22, 2015
1 parent 220a440 commit c28fd37
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY : mingw ej2d linux undefined

CFLAGS = -g -Wall -Ilib -Ilib/render -Ilua -D EJOY2D_OS=$(OS)
CFLAGS = -g -Wall -Ilib -Ilib/render -Ilua -D EJOY2D_OS=$(OS) -D FONT_EDGE_HASH
LDFLAGS :=

RENDER := \
Expand Down
2 changes: 2 additions & 0 deletions ejoy2d/geometry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ local geo = {}
geo.line = assert(core.line)
geo.box = assert(core.box)
geo.polygon = assert(core.polygon)
geo.frame = assert(core.frame)
geo.scissor = assert(core.scissor)

return geo
38 changes: 38 additions & 0 deletions examples/ex09.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local ej = require "ejoy2d"
local geo = require "ejoy2d.geometry"
local spr = require "ejoy2d.sprite.c" -- for internal use

local game = {}

Expand All @@ -20,11 +21,48 @@ for i = 0, 5 do
end


local style_button = {
frame = 0xffffffff,
bgcolor = 0x80404040,
margin = 6,
border = 1,
}

local function button(x,y,w,h,text,s)
local default = style_button
s = s or default
local margin = s.margin or default.margin
local color = s.frame or default.frame
local bgcolor = s.bgcolor or default.bgcolor
local charsize = h - margin * 2
local border = s.border or default.border
geo.frame(x,y,w,h,color, border)
geo.box(x+border,y+border,w-border*2, h-border*2, bgcolor)
geo.scissor(x+border,y+border,w-border*2, h-border*2)
spr.drawtext(text,x,y+margin,w,charsize,color)
geo.scissor()
end

local function button_hover(x,y,w,h,text,s)
local default = style_button
s = s or default
local margin = s.margin or default.margin
local color = s.frame or default.frame
local bgcolor = s.bgcolor or default.bgcolor
local charsize = h - margin * 2
local border = s.border or default.border
geo.box(x,y,w, h, color)
geo.frame(x+border,y+border,w-border*2,h-border*2,bgcolor, border)
spr.drawtext(text,x,y+margin,w,charsize,bgcolor)
end

function game.drawframe()
ej.clear(0xff808080) -- clear (0.5,0.5,0.5,1) gray
geo.line(0,0,x,y,0xffffffff)
geo.box(100,100,200,300, 0x80ff0000)
geo.polygon(hexagon, 0x40ffff00)
button(400,400,80, 32, "我是一个按钮")
button_hover(400,440,80, 32, "按钮")
end

function game.touch(what, x, y)
Expand Down
73 changes: 73 additions & 0 deletions lib/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,79 @@ label_get_color(struct pack_label * l, const struct sprite_trans *arg) {
return color;
}

int
label_rawline(const char * str, struct pack_label *l) {
char utf8[7];
int i;
int w=0;
for (i=0; str[i];) {
int len = unicode_len(str[i]);
int unicode = copystr(utf8, str+i, len);
struct font_context ct = char_size(unicode, utf8, l->size, l->edge);
if (w+ct.w > l->width) {
break;
}
i+=len;
w += ct.w + l->space_w;
}
return i;
}

static int
raw_width(const char * str, struct pack_label * l) {
char utf8[7];
int i;
int w=0;
for (i=0; str[i];) {
int len = unicode_len(str[i]);
int unicode = copystr(utf8, str+i, len);
i+=len;
struct font_context ct = char_size(unicode, utf8, l->size, l->edge);
w += ct.w + l->space_w;
}
return w;
}

void
label_rawdraw(const char * str, float fx, float y, struct pack_label * l) {
shader_texture(Tex, 0);
uint32_t color = l->color;
int edge = l->edge;
int size = l->size;
int width = raw_width(str, l);
int x = (int)fx;

switch (l->align) {
case LABEL_ALIGN_LEFT:
break;
case LABEL_ALIGN_RIGHT:
x += l->width - width;
break;
case LABEL_ALIGN_CENTER:
x += (l->width - width)/2;
break;
}

char utf8[7];
int i;
struct matrix mat = {{ 1024,0,0,1024,0,y * SCREEN_SCALE}};
for (i=0; str[i];) {
int len = unicode_len(str[i]);
int unicode = copystr(utf8, str+i, len);
i+=len;
const struct dfont_rect * rect = dfont_lookup(Dfont, unicode, FONT_SIZE, edge);
if (rect == NULL) {
rect = gen_char(unicode,utf8,FONT_SIZE,edge);
if (rect == NULL)
continue;
}
mat.m[4]=x*SCREEN_SCALE;
draw_rect(rect,size,&mat,color,0);

x += (rect->w-1) * size / FONT_SIZE + l->space_w;
}
}

void
label_draw(const struct rich_text *rich, struct pack_label * l, struct srt *srt, const struct sprite_trans *arg) {
shader_texture(Tex, 0);
Expand Down
2 changes: 2 additions & 0 deletions lib/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ void label_load();
void label_unload();
void label_flush();

void label_rawdraw(const char * str, float x, float y, struct pack_label * l);
int label_rawline(const char * str, struct pack_label *l);
void label_draw(const struct rich_text *rich, struct pack_label * l, struct srt *srt, const struct sprite_trans *arg);
void label_size(const char * str, struct pack_label * l, int* width, int* height);
int label_char_size(struct pack_label* l, const char* chr, int* width, int* height, int* unicode);
Expand Down
99 changes: 99 additions & 0 deletions lib/lgeometry.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "renderbuffer.h"
#include "spritepack.h"
#include "array.h"
#include "scissor.h"

static int PROGRAM = 0;

Expand Down Expand Up @@ -112,6 +113,88 @@ lbox(lua_State *L) {
return 0;
}

/*
float x,y
float w,h
uint32_t color
float width
*/

static int
lframe(lua_State *L) {
float x = luaL_checknumber(L, 1) * SCREEN_SCALE;
float y = luaL_checknumber(L, 2) * SCREEN_SCALE;
float w = luaL_checknumber(L, 3) * SCREEN_SCALE;
float h = luaL_checknumber(L, 4) * SCREEN_SCALE;
uint32_t color = convert_color(luaL_checkinteger(L, 5));
float width = luaL_optnumber(L, 6, 1.0f) * SCREEN_SCALE;
struct vertex_pack vp[6];
int i;
for (i=0;i<6;i++) {
vp[i].tx = 0;
vp[i].ty = 0;
}
shader_program(PROGRAM, NULL);

/*
0 (x,y) 1 (x+w,y)
+----------------------------+
|\ /|
| +------------------------+ |
| |3 2| |
| | | |
| | | |
| | | |
| |4 3'| |
| +------------------------+ |
|/ \|
+----------------------------+
5 0'
*/

vp[0].vx = x;
vp[0].vy = y;
vp[1].vx = x+w;
vp[1].vy = y;

vp[2].vx = x+w-width;
vp[2].vy = y+width;
vp[3].vx = x+width;
vp[3].vy = y+width;

vp[4].vx = x+width;
vp[4].vy = y+h-width;
vp[5].vx = x;
vp[5].vy = y+h;

for (i=0;i<6;i++) {
screen_trans(&vp[i].vx, &vp[i].vy);
}
shader_drawpolygon(6, vp, color, 0);

vp[0].vx = x+w;
vp[0].vy = y+h;
vp[1].vx = x+w;
vp[1].vy = y;

vp[2].vx = x+w-width;
vp[2].vy = y+width;
vp[3].vx = x+w-width;
vp[3].vy = y+h-width;

vp[4].vx = x+width;
vp[4].vy = y+h-width;
vp[5].vx = x;
vp[5].vy = y+h;

for (i=0;i<6;i++) {
screen_trans(&vp[i].vx, &vp[i].vy);
}
shader_drawpolygon(6, vp, color, 0);

return 0;
}


/*
table float[]
Expand Down Expand Up @@ -150,13 +233,29 @@ lpolygon(lua_State *L) {
return 0;
}

static int
lscissor(lua_State *L) {
if (lua_gettop(L) == 0) {
scissor_pop();
} else {
int x = luaL_checkinteger(L,1);
int y = luaL_checkinteger(L,2);
int w = luaL_checkinteger(L,3);
int h = luaL_checkinteger(L,4);
scissor_push(x,y,w,h);
}
return 0;
}

int
ejoy2d_geometry(lua_State *L) {
luaL_Reg l[] = {
{"setprogram", lsetprogram},
{"line", lline },
{"box", lbox },
{"frame", lframe },
{"polygon", lpolygon },
{"scissor", lscissor },

{NULL,NULL},
};
Expand Down
69 changes: 69 additions & 0 deletions lib/lsprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -1605,11 +1605,80 @@ ldfont_mothod(lua_State *L) {
luaL_newlib(L, l);
}

/*
string text
number x
number y
integer w
integer size
uinteger color
boolean edge
string align
*/
static int
ldrawtext(lua_State *L) {
const char * str = luaL_checkstring(L, 1);
float x = luaL_checknumber(L, 2);
float y = luaL_checknumber(L, 3);
struct pack_label pl;
pl.width = luaL_checkinteger(L, 4);
pl.size = luaL_checkinteger(L, 5);
pl.height = pl.size;
pl.color = luaL_optinteger(L, 6, 0xffffffff);
pl.space_h = 0;
pl.space_w = 0;
pl.auto_scale = 0;
pl.align = LABEL_ALIGN_CENTER;
pl.edge = lua_toboolean(L, 7);
const char *align = lua_tostring(L, 8);
if (align) {
switch(align[0]) {
case 'l': case 'L' :
pl.align = LABEL_ALIGN_LEFT;
break;
case 'r': case 'R' :
pl.align = LABEL_ALIGN_RIGHT;
break;
}
}
shader_program(pl.edge ? PROGRAM_TEXT_EDGE : PROGRAM_TEXT, NULL);
label_rawdraw(str, x,y, &pl);
return 0;
}

/*
string text
integer width
integer size
integer from (default 0)
boolean edge (default false)
return
integer n
*/
static int
lsplittext(lua_State *L) {
size_t sz = 0;
const char * str = luaL_checklstring(L, 1, &sz);
struct pack_label pl;
pl.width = luaL_checkinteger(L, 2);
pl.size = luaL_checkinteger(L, 3);
int from = luaL_optinteger(L, 4, 0);
pl.edge = lua_toboolean(L, 5);
if (from < 0 || from >= sz)
return luaL_error(L, "invalid offset %d", from);
int n = label_rawline(str + from, &pl);
lua_pushinteger(L, n);
return 1;
}

int
ejoy2d_sprite(lua_State *L) {
luaL_Reg l[] ={
{ "new", lnew },
{ "label", lnewlabel },
{ "drawtext", ldrawtext },
{ "splittext", lsplittext },
{ "proxy", lnewproxy },
{ "dfont", lnewdfont },
{ "delete_dfont", ldeldfont },
Expand Down
2 changes: 1 addition & 1 deletion lib/scissor.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct scissor {

static struct scissor S;

void
static void
intersection(struct box * b, int * x, int * y, int * w, int * h) {
int newx = b->x > *x ? b->x : *x;
int newy = b->y > *y ? b->y : *y;
Expand Down

0 comments on commit c28fd37

Please sign in to comment.