freemyipod r762 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r761‎ | r762 | r763 >
Date:12:14, 14 August 2011
Author:user890104
Status:new
Tags:
Comment:
ball: avoid generating the shape every frame, use a pre-generated one instead
Modified paths:
  • /apps/ball/main.c (modified) (history)

Diff [purge]

Index: apps/ball/main.c
@@ -26,29 +26,17 @@
2727 #define BALL_W 5
2828 #define BALL_H 5
2929
30 -unsigned int dw, dh, dbpp;
31 -void* fb;
 30+// 24-bit FB (rgb888)
 31+#define DBPP 3
3232
33 -static inline void drawat(unsigned int x, unsigned int y, unsigned char color)
34 -{
35 - if (x >= dw || y >= dh) return;
36 -
37 - // TODO: is there a better way?
38 - *((char *)(fb + dbpp * x + dbpp * dw * y)) = color;
39 - *((char *)(fb + dbpp * x + dbpp * dw * y + 1)) = color;
40 - *((char *)(fb + dbpp * x + dbpp * dw * y + 2)) = color;
41 -}
42 -
4333 static void main()
4434 {
45 - unsigned int run_cycles = 5000;
46 -
47 - dw = lcd_get_width();
 35+ unsigned int run_cycles = 5000,
 36+ dw = lcd_get_width(),
4837 dh = lcd_get_height();
49 - dbpp = 3; // 24-bit FB (rgb888)
5038
51 - unsigned int fb_size = dbpp * dw * dh;
52 - fb = malloc(fb_size);
 39+ unsigned int fb_size = DBPP * dw * dh;
 40+ void* fb = malloc(fb_size);
5341
5442 if (fb == NULL)
5543 {
@@ -58,8 +46,22 @@
5947 unsigned int i, x = 0, y = 0,
6048 old_x, old_y, bx, by,
6149 size = BALL_W * BALL_H;
 50+ unsigned char shape[BALL_H][BALL_W][DBPP];
6251 int vx = 1, vy = 1;
6352
 53+ // generate our shape
 54+ memset(&shape, 0xff, sizeof(shape));
 55+
 56+ // skip the first and the last pixel
 57+ // then skip the ones in top-right and bottom-left
 58+ // to create a rounded square
 59+ for (i = 1; i < size - 1; ++i)
 60+ {
 61+ if (i == BALL_W - 1 || i == size - BALL_W) continue;
 62+
 63+ memset(((void *)&shape) + i * DBPP, 0, DBPP);
 64+ }
 65+
6466 //filllcd(0, 0, dw, dh, 0xffffff); // broken on Nano 4G?
6567 memset(fb, 0xff, fb_size);
6668 displaylcd(0, 0, dw, dh, fb, 0, 0, dw);
@@ -85,24 +87,12 @@
8688
8789 x += vx; y += vy;
8890
89 - // draw our ball-like object
90 - // a circle-like actually, since we
91 - // don't have a 3D engine yet :)
92 - for (i = 1; i < BALL_W - 1; ++i)
 91+ // copy the shape to the framebuffer
 92+ for (i = 0; i < BALL_H; ++i)
9393 {
94 - drawat(x + i, y, 0);
 94+ memcpy(fb + x * DBPP + (y + i) * dw * DBPP, &shape[i], sizeof(shape[i]));
9595 }
9696
97 - for (i = BALL_W; i < size - BALL_W; ++i)
98 - {
99 - drawat(x + i % BALL_W, y + i / BALL_W, 0);
100 - }
101 -
102 - for (i = 1; i < BALL_W - 1; ++i)
103 - {
104 - drawat(x + i, y + BALL_H - 1, 0);
105 - }
106 -
10797 // offset to redraw from
10898 bx = vx > 0 ? old_x : x;
10999 by = vy > 0 ? old_y : y;