九鼎创展论坛中文版English
登录 | 立即注册 设为首页收藏本站 切换到宽版
查看: 2588|回复: 1

miniGAME代码分析

[复制链接]
发表于 2014-10-10 16:20:39 | 显示全部楼层 |阅读模式
本帖最后由 laotang365 于 2014-10-10 16:59 编辑

开始
  1. static struct
  2. {
  3.     float x, y;                 /* position of happyface */
  4.     float xvel, yvel;           /* velocity of happyface */
  5. } faces[NUM_HAPPY_FACES];

  6. /*
  7.         Produces a random float x, min <= x <= max
  8.         following a uniform distribution
  9. */
  10. static float randomFloat(float min, float max)
  11. {
  12.         return rand() / (float) RAND_MAX * (max - min) + min;
  13. }

  14. static void initializeHappyFaces()
  15. {
  16.         int i;
  17.         for (i = 0; i < NUM_HAPPY_FACES; i++)
  18.         {
  19.                 faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
  20.                 faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
  21.                 faces[i].xvel = randomFloat(-0.4f, 0.4f);
  22.                 faces[i].yvel = randomFloat(-0.4f, 0.4f);
  23.         }
  24. }
复制代码
  1. static void minigame(void)
  2. {
  3.         struct surface_t * screen;
  4.         struct surface_t * obj;
  5.     struct rect_t dstRect, srcRect;
  6.         u32_t c;
  7.         int i;
  8.         u32_t startFrame, endFrame;
  9.         u32_t delay;

  10.         initializeHappyFaces();

  11.         screen = s5pv210_screen_surface();//初始化图片的地址,速率。

  12.         c = surface_map_color(screen, get_named_color("chocolate"));//得到咖啡色
  13.         obj = surface_alloc_from_gimage(&obj_image);//得到图片的surface_t结构

  14.         /* setup boundaries for happyface bouncing */
  15.     u32_t maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
  16.     u32_t maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
  17.     u32_t minx = 0;
  18.     u32_t miny = 0;

  19.     /* setup rects for drawing */
  20.     srcRect.x = 0;
  21.     srcRect.y = 0;
  22.     srcRect.w = HAPPY_FACE_SIZE;
  23.     srcRect.h = HAPPY_FACE_SIZE;
  24.     dstRect.w = HAPPY_FACE_SIZE;
  25.     dstRect.h = HAPPY_FACE_SIZE;

  26.     while (1)
  27.         {
  28.             startFrame = jiffies;//标记cpu时刻为帧起始时刻

  29.             s5pv210_screen_swap();//切换图层
  30.                 surface_fill(screen, &screen->clip, c, BLEND_MODE_REPLACE);//用咖啡色清屏
  31.                 for (i = 0; i < NUM_HAPPY_FACES; i++)
  32.                 {
  33.                         faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;//x += ((-0.4~0.4) * 20  )
  34.                         faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;//
  35.                         if (faces[i].x > maxx)//横坐标越界
  36.                         {
  37.                                 faces[i].x = maxx;
  38.                                 faces[i].xvel = -faces[i].xvel;
  39.                         }
  40.                         else if (faces[i].y > maxy)//纵坐标越界
  41.                         {
  42.                                 faces[i].y = maxy;
  43.                                 faces[i].yvel = -faces[i].yvel;
  44.                         }
  45.                         if (faces[i].x < minx)//横坐标越界
  46.                         {
  47.                                 faces[i].x = minx;
  48.                                 faces[i].xvel = -faces[i].xvel;
  49.                         }
  50.                         else if (faces[i].y < miny)//纵坐标越界
  51.                         {
  52.                                 faces[i].y = miny;
  53.                                 faces[i].yvel = -faces[i].yvel;
  54.                         }
  55.                         dstRect.x = faces[i].x;
  56.                         dstRect.y = faces[i].y;
  57.                         surface_blit(screen, &dstRect, obj, &srcRect, BLEND_MODE_REPLACE);//移动图片
  58.                 }
  59.                 s5pv210_screen_flush();

  60.                 endFrame = jiffies;

  61.                 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame) * 10;//计算延时
  62.                 if (delay < 0)
  63.                         delay = 0;
  64.                 else if (delay > MILLESECONDS_PER_FRAME)
  65.                         delay = MILLESECONDS_PER_FRAME;
  66.                 mdelay(delay);//延时到帧间隔20ms
  67.         }

  68.     surface_free(obj);
  69. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳市九鼎创展科技官方论坛 ( 粤ICP备11028681号-2  

GMT+8, 2024-3-29 01:42 , Processed in 0.017140 second(s), 17 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表