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("out/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("out/assets/") + textureFileLength + 1);
53 strcpy(texturePath, "out/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("out/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 z = 0; z < newGrid.depth; ++z) {
99 for (int x = 0; x < newGrid.width; ++x) {
100 uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(z * newGrid.width) + x];
102 switch (pixelColorARGB) {
104 block = &blockWall01;
108 playerSpawnPos = (AiVector3D) { x * BLOCKGRID_CELL_SIZE, 0.0f, z * BLOCKGRID_CELL_SIZE };
114 setBlockInGrid(newGrid, x, z, block);
123 * The following function will not work properly with texture
124 * file names (excluding directory part) beginning with '.'
126 static const char* replaceFileExtension(const AiString path, const char* ext) {
127 size_t lengthToCopy = path.length;
129 char* lastDotSubstr = strrchr(path.data, '.');
130 if (lastDotSubstr != NULL) {
131 if (strpbrk(lastDotSubstr, "\\/") == NULL) {
132 lengthToCopy = lastDotSubstr - path.data;
136 size_t extLength = strlen(ext) + 1;
137 char* newPath = malloc(lengthToCopy + extLength);
138 strncpy(newPath, path.data, lengthToCopy);
139 strncpy(newPath + lengthToCopy, ext, extLength);