git @ Cat's Eye Technologies linapple / c89d4e3
Extra safety to assets, fix dumb typo Fixed dumb typo that inverted logic—figures right before bed. While I was at it, we now check the return from Asset_Init and exit with an error if it's false—it prints its own error messages. And while I was att it, I made assets itself a pointer that starts out null. This might be all totally unnecessary once we start cleaning up other things, but this is in an early enough state I'd like us to have options. T. Joseph Carter 3 years ago
6 changed file(s) with 83 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
2727 SDL_Surface *splash;
2828 } assets_t;
2929
30 extern assets_t assets;
30 extern assets_t *assets;
3131
3232 bool Asset_Init(void);
3333 void Asset_Quit(void);
790790
791791 int main(int argc, char * lpCmdLine[])
792792 {
793 Asset_Init();
793 if (!Asset_Init()) {
794 return 1;
795 }
794796
795797 // GPH: The very first thing we do is attempt to grab the needed configuration files and put them in the user's folder.
796798 Config config;
563563
564564 rectangle(screen, 1, 1, /*SCREEN_WIDTH*/g_ScreenWidth - 2, (Help_TopX - 8), SDL_MapRGB(screen->format, 255, 255, 0));
565565
566 if(assets.icon != NULL) { // display Apple logo
567 tempSurface = SDL_DisplayFormat(assets.icon);
566 if(assets->icon != NULL) { // display Apple logo
567 tempSurface = SDL_DisplayFormat(assets->icon);
568568 SDL_Rect logo, scrr;
569569 logo.x = logo.y = 0;
570570 logo.w = tempSurface->w;
12411241 // SDL ref: Icon should be set *before* the first call to SDL_SetVideoMode.
12421242 // Uint32 colorkey;
12431243
1244 /* assets.icon = SDL_CreateRGBSurfaceFrom((void*)Apple_icon, 32, 32, 8, 32, 0, 0, 0, 0);
1245 Uint32 colorkey = SDL_MapRGB(assets.icon->format, 0, 0, 0);
1246 SDL_SetColorKey(assets.icon, SDL_SRCCOLORKEY, colorkey);
1247 SDL_WM_SetIcon(assets.icon, NULL);
1248 printf("Icon was set! Width=%d, height=%d\n", assets.icon->w, assets.icon->h);*/
1249
1250 if(assets.icon != NULL) {
1251 Uint32 colorkey = SDL_MapRGB(assets.icon->format, 0, 0, 0);
1252 SDL_SetColorKey(assets.icon, SDL_SRCCOLORKEY, colorkey);
1253 SDL_WM_SetIcon(assets.icon, NULL);
1254 // printf("Icon was set! Width=%d, height=%d\n", assets.icon->w, assets.icon->h);
1244 /* assets->icon = SDL_CreateRGBSurfaceFrom((void*)Apple_icon, 32, 32, 8, 32, 0, 0, 0, 0);
1245 Uint32 colorkey = SDL_MapRGB(assets->icon->format, 0, 0, 0);
1246 SDL_SetColorKey(assets->icon, SDL_SRCCOLORKEY, colorkey);
1247 SDL_WM_SetIcon(assets->icon, NULL);
1248 printf("Icon was set! Width=%d, height=%d\n", assets->icon->w, assets->icon->h);*/
1249
1250 if(assets->icon != NULL) {
1251 Uint32 colorkey = SDL_MapRGB(assets->icon->format, 0, 0, 0);
1252 SDL_SetColorKey(assets->icon, SDL_SRCCOLORKEY, colorkey);
1253 SDL_WM_SetIcon(assets->icon, NULL);
1254 // printf("Icon was set! Width=%d, height=%d\n", assets->icon->w, assets->icon->h);
12551255 }
12561256 //////////////////////////////////////////////////////////////////////
12571257 return 0;
18941894 ZeroMemory(vidlastmem,0x10000);
18951895
18961896 // LOAD THE splash screen
1897 g_hLogoBitmap = SDL_DisplayFormat(assets.splash);
1897 g_hLogoBitmap = SDL_DisplayFormat(assets->splash);
18981898
18991899 // LOAD APPLE CHARSET40
1900 charset40 = SDL_DisplayFormat(assets.charset40);
1900 charset40 = SDL_DisplayFormat(assets->charset40);
19011901
19021902 // CREATE AN IDENTITY PALETTE AND FILL IN THE CORRESPONDING COLORS IN
19031903 // THE BITMAPINFO STRUCTURE
2828 #define ASSET_CHARSET40_BMP "charset40.bmp"
2929 #define ASSET_FONT_BMP "font.bmp"
3030
31 assets_t assets;
31 assets_t *assets = NULL;
3232
3333 bool Asset_Init(void)
3434 {
35 char *path = (char *)SDL_malloc(sizeof(char[PATH_MAX]));
36
37 assets.basepath = SDL_GetBasePath();
38 if (NULL != assets.basepath) {
39 fprintf(stderr, "SDL_GetBasePath() returned NULL, using current directory.\n");
40 assets.basepath = SDL_strdup("./");
41 }
42 assets.icon = NULL;
43 assets.font = NULL;
44 assets.charset40 = NULL;
45 assets.splash = NULL;
46
47 snprintf(path, PATH_MAX, "%s%s", assets.basepath, ASSET_ICON_BMP);
48 assets.icon = SDL_LoadBMP(path);
49 if (NULL == assets.icon) {
50 fprintf(stderr, "Unable to locate required asset " ASSET_ICON_BMP "\n");
35 assets = (assets_t *)SDL_calloc(1, sizeof(assets_t));
36 if (NULL == assets) {
37 fprintf(stderr, "Asset_Init: Allocating assets: %s\n", SDL_GetError());
5138 return false;
5239 }
5340
54 snprintf(path, PATH_MAX, "%s%s", assets.basepath, ASSET_FONT_BMP);
55 assets.font = SDL_LoadBMP(path);
56 if (NULL == assets.font) {
57 fprintf(stderr, "Unable to locate required asset " ASSET_FONT_BMP "\n");
41 char *path = (char *)SDL_malloc(sizeof(char[PATH_MAX]));
42 if (NULL == path) {
43 fprintf(stderr, "Asset_Init: Allocating path: %s\n", SDL_GetError());
5844 return false;
5945 }
6046
61 snprintf(path, PATH_MAX, "%s%s", assets.basepath, ASSET_CHARSET40_BMP);
62 assets.charset40 = SDL_LoadBMP(path);
63 if (NULL == assets.charset40) {
64 fprintf(stderr, "Unable to locate required asset " ASSET_CHARSET40_BMP "\n");
47 assets->basepath = SDL_GetBasePath();
48 if (NULL == assets->basepath) {
49 fprintf(stderr, "Asset_Init: Warning: SDL_GetBasePath() returned NULL, using \"./\"\n");
50 assets->basepath = SDL_strdup("./");
51 }
52
53 snprintf(path, PATH_MAX, "%s%s", assets->basepath, ASSET_ICON_BMP);
54 assets->icon = SDL_LoadBMP(path);
55 if (NULL == assets->icon) {
56 fprintf(stderr, "Error loading required asset: %s\n", SDL_GetError());
6557 return false;
6658 }
6759
68 snprintf(path, PATH_MAX, "%s%s", assets.basepath, ASSET_SPLASH_BMP);
69 assets.splash = SDL_LoadBMP(path);
70 if (NULL == assets.splash) {
71 fprintf(stderr, "Unable to locate required asset " ASSET_SPLASH_BMP "\n");
60 snprintf(path, PATH_MAX, "%s%s", assets->basepath, ASSET_FONT_BMP);
61 assets->font = SDL_LoadBMP(path);
62 if (NULL == assets->font) {
63 fprintf(stderr, "Error loading required asset: %s\n", SDL_GetError());
64 return false;
65 }
66
67 snprintf(path, PATH_MAX, "%s%s", assets->basepath, ASSET_CHARSET40_BMP);
68 assets->charset40 = SDL_LoadBMP(path);
69 if (NULL == assets->charset40) {
70 fprintf(stderr, "Error loading required asset: %s\n", SDL_GetError());
71 return false;
72 }
73
74 snprintf(path, PATH_MAX, "%s%s", assets->basepath, ASSET_SPLASH_BMP);
75 assets->splash = SDL_LoadBMP(path);
76 if (NULL == assets->splash) {
77 fprintf(stderr, "Error loading required asset: %s\n", SDL_GetError());
7278 return false;
7379 }
7480
7884
7985 void Asset_Quit(void)
8086 {
81 if (NULL != assets.icon) {
82 SDL_FreeSurface(assets.icon);
83 assets.icon = NULL;
87 if (NULL != assets) {
88 if (NULL != assets->icon) {
89 SDL_FreeSurface(assets->icon);
90 assets->icon = NULL;
91 }
92
93 if (NULL != assets->font) {
94 SDL_FreeSurface(assets->font);
95 assets->font = NULL;
96 }
97
98 if (NULL != assets->charset40) {
99 SDL_FreeSurface(assets->charset40);
100 assets->charset40 = NULL;
101 }
102
103 if (NULL != assets->splash) {
104 SDL_FreeSurface(assets->splash);
105 assets->splash = NULL;
106 }
107
108 if (NULL != assets->basepath) {
109 SDL_free(assets->basepath);
110 assets->basepath = NULL;
111 }
112
113 SDL_free(assets);
84114 }
85
86 if (NULL != assets.font) {
87 SDL_FreeSurface(assets.font);
88 assets.font = NULL;
89 }
90
91 if (NULL != assets.charset40) {
92 SDL_FreeSurface(assets.charset40);
93 assets.charset40 = NULL;
94 }
95
96 if (NULL != assets.splash) {
97 SDL_FreeSurface(assets.splash);
98 assets.splash = NULL;
99 }
100
101 SDL_free(assets.basepath);
102115 }
447447
448448 bool fonts_initialization(void)
449449 {
450 if(!assets.font) {
450 if(!assets->font) {
451451 return false;
452452 }
453 font_sfc = SDL_DisplayFormat(assets.font);
453 font_sfc = SDL_DisplayFormat(assets->font);
454454
455455 /* Transparant color is BLACK: */
456456 SDL_SetColorKey(font_sfc,SDL_SRCCOLORKEY,SDL_MapRGB(font_sfc->format,0,0,0));