Revision 87d0c022 main.c

View differences:

main.c
33 33
// note that both controllers are on the same port, the reason is
34 34
// controllerOffset for faster input handling
35 35
#define CONTROLLER0_PORT   D
36
#define CONTROLLER0_LEFT   PD7
37
#define CONTROLLER0_RIGHT  PD6
38
#define CONTROLLER0_DOWN   PD5
39
#define CONTROLLER0_ROTATE PD4
36
#define CONTROLLER0_PLAYER0_LEFT   PD7
37
#define CONTROLLER0_PLAYER0_RIGHT  PD6
38
#define CONTROLLER0_PLAYER0_DOWN   PD5
39
#define CONTROLLER0_PLAYER0_ROTATE PD4
40 40
#define CONTROLLER1_PORT   D
41
#define CONTROLLER1_LEFT   PD3
42
#define CONTROLLER1_RIGHT  PD2
43
#define CONTROLLER1_DOWN   PD1
44
#define CONTROLLER1_ROTATE PD0
41
#define CONTROLLER1_PLAYER0_LEFT   PD3
42
#define CONTROLLER1_PLAYER0_RIGHT  PD2
43
#define CONTROLLER1_PLAYER0_DOWN   PD1
44
#define CONTROLLER1_PLAYER0_ROTATE PD0
45 45

  
46 46
// control mappings for atari joystick
47
#define LEFT   0b0001
48
#define DOWN   0b0100
49
#define RIGHT  0b0010
50
#define ROTATE 0b1000
47
#define PLAYER0_LEFT   0b0100
48
#define PLAYER0_DOWN   0b0010 
49
#define PLAYER0_RIGHT  0b1000
50
#define PLAYER0_ROTATE 0b0001
51

  
52
#define PLAYER1_LEFT   0b1000
53
#define PLAYER1_DOWN   0b0010 
54
#define PLAYER1_RIGHT  0b0100
55
#define PLAYER1_ROTATE 0b0001
51 56

  
52 57
// note NOGAMEAREA needs has to be one pixel bigger
53 58
// because we are lazy in moveSpriteHorizontal (forward check)
......
92 97
void moveSpriteToLines(sprite_t* sprite);
93 98
uint8_t checkPlayerSpriteForCollision(player_t* player, uint16_t block[4], int8_t lookahead); // lookahead -1 -> look down
94 99
void moveSpriteHorizontal(volatile sprite_t *sprite, int8_t dir); // -1 if left, 1 if right
95
void handleGamepadInput(player_t* player, uint8_t input);
100
void handleGamepadInput(player_t* player, uint8_t player_number, uint8_t input);
96 101
inline void printLine(int16_t line, uint8_t round, sprite_t* sprite, uint8_t player_number);
97 102
static inline void drawGameArea(player_t player[NUMBER_OF_PLAYERS]);
98 103
void gameOver(player_t* player);
......
112 117
	// 7...4 left down right rotate
113 118
	//
114 119
	// move > rotate > down
115
	DDR(CONTROLLER0_PORT) &= ~((1<<CONTROLLER0_LEFT) | (1<<CONTROLLER0_RIGHT) | (1<<CONTROLLER0_DOWN) | (1<<CONTROLLER0_ROTATE));
116
	PORT(CONTROLLER0_PORT) |= (1<<CONTROLLER0_LEFT) | (1<<CONTROLLER0_RIGHT) | (1<<CONTROLLER0_DOWN) | (1<<CONTROLLER0_ROTATE);
117
	DDR(CONTROLLER1_PORT) &= ~((1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_ROTATE));
118
	PORT(CONTROLLER1_PORT) |= (1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_ROTATE);
120
	DDR(CONTROLLER0_PORT) &= ~((1<<CONTROLLER0_PLAYER0_LEFT) | (1<<CONTROLLER0_PLAYER0_RIGHT) | (1<<CONTROLLER0_PLAYER0_DOWN) | (1<<CONTROLLER0_PLAYER0_ROTATE));
121
	PORT(CONTROLLER0_PORT) |= (1<<CONTROLLER0_PLAYER0_LEFT) | (1<<CONTROLLER0_PLAYER0_RIGHT) | (1<<CONTROLLER0_PLAYER0_DOWN) | (1<<CONTROLLER0_PLAYER0_ROTATE);
