C programming. I need some help with void* pointer

Hello guys.. I have a function that is helping me to load assets from and archive file, and it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
DWORD LoadAssetsFromArchive(_In_ char* ArchiveName, _In_ char* AssetFileName, _In_ RESOURCE_TYPE ResourceType, _Inout_ void* Resource)
{
    DWORD Error = ERROR_SUCCESS;
    mz_zip_archive Archive = { 0 };
    BYTE* DecompressedBuffer = NULL;
    size_t DecompressedSize = 0;
    BOOL FileFoundInArchive = FALSE;

    if (mz_zip_reader_init_file(&Archive, ArchiveName, 0) == MZ_FALSE)
    {
        Error = mz_zip_get_last_error(&Archive);
        LogMessageA(LL_ERROR, "[%s] mz_zip_reader_init_file failed with 0x%08lx on archive file %s Error: %s !", __FUNCTION__, Error, ArchiveName, mz_zip_get_error_string(Error));
        goto Exit;
    }

    for (int FileIndex = 0; FileIndex < (int)mz_zip_reader_get_num_files(&Archive); FileIndex++)
    {
        mz_zip_archive_file_stat CompressedFileStatistics = { 0 };

        if (mz_zip_reader_file_stat(&Archive, FileIndex, &CompressedFileStatistics) == MZ_FALSE)
        {
            Error = mz_zip_get_last_error(&Archive);
            LogMessageA(LL_ERROR, "[%s] mz_zip_reader_file_stat failed with 0x%08lx Archive %s File %s Error: %s !", __FUNCTION__, Error, ArchiveName, AssetFileName, mz_zip_get_error_string(Error));
            goto Exit;
        }

        if (_stricmp(CompressedFileStatistics.m_filename, AssetFileName) == 0)
        {
            FileFoundInArchive = TRUE;

            if ((DecompressedBuffer = mz_zip_reader_extract_to_heap(&Archive, FileIndex, &DecompressedSize, 0)) == NULL)
            {
                Error = mz_zip_get_last_error(&Archive);
                LogMessageA(LL_ERROR, "[%s] mz_zip_reader_extract_to_heap failed with 0x%08lx Archive %s File %s Error %s !", __FUNCTION__, Error, ArchiveName, AssetFileName, mz_zip_get_error_string(Error));
                goto Exit;
            }
            break;
        }
    }

    if (FileFoundInArchive == FALSE)
    {
        Error = ERROR_FILE_NOT_FOUND;
        LogMessageA(LL_ERROR, "[%s] File %s was not found in archive %s ! 0x%08lx", __FUNCTION__, AssetFileName, ArchiveName, Error);
        goto Exit;
    }

    switch (ResourceType)
    {
        case RESOURCE_TYPE_WAV:
        {
            Error = LoadWavFromMemory(DecompressedBuffer, Resource);
            break;
        }

        case RESOURCE_TYPE_OGG:
        {
            Error = LoadOggFromMemory(DecompressedBuffer, (uint32_t)DecompressedSize, Resource);
            break;
        }

        case RESOURCE_TYPE_TILEMAP:
        {
            Error = LoadTilemapFromMemory(DecompressedBuffer, (uint32_t)DecompressedSize, Resource);
            break;
        }

        case RESOURCE_TYPE_BMPX:
        {
            Error = Load32BppBitmapFromMemory(DecompressedBuffer, Resource);
            break;
        }

        default:
            ASSERT(FALSE, "Unknown resource type !");
    }

Exit:

    mz_zip_reader_end(&Archive);

    return(Error);
}


Okay I'm calling this function here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
DWORD WINAPI AssetLoadingThreadProc(_In_ LPVOID lpParam) // Load game assets, audio, tile maps, etc. HERE
{
    UNREFERENCED_PARAMETER(lpParam);
    DWORD Error = ERROR_SUCCESS;

    LogMessageA(LL_INFO, "[%s] Asset loading started..", __FUNCTION__);
    
    // These 2 Assets have to be loaded first.
    // Game font
    if ((Error = LoadAssetsFromArchive(ASSET_FILE, "6x7Font.bmpx", RESOURCE_TYPE_BMPX, &g6x7Font)) != ERROR_SUCCESS)
    {
        LogMessageA(LL_ERROR, "[%s] Loading 6x7Font.bmpx failed !", __FUNCTION__);
        goto Exit;
    }
    // Menu wave transition
    if ((Error = LoadAssetsFromArchive(ASSET_FILE, "TransitionScreen.wav", RESOURCE_TYPE_WAV, &gSoundTransictionScreen)) != ERROR_SUCCESS)
    {
        LogMessageA(LL_ERROR, "[%s] Loading TransitionScreen.wav failed !", __FUNCTION__);
        goto Exit;
    }

    //Set Event to load first
    SetEvent(gEssentialAssetsLoadedEvent);

    // TMX FILE MAP------------------------------------------------------------------------------------
    // Overworld Tile
    if ((Error = LoadAssetsFromArchive(ASSET_FILE, "Overworld01.tmx", RESOURCE_TYPE_TILEMAP, &gOverworld01.TileMap)) != ERROR_SUCCESS)
    {
        LogMessageA(LL_ERROR, "[%s] Loading Overworld01.tmx failed with 0x%08lx !", __FUNCTION__, Error);
        goto Exit;
    }
.....
...
....
...
....
// they are like 250 files, and I'm only showing you 3 of them at the begining

Exit:

    if (Error == ERROR_SUCCESS)
        LogMessageA(LL_INFO, "[%s] Asset loading has ended successfully.", __FUNCTION__);
    else LogMessageA(LL_INFO, "[%s] Asset loading has failed with result 0x%08lx!", __FUNCTION__, Error);

    return(Error);
}


