-
Notifications
You must be signed in to change notification settings - Fork 24
/
example2.c
91 lines (58 loc) · 1.56 KB
/
example2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//this example shows how to set up a palette and use 8x8 HW sprites
//also shows how fast (or slow) C code is
#include "neslib.h"
//general purpose vars
static unsigned char i,j;
static unsigned char spr;
//total number of balls on the screen
//since there are 64 HW sprites, it is absolute max
#define BALLS_MAX 64
//balls parameters
static unsigned char ball_x[BALLS_MAX];
static unsigned char ball_y[BALLS_MAX];
static unsigned char ball_dx[BALLS_MAX];
static unsigned char ball_dy[BALLS_MAX];
//palette for balls, there are four sets for different ball colors
const unsigned char palSprites[16]={
0x0f,0x17,0x27,0x37,
0x0f,0x11,0x21,0x31,
0x0f,0x15,0x25,0x35,
0x0f,0x19,0x29,0x39
};
void main(void)
{
pal_spr(palSprites);//set palette for sprites
ppu_on_all();//enable rendering
//initialize balls parameters
for(i=0;i<BALLS_MAX;++i)
{
//starting coordinates
ball_x[i]=rand8();
ball_y[i]=rand8();
//direction bits
j=rand8();
//horizontal speed -3..-3, excluding 0
spr=1+(rand8()%3);
ball_dx[i]=j&1?-spr:spr;
//vertical speed
spr=1+(rand8()%3);
ball_dy[i]=j&2?-spr:spr;
}
//now the main loop
while(1)
{
ppu_wait_frame();//wait for next TV frame
spr=0;
for(i=0;i<BALLS_MAX;++i)
{
//set a sprite for current ball
spr=oam_spr(ball_x[i],ball_y[i],0x40,i&3,spr);//0x40 is tile number, i&3 is palette
//move the ball
ball_x[i]+=ball_dx[i];
ball_y[i]+=ball_dy[i];
//bounce the ball off the edges
if(ball_x[i]>=(256-8)) ball_dx[i]=-ball_dx[i];
if(ball_y[i]>=(240-8)) ball_dy[i]=-ball_dy[i];
}
}
}