2 * Copyright 2018-2020 Iwo 'Outfrost' Bujkiewicz
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 #include "engine/logger.h"
15 #include "engine/scene.h"
16 #include "engine/tga.h"
22 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
24 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z,
27 static Transform playerSpawnTransform;
29 static void buildLevelFromImage(const TgaImage* image);
34 playerSpawnTransform = identity();
35 translate(&playerSpawnTransform, (Vector) { -BLOCKGRID_CELL_SIZE,
37 -BLOCKGRID_CELL_SIZE });
39 blockWall01.solid = importSolid("assets/wall01.3ds");
41 buildLevelFromImage(readTga("assets/level01.tga"));
43 Scene* levelScene = newScene();
45 for (size_t z = 0; z < levelGrid.depth; ++z) {
46 for (size_t x = 0; x < levelGrid.width; ++x) {
47 Scene* blockScene = newScene();
48 translate(&blockScene->transform, (Vector) {
49 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
51 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
52 blockScene->solid = getBlockFromGrid(levelGrid, x, z)->solid;
53 insertChildScene(levelScene, blockScene);
57 currentScene = levelScene;
61 spawnPlayer(playerSpawnTransform);
64 static void buildLevelFromImage(const TgaImage* image) {
66 logError("Null image received, cannot build level");
70 if (image->header.imageBpp != 32) {
71 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
75 BlockGrid newGrid = { .width = image->header.imageWidth,
76 .depth = image->header.imageHeight,
77 .blocks = malloc(image->header.imageWidth
78 * image->header.imageHeight
81 for (size_t row = 0; row < newGrid.depth; ++row) {
82 for (size_t x = 0; x < newGrid.width; ++x) {
83 // Flip the image vertically due to (0, 0) being bottom left
84 size_t z = newGrid.depth - row - 1;
86 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
88 switch (pixelColorARGB) {
94 playerSpawnTransform = identity();
95 translate(&playerSpawnTransform, (Vector) {
96 (x * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f),
98 (z * BLOCKGRID_CELL_SIZE) + (BLOCKGRID_CELL_SIZE * 0.5f) });
104 setBlockInGrid(newGrid, x, z, block);
111 static inline size_t nonNegative(long n) {
112 return n < 0 ? 0u : n;
115 GridLocation gridLocationFromPosition(Vector pos) {
116 Vector scaledPos = scaleVector(pos, 1.0f / BLOCKGRID_CELL_SIZE);
117 return (GridLocation) { .x = nonNegative(scaledPos.x),
118 .z = nonNegative(scaledPos.z) };
121 Obstacle getObstacles(GridLocation loc) {
122 Obstacle result = OBSTACLE_NONE;
124 result |= OBSTACLE_XN | OBSTACLE_XN_ZP | OBSTACLE_XN_ZN;
126 if (loc.x >= levelGrid.width - 1) {
127 result |= OBSTACLE_XP | OBSTACLE_XP_ZP | OBSTACLE_XP_ZN;
130 result |= OBSTACLE_ZN | OBSTACLE_XP_ZN | OBSTACLE_XN_ZN;
132 if (loc.z >= levelGrid.depth - 1) {
133 result |= OBSTACLE_ZP | OBSTACLE_XP_ZP | OBSTACLE_XN_ZP;
135 if (!(result & OBSTACLE_XP)
136 && getBlockFromGrid(levelGrid, loc.x + 1, loc.z)->type & BLOCKTYPE_OBSTACLE_X) {
137 result |= OBSTACLE_XP;
139 if (!(result & OBSTACLE_XN)
140 && getBlockFromGrid(levelGrid, loc.x - 1, loc.z)->type & BLOCKTYPE_OBSTACLE_X) {
141 result |= OBSTACLE_XN;
143 if (!(result & OBSTACLE_ZP)
144 && getBlockFromGrid(levelGrid, loc.x, loc.z + 1)->type & BLOCKTYPE_OBSTACLE_Z) {
145 result |= OBSTACLE_ZP;
147 if (!(result & OBSTACLE_ZN)
148 && getBlockFromGrid(levelGrid, loc.x, loc.z - 1)->type & BLOCKTYPE_OBSTACLE_Z) {
149 result |= OBSTACLE_ZN;
151 if (!(result & OBSTACLE_XP_ZP)
152 && getBlockFromGrid(levelGrid, loc.x + 1, loc.z + 1)->type
153 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
154 result |= OBSTACLE_XP_ZP;
156 if (!(result & OBSTACLE_XP_ZN)
157 && getBlockFromGrid(levelGrid, loc.x + 1, loc.z - 1)->type
158 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
159 result |= OBSTACLE_XP_ZN;
161 if (!(result & OBSTACLE_XN_ZP)
162 && getBlockFromGrid(levelGrid, loc.x - 1, loc.z + 1)->type
163 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
164 result |= OBSTACLE_XN_ZP;
166 if (!(result & OBSTACLE_XN_ZN)
167 && getBlockFromGrid(levelGrid, loc.x - 1, loc.z - 1)->type
168 & (BLOCKTYPE_OBSTACLE_X | BLOCKTYPE_OBSTACLE_Z)) {
169 result |= OBSTACLE_XN_ZN;