..Now this is working just fine, and isn't that the problem I just want to make this function significantly shorter to be more readable, and I've come with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define NUMBER_OF_ASSETS	250 // Modify here every time you add a new file to be loaded

DWORD WINAPI AssetLoadingThreadProc(_In_ LPVOID lpParam)
{
	UNREFERENCED_PARAMETER(lpParam);
    DWORD Error = ERROR_SUCCESS;
	FILE* fp = NULL;
	const char* fileName = "assetList";
	char assetFileName[50] = { 0 };
	char pName[50] = { 0 }; // This is the third parameter which I load from the file
	int resourceType = EMPTY;
	
	LogMessageA(LL_INFO, "[%s] Asset loading started..", __FUNCTION__);
	
	fp = fopen(fileName, "r");
	
	if (fp)
	{
		for (int i = 0; i < NUMBER_OF_ASSETS; i++)
		{
			if (fscanf(fp, "%s%d%s", assetFileName, resourceType, pName) == 3)
			{
				if ((Error = LoadAssetsFromArchive(ASSET_FILE, assetFileName, resourceType, /* the problem is HERE, third parameter*/)) != ERROR_SUCCESS)
				{
					LogMessageA(LL_ERROR, "[%s] Loading %s failed !", __FUNCTION__, assetFileName);
					break;
				}
			}
			
			// Set event to load first 2 files needed when the game starts.
			if (i == 1)
				SetEvent(gEssentialAssetsLoadedEvent);
		}
		
		fclose(fp);
	}
	
	
	if(Error == ERROR_SUCCESS)
		LogMessageA(LL_INFO, "[%s] Asset loading has ended successfully.", __FUNCTION__);
    else LogMessageA(LL_INFO, "[%s] Asset loading has failed with result 0x%08lx!", __FUNCTION__, Error);

    return(Error);
}


..Well all the resources pointers are stored in a header file and I want to associate all the pointers with the strings that are saved to the file which will be the third parameter to that function.. aaannd noting is coming in mind. How can I do that.. ?
Last edited on
And the text file that is storing the data for the list of the file in archive looks like this:

1
2
3
6x7Font.bmpx 3 g6x7Font
TransitionScreen.wav 0 gSoundTransictionScreen
Overworld01.tmx 2 gOverworld01.TileMap
Now this is working just fine


IINBDFI !!
You need a way to turn a string such as "g6x7Font" into an address.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static struct {
    const char *varname;
    void *varaddr;
} lut[] = {
    { "g6x7Font", &g6x7Font },
    { "gSoundTransictionScreen", &gSoundTransictionScreen },
    { "gOverworld01.TileMap", &gOverworld01.TileMap },
    // 250 times
};

static void *lookup( const char *varname ) {
    for ( size_t i = 0 ; i < sizeof(lut)/sizeof(*lut) ; i++ ) {
        if ( strcmp(varname, lut[i].varname) == 0 ) {
            return lut[i].varaddr;
        }
    }
    return NULL;// oops!
}


Then you can do
1
2
3
4
5
6
void *varptr = lookup(pName);
if ((Error = LoadAssetsFromArchive(ASSET_FILE, assetFileName, resourceType, varptr)) != ERROR_SUCCESS)
{
    LogMessageA(LL_ERROR, "[%s] Loading %s failed !", __FUNCTION__, assetFileName);
    break;
}

Yes Salem_c this is what I needed. And I was up to this point:

1
2
3
4
5
6
void *varptr = lookup(pName);
if ((Error = LoadAssetsFromArchive(ASSET_FILE, assetFileName, resourceType, varptr)) != ERROR_SUCCESS)
{
    LogMessageA(LL_ERROR, "[%s] Loading %s failed !", __FUNCTION__, assetFileName);
    break;
}


.. but I didin't know how, I mean the first part :).

And finally got rid of a bunch of lines, thanks to you.
Problem solved.

Many thanks again..
Now this is working just fine


IINBDFI !!


No its not broken true ..seeplus but I it was so big and every time I expand that function and try to add another file .. copy paste the if statement, it's a pain in the a**. I made the game to this point and it's working ~97% and I'm in a lack of ideas, that's why I'm going back to the old code and try to give a polish where possible. :)
DFCL, DFCL....
Topic archived. No new replies allowed.