Revision 0654a4da

View differences:

main.c
6 6
#include <avr/interrupt.h>
7 7
#include <avr/pgmspace.h>
8 8
#include "bricks.h"
9

  
9
#include "numbers.h"
10
#include "buffer.h"
10 11
//let's make our lives easier :)
11 12
#define PORT_(port) PORT ## port
12 13
#define DDR_(port)  DDR  ## port
......
96 97
void gameOver(player_t* player);
97 98
void setNewSprite(sprite_t* sprite);
98 99
void drawScore(uint16_t* score, uint8_t round);
100
void drawNumber(uint8_t startpos, uint16_t number[5]);
101
static inline void drawEverything(void);
102
inline void printLineBasic(int16_t line, uint8_t round);
103
void printSignedNumber16(int16_t number);
99 104

  
100
volatile uint16_t lines[90] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
105
//volatile uint16_t lines[90] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
101 106
volatile player_t player[NUMBER_OF_PLAYERS];
102 107

  
103 108
volatile uint8_t keypressCounter = 0;
......
135 140
	player[1].mirrored = 1;
136 141
	player[1].controllerInputMask = (1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_ROTATE);
137 142
	player[1].controllerOffset = 0;
143

  
144
	int16_t countdown = 1000;
138 145
	while(1){
139
		for(uint8_t delay = 0; delay < TICKS; delay++){
140
			drawGameArea((player_t*)player);
141
			if(delay%(TICKS/(NUMBER_OF_PLAYERS)) == 0){
142
				for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){ //fair handing of input
143
					handleGamepadInput((player_t*)&player[playerNumber],
144
								((~PIN(CONTROLLER0_PORT))& player[playerNumber].controllerInputMask)
145
									>>player[playerNumber].controllerOffset);
146
				}
147
			}
148
		}
149
		for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
150
			moveSpriteDown((sprite_t*)&(player[playerNumber].sprite));
151
			clearCompleteLines((player_t*)&player[playerNumber]);
152
			moveFlyingRowsDown((player_t*)&player[playerNumber]);
146
		for(uint8_t i = 0; i < 255; i++){
147
			drawEverything();
153 148
		}
149
		printSignedNumber16(countdown);
150
		countdown--;
151
	}
152
}
153

  
154
void printSignedNumber16(int16_t number){ // quick and dirty
155
	// inspired by:
156
	// http://www.dattalo.com/technical/software/pic/bcd.txt
157

  
158
	uint8_t output[8] = {0,0,0,0,0,0,0,0};
159

  
160
	uint8_t a[4];
161
	a[0] = (number & 0b1111);
162
	a[1] = (number & ((0b1111 << 4))) >> 4;
163
	a[2] = (number & ((0b1111 << 8))) >> 8;
164
	a[3] = (number & ((0b0111 << 12))) >> 12; // ignore the sign
165

  
166
	uint8_t b[5];
167
	b[0] = 6*(a[3] + a[2] + a[1]) + a[0];
168
	b[1] = a[3]*9 + a[2]*5 + a[1];
169
	b[2] = 2*a[2];
170
	b[3] = 4*a[3];
171
	b[4] = 0;
172

  
173
	output[6] = b[0]%10;
174

  
175
	b[1] = (b[1] + (b[0] - output[6])/10);
176
	output[5] = b[1]%10;
177
	b[2] = (b[2] + (b[1] - output[5])/10);
178
	output[4] = b[2]%10;
179

  
180
	b[3] = (b[3] + (b[2] - output[4])/10);
181
	output[3] = b[3]%10;
182

  
183
	output[2] = (b[4] + (b[3] - output[3])/10)%10;
184

  
185
	for(uint8_t i = 0; i < 7; i++){
186
		drawNumber(80-7*i, (uint16_t)bigNumbers[output[i]]);
187
	}
188
}
189

  
190

  
191
void drawNumber(uint8_t startpos, uint16_t number[5]){
192
	for(uint8_t i = 0; i<5; i++){
193
		lines[startpos+4-i] = number[i];
154 194
	}
155 195
}
156 196

  
......
178 218
		noDot();
179 219
	}
180 220
}
181

  
221
static inline void drawEverything(){
222
	for(uint8_t round = 0; round < 16; round++){
223
		for(int16_t line = 0; line < 90; line++){
224
			printLineBasic(line, round);
225
		}
226
		OE();
227
		_delay_us(80);
228
		ODE();
229
		_delay_us(1); // avoid ghosting
230
		PORT(ADDRESS_PORT) = round+1;
231
	}
232
}
182 233
static inline void drawGameArea(player_t player[NUMBER_OF_PLAYERS]){
183 234
	for(uint8_t round = 0; round < 16; round++){
184 235
		for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){
......
202 253
		PORT(ADDRESS_PORT) = round+1;
203 254
	}
204 255
}
205

  
256
inline void printLineBasic(int16_t line, uint8_t round){
257
	if((1<<round) & lines[line]){
258
		yellowDot();
259
	}
260
	else{
261
		noDot();
262
	}
263
}
206 264
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite){
207 265
	if((1<<round) & lines[line]){
208 266
		yellowDot();

Also available in: Unified diff