Store user data in $HOME
[kball.git] / src / backgbmp.cpp
blob34d66fc12ed5ee7231612f1c4009c97959cbdb60
1 // ------------------------------------------------------------------
2 // backgbmp.cpp
3 // System for handling level backgrounds
4 // ------------------------------------------------------------------
5 // By Kronoman - In loving memory of my father
6 // Copyright (c) 2004, Kronoman
7 // ------------------------------------------------------------------
9 #include "backgbmp.h"
10 #include "gerror.h"
11 #include "filehelp.h"
13 CBackground::CBackground()
15 bmp_in_ram = NULL;
16 bmp_default = NULL;
17 data_loaded = NULL;
18 free_memory();
21 CBackground::~CBackground()
23 free_memory();
25 if (bmp_default != NULL)
26 destroy_bitmap(bmp_default);
29 BITMAP *CBackground::get_background(char *filename, int index)
31 char str[1024];
32 DATAFILE *data = NULL;
33 char tmp_file_buf[2048];
35 where_is_the_filename(tmp_file_buf, filename);
37 // use the cache if it is present
38 if (index == index_loaded)
40 if (ustrcmp(tmp_file_buf, file_loaded) == 0)
41 return bmp_in_ram;
44 free_memory();
46 usprintf(str, "B%d_BMP", index);
47 data = load_datafile_object(tmp_file_buf, str);
49 if (data != NULL)
51 usprintf(file_loaded, "%s", tmp_file_buf);
52 index_loaded = index;
54 data_loaded = data;
55 bmp_in_ram = (BITMAP *)data->dat;
56 return bmp_in_ram;
60 return bmp_default;
63 void CBackground::free_memory()
65 if (data_loaded != NULL)
66 unload_datafile_object(data_loaded);
68 data_loaded = NULL;
70 bmp_in_ram = NULL;
72 if (bmp_default != NULL)
73 destroy_bitmap(bmp_default);
75 bmp_default = create_bitmap(16, 16);
77 if (bmp_default == NULL)
78 raise_error("* FATAL ERROR * \nCBackgrounds::free_memory()\n\tCan't create default bitmap!");
80 clear(bmp_default);
82 file_loaded[0] = '\0';
84 index_loaded = -1;