From 91d6b2af96be2e36894d5e13d9be2a0fea79c0c9 Mon Sep 17 00:00:00 2001 From: T-moe Date: Mon, 18 Jan 2016 16:15:51 +0100 Subject: [PATCH] Started with game logic, implemented some draw stuff. --- .gitignore | 1 + src/game.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/game.h | 19 +++++--- src/io.c | 49 +++++++++++++++++++++ src/io.h | 17 ++++++++ src/main.c | 75 ++++++++------------------------ src/player.c | 21 +++++++++ src/player.h | 11 ++++- 8 files changed, 248 insertions(+), 64 deletions(-) create mode 100644 src/game.c create mode 100644 src/io.c create mode 100644 src/io.h create mode 100644 src/player.c diff --git a/.gitignore b/.gitignore index 7a81269..530a3c0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.elf *.map *.bin +*.swp diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..fdf901a --- /dev/null +++ b/src/game.c @@ -0,0 +1,119 @@ +#include "game.h" +#include +#include +#include +#include "io.h" + +static pin_t pin_left; +static pin_t pin_right; + + +void game_init(game_t* game) { + //Sysinit + + //gpio init (move to module?) + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC,ENABLE); + pin_create(&pin_right, GPIOC, 7, true); + pin_create(&pin_left, GPIOI, 0, true); + + //uart init + + //lcd init + LCD_Init(); + LCD_Clear(GUI_COLOR_BLACK); + + //struct init + game->state=prestart; + game->host_id = 0; + game->ticks_per_pixel = SPEED_SLOW; + game->ticks_leftover =0; +} + + +void game_step(game_t* game, uint64_t deltaTime) { + static long l = 0; + switch(game->state) { + case prestart: + //Draw welcome screen + LCD_DrawRectF(10,10,100,50,GUI_COLOR_BLUE); + + //wait on player to press start (host) + while(!pin_get(&pin_right)); + while(pin_get(&pin_right)); + + //send game start request to slave + //wait on game accept response + + //setup + player_init(&(game->player[game->host_id])); + + + //switch state + game->state = running; + LCD_Clear(GUI_COLOR_BLACK); + break; + case running: + { + bool directionChange = false; + player_t* host_player = &(game->player[game->host_id]); + if(pin_get(&pin_left)) { + while(pin_get(&pin_left)); + host_player->direction= (host_player->direction + 3) % 4 ; // "decrement enum value" + directionChange = true; + } else if(pin_get(&pin_right)) { + while(pin_get(&pin_right)); + host_player->direction= (host_player->direction + 1) % 4 ; // "increment enum value" + directionChange = true; + } + + if(directionChange) { + player_append_position(host_player,host_player->position); + } + if(deltaTime) { + uint16_t ticks = game->ticks_leftover + deltaTime; + uint16_t pixels = ticks / game->ticks_per_pixel; + game->ticks_leftover = ticks % game->ticks_per_pixel; + if(pixels) { + point_t last_point = host_player->past_positions[host_player->num_positions-1]; + switch(host_player->direction) { + case down: + host_player->position.y+=pixels; + LCD_DrawRectF( host_player->position.x, + last_point.y, + PLAYER_WIDTH, + host_player->position.y - last_point.y, + host_player->color); + break; + case left: + host_player->position.x-=pixels; + LCD_DrawRectF( host_player->position.x, + host_player->position.y, + last_point.x -host_player->position.x, + PLAYER_WIDTH, + host_player->color); + break; + case up: + host_player->position.y-=pixels; + LCD_DrawRectF( host_player->position.x, + host_player->position.y, + PLAYER_WIDTH, + last_point.y - host_player->position.y, + host_player->color); + break; + case right: + host_player->position.x+=pixels; + LCD_DrawRectF( last_point.x, + host_player->position.y, + host_player->position.x - last_point.x, + PLAYER_WIDTH, + host_player->color); + break; + } + } + } + } + break; + } +} + + diff --git a/src/game.h b/src/game.h index c317f17..8e8fb61 100644 --- a/src/game.h +++ b/src/game.h @@ -1,26 +1,35 @@ -#ifndef PLAYER_H -#define PLAYER_H +#ifndef GAME_H +#define GAME_H #include #include #include"player.h" #define PLAYER_COUNT 2 +#define PLAYER_WIDTH 3 +#define SPEED_SLOW 10 typedef struct game_s{ + //public section + uint16_t time; // seconds since game start - uint8_t winner_id; + int8_t winner_id; uint8_t host_id; + uint8_t ticks_per_pixel; player_t player[PLAYER_COUNT]; enum{ + prestart, running, ended } state; + + //private section ahead: + uint8_t ticks_leftover; } game_t; void game_init(game_t* game); -void game_step(game_t* game, uint16_t time_delta); +void game_step(game_t* game, uint64_t deltaTime); -#endif /* PLAYER_H */ +#endif /* GAME_H */ diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..9e674a8 --- /dev/null +++ b/src/io.c @@ -0,0 +1,49 @@ +#include "io.h" + + +void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input) { + GPIO_InitTypeDef gi; + GPIO_StructInit(&gi); + + gi.GPIO_Pin = 1 << pinnr; + + if(input) { + gi.GPIO_Mode = GPIO_Mode_IN; + gi.GPIO_OType = GPIO_OType_OD; + gi.GPIO_PuPd = GPIO_PuPd_UP; + } else { + gi.GPIO_Mode = GPIO_Mode_OUT; + gi.GPIO_OType = GPIO_OType_PP; + gi.GPIO_PuPd = GPIO_PuPd_NOPULL; + } + + GPIO_Init(GPIO,&gi); + + pin->GPIO=GPIO; + pin->pinmask=0x01<input = input; +} + + +bool pin_get(pin_t* pin) { + if(pin->input) { + return GPIO_ReadInputDataBit(pin->GPIO,pin->pinmask); + } else { + return GPIO_ReadOutputDataBit(pin->GPIO,pin->pinmask); + } +} + +void pin_set(pin_t* pin, bool status) { + if(!pin->input) { + GPIO_WriteBit(pin->GPIO,pin->pinmask,status); + } +} + +void pin_toggle(pin_t* pin) { + if(!pin->input) { + set_pin(pin,!get_pin(pin)); + } +} + + + diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..f200233 --- /dev/null +++ b/src/io.h @@ -0,0 +1,17 @@ +#include +#include + +typedef struct pin_s { + GPIO_TypeDef* GPIO; + uint16_t pinmask; + bool input; +} pin_t; + +void pin_create(pin_t* pin, GPIO_TypeDef* GPIO, uint8_t pinnr, bool input); +bool pin_get(pin_t* pin); +void pin_set(pin_t* pin, bool status); +void pin_toggle(pin_t* pin); + + + + diff --git a/src/main.c b/src/main.c index 25d977f..ccbe391 100644 --- a/src/main.c +++ b/src/main.c @@ -30,71 +30,32 @@ #include "game.h" #include "uart.h" -#define SPEED 500000 -uint16_t colors[] = { - GUI_COLOR_BLACK, - GUI_COLOR_WHITE, - GUI_COLOR_LIGHT_GRAY, - GUI_COLOR_DARK_GREY, - GUI_COLOR_RED, - GUI_COLOR_YELLOW, - GUI_COLOR_ORANGE, - GUI_COLOR_BROWN, - GUI_COLOR_GREEN, - GUI_COLOR_CYAN, - GUI_COLOR_BLUE, - GUI_COLOR_PINK, - GUI_COLOR_MAGENTA -}; +game_t gameobj; -volatile unsigned char* LED = (volatile unsigned char*)0x6C000200; -volatile unsigned char* SWITCH = (volatile unsigned char*)0x6C000400; -GPIO_InitTypeDef g; +uint64_t ticks; +uint64_t lastTicks; - -void init_gpio(void) -{ - RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); - g.GPIO_Pin = GPIO_Pin_0; - g.GPIO_Mode = GPIO_Mode_OUT; - g.GPIO_OType = GPIO_OType_PP; - g.GPIO_Speed = GPIO_Speed_2MHz; - g.GPIO_PuPd = GPIO_PuPd_NOPULL; - GPIO_Init(GPIOA, &g); +void SysTick_Handler() { + ticks++; } + +#define SYSCLK 168e6 +#define TICKS_PER_SECOND 100 + int main(void) { - int i = 0, j = 0, m = 0; + if(SysTick_Config(SYSCLK/TICKS_PER_SECOND)) { //if systick config fails + while(1); //sleep forever + } - *LED = 0b00000001; - - LCD_Init(); - init_gpio(); - LCD_Clear(GUI_COLOR_RED); - - while(1){ - - for(m = 0; (m <= 12); m++){ - - LCD_Clear(colors[m]); - - GPIO_WriteBit(GPIOA, GPIO_Pin_0, 1); - - for(i = 0; i < 7; i++){ - *LED<<=1; - for(j = 0; j < SPEED; j++); - } - - GPIO_WriteBit(GPIOA, GPIO_Pin_0, 0); - - for(i = 7; i > 0; i--){ - *LED>>=1; - for(j = 0; j < SPEED; j++); - } - } - } + game_init(&gameobj); + while(1) { + uint64_t curTicks = ticks; + game_step(&gameobj,curTicks-lastTicks); //calculate next game step, and pass it the delta time + lastTicks = curTicks; + } return 0; } diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..f3ceb87 --- /dev/null +++ b/src/player.c @@ -0,0 +1,21 @@ +#include "player.h" +#include + +void player_init(player_t* player) { + player->id=0; + player->color = GUI_COLOR_BLUE; + player->num_positions=1; + player->position = (point_t){.x=10,.y=100}; + player->past_positions[0] = player->position; + player->direction = right; + player->state = alive; +} + + + + +void player_append_position(player_t* player, point_t point) { + if(player->num_positions < max_positions) { + player->past_positions[player->num_positions++] = point; + } +} diff --git a/src/player.h b/src/player.h index 43d85d7..c5577fb 100644 --- a/src/player.h +++ b/src/player.h @@ -18,13 +18,20 @@ typedef struct player_s { uint16_t color; uint16_t num_positions; + enum { + right, + down, + left, + up + } direction; + enum{ dead, alive, - }state; + } state; point_t past_positions[max_positions]; - point_t curr_position; + point_t position; } player_t;