刚买到OK6410的开发板之后,按照光盘上的用户手册的教程,烧写Linux系统。然后就是在PC端红帽编写Linux C程序,交叉编译后,用SD卡或者是挂载U盘的办法,把编译出来的可执行文件放到开发板上的文件系统中,然后再终端执行./xxx(xxx是可执行文件名)。就完成了Linux应用的编写与执行。
Linux应用是建立在Linux系统的基础上执行的。如果我们要跑裸板程序呢?那我们就不得不抛弃操作系统。我看了国嵌的视频,还有网上相关的博客,惊奇地发现,竟然没有OK6410的裸板程序烧录方法。那些AXD,JLink都只是仿真,loady 0x5000800 go 0x50008000是把程序烧到RAM里,并没有真正的把程序烧录到Nand Flash当中。
于是,小子就开始研究一下OK6410如何烧录裸板程序。本次的教程,烧录的程序绝对不是仿真,也不是烧到RAM里面。
准备工作,先把SD启动程序烧到SD卡上,详细方法见OK6410的Linux用户手册。编写好一个简单的裸板程序(不超过8K)。
1.首先,我们打开SecureCRT,用串口连接好OK6410开发板,开发板设置为SD卡启动(启动拨动开关为1-2-3-4-5-6-7-8)
0-0-0-1-1-1-1-1
2.SecureCRT点击文件->连接串口,然后给开发板上电,马上在键盘上按空格,这就就可以执行SD卡的U-Boot命令行
3.输入loady 0x50008000,然后点传输->发送Ymodem,把编译好的裸板程序上传。这样就可以把程序放到RAM里面去
输入命令后,等待文件的上传
文件上传成功
这时候可以通过执行go 0x50008000 在RAM里面执行程序,不过开发板重新上电后,程序就没有了。
4.先执行nand erase 0 100000 擦除Nand Flash中的0到1M的地址内存,如果之前在Nand Flash里烧录过U-boot的话,那么U-boot就会被擦除。以后等你再玩Linux了,你可以把U-boot烧录回去
5.执行nand write 50008000 0 100000 即可从RAM中把刚才上传的文件烧录到Nand Flash 0到1M的地址上
烧录成功的信息
6.这时候,你需要断电,并且把启动开关设置为Nand Flash启动
(1-2-3-4-5-6-7-8)
(0-0-0-1-1-0-0-1)
这时候重新上电就可以玩裸板程序了。
方法有点笨,但是如果你没有买JLink,你可以尝试一下用这种方法烧录。如果您有更好的方法,可以给我留言。
----------region.aspx前台-------------
<body>
<form id="form1" runat="server">
<div>
<table >
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
用户密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
邮箱地址:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" /></td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
--------region.aspx.cs后台-----------------
public void sendMail(string email, string activeCode,int id)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("邮箱详细账号");
msg.To.Add(email);
msg.Subject = "请激活注册";
StringBuilder contentBuilder = new StringBuilder();
contentBuilder.Append("请单击以下连接完成激活!");
contentBuilder.Append("<a href='http://localhost:5566/CheckActiveCode.aspx?activecode=" + activeCode + "&id="+id+"'>激活</a>");
msg.Body = contentBuilder.ToString(); ;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.126.com";
client.Port = 25;
NetworkCredential credetial = new NetworkCredential();
credetial.UserName = "@之前的部分";
credetial.Password = "邮箱密码";
client.Credentials = credetial;
client.Send(msg);
}
protected void Button1_Click(object sender, EventArgs e)
{
string userName = this.TextBox1.Text;
string password = this.TextBox2.Text;
string email = this.TextBox3.Text;
string activeCode = Guid.NewGuid().ToString().Substring(0, 8);
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "insert into T_Users (UserName,Password,Email,Active,ActiveCode) values(@username,@password,@email,@active,@activecode) select @@identity";
SqlParameter[] prams = new SqlParameter[] {
new SqlParameter("@username",userName),
new SqlParameter("@password",password),
new SqlParameter("@email",email),
new SqlParameter("@active",false),
new SqlParameter("@activecode",activeCode)
};
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(prams);
number = Convert.ToInt32(cmd.ExecuteScalar());
}
}
if (number > 0)
{
sendMail(email, activeCode,number);
Response.Redirect("regionMessage.aspx");
}
else
{
Response.Write("注册失败,请重新注册!");
}
}
---------CheckActiveCode.aspx.cs后台-------------------------------
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request["id"]);
string activeCode = Request["activecode"].ToString();
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select count(*) from T_Users where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
number = Convert.ToInt32(cmd.ExecuteScalar());
}
}
if (number > 0)
{
string AC;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "select ActiveCode from T_Users where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
AC = cmd.ExecuteScalar().ToString();
}
}
if (activeCode == AC)
{
Response.Write("激活成功!");
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "update T_Users set Active=1 where id=@id";
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", id);
number = Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
else
{
Response.Write("用户已存在,但是激活码错误!");
}
}
else
{
Response.Write("用户不存在,还没有注册成功!");
}
}
--------Web.config-----------------
<connectionStrings>
<add name="conStr" connectionString="data source=.;initial catalog=student;user id=sa;password=111111"/>
</connectionStrings>
moving th image 跟display差不多,也不翻译了
游戏循环式每个游戏的心跳。我们用过一次非常基础的,它没有控制游戏状态速度的更新,也没有控制哪些帧要渲染。简单的说,大部分的游戏循环都是在循环里面执行一些指令,知道我们发信号通知结束,通常是设置running为false
1 boolean running = true;
2 while (!running)
3 {
4 updateGameState();
5 displayGameState();
6 }
上面的代码完全没有关注时间和资源。如果是快的设备,它会运行的很快,如果设备很慢,运行的也会很慢
有两个指标是我们必须要关注的 FPS和UPS
FPS 帧每秒 ---displayGameState每秒被调用的次数
UPS 更新每秒 updateGameState每秒被调用的次数
理想情况下 update和render方法每秒被调用的次数英爱是相当的,每秒不少于20-25帧。25帧对于手机来说已经够用了,通常就不会感觉到动画的迟钝了
比如 如果我们目标是25帧,那么意味着我们要每40m调用一次displayGameState方法。我们要知道updateGameState方法是在display方法之前调用的,为了达到25帧,我们必须要保证update-display序列的执行时间是40ms,如果小于40ms,那么FPS会高一点,否则会慢一点
为了更好理解,看几个例子
下面的图示 正好是1FPS,update-render 正好用了1秒来执行。这就意味着我们一秒就会看到一次更新屏幕
1 Frame per Second
The following diagram shows 10FPS. An update – render cycle takes 100ms. This means every tenth of a second the image changes.
下面的图显示的10FPS,update-render花费了100ms,这就意味着0.1秒看到图片发生改变
10 FPS
但是上面的场景意味我们每次的执行时间都是0.1秒吗?上图只是个假设,我们不能控制游戏的循环时间,如果有200个敌人,每个敌人都向我们射击,我们需要更新每个敌人和子弹的状态,还要检测碰撞,这当然是和两个人敌人的更新时间是不同的,同样也适用于渲染方法。
那么真实的场景是什么?我们有循环时间少于100ms的,等于100ms,和大于100ms的,高富帅的机器比屌丝的机器要给力的多,让我们看图
这次循环小于我们设计的时间帧,所以我们有一点休息的时间 然后再干活
Frame with time to spare
下图有点落后了,时间超过了预订的时间,如果花了12ms,就意味我们落后2ms了,然后认为是10FPS,这个可以叠加,每个循环我们的时间都不准确,我们的游戏会很慢
Overdue Frame
第一种情况很好,下次干活之前 给了我们一点休息时间。我们啥也不要干就直接告诉循环休息一会,下次干活的时间醒来就行了,如果不这么干 游戏会运行的快一些,下面介绍我们要获得睡眠时间
不变的帧率
第二种落后的情况,我们需要点其他的办法
为了完成游戏不变的帧率,想象一下,敌人以常速靠近你,如果一秒走了半屏,那么下一秒就会走完一个屏幕,为了计算位置,我们需要知道时间的增量和速率,或者直接更新敌人的位置,我选择后者,因为前者很复杂。为了完成不变的游戏速度,我们会丢掉一些显示帧,游戏速度不等于帧率。
看下面的图,循环时间超过了设计的时间,所以我们要赶上,我们跳过这帧,做另外一个update,游戏速度就不会受影响,我们会有一个正常帧,甚至可以让CPU休息一会
Constant Game Speed with Variable FPS
上面场景有很多的变量,想象一下游戏的更新超过了一整个帧,这种情况下我们没有办法让游戏速度保持不变。我们不能让跳过的帧太多,否则的游戏就不能玩了
The MainThread.java‘s run() looks like this:
01 // desired fps
02 private final static int MAX_FPS = 50;
03 // maximum number of frames to be skipped
04 private final static int MAX_FRAME_SKIPS = 5;
05 // the frame period
06 private final static int FRAME_PERIOD = 1000 / MAX_FPS;
07 @Override
08 public void run() {
09 Canvas canvas;
10 Log.d(TAG, "Starting game loop");
11 long beginTime; // the time when the cycle begun
12 long timeDiff; // the time it took for the cycle to execute
13 int sleepTime; // ms to sleep (<0 if we're behind)
14 int framesSkipped; // number of frames being skipped
15 sleepTime = 0;
16 while (running) {
17 canvas = null;
18 // try locking the canvas for exclusive pixel editing
19 // in the surface
20 try {
21 canvas = this.surfaceHolder.lockCanvas();
22 synchronized (surfaceHolder) {
23 beginTime = System.currentTimeMillis();
24 framesSkipped = 0; // resetting the frames skipped
25 // update game state
26 this.gamePanel.update();
27 // render state to the screen
28 // draws the canvas on the panel
29 this.gamePanel.render(canvas);
30 // calculate how long did the cycle take
31 timeDiff = System.currentTimeMillis() - beginTime;
32 // calculate sleep time
33 sleepTime = (int)(FRAME_PERIOD - timeDiff);
34 if (sleepTime > 0) {
35 // if sleepTime > 0 we're OK
36 try {
37 // send the thread to sleep for a short period
38 // very useful for battery saving
39 Thread.sleep(sleepTime);
40 } catch (InterruptedException e) {}
41 }
42 while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
43 // we need to catch up
44 // update without rendering
45 this.gamePanel.update();
46 // add frame period to check if in next frame
47 sleepTime += FRAME_PERIOD;
48 framesSkipped++;
49 }
50 }
51 } finally {
52 // in case of an exception the surface is not left in
53 // an inconsistent state
54 if (canvas != null) {
55 surfaceHolder.unlockCanvasAndPost(canvas);
56 }
57 } // end finally
58 }
59 }
看一下上面的code,它对应图形中的逻辑
还有另外一种方法,最大帧率的不变游戏速率,它 用插值法来画状态,在快速的硬件上被使用,它可以加强游戏效果,使动画更平滑,因为我们是移动设备,给CPU一点休息,还是可以省不少电的