6 #include "engine/logger.h"
7 #include "engine/scene.h"
8 #include "engine/tga.h"
14 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
16 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z,
19 static Transform playerSpawnTransform;
21 static void buildLevelFromImage(const TgaImage* image);
26 playerSpawnTransform = identity();
27 translate(&playerSpawnTransform, (Vector) { -BLOCKGRID_CELL_SIZE,
29 -BLOCKGRID_CELL_SIZE });
31 blockWall01.solid = importSolid("assets/wall01.3ds");
33 buildLevelFromImage(readTga("assets/level01.tga"));
35 Scene* levelScene = newScene();
37 for (size_t z = 0; z < levelGrid.depth; ++z) {
38 for (size_t x = 0; x < levelGrid.width; ++x) {
39 Scene* blockScene = newScene();
40 translate(&blockScene->transform, (Vector) {
41 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
43 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
44 blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
45 insertChildScene(levelScene, blockScene);
49 currentScene = levelScene;
53 spawnPlayer(playerSpawnTransform);
56 static void buildLevelFromImage(const TgaImage* image) {
58 logError("Null image received, cannot build level");
62 if (image->header.imageBpp != 32) {
63 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
67 BlockGrid newGrid = { .width = image->header.imageWidth,
68 .depth = image->header.imageHeight,
69 .blocks = malloc(image->header.imageWidth
70 * image->header.imageHeight
73 for (size_t row = 0; row < newGrid.depth; ++row) {
74 for (size_t x = 0; x < newGrid.width; ++x) {
75 // Flip the image vertically due to (0, 0) being bottom left
76 size_t z = newGrid.depth - row - 1;
78 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
80 switch (pixelColorARGB) {
86 playerSpawnTransform = identity();
87 translate(&playerSpawnTransform, (Vector) {
88 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
90 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
96 setBlockInGrid(newGrid, x, z, block);
103 static inline size_t nonNegative(long n) {
104 return n < 0 ? 0u : n;
107 GridLocation gridLocationFromPosition(Vector pos) {
108 Vector scaledPos = scaleVector(pos, 1.0f / BLOCKGRID_CELL_SIZE);
109 return (GridLocation) { .x = nonNegative(scaledPos.x),
110 .z = nonNegative(scaledPos.z) };
113 Obstacle getObstacles(GridLocation loc) {
114 Obstacle result = OBSTACLE_NONE;
116 result |= OBSTACLE_XN | OBSTACLE_XN_ZP | OBSTACLE_XN_ZN;
118 if (loc.x >= levelGrid.width - 1) {
119 result |= OBSTACLE_XP | OBSTACLE_XP_ZP | OBSTACLE_XP_ZN;
122 result |= OBSTACLE_ZN | OBSTACLE_XP_ZN | OBSTACLE_XN_ZN;
124 if (loc.z >= levelGrid.depth - 1) {
125 result |= OBSTACLE_ZP | OBSTACLE_XP_ZP | OBSTACLE_XN_ZP;
127 if (!(result & OBSTACLE_XP)
128 && getBlockFromGrid(levelGrid, loc.x + 1, loc.z)->type & BLOCKTYPE_OBSTACLE_X) {
129 result |= OBSTACLE_XP;
131 if (!(result & OBSTACLE_XN)
132 && getBlockFromGrid(levelGrid, loc.x - 1, loc.z)->type & BLOCKTYPE_OBSTACLE_X) {
133 result |= OBSTACLE_XN;
135 if (!(result & OBSTACLE_ZP)
136 && getBlockFromGrid(levelGrid, loc.x, loc.z + 1)->type & BLOCKTYPE_OBSTACLE_Z) {
137 result |= OBSTACLE_ZP;
139 if (!(result & OBSTACLE_ZN)
140 && getBlockFromGrid(levelGrid, loc.x, loc.z - 1)->type & BLOCKTYPE_OBSTACLE_Z) {
141 result |= OBSTACLE_ZN;
143 if (!(result & OBSTACLE_XP_ZP)
144 && getBlockFromGrid(levelGrid, loc.x + 1, loc.z + 1)->type
145 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
146 result |= OBSTACLE_XP_ZP;
148 if (!(result & OBSTACLE_XP_ZN)
149 && getBlockFromGrid(levelGrid, loc.x + 1, loc.z - 1)->type
150 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
151 result |= OBSTACLE_XP_ZN;
153 if (!(result & OBSTACLE_XN_ZP)
154 && getBlockFromGrid(levelGrid, loc.x - 1, loc.z + 1)->type
155 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
156 result |= OBSTACLE_XN_ZP;
158 if (!(result & OBSTACLE_XN_ZN)
159 && getBlockFromGrid(levelGrid, loc.x - 1, loc.z - 1)->type
160 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
161 result |= OBSTACLE_XN_ZN;