2008年1月13日 星期日

學習 Evas

這陣子開始需要用 Evas 寫一些東西…
個人覺得學一個 library 最快的方法就是去真的拿它去寫程式。
如我的一個好友常說的:學中作,作中學 ^^
所以就寫了支小小的玩具… 和大家分享…


亂畫一個方塊,設定一個重力場,和一個 boundary反彈機制. ( 和一些阻泥 (mark 掉了)) 加上一個色彩空間的旋轉。就玩起來了 ^^

程式:

Filename: toy.c

#include <Ecore_Evas.h>
#include <Ecore.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define WIDTH 400
#define HEIGHT 400

Ecore_Evas * ee;
Evas * evas;
Evas_Object * base_rect;

double rotation[3][3] = { {0 , 0 , 1},
{1, 0, 0},
{0 , 1, 0}
};

struct bouncer {
Evas_Object *o;
int w,h;
double xs,ys;
double x,y;
double color[3];
};

inline void change_color(struct bouncer *bounce) {
double X[3];
int i,j;
int R,G,B;
for (i=0;i<3;i++) {
X[i]=bounce->color[i];
bounce->color[i]=0;
}
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
bounce->color[i] += rotation[i][j]*X[j];
}
}
R=bounce->color[0] < 0 ? bounce->color[0] + 255: bounce->color[0];
G=bounce->color[1] < 0 ? bounce->color[1] + 255: bounce->color[1];
B=bounce->color[2] < 0 ? bounce->color[2] + 255: bounce->color[2];
evas_object_color_set( bounce->o , R, G, B, 255);
}

inline void center(struct bouncer *bounce,double *x,double *y) {
*x = bounce->x + bounce->w/2;
*y = bounce->y + bounce->h/2;
}

inline void grav(struct bouncer *bounce) {
const double G=300;
double Gx,Gy,Gs;
double x,y;
double dist;
center(bounce,&x,&y);

dist = pow(pow(WIDTH/2-x,2) + pow(HEIGHT/2-y,2),0.5);
Gs = G/pow(dist,2);
Gx = (WIDTH/2 - x)/dist;
Gy = (HEIGHT/2 -y)/dist;
Gx = Gx * Gs;
Gy = Gy * Gs;

bounce->xs += Gx;
bounce->ys += Gy;

// Slow down~~
//bounce->xs = abs(bounce->xs) > 13 ? bounce->xs *(1/ pow(bounce->xs-10,2)): bounce->xs;
//bounce->ys = abs(bounce->ys) > 13 ? bounce->ys *(1/ pow(bounce->ys-10,2)): bounce->ys;
printf ("x=%2.2f y=%2.2f dist=%4.2f Gs=%2.2f Gx=%2.2f Gy=%2.2f xs=%2.2f ys=%2.2f\n",x,y,dist,Gs,Gx,Gy,bounce->xs,bounce->ys);
}
inline void calculate_pos(struct bouncer *bounce) {
grav(bounce);
bounce->x += bounce->xs;
bounce->y += bounce->ys;

// bounce
bounce->xs = bounce->x >=0 ? bounce->x + bounce->w <= WIDTH ? bounce->xs : -bounce->xs : -bounce->xs;
bounce->ys = bounce->y >=0 ? bounce->y + bounce->h <= HEIGHT? bounce->ys : - bounce->ys : - bounce->ys;
}

int move(void *data) {
struct bouncer *bounce =(struct bouncer *)data;
calculate_pos(bounce);
evas_object_move(bounce->o,bounce->x,bounce->y);
change_color(bounce);
return 1;
}

int main() {
struct bouncer bounce;
ecore_evas_init();

ee = ecore_evas_software_x11_new(NULL, 0, 0, 0, WIDTH, HEIGHT);
ecore_evas_title_set(ee, "Tick's Evas Study");
// ecore_evas_borderless_set(ee, 1); // borderless
ecore_evas_borderless_set(ee, 0); // border
ecore_evas_show(ee);


evas = ecore_evas_get(ee);
evas_font_path_append(evas, "fonts/");


bounce.o = evas_object_rectangle_add(evas);
bounce.w=bounce.h= (double)WIDTH/4;
bounce.x=(double)(WIDTH - bounce.w)/3;
bounce.y=(double)(HEIGHT- bounce.h)/6;
bounce.xs=-1.1; bounce.ys=1.1;
bounce.color[0]=128;
bounce.color[1]=64;
bounce.color[2]=255;
change_color(&bounce);
evas_object_resize( bounce.o, bounce.w, bounce.h);
evas_object_move( bounce.o,bounce.x,bounce.y);
evas_object_show( bounce.o);

ecore_timer_add((double)0.01,move,&bounce);

ecore_main_loop_begin();

return 0;
}


沒有留言: