Take it one step at a time

This commit is contained in:
Shadow15510 2021-10-31 15:12:30 +01:00
parent 5450a7e748
commit f40c8464ae
8 changed files with 192 additions and 57 deletions

View File

@ -11,6 +11,8 @@ find_package(Gint 2.1 REQUIRED)
set(SOURCES
src/main.c
src/core.c
src/display.c
)
set(ASSETS)

View File

@ -0,0 +1,2 @@
#include "calccity.h"

View File

@ -1,6 +1,9 @@
#ifndef _CALCCITY_H
#define _CALCCITY_H
#include <gint/std/stdlib.h>
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 */

100
src/core.c Normal file
View File

@ -0,0 +1,100 @@
#include <gint/keyboard.h>
#include <gint/std/stdlib.h>
#include <gint/std/string.h>
#include <gint/timer.h>
#include <gint/clock.h>
#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);
}

19
src/core.h Normal file
View File

@ -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 */

47
src/display.c Normal file
View File

@ -0,0 +1,47 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#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();
}

12
src/display.h Normal file
View File

@ -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 */

View File

@ -1,69 +1,16 @@
#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/std/stdlib.h>
#include <gint/std/string.h>
#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;
}
}