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
27 | 27 | SDL_Surface *splash; |
28 | 28 | } assets_t; |
29 | 29 | |
30 | extern assets_t assets; | |
30 | extern assets_t *assets; | |
31 | 31 | |
32 | 32 | bool Asset_Init(void); |
33 | 33 | void Asset_Quit(void); |
790 | 790 | |
791 | 791 | int main(int argc, char * lpCmdLine[]) |
792 | 792 | { |
793 | Asset_Init(); | |
793 | if (!Asset_Init()) { | |
794 | return 1; | |
795 | } | |
794 | 796 | |
795 | 797 | // GPH: The very first thing we do is attempt to grab the needed configuration files and put them in the user's folder. |
796 | 798 | Config config; |
563 | 563 | |
564 | 564 | rectangle(screen, 1, 1, /*SCREEN_WIDTH*/g_ScreenWidth - 2, (Help_TopX - 8), SDL_MapRGB(screen->format, 255, 255, 0)); |
565 | 565 | |
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); | |
568 | 568 | SDL_Rect logo, scrr; |
569 | 569 | logo.x = logo.y = 0; |
570 | 570 | logo.w = tempSurface->w; |
1241 | 1241 | // SDL ref: Icon should be set *before* the first call to SDL_SetVideoMode. |
1242 | 1242 | // Uint32 colorkey; |
1243 | 1243 | |
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); | |
1255 | 1255 | } |
1256 | 1256 | ////////////////////////////////////////////////////////////////////// |
1257 | 1257 | return 0; |
1894 | 1894 | ZeroMemory(vidlastmem,0x10000); |
1895 | 1895 | |
1896 | 1896 | // LOAD THE splash screen |
1897 | g_hLogoBitmap = SDL_DisplayFormat(assets.splash); | |
1897 | g_hLogoBitmap = SDL_DisplayFormat(assets->splash); | |
1898 | 1898 | |
1899 | 1899 | // LOAD APPLE CHARSET40 |
1900 | charset40 = SDL_DisplayFormat(assets.charset40); | |
1900 | charset40 = SDL_DisplayFormat(assets->charset40); | |
1901 | 1901 | |
1902 | 1902 | // CREATE AN IDENTITY PALETTE AND FILL IN THE CORRESPONDING COLORS IN |
1903 | 1903 | // THE BITMAPINFO STRUCTURE |
28 | 28 | #define ASSET_CHARSET40_BMP "charset40.bmp" |
29 | 29 | #define ASSET_FONT_BMP "font.bmp" |
30 | 30 | |
31 | assets_t assets; | |
31 | assets_t *assets = NULL; | |
32 | 32 | |
33 | 33 | bool Asset_Init(void) |
34 | 34 | { |
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()); | |
51 | 38 | return false; |
52 | 39 | } |
53 | 40 | |
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()); | |
58 | 44 | return false; |
59 | 45 | } |
60 | 46 | |
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()); | |
65 | 57 | return false; |
66 | 58 | } |
67 | 59 | |
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()); | |
72 | 78 | return false; |
73 | 79 | } |
74 | 80 | |
78 | 84 | |
79 | 85 | void Asset_Quit(void) |
80 | 86 | { |
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); | |
84 | 114 | } |
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); | |
102 | 115 | } |
447 | 447 | |
448 | 448 | bool fonts_initialization(void) |
449 | 449 | { |
450 | if(!assets.font) { | |
450 | if(!assets->font) { | |
451 | 451 | return false; |
452 | 452 | } |
453 | font_sfc = SDL_DisplayFormat(assets.font); | |
453 | font_sfc = SDL_DisplayFormat(assets->font); | |
454 | 454 | |
455 | 455 | /* Transparant color is BLACK: */ |
456 | 456 | SDL_SetColorKey(font_sfc,SDL_SRCCOLORKEY,SDL_MapRGB(font_sfc->format,0,0,0)); |