summaryrefslogtreecommitdiff
path: root/System.c
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2018-07-09 19:26:13 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2018-07-09 19:26:13 +0200
commitf0b654b9bf3bc2a93c1f89d4cc3edcf77b948555 (patch)
tree91b7404dca0b6eb136cb7f9b144435419144b777 /System.c
parentd85464781580796bbcc744ae732e56d1920e3b0f (diff)
downloadpocketempires-f0b654b9bf3bc2a93c1f89d4cc3edcf77b948555.tar.gz
Game has been restructured in favor of OOP
Diffstat (limited to 'System.c')
-rw-r--r--System.c333
1 files changed, 0 insertions, 333 deletions
diff --git a/System.c b/System.c
deleted file mode 100644
index 91ddea8..0000000
--- a/System.c
+++ /dev/null
@@ -1,333 +0,0 @@
-/* *************************************
- * Includes
- * *************************************/
-
-#include "System.h"
-
-/* *************************************
- * Defines
- * *************************************/
-
-#define SYSTEM_MAX_TIMERS 8
-#define check_bb_collision(x1,y1,w1,h1,x2,y2,w2,h2) (!( ((x1)>=(x2)+(w2)) || ((x2)>=(x1)+(w1)) || \
- ((y1)>=(y2)+(h2)) || ((y2)>=(y1)+(h1)) ))
-
-/* *************************************
- * Local Prototypes
- * *************************************/
-
-//static void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step);
-
-/* *************************************
- * Local Variables
- * *************************************/
-
-//Global timer (called by interrupt)
-static volatile uint64_t global_timer;
-//Tells whether rand seed has been set
-static bool rand_seed;
-//Timers
-static bool one_second_timer;
-static bool hundred_ms_timer;
-//Critical section is entered
-static bool system_busy;
-//Timer array
-static TYPE_TIMER timer_array[SYSTEM_MAX_TIMERS];
-
-/* *************************************
- * @name: void SystemInit(void)
- * @date: 19/05/2016
- * @author: Xavier Del Campo
- * @brief:
- * *************************************/
-
-void SystemInit(void)
-{
- //Reset global timer
- global_timer = 0;
- //Reset 1 second timer
- one_second_timer = 0;
- //Reset all user-handled timers
- SystemResetTimers();
- //Initial value for system_busy
- system_busy = false;
-
- #if defined(USBCON)
- USBDevice.attach();
- #endif
-}
-
-void SystemSetRandSeed(void)
-{
- if (rand_seed == false)
- {
- rand_seed = true;
- //Set random seed using global timer as reference
- srand((unsigned int)global_timer);
- }
-}
-
-bool SystemIsRandSeedSet(void)
-{
- return rand_seed;
-}
-
-void SystemIncreaseGlobalTimer(void)
-{
- global_timer++;
-}
-
-uint64_t SystemGetGlobalTimer(void)
-{
- return global_timer;
-}
-
-bool System1SecondTick(void)
-{
- return one_second_timer;
-}
-
-bool System100msTick(void)
-{
- return hundred_ms_timer;
-}
-
-void SystemRunTimers(void)
-{
-/* static uint64_t last_one_second_tick;
- static uint64_t last_100_ms_tick;
-
- SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY);
- SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 2);
- * */
-}
-
-void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step)
-{
- if (*timer != false)
- {
- *timer = false;
- *last_timer = global_timer;
- }
-
- if (global_timer >= (*last_timer + step) )
- {
- *timer = true;
- }
-}
-
-void SystemWaitCycles(uint32_t cycles)
-{
- uint64_t currentTime = global_timer;
-
- while (global_timer < (currentTime + cycles) );
-}
-
-uint32_t SystemRand(uint32_t min, uint32_t max)
-{
- return rand() % (max - min + 1) + min;
-}
-
-bool SystemIsBusy(void)
-{
- return system_busy;
-}
-
-bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz)
-{
- size_t i = 0;
-
- for (i = 0; i < sz; i++)
- {
- if (buffer[i] == value)
- {
- return true;
- }
- }
-
- return false;
-}
-
-bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz)
-{
- size_t i = 0;
-
- for (i = 0; i < sz; i++)
- {
- if (buffer[i] == value)
- {
- return true;
- }
- }
-
- return false;
-}
-
-TYPE_TIMER * SystemCreateTimer(uint32_t seconds, bool rf, void (*timer_callback)(void) )
-{
- bool success = false;
- uint8_t i;
-
- if (seconds == 0)
- {
- return NULL;
- }
-
- for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
- {
- if (timer_array[i].busy == false)
- {
- timer_array[i].Timeout_Callback = timer_callback;
- timer_array[i].time = seconds;
- timer_array[i].orig_time = seconds;
- timer_array[i].repeat_flag = rf;
- timer_array[i].busy = true;
- success = true;
- break;
- }
- }
-
- if (success == false)
- {
- return NULL;
- }
-
- return &timer_array[i];
-}
-
-void SystemResetTimers(void)
-{
- uint8_t i;
-
- for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
- {
- timer_array[i].Timeout_Callback = NULL;
- timer_array[i].busy = false;
- timer_array[i].repeat_flag = false;
- timer_array[i].time = 0;
- timer_array[i].orig_time = 0;
- }
-}
-
-void SystemUserTimersHandler(void)
-{
- uint8_t i;
-
- for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
- {
- if (timer_array[i].busy != false)
- {
- if (System1SecondTick() != false)
- {
- timer_array[i].time--;
-
- if (timer_array[i].time == 0)
- {
- timer_array[i].Timeout_Callback();
-
- if (timer_array[i].repeat_flag != false)
- {
- timer_array[i].time = timer_array[i].orig_time;
- }
- else
- {
- // Clean timer data
- timer_array[i].busy = false;
- timer_array[i].orig_time = 0;
- timer_array[i].Timeout_Callback = NULL;
- }
- }
- }
- }
- }
-}
-
-void SystemTimerRestart(TYPE_TIMER * timer)
-{
- timer->time = timer->orig_time;
-}
-
-void SystemTimerRemove(TYPE_TIMER * timer)
-{
- timer->time = 0;
- timer->orig_time = 0;
- timer->Timeout_Callback = NULL;
- timer->busy = false;
- timer->repeat_flag = false;
-}
-
-bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz)
-{
- size_t i;
-
- for (i = 0; i < sz; i++)
- {
- if (arr1[i] != arr2[i])
- {
- return false;
- }
- }
-
- return true;
-}
-
-bool SystemCollisionCheck(TYPE_COLLISION_BLOCK* c1, TYPE_COLLISION_BLOCK* c2)
-{
- return (bool)check_bb_collision( c1->x, c1->y, c1->w, c1->h,
- c2->x, c2->y, c2->w, c2->h );
-}
-
-size_t Systemitoa(char* str, size_t sz, int16_t value)
-{
- if (sz != 0)
- {
- bool first_digit_found = false;
- uint16_t i;
- uint8_t bytes_written = 0;
-
- /* Example: 65535 */
- /* Another example: -32767 */
-
- if (value & 0x8000)
- {
- /* Sign bit */
- str[bytes_written++] = '-';
- }
-
- for (i = 10000; i >= 1; i /= 10)
- {
- uint8_t digit = (uint8_t)(value / i);
- value -= (uint16_t)(digit * i);
-
- if (digit != 0)
- {
- if (first_digit_found == false)
- {
- first_digit_found = true;
- }
- }
- else if (first_digit_found == false)
- {
- continue;
- }
-
- str[bytes_written++] = digit + '0';
-
- if (bytes_written >= (sz - 1))
- {
- return 0;
- }
- }
-
- str[bytes_written] = '\0';
-
- return bytes_written;
- }
-
- return 0;
-}
-
-uint32_t SystemGetHyp(uint16_t x, uint16_t y)
-{
- return (uint32_t)((uint32_t)(x * x) + (uint32_t)(y * y));
-}