207 lines
4.3 KiB
C
207 lines
4.3 KiB
C
/*
|
|
===============================================================================
|
|
Name : labo2 Ecran SPI
|
|
Author : M-Rohmer
|
|
Copyright : HES-SO hepia
|
|
Year : 2024
|
|
===============================================================================
|
|
*/
|
|
|
|
|
|
#include "config_LPC1769.h"
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#include "GPIO.h"
|
|
#include "SPI.h"
|
|
|
|
const Point SCREEN_ORIGIN = {0,0};
|
|
const Size SCREEN_SIZE = {240,320};
|
|
const Rectangle SCREEN = {SCREEN_ORIGIN,SCREEN_SIZE};
|
|
|
|
Player player;
|
|
|
|
void init(void)
|
|
{
|
|
ILI9341_Initial();
|
|
}
|
|
|
|
int scrolling = 0;
|
|
bool need_to_scroll = false;
|
|
|
|
bool on = true;
|
|
void TIMER0_IRQHandler(void){
|
|
//8000HZ smaple rate
|
|
ClearTimer0();
|
|
|
|
SetAudio(800);
|
|
on = true;
|
|
SetAudio(200);
|
|
}
|
|
|
|
void EINT3_IRQHandler(void){
|
|
int buttonPressed = Detect_AB_buttons();
|
|
if(buttonPressed == 2){
|
|
//button A pressed
|
|
}else{
|
|
//button B pressed
|
|
}
|
|
ClearButtons();
|
|
}
|
|
|
|
int timer_count = 0;
|
|
int scroller_divider = 25;
|
|
int scroll_increment = 1;
|
|
void increment_scroll(int value){
|
|
scrolling += value;
|
|
if(scrolling <= 0)
|
|
scrolling = 319;
|
|
if(scrolling >= 320)
|
|
scrolling = 1;
|
|
}
|
|
void SysTick_Handler(){
|
|
timer_count += 1;
|
|
//update the screen
|
|
if(timer_count >= scroller_divider){
|
|
increment_scroll(-scroll_increment);
|
|
need_to_scroll = true;
|
|
timer_count = 0;
|
|
}
|
|
}
|
|
|
|
int number_of_digits (int value){
|
|
int interVal = value;
|
|
int count = 0;
|
|
while(interVal > 0){
|
|
interVal = interVal /10;
|
|
count ++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
void convert_to_string(int value, int nbr_digits, char* str){
|
|
for(int i = nbr_digits-1; i >= 0; i--){
|
|
int interVal = value;
|
|
interVal /= 10;
|
|
int res = value - interVal * 10;
|
|
str[i] = '0'+res;
|
|
value /= 10;
|
|
}
|
|
str[nbr_digits] = '\0';
|
|
}
|
|
|
|
void scroll_array(int **array,int **newArray,int width,int height){
|
|
for(int y = 0;y < height;y++){
|
|
for(int x = 0;x < width;x++){
|
|
if(y < height-1 && x < width -1)
|
|
newArray[x+1][y+1] = array[x][y];
|
|
}
|
|
}
|
|
}
|
|
|
|
Player CreatePlayer(){
|
|
int HP = 100;
|
|
Size playerSize = {32,32};
|
|
Point position = {SCREEN_SIZE.Width / 2 - playerSize.Width /2,SCREEN_SIZE.Height / 2 - playerSize.Height/2};
|
|
Rectangle playerHitbox = {position,playerSize};
|
|
Player player = {playerHitbox,HP};
|
|
return player;
|
|
}
|
|
|
|
void RefreshPlayer(Point vector){
|
|
if(player.Hitbox.Position.Y + vector.Y < 0){
|
|
player.Hitbox.Position.Y = SCREEN_SIZE.Height + (player.Hitbox.Position.Y - (0-vector.Y));
|
|
}else{
|
|
if(player.Hitbox.Position.Y + vector.Y >= SCREEN_SIZE.Height){
|
|
player.Hitbox.Position.Y = vector.Y - (SCREEN_SIZE.Height - player.Hitbox.Position.Y);
|
|
}else{
|
|
player.Hitbox.Position.Y += vector.Y;
|
|
}
|
|
}
|
|
|
|
player.Hitbox.Position.X += vector.X;
|
|
|
|
if(player.Hitbox.Position.X + player.Hitbox.Size.Width >= SCREEN_SIZE.Width)
|
|
player.Hitbox.Position.X = SCREEN_SIZE.Width - player.Hitbox.Size.Width -1;
|
|
if(player.Hitbox.Position.X < 0)
|
|
player.Hitbox.Position.X = 0;
|
|
|
|
|
|
Rectangle rect = player.Hitbox;
|
|
rect.Position = (Point){0,0};
|
|
DrawPlayer(player,rect);
|
|
Refresh_Area(player.Hitbox);
|
|
}
|
|
|
|
int main(void) {
|
|
init();
|
|
InitButtons();
|
|
InitTimer();
|
|
InitAudio();
|
|
LedSetState8(128+32+8+2);
|
|
Point text_origin = {240 / 2 - 8/2,(320 / 6) * 5 - 4};
|
|
|
|
player = CreatePlayer();
|
|
|
|
Resize_Window(SCREEN);
|
|
Draw_stars();
|
|
|
|
|
|
int increment = 3;
|
|
Point momentum = {0,0};
|
|
Size charSize = {7,11};
|
|
|
|
while (true)
|
|
{
|
|
int JoystickState = JoystickGetState();
|
|
Point vec = {0,0};
|
|
|
|
if(JoystickState & (1 << 4)){
|
|
vec.X -= increment;
|
|
}
|
|
if(JoystickState & (1 << 3)){
|
|
vec.Y += increment;
|
|
}
|
|
if(JoystickState & (1 << 2)){
|
|
vec.X += increment;
|
|
}
|
|
if(JoystickState & (1 << 1)){
|
|
vec.Y -= increment;
|
|
}
|
|
|
|
if(need_to_scroll){
|
|
//Refresh_Area((Rectangle){text_origin,(Size){charSize.Width * 3,charSize.Height}});
|
|
scroll(scrolling);
|
|
player.Hitbox.Position.Y -= 1;
|
|
need_to_scroll = false;
|
|
|
|
int digits = number_of_digits(scrolling);
|
|
char newStr[digits+1];
|
|
convert_to_string(scrolling,digits,&newStr);
|
|
Point new_origin = {text_origin.X,(text_origin.Y + scrolling)%320};
|
|
Rectangle areaToRefresh = {{new_origin.X,new_origin.Y+3},{charSize.Width* 4,charSize.Height+5}};
|
|
|
|
Refresh_Area(areaToRefresh);
|
|
DrawText(new_origin,newStr,charSize);
|
|
}
|
|
|
|
|
|
momentum.X += vec.X;
|
|
momentum.Y += vec.Y;
|
|
|
|
if(momentum.X > 35)
|
|
momentum.X = 35;
|
|
if(momentum.X < -40)
|
|
momentum.X = -40;
|
|
|
|
if(momentum.Y > 35)
|
|
momentum.Y = 35;
|
|
if(momentum.Y < -35)
|
|
momentum.Y = -35;
|
|
|
|
RefreshPlayer(momentum);
|
|
|
|
}
|
|
|
|
}
|