aboutsummaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-07-03 00:49:03 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:20 +0200
commit6b9f686913efc3725b2690033cd4f398e07076ba (patch)
treee9aa91a6b9f617d78123ebe7ad272fc42a60d306 /src/game
parentc9e6ae44a9aeb89b3f48f3443d6baa80103f7445 (diff)
downloadjancity-6b9f686913efc3725b2690033cd4f398e07076ba.tar.gz
Add project source code
Diffstat (limited to 'src/game')
-rw-r--r--src/game/CMakeLists.txt15
-rw-r--r--src/game/inc/game.h15
-rw-r--r--src/game/privinc/game_private.h16
-rw-r--r--src/game/src/game.c101
-rw-r--r--src/game/src/res.c247
5 files changed, 394 insertions, 0 deletions
diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt
new file mode 100644
index 0000000..0c21b0c
--- /dev/null
+++ b/src/game/CMakeLists.txt
@@ -0,0 +1,15 @@
+add_library(game "src/game.c" "src/res.c")
+target_include_directories(game PUBLIC "inc" PRIVATE "privinc")
+target_link_libraries(game PRIVATE
+ building
+ container
+ font
+ gfx
+ gui
+ instance
+ pad
+ player
+ resource
+ system
+ terrain
+ unit)
diff --git a/src/game/inc/game.h b/src/game/inc/game.h
new file mode 100644
index 0000000..8275d54
--- /dev/null
+++ b/src/game/inc/game.h
@@ -0,0 +1,15 @@
+#ifndef GAME_H
+#define GAME_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int game(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GAME_H */
diff --git a/src/game/privinc/game_private.h b/src/game/privinc/game_private.h
new file mode 100644
index 0000000..1b9449f
--- /dev/null
+++ b/src/game/privinc/game_private.h
@@ -0,0 +1,16 @@
+#ifndef GAME_PRIVATE_H
+#define GAME_PRIVATE_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int game_resinit(void);
+void game_free(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GAME_PRIVATE_H */
diff --git a/src/game/src/game.c b/src/game/src/game.c
new file mode 100644
index 0000000..2f13a64
--- /dev/null
+++ b/src/game/src/game.c
@@ -0,0 +1,101 @@
+#include <game.h>
+#include <game_private.h>
+#include <gfx.h>
+#include <human_player.h>
+#include <player.h>
+#include <resource.h>
+#include <system.h>
+#include <terrain.h>
+#include <stddef.h>
+
+int game(void)
+{
+ int ret = -1;
+
+ if (game_resinit())
+ goto end;
+
+ enum {HUMAN_PLAYERS = 1, MAP_RESOURCES = 3};
+ struct human_player humans[HUMAN_PLAYERS];
+
+ for (size_t i = 0; i < sizeof humans / sizeof *humans; i++)
+ {
+ const struct human_player_cfg cfg =
+ {
+ .padn = i,
+ .pl =
+ {
+ .team = PLAYER_COLOR_BLUE,
+ .x = 80,
+ .y = 40
+ }
+ };
+
+ if (human_player_init(&cfg, &humans[i]))
+ goto end;
+ }
+
+ struct terrain_map map;
+ struct resource res[MAP_RESOURCES] = {0};
+
+ terrain_init(&map);
+#if 1
+ if (resource_create(&(const struct resource_cfg)
+ {
+ .type = RESOURCE_TYPE_GOLD,
+ .x = 50,
+ .y = 200
+ }, res, sizeof res / sizeof *res)
+ || resource_create(&(const struct resource_cfg)
+ {
+ .type = RESOURCE_TYPE_WOOD,
+ .x = 180,
+ .y = 200
+ }, res, sizeof res / sizeof *res)
+ || resource_create(&(const struct resource_cfg)
+ {
+ .type = RESOURCE_TYPE_WOOD,
+ .x = 240,
+ .y = 200
+ }, res, sizeof res / sizeof *res))
+ goto end;
+#endif
+ bool exit = false;
+
+ while (!exit)
+ {
+ system_loop();
+
+ for (size_t i = 0; i < sizeof humans / sizeof *humans; i++)
+ {
+ const struct human_player *const h = &humans[i];
+
+ if (h->pl.alive)
+ {
+ struct player_others o =
+ {
+ .res = res,
+ .n_res = sizeof res / sizeof *res
+ };
+
+ exit |= human_player_update(&humans[i], &o);
+
+ if (terrain_render(&map, &h->cam)
+ || human_player_render(h, &o)
+ || cursor_render(&h->cam.cursor))
+ goto end;
+
+ /* TODO: render AI players. */
+ }
+ }
+
+ instance_cyclic();
+ gfx_draw();
+ }
+
+ ret = 0;
+
+end:
+ game_free();
+ return ret;
+}
diff --git a/src/game/src/res.c b/src/game/src/res.c
new file mode 100644
index 0000000..39b3b5e
--- /dev/null
+++ b/src/game/src/res.c
@@ -0,0 +1,247 @@
+#include <game_private.h>
+#include <building.h>
+#include <container.h>
+#include <font.h>
+#include <gfx.h>
+#include <gui.h>
+#include <resource.h>
+#include <terrain.h>
+#include <unit.h>
+#include <stdbool.h>
+
+static const struct container c[] =
+{
+ {
+ .path = "barracks",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &building_sprites[BUILDING_TYPE_BARRACKS]
+ }
+ },
+
+ {
+ .path = "worker_n",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &unit_sprites[UNIT_SPRITE_N]
+ }
+ },
+
+ {
+ .path = "worker_ne",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &unit_sprites[UNIT_SPRITE_NE]
+ }
+ },
+
+ {
+ .path = "worker_e",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &unit_sprites[UNIT_SPRITE_E]
+ }
+ },
+
+ {
+ .path = "worker_se",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &unit_sprites[UNIT_SPRITE_SE]
+ }
+ },
+
+ {
+ .path = "worker_s",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &unit_sprites[UNIT_SPRITE_S]
+ }
+ },
+
+ {
+ .path = "grass",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &grass_sprite
+ }
+ },
+
+ {
+ .path = "cursor",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &cursor_sprite
+ }
+ },
+
+ {
+ .path = "gui_bar_left",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_BAR_LEFT]
+ }
+ },
+
+ {
+ .path = "gui_bar_mid",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_BAR_MID]
+ }
+ },
+
+ {
+ .path = "gui_bar_right",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_BAR_RIGHT]
+ }
+ },
+
+ {
+ .path = "sel_up_left",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_SELECTION_UP_LEFT]
+ }
+ },
+
+ {
+ .path = "sel_up_right",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_SELECTION_UP_RIGHT]
+ }
+ },
+
+ {
+ .path = "sel_down_left",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_SELECTION_DOWN_LEFT]
+ }
+ },
+
+ {
+ .path = "sel_down_right",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_SELECTION_DOWN_RIGHT]
+ }
+ },
+
+ {
+ .path = "sel_mid",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_SELECTION_MID]
+ }
+ },
+
+ {
+ .path = "sel_mid_v",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &gui_sprites[GUI_SELECTION_MID_VERT]
+ }
+ },
+
+ {
+ .path = "font",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &font_sprite
+ }
+ },
+
+ {
+ .path = "gold_mine",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &resource_sprites[RESOURCE_TYPE_GOLD]
+ }
+ },
+
+ {
+ .path = "tree",
+ .type = CONTAINER_TYPE_SPRITE,
+ .data =
+ {
+ .sprite = &resource_sprites[RESOURCE_TYPE_WOOD]
+ }
+ },
+
+ {
+ .path = "acknowledge_01",
+ .type = CONTAINER_TYPE_SOUND,
+ .data =
+ {
+ .sound = &unit_sounds[UNIT_SOUND_MOVE]
+ }
+ },
+
+ {
+ .path = "acknowledge_02",
+ .type = CONTAINER_TYPE_SOUND,
+ .data =
+ {
+ .sound = &unit_sounds[UNIT_SOUND_MOVE_2]
+ }
+ },
+
+ {
+ .path = "selected_01",
+ .type = CONTAINER_TYPE_SOUND,
+ .data =
+ {
+ .sound = &unit_sounds[UNIT_SOUND_SELECTED]
+ }
+ }
+};
+
+static bool init;
+
+void game_free(void)
+{
+ if (init)
+ {
+ container_free(c, sizeof c / sizeof *c);
+ init = false;
+ }
+}
+
+int game_resinit(void)
+{
+ if (!init)
+ {
+ if (container_load("rts.cnt", c, sizeof c / sizeof *c))
+ {
+ perror("container_load");
+ return -1;
+ }
+
+ init = true;
+ }
+
+ return 0;
+}