diff --git a/CMakeLists.txt b/CMakeLists.txt index 8470387..2b59d06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,8 @@ find_package(Gint 2.1 REQUIRED) set(SOURCES src/main.c + src/core.c + src/display.c ) set(ASSETS) diff --git a/src/calccity.c b/src/calccity.c index e69de29..510dfd5 100644 --- a/src/calccity.c +++ b/src/calccity.c @@ -0,0 +1,2 @@ +#include "calccity.h" + diff --git a/src/calccity.h b/src/calccity.h index 2a8cc5b..7666150 100644 --- a/src/calccity.h +++ b/src/calccity.h @@ -1,6 +1,9 @@ #ifndef _CALCCITY_H #define _CALCCITY_H +#include + + struct calccity { // human's statistics = {happyness, health, education, housing, work, food} @@ -32,7 +35,8 @@ struct calccity int blinker; // in-game options - bool disaster; + uint8_t disaster; + uint8_t animation; int time_speed; }; @@ -53,4 +57,6 @@ struct map }; +#define ENGINE_TICK 1000 + #endif /* _CALCCITY_H */ \ No newline at end of file diff --git a/src/core.c b/src/core.c new file mode 100644 index 0000000..81cb056 --- /dev/null +++ b/src/core.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + +#include "core.h" + + +int callback_tick(volatile int *tick) +{ + *tick = 1; + return TIMER_CONTINUE; +} + + + +void default_values(struct calccity *calccity, struct camera *camera, struct map *map) +{ + // Initialisation of struct calccity + memset(calccity, 0, sizeof *calccity); + + // Treasure + calccity->misc[0] = 500000; + + for (int i = 0; i < 4; i++) + calccity->taxes[i] = 10; + + calccity->month = 1; + calccity->year = 1900; + calccity->blinker = 0; + + calccity->disaster = 1; + calccity->animation = 1; + calccity->time_speed = 30; + + + // Initialisation of struct camera + memset(camera, 0, sizeof *camera); + camera->cursor_x = 64; + camera->cursor_y = 32; + + + // Initialisation of struct map + for (int y = 0; y < 50; y++) + { + for (int x = 0; x < 50; x++) + { + // Water + if ((x * y == 0) || (x == 49 || y == 49)) + map->data[y][x] = 139; + + // Shorelines + + + // Ground + else + map->data[y][x] = 48 + rand() % 2; + + + } + } + +} + + +int rtc_key(void) +{ + int opt = GETKEY_DEFAULT & ~GETKEY_MOD_SHIFT & ~GETKEY_MOD_ALPHA & ~GETKEY_REP_ARROWS; + int timeout = 1; + + key_event_t ev = getkey_opt(opt, &timeout); + if(ev.type == KEYEV_NONE) return 0; + + return ev.key; +} + + +void main_loop(struct calccity *calccity, struct camera *camera, struct map *map) +{ + // Timer initialisation + static volatile int tick = 1; + int t = timer_configure(TIMER_ANY, ENGINE_TICK*1000, GINT_CALL(callback_tick, &tick)); + if (t >= 0) timer_start(t); + + int end = 0, key = 0; + + //int build_mode = 0; + + while (!end) + { + display_map(calccity, camera, map); + + key = rtc_key(); + + } + + // Free timer + if (t >= 0) timer_stop(t); +} \ No newline at end of file diff --git a/src/core.h b/src/core.h new file mode 100644 index 0000000..3289348 --- /dev/null +++ b/src/core.h @@ -0,0 +1,19 @@ +#ifndef _CORE_H +#define _CORE_H + +#include "calccity.h" +#include "display.h" + +// callback_tick : time function +int callback_tick(volatile int *tick); + +// default_values : initialize the game +void default_values(struct calccity *current_game, struct camera *camera, struct map *map); + +// rtc_key : get a key with RTC system +int rtc_key(void); + +// main_loop : game main loop +void main_loop(struct calccity *calccity, struct camera *camera, struct map *map); + +#endif /* _CORE_H */ \ No newline at end of file diff --git a/src/display.c b/src/display.c new file mode 100644 index 0000000..260bcdd --- /dev/null +++ b/src/display.c @@ -0,0 +1,47 @@ +#include +#include + +#include "display.h" + + +void title_screen(void) +{ + extern const bopti_image_t img_title; + dclear(C_WHITE); + + dimage(0, 0, &img_title); + dupdate(); + getkey(); +} + + +void display_map(struct calccity *calccity, struct camera *camera, struct map *map) +{ + extern const bopti_image_t img_large_tileset; + extern const bopti_image_t img_large_water; + + if (calccity->animation) + calccity->blinker = (calccity->blinker + 1 ) % 2; + else + calccity->blinker = 0; + + dclear(C_WHITE); + for (int y = camera->y; y <= camera->y + 3; y++) + { + for (int x = camera->x; x <= camera->x + 7; x ++) + { + // Water + if (map->data[y][x] == 139) + dsubimage(x * 15, y * 15, &img_large_water, 15 * calccity->blinker, 0, 15 * (calccity->blinker + 1), 15, DIMAGE_NONE); + else + { + unsigned tile_id = map->data[y][x]; + unsigned int tile_x = 16 * (tile_id % 10); + unsigned int tile_y = 16 * (tile_id / 10); + + dsubimage(x * 15, y * 15, &img_large_tileset, tile_x, tile_y, 15, 15, DIMAGE_NONE); + } + } + } + dupdate(); +} diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..78fa18a --- /dev/null +++ b/src/display.h @@ -0,0 +1,12 @@ +#ifndef _DISPLAY_H +#define _DISPLAY_H + +#include "calccity.h" + +// title_screen : display title screen +void title_screen(void); + +// display_map : display the current state of the map +void display_map(struct calccity *calccity, struct camera *camera, struct map *map); + +#endif /* _DISPLAY_H */ \ No newline at end of file diff --git a/src/main.c b/src/main.c index a678939..9b49904 100644 --- a/src/main.c +++ b/src/main.c @@ -1,69 +1,16 @@ -#include -#include -#include -#include - -#include "calccity.h" - -void title_screen(void); -void default_values(struct calccity *current_game, struct camera *camera, struct map *map); +#include "core.h" int main(void) { title_screen(); + // Game initialisation struct calccity calccity; struct camera camera; struct map map; default_values(&calccity, &camera, &map); + main_loop(&calccity, &camera, &map); return 1; -} - - -void title_screen(void) -{ - extern const bopti_image_t img_title; - dclear(C_WHITE); - - dimage(0, 0, &img_title); - dupdate(); - - getkey(); -} - - -void default_values(struct calccity *calccity, struct camera *camera, struct map *map) -{ - // Initialisation of struct calccity - memset(calccity, 0, sizeof *calccity); - - // Treasure - calccity->misc[0] = 500000; - - for (int i = 0; i < 4; i++) - calccity->taxes[i] = 10; - - calccity->month = 1; - calccity->year = 1900; - calccity->blinker = 0; - - calccity->disaster = true; - calccity->time_speed = 30; - - - // Initialisation of struct camera - memset(camera, 0, sizeof *camera); - camera->cursor_x = 64; - camera->cursor_y = 32; - - - // Initialisation of struct map - for (int y = 0; y < 50; y++) - { - for (int x = 0; x < 50; x++) - map->data[y][x] = 48 + rand() % 2; - } - } \ No newline at end of file