added game.h

master
Dmitry Maylarov 7 years ago
parent f3fcb6b784
commit 60a9e78fab

@ -0,0 +1,28 @@
#define MAX_PLAYERS 6
#define TIMER_MAX 180
#define TIMER_STEP 5
#include <stdint.h>
#include <stdbool.h>
typedef struct {
bool countScores;
uint8_t activePlayers;
uint8_t currentPlayer;
uint32_t turnTime;
uint32_t timerValue;
uint32_t scores[MAX_PLAYERS];
} GameEngine;
extern GameEngine game;
void InitGameEngine();
void IncrementTurnTime();
void DecrementTurnTime();
void AddPlayer();
void RemovePlayer();
uint32_t GetCurrentPlayer();
uint32_t GetTimerValue();
void ChangeScore(int8_t delta);
void ResetTurnTimer();
void NextPlayer();

@ -0,0 +1,61 @@
#include "game.h"
void InitGameEngine() {
game.timerValue = game.turnTime;
game.countScores = false;
game.activePlayers = 0;
game.currentPlayer = 0;
game.turnTime = TIMER_MAX / 2;
game.timerValue = game.turnTime;
for (int i=0; i<MAX_PLAYERS; i++)
game.scores[i] = -1;
}
void DecrementTurnTime() {
if (game.turnTime > TIMER_STEP)
game.turnTime -= TIMER_STEP;
else
game.turnTime = TIMER_MAX;
game.timerValue = game.turnTime;
}
void IncrementTurnTime() {
if (game.turnTime + TIMER_STEP <= TIMER_MAX)
game.turnTime += TIMER_STEP;
else
game.turnTime = TIMER_STEP;
game.timerValue = game.turnTime;
}
void AddPlayer() {
if (game.activePlayers < MAX_PLAYERS) {
game.scores[game.activePlayers++] = 0;
}
}
void RemovePlayer() {
if (game.activePlayers > 0) {
game.scores[game.activePlayers--] = -1;
}
}
uint32_t GetCurrentPlayer() {
return game.currentPlayer;
}
uint32_t GetTimerValue() {
return game.timerValue;
}
void ChangeScore(int8_t delta) {
game.scores[game.currentPlayer] += delta;
}
void ResetTurnTimer() {
game.timerValue = game.turnTime;
}
void NextPlayer() {
game.currentPlayer = (game.currentPlayer + 1) % game.activePlayers;
ResetTurnTimer();
}

@ -25,6 +25,7 @@
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include "lcd.h" #include "lcd.h"
#include "game.h"
#include <stdlib.h> #include <stdlib.h>
/* USER CODE END Includes */ /* USER CODE END Includes */
@ -36,6 +37,9 @@
/* Private define ------------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */ /* USER CODE BEGIN PD */
#define LCD_ADDR (0x27 << 1)
#define LCD_COLS 20
#define LCD_ROWS 4
/* USER CODE END PD */ /* USER CODE END PD */
@ -55,11 +59,13 @@ UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_tx; DMA_HandleTypeDef hdma_usart1_tx;
/* USER CODE BEGIN PV */ /* USER CODE BEGIN PV */
GameEngine game;
LCD_HandleTypeDef hlcd = { LCD_HandleTypeDef hlcd = {
&hi2c1, &hi2c1,
(0x27 << 1), LCD_ADDR,
4, LCD_ROWS,
20 LCD_COLS
}; };
/* USER CODE END PV */ /* USER CODE END PV */
@ -118,13 +124,13 @@ int main(void)
MX_USB_DEVICE_Init(); MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
LCD_Init(&hlcd); LCD_Init(&hlcd);
InitGameEngine();
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
while (1) while (1)
{ {
/* USER CODE END WHILE */ /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */

@ -38,6 +38,7 @@ BUILD_DIR = build
C_SOURCES = \ C_SOURCES = \
Core/Src/main.c \ Core/Src/main.c \
Core/Src/lcd.c \ Core/Src/lcd.c \
Core/Src/game.c \
Core/Src/stm32f1xx_it.c \ Core/Src/stm32f1xx_it.c \
Core/Src/stm32f1xx_hal_msp.c \ Core/Src/stm32f1xx_hal_msp.c \
USB_DEVICE/App/usb_device.c \ USB_DEVICE/App/usb_device.c \

Loading…
Cancel
Save