122
	DDR(CONTROLLER1_PORT) &= ~((1<<CONTROLLER1_PLAYER0_LEFT) | (1<<CONTROLLER1_PLAYER0_RIGHT) | (1<<CONTROLLER1_PLAYER0_DOWN) | (1<<CONTROLLER1_PLAYER0_ROTATE));
123
	PORT(CONTROLLER1_PORT) |= (1<<CONTROLLER1_PLAYER0_LEFT) | (1<<CONTROLLER1_PLAYER0_RIGHT) | (1<<CONTROLLER1_PLAYER0_DOWN) | (1<<CONTROLLER1_PLAYER0_ROTATE);
119 124
	// address lines:
120 125
	DDR(ADDRESS_PORT) |= (1<<A3) | (1<<A2) | (1<<A1) | (1<<A0);
121 126

  
......
126 131
	player[0].sprite.owner = (player_t*)&player[0];
127 132
	setNewSprite((sprite_t*)&(player[0].sprite));
128 133
	player[0].mirrored = 0;
129
	player[0].controllerInputMask = (1<<CONTROLLER0_LEFT) | (1<<CONTROLLER0_DOWN) | (1<<CONTROLLER0_RIGHT) | (1<<CONTROLLER0_ROTATE);
134
	player[0].controllerInputMask = (1<<CONTROLLER0_PLAYER0_LEFT) | (1<<CONTROLLER0_PLAYER0_DOWN) | (1<<CONTROLLER0_PLAYER0_RIGHT) | (1<<CONTROLLER0_PLAYER0_ROTATE);
130 135
	player[0].controllerOffset = 4;
131 136
	player[0].nextSprite.owner = (player_t*)&player[0];
132 137
	setNewSprite((sprite_t*)&(player[0].nextSprite));
......
137 142
	player[1].sprite.owner = (player_t*)&player[1];
138 143
	setNewSprite((sprite_t*)&(player[1].sprite));
139 144
	player[1].mirrored = 1;
140
	player[1].controllerInputMask = (1<<CONTROLLER1_LEFT) | (1<<CONTROLLER1_DOWN) | (1<<CONTROLLER1_RIGHT) | (1<<CONTROLLER1_ROTATE);
145
	player[1].controllerInputMask = (1<<CONTROLLER1_PLAYER0_LEFT) | (1<<CONTROLLER1_PLAYER0_DOWN) | (1<<CONTROLLER1_PLAYER0_RIGHT) | (1<<CONTROLLER1_PLAYER0_ROTATE);
141 146
	player[1].controllerOffset = 0;
142 147
	player[1].nextSprite.owner = (player_t*)&player[1];
143 148
	setNewSprite((sprite_t*)&(player[1].nextSprite));
......
146 151
			drawGameArea((player_t*)player);
