aboutsummaryrefslogtreecommitdiff
path: root/modules/libgrbase/g_grlib.c
blob: fdb4fe589893d064c94f399040f6a06c7c613faa (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 *  Copyright � 2006-2016 SplinterGU (Fenix/Bennugd)
 *  Copyright � 2002-2006 Fenix Team (Fenix)
 *  Copyright � 1999-2002 Jos� Luis Cebri�n Pag�e (Fenix)
 *
 *  This file is part of Bennu - Game Development
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *     2. Altered source versions must be plainly marked as such, and must not be
 *     misrepresented as being the original software.
 *
 *     3. This notice may not be removed or altered from any source
 *     distribution.
 *
 */

/* --------------------------------------------------------------------------- */

#include <stdlib.h>
#include <string.h>

#include "libgrbase.h"
#include "bitwise_map.h"

/* --------------------------------------------------------------------------- */

GRLIB * syslib = 0 ;

static uint32_t * libs_bmp = NULL ;
static int libs_allocated = 0 ;
static int libs_last = 0;
static GRLIB ** libs = NULL;

/* --------------------------------------------------------------------------- */

static GRLIB * grlib_create() ;

/* --------------------------------------------------------------------------- */

int grlib_newid()
{
    int n, nb, lim, ini ;

    // Si tengo suficientes alocados, retorno el siguiente segun libs_last
    if ( libs_last < libs_allocated )
    {
        if ( !bit_tst( libs_bmp, libs_last ) )
        {
            bit_set( libs_bmp, libs_last );
            return libs_last++ ;
        }
    }

    // Ya no tengo mas espacio, entonces busco alguno libre entre ~+32 desde el ultimo fijo y ~-32 del ultimo asignado

    ini = ( libs_last < libs_allocated ) ? ( libs_last >> 5 ) : 0 ;
    lim = ( libs_allocated >> 5 ) ;

    while ( 1 )
    {
        for ( n = ini; n < lim ; n++ )
        {
            if ( libs_bmp[n] != ( uint32_t ) 0xFFFFFFFF ) // Aca hay 1 libre, busco cual es
            {
                for ( nb = 0; nb < 32; nb++ )
                {
                    if ( !bit_tst( libs_bmp + n, nb ) )
                    {
                        libs_last = ( n << 5 ) + nb ;
                        bit_set( libs_bmp, libs_last );
                        return libs_last++ ;
                    }
                }
            }
        }
        if ( !ini ) break;
        lim = ini;
        ini = 0;
    }

    libs_last = libs_allocated ;

    /* Increment space, no more slots availables
       256 new maps available for alloc */

    libs_allocated += 256 ;
    libs_bmp = ( uint32_t * ) realloc( libs_bmp, sizeof( uint32_t ) * ( libs_allocated >> 5 ) );
    memset( &libs_bmp[( libs_last >> 5 )], 0, 32 );  /* 256 >> 5 = 8 * sizeof ( uint32_t ) = 8 * 4 = 32 */

    libs = ( GRLIB ** ) realloc( libs, sizeof( GRLIB * ) * libs_allocated );
    memset( &libs[ libs_last ], 0, sizeof( GRLIB * ) * 256 );

    /* Devuelvo libs_last e incremento en 1, ya que ahora tengo BLOCK_INCR mas que antes */
    bit_set( libs_bmp, libs_last );

    return libs_last++ ;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : grlib_new
 *
 *  Create a new library
 *
 *  PARAMS :
 *  None
 *
 *  RETURN VALUE :
 *      ID of the new library or -1 if error
 */

int grlib_new()
{
    GRLIB * lib = grlib_create() ;
    int i ;

    if ( !lib ) return -1;

    i = grlib_newid();
    libs[ i ] = lib;

    return ( i );
}

/* --------------------------------------------------------------------------- */
/* Static convenience function */

static GRLIB * grlib_create()
{
    GRLIB * lib ;

    lib = ( GRLIB * ) malloc( sizeof( GRLIB ) ) ;
    if ( !lib ) return 0 ;

    lib->maps = ( GRAPH ** ) calloc( 32, sizeof( GRAPH * ) ) ;
    if ( !lib->maps )
    {
        free( lib );
        return 0 ;
    }

    lib->name[ 0 ] = 0 ;
    lib->map_reserved = 32 ;

    return lib ;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : grlib_get
 *
 *  Get a pointer to an library in memory given the internal ID
 *
 *  PARAMS :
 *  libid   Library code
 *
 *  RETURN VALUE :
 *      Pointer to the object required or NULL if it does not exist
 */

GRLIB * grlib_get( int libid )
{
    if ( libid < 0 || libid >= libs_allocated ) return 0 ;
    return libs[ libid ] ;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : grlib_destroy
 *
 *  Remove a library and all its maps from memory
 *
 *  PARAMS :
 *  libid   ID of the library
 *
 *  RETURN VALUE :
 *      0 if there wasn't a map with this code, 1 otherwise
 */

void grlib_destroy( int libid )
{
    int i ;
    GRLIB * lib = grlib_get( libid ) ;
    if ( !lib ) return ;

    libs[ libid ] = 0 ;

    for ( i = 0; i < lib->map_reserved; i++ ) bitmap_destroy( lib->maps[ i ] ) ;

    free( lib->maps ) ;
    free( lib ) ;

    bit_clr( libs_bmp, libid );
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : grlib_unload_map
 *
 *  Remove a map from a library, if present
 *
 *  PARAMS :
 *  libid   ID of the library
 *  mapcode   ID of the map
 *
 *  RETURN VALUE :
 *      0 if there wasn't a map with this code, 1 otherwise
 */

int grlib_unload_map( int libid, int mapcode )
{
    GRLIB * lib ;

    if ( mapcode < 1 || mapcode > 999 )
        lib = syslib ;
    else
        lib = grlib_get( libid ) ;

    if ( lib == NULL ) return 0 ;

    if ( lib->map_reserved <= mapcode ) return 0 ;
    if ( !lib->maps[ mapcode ] ) return 0 ;

    bitmap_destroy( lib->maps[ mapcode ] ) ;
    lib->maps[ mapcode ] = 0 ;
    return 1 ;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : grlib_add_map
 *
 *  Add a map to a graphic library. If any map with the same code
 *  is in the library, it will be unload first
 *
 *  PARAMS :
 *  lib    Pointer to the library
 *  map    Pointer to the graphic
 *         (fill map->code first!)
 *
 *  RETURN VALUE :
 *      -1 if error, a number >= 0 otherwise
 */

int grlib_add_map( int libid, GRAPH * map )
{
    GRLIB * lib ;

    if ( map->code < 1 || map->code > 999 )
        lib = syslib ;
    else
        lib = grlib_get( libid ) ;

    if ( !lib ) return -1 ;
    if ( map->code < 0 ) return -1 ;

    if ( map->code > 0 ) grlib_unload_map( libid, map->code ) ;

    if ( lib->map_reserved <= map->code )
    {
        GRAPH ** lmaps;
        int new_reserved = ( map->code & ~0x001F ) + 32 ;

        lmaps = ( GRAPH ** ) realloc( lib->maps, sizeof( GRAPH* ) * new_reserved ) ;
        if ( !lmaps ) return -1; // No memory
        lib->maps = lmaps;

        memset( lib->maps + lib->map_reserved, 0, ( new_reserved - lib->map_reserved ) * sizeof( GRAPH * ) ) ;
        lib->map_reserved = new_reserved ;
    }

    lib->maps[ map->code ] = map ;

    return map->code ;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : bitmap_get
 *
 *  Get a bitmap using the DIV syntax (libcode, mapcode)
 *
 *  PARAMS :
 *  libid   Library code or 0 for system/global bitmap
 *  mapcode   Code of the bitmap
 *
 *  RETURN VALUE :
 *      Pointer to the graphic required or NULL if not found
 *
 *  (0, -1) gets the scrbitmap graphic (undocumented!)
 *  (0,  0) gets the background
 *
 */

GRAPH * bitmap_get( int libid, int mapcode )
{
    GRLIB * lib = NULL ;

    if ( !libid )
    {
        /* Using (0, 0) we can get the background map */
        if ( !mapcode ) return background ;

        /* Using (0, -1) we can get the screen bitmap (undocumented bug/feature) */
        if ( mapcode == -1 ) return scrbitmap ;
    }

    /* Get the map from the system library
     * (the only one that can have more than 1000 maps)
     */
    if ( mapcode > 999 ) lib = syslib ;

    if ( !lib ) lib = grlib_get( libid ) ;

    /* Get the map from a library */

    if ( lib && lib->map_reserved > mapcode && mapcode >= 0 ) return lib->maps[ mapcode ] ;

    return 0 ;
}

/* --------------------------------------------------------------------------- */
/*
 *  FUNCTION : grlib_init
 *
 *  Create the system library. This should be called at program startup.
 *
 *  PARAMS :
 *  None
 *
 *  RETURN VALUE :
 *      None
 *
 */

void grlib_init()
{
    if ( !syslib ) syslib = grlib_create() ;
}

/* --------------------------------------------------------------------------- */