9 static Block blockEmpty = { .type = BLOCKTYPE_SPACE,
12 static Block blockWall01 = { .type = BLOCKTYPE_OBSTACLE,
16 static Block* testBlocks[9] = { &blockWall01, &blockWall01, &blockWall01,
17 &blockEmpty, &blockEmpty, &blockEmpty,
18 &blockWall01, &blockEmpty, &blockWall01 };
20 BlockGrid levelGrid = { .width = 3,
22 .blocks = testBlocks };
24 #define DEFAULT_PLAYER_SPAWN_POS { -BLOCKGRID_CELL_SIZE, 0.0f, -BLOCKGRID_CELL_SIZE }
25 AiVector3D playerSpawnPos = DEFAULT_PLAYER_SPAWN_POS;
27 static const char* replaceFileExtension(const AiString path, const char* ext);
32 const AiScene* sceneData = importScene("assets/wall01.3ds");
33 blockWall01.sceneData = sceneData;
34 if (sceneData != NULL) {
35 const unsigned int numTextures = sceneData->mNumMeshes;
37 blockWall01.textureIds = malloc(numTextures * sizeof(GLuint));
38 glGenTextures(numTextures, blockWall01.textureIds);
40 for (unsigned int i = 0; i < numTextures; ++i) {
41 glBindTexture(GL_TEXTURE_2D, blockWall01.textureIds[i]);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
44 AiString originalTexturePath;
45 if (aiGetMaterialTexture(sceneData->mMaterials[sceneData->mMeshes[i]->mMaterialIndex],
46 aiTextureType_DIFFUSE,
49 NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
50 const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
51 size_t textureFileLength = strlen(textureFile);
52 char* texturePath = malloc(strlen("assets/") + textureFileLength + 1);
53 strcpy(texturePath, "assets/");
54 strncat(texturePath, textureFile, textureFileLength);
55 TgaImage* textureImage = readTga(texturePath);
56 if (textureImage == NULL) {
57 logError("Asset texture file not found: %s", texturePath);
60 glTexImage2D(GL_TEXTURE_2D,
62 textureImage->imageComponents,
63 textureImage->header.imageWidth,
64 textureImage->header.imageHeight,
66 textureImage->imageFormat,
69 free(textureImage->bytes);
74 glBindTexture(GL_TEXTURE_2D, 0);
77 buildLevelFromImage(readTga("assets/level01.tga"));
80 void buildLevelFromImage(TgaImage* image) {
82 logError("Null image received, cannot build level");
86 if (image->header.imageBpp != 32) {
87 logError("Invalid level image format (%d bpp)", image->header.imageBpp);
91 BlockGrid newGrid = { .width = image->header.imageWidth,
92 .depth = image->header.imageHeight,
93 .blocks = malloc(image->header.imageWidth
94 * image->header.imageHeight
96 playerSpawnPos = (AiVector3D) DEFAULT_PLAYER_SPAWN_POS;
98 for (int row = 0; row < newGrid.depth; ++row) {
99 for (int x = 0; x < newGrid.width; ++x) {
100 // Flip the image vertically due to (0, 0) being bottom left
101 int z = newGrid.depth - row - 1;
103 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
105 switch (pixelColorARGB) {
107 block = &blockWall01;
111 playerSpawnPos = (AiVector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
117 setBlockInGrid(newGrid, x, z, block);
126 * The following function will not work properly with texture
127 * file names (excluding directory part) beginning with '.'
129 static const char* replaceFileExtension(const AiString path, const char* ext) {
130 size_t lengthToCopy = path.length;
132 char* lastDotSubstr = strrchr(path.data, '.');
133 if (lastDotSubstr != NULL) {
134 if (strpbrk(lastDotSubstr, "\\/") == NULL) {
135 lengthToCopy = lastDotSubstr - path.data;
139 size_t extLength = strlen(ext) + 1;
140 char* newPath = malloc(lengthToCopy + extLength);
141 strncpy(newPath, path.data, lengthToCopy);
142 strncpy(newPath + lengthToCopy, ext, extLength);