Changeset 98


Ignore:
Timestamp:
Aug 11, 2010, 6:18:38 PM (10 years ago)
Author:
sam
Message:

Implement a naive garbage collector.

Location:
trunk/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/asset.cpp

    r94 r98  
    11
    2 #include <malloc.h>
     2#include <cstdlib>
    33
    44#include "asset.h"
     
    1010
    1111Asset::Asset() :
    12     index(0),
     12    next(0),
    1313    ref(0),
    1414    destroy(0)
  • trunk/src/asset.h

    r96 r98  
    2424
    2525protected:
    26     int index, ref, destroy;
     26    Asset *next;
     27    int ref, destroy;
    2728};
    2829
  • trunk/src/editor.cpp

    r96 r98  
     1
     2#include <cstdio>
     3#include <cmath>
     4
    15#include <SDL.h>
    26
    3 #include <stdio.h>
    4 #include <math.h>
     7#include <gtk/gtk.h>
     8#include <gtkgl/gtkglarea.h>
     9#include <GL/gl.h>
    510
    611#include "gtkvideo.h"
    712#include "ticker.h"
    813#include "game.h"
    9 
    10 #include <math.h>
    11 #include <gtk/gtk.h>
    12 #include <gtkgl/gtkglarea.h>
    13 #include <GL/gl.h>
    1414
    1515volatile int quit = 0;
  • trunk/src/gtkvideo.cpp

    r96 r98  
     1
     2#include <cstdlib>
    13
    24#include <gtk/gtk.h>
     
    1315#   include <GL/gl.h>
    1416#endif
    15 
    16 #include <stdlib.h>
    1717
    1818#include "gtkvideo.h"
  • trunk/src/map.cpp

    r79 r98  
    33#include <cstring>
    44#include <cstdlib>
    5 #include <malloc.h>
    65#include <ctype.h>
    76
  • trunk/src/test-map.cpp

    r96 r98  
    11// Test stuff
    22
     3#include <cstdio>
     4#include <cmath>
     5
    36#include <SDL.h>
    4 
    5 #include <stdio.h>
    6 #include <math.h>
    77
    88#include "sdlvideo.h"
  • trunk/src/ticker.cpp

    r94 r98  
    44 */
    55
     6#include <cstdlib>
     7#include <cstdio>
    68#include <stdint.h>
    7 #include <stdlib.h>
    89
    910#include "ticker.h"
     
    2021public:
    2122    TickerData() :
    22         assets(0),
     23        list(0),
    2324        nassets(0)
    2425    {
    25         /* Nothing to do */
    2626    }
    2727
    2828    ~TickerData()
    2929    {
    30         free(assets);
     30        if (nassets)
     31            fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets);
    3132    }
    3233
    3334private:
    34     Asset **assets;
     35    Asset *list;
    3536    int nassets;
    3637}
     
    4546void Ticker::Register(Asset *asset)
    4647{
    47     int tmp = data->nassets++;
    48     data->assets = (Asset **)realloc(data->assets,
    49                                      data->nassets * sizeof(Asset *));
    50     data->assets[tmp] = asset;
    51 
    52     asset->index = tmp;
     48    asset->next = data->list;
     49    data->list = asset;
     50    data->nassets++;
    5351}
    5452
    5553void Ticker::TickGame(float delta_time)
    5654{
    57     for (int i = 0; i < data->nassets; i++)
    58         data->assets[i]->TickGame(delta_time);
     55    /* Garbage collect objects that can be destroyed */
     56    for (Asset *asset = data->list, *prev = NULL;
     57         asset;
     58         prev = asset, asset = asset->next)
     59        if (asset->destroy)
     60        {
     61            if (prev)
     62                prev->next = asset->next;
     63            else
     64                data->list = asset->next;
     65
     66            data->nassets--;
     67            delete asset;
     68        }
     69
     70    /* Tick objects for the game loop */
     71    for (Asset *asset = data->list; asset; asset = asset->next)
     72        asset->TickGame(delta_time);
    5973}
    6074
    6175void Ticker::TickRender(float delta_time)
    6276{
    63     for (int i = 0; i < data->nassets; i++)
    64         data->assets[i]->TickRender(delta_time);
     77    /* Tick objects for the render loop */
     78    for (Asset *asset = data->list; asset; asset = asset->next)
     79        asset->TickRender(delta_time);
    6580}
    6681
  • trunk/src/tiler.cpp

    r81 r98  
    22#include <cstring>
    33#include <cstdio>
    4 #include <stdlib.h>
     4#include <cstdlib>
    55
    66#include "tiler.h"
     
    2929    ~TilerData()
    3030    {
     31        if (ntilesets)
     32            fprintf(stderr, "ERROR: still %i tilesets in tiler\n", ntilesets);
    3133        free(tilesets);
    3234    }
     
    8284
    8385    if (data->tilesets[id]->Unref() == 0)
    84     {
    85         delete data->tilesets[id];
    8686        data->tilesets[id] = NULL;
    87     }
    8887}
    8988
  • trunk/src/tileset.cpp

    r96 r98  
     1
     2#include <cstdlib>
    13
    24#ifdef WIN32
     
    1315#include <SDL.h>
    1416#include <SDL_image.h>
    15 
    16 #include <malloc.h>
    1717
    1818#include "tileset.h"
Note: See TracChangeset for help on using the changeset viewer.