147 152
			if(delay%(TICKS/(NUMBER_OF_PLAYERS)) == 0){
148 153
				for(uint8_t playerNumber = 0; playerNumber < NUMBER_OF_PLAYERS; playerNumber++){ //fair handing of input
149
					handleGamepadInput((player_t*)&player[playerNumber],
154
					handleGamepadInput(&player[playerNumber], playerNumber,
150 155
								((~PIN(CONTROLLER0_PORT))& player[playerNumber].controllerInputMask)
151 156
									>>player[playerNumber].controllerOffset);
152 157
				}
......
333 338
	}
334 339
}
335 340

  
336
void handleGamepadInput(player_t* player, uint8_t input){
337
	switch(input){
338
		// move > rotate > down
339
		case (LEFT | DOWN | RIGHT | ROTATE):
340
			keypressCounter+=2;
341
		case (DOWN | ROTATE):
342
			rotateLeft((sprite_t*)&((*player).sprite));
343
			rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
344
			moveSpriteDown((sprite_t*)&((*player).sprite));
345
			keypressCounter+=2;
346
			break;
347
		case (LEFT | DOWN | RIGHT):
348
			keypressCounter+=2;
349
		case (DOWN):
350
			moveSpriteDown((sprite_t*)&((*player).sprite));
351
			keypressCounter+=1;
352
			break;
353
		case (RIGHT):
354
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
355
			keypressCounter+=1;
356
			break;
357
		case (LEFT | RIGHT | ROTATE):
358
			keypressCounter+=1;
359
		case (RIGHT | ROTATE):
360
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
361
			keypressCounter+=1;
362
		case (ROTATE):
363
			rotateLeft((sprite_t*)&((*player).sprite));
364
			rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
365
			keypressCounter+=1;
366
			break;
367
		case (DOWN | RIGHT):
368
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
369
			moveSpriteDown((sprite_t*)&((*player).sprite));
370
			keypressCounter+=2;
371
			break;
372
		case (DOWN | RIGHT | ROTATE):
373
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
374
			rotateLeft((sprite_t*)&((*player).sprite));
375
			rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
376
			moveSpriteDown((sprite_t*)&((*player).sprite));
377
			keypressCounter+=3;
378
			break;
379
		case (LEFT):
380
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
381
			keypressCounter+=1;
382
			break;
383
		case (LEFT | ROTATE):
384
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
385
			rotateLeft((sprite_t*)&((*player).sprite));
386
			rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
387
			keypressCounter+=2;
388
			break;
389
		case (LEFT | DOWN):
390
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
391
			moveSpriteDown((sprite_t*)&((*player).sprite));
392
			keypressCounter+=2;
393
			break;
394
		case (LEFT | DOWN | ROTATE):
395
			moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
396
			rotateLeft((sprite_t*)&((*player).sprite));
397
			rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
398
			moveSpriteDown((sprite_t*)&((*player).sprite));
399
			keypressCounter+=3;
400
			break;
401
		// "nops"
402
		case (LEFT | RIGHT): // left+right = no move
403
			keypressCounter+=2;
404
		case (0b0000): // nothing pressed
405
			break;
341
void handleGamepadInput(player_t* player, uint8_t player_number, uint8_t input){
342
	if(player_number == 0){
343
		switch(input){
344
			// move > rotate > down
345
			case (PLAYER0_LEFT | PLAYER0_DOWN | PLAYER0_RIGHT | PLAYER0_ROTATE):
346
				keypressCounter+=2;
347
			case (PLAYER0_DOWN | PLAYER0_ROTATE):
348
				rotateLeft((sprite_t*)&((*player).sprite));
349
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
350
				moveSpriteDown((sprite_t*)&((*player).sprite));
351
				keypressCounter+=2;
352
				break;
353
			case (PLAYER0_LEFT | PLAYER0_DOWN | PLAYER0_RIGHT):
354
				keypressCounter+=2;
355
			case (PLAYER0_DOWN):
356
				moveSpriteDown((sprite_t*)&((*player).sprite));
357
				keypressCounter+=1;
358
				break;
359
			case (PLAYER0_RIGHT):
360
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
361
				keypressCounter+=1;
362
				break;
363
			case (PLAYER0_LEFT | PLAYER0_RIGHT | PLAYER0_ROTATE):
364
				keypressCounter+=1;
365
			case (PLAYER0_RIGHT | PLAYER0_ROTATE):
366
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
367
				keypressCounter+=1;
368
			case (PLAYER0_ROTATE):
369
				rotateLeft((sprite_t*)&((*player).sprite));
370
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
371
				keypressCounter+=1;
372
				break;
373
			case (PLAYER0_DOWN | PLAYER0_RIGHT):
374
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
375
				moveSpriteDown((sprite_t*)&((*player).sprite));
376
				keypressCounter+=2;
377
				break;
378
			case (PLAYER0_DOWN | PLAYER0_RIGHT | PLAYER0_ROTATE):
379
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
380
				rotateLeft((sprite_t*)&((*player).sprite));
381
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
382
				moveSpriteDown((sprite_t*)&((*player).sprite));
383
				keypressCounter+=3;
384
				break;
385
			case (PLAYER0_LEFT):
386
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
387
				keypressCounter+=1;
388
				break;
389
			case (PLAYER0_LEFT | PLAYER0_ROTATE):
390
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
391
				rotateLeft((sprite_t*)&((*player).sprite));
392
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
393
				keypressCounter+=2;
394
				break;
395
			case (PLAYER0_LEFT | PLAYER0_DOWN):
396
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
397
				moveSpriteDown((sprite_t*)&((*player).sprite));
398
				keypressCounter+=2;
399
				break;
400
			case (PLAYER0_LEFT | PLAYER0_DOWN | PLAYER0_ROTATE):
401
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
402
				rotateLeft((sprite_t*)&((*player).sprite));
403
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
404
				moveSpriteDown((sprite_t*)&((*player).sprite));
405
				keypressCounter+=3;
406
				break;
407
			// "nops"
408
			case (PLAYER0_LEFT | PLAYER0_RIGHT): // left+right = no move
409
				keypressCounter+=2;
410
			case (0b0000): // nothing pressed
411
				break;
412
		}
413
	}
414
	else if(player_number == 1){
415
		switch(input){
416
			// move > rotate > down
417
			case (PLAYER1_LEFT | PLAYER1_DOWN | PLAYER1_RIGHT | PLAYER1_ROTATE):
418
				keypressCounter+=2;
419
			case (PLAYER1_DOWN | PLAYER1_ROTATE):
420
				rotateLeft((sprite_t*)&((*player).sprite));
421
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
422
				moveSpriteDown((sprite_t*)&((*player).sprite));
423
				keypressCounter+=2;
424
				break;
425
			case (PLAYER1_LEFT | PLAYER1_DOWN | PLAYER1_RIGHT):
426
				keypressCounter+=2;
427
			case (PLAYER1_DOWN):
428
				moveSpriteDown((sprite_t*)&((*player).sprite));
429
				keypressCounter+=1;
430
				break;
431
			case (PLAYER1_RIGHT):
432
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
433
				keypressCounter+=1;
434
				break;
435
			case (PLAYER1_LEFT | PLAYER1_RIGHT | PLAYER1_ROTATE):
436
				keypressCounter+=1;
437
			case (PLAYER1_RIGHT | PLAYER1_ROTATE):
438
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
439
				keypressCounter+=1;
440
			case (PLAYER1_ROTATE):
441
				rotateLeft((sprite_t*)&((*player).sprite));
442
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
443
				keypressCounter+=1;
444
				break;
445
			case (PLAYER1_DOWN | PLAYER1_RIGHT):
446
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
447
				moveSpriteDown((sprite_t*)&((*player).sprite));
448
				keypressCounter+=2;
449
				break;
450
			case (PLAYER1_DOWN | PLAYER1_RIGHT | PLAYER1_ROTATE):
451
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), -1);
452
				rotateLeft((sprite_t*)&((*player).sprite));
453
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
454
				moveSpriteDown((sprite_t*)&((*player).sprite));
455
				keypressCounter+=3;
456
				break;
457
			case (PLAYER1_LEFT):
458
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
459
				keypressCounter+=1;
460
				break;
461
			case (PLAYER1_LEFT | PLAYER1_ROTATE):
462
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
463
				rotateLeft((sprite_t*)&((*player).sprite));
464
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
465
				keypressCounter+=2;
466
				break;
467
			case (PLAYER1_LEFT | PLAYER1_DOWN):
468
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
469
				moveSpriteDown((sprite_t*)&((*player).sprite));
470
				keypressCounter+=2;
471
				break;
472
			case (PLAYER1_LEFT | PLAYER1_DOWN | PLAYER1_ROTATE):
473
				moveSpriteHorizontal((sprite_t*)&((*player).sprite), 1);
474
				rotateLeft((sprite_t*)&((*player).sprite));
475
				rawToBlock((uint16_t*)(*player).sprite.block, (*player).sprite.raw, (*player).sprite.offset);
476
				moveSpriteDown((sprite_t*)&((*player).sprite));
477
				keypressCounter+=3;
478
				break;
479
			// "nops"
480
			case (PLAYER1_LEFT | PLAYER1_RIGHT): // left+right = no move
481
				keypressCounter+=2;
482
			case (0b0000): // nothing pressed
483
				break;
484
		}
485
	}
486
	else{
487
		return;
406 488
	}
407 489
}
408 490

  

Also available in: Unified diff