Browse Source

Implement a naive garbage collector.

legacy
Sam Hocevar sam 14 years ago
parent
commit
4d005a7dbb
9 changed files with 51 additions and 37 deletions
  1. +2
    -2
      src/asset.cpp
  2. +2
    -1
      src/asset.h
  3. +7
    -7
      src/editor.cpp
  4. +2
    -2
      src/gtkvideo.cpp
  5. +0
    -1
      src/map.cpp
  6. +3
    -3
      src/test-map.cpp
  7. +30
    -15
      src/ticker.cpp
  8. +3
    -4
      src/tiler.cpp
  9. +2
    -2
      src/tileset.cpp

+ 2
- 2
src/asset.cpp View File

@@ -1,5 +1,5 @@

#include <malloc.h>
#include <cstdlib>

#include "asset.h"
#include "ticker.h"
@@ -9,7 +9,7 @@
*/

Asset::Asset() :
index(0),
next(0),
ref(0),
destroy(0)
{


+ 2
- 1
src/asset.h View File

@@ -23,7 +23,8 @@ public:
virtual int Unref();

protected:
int index, ref, destroy;
Asset *next;
int ref, destroy;
};

#endif // __DH_ASSET_H__


+ 7
- 7
src/editor.cpp View File

@@ -1,17 +1,17 @@
#include <SDL.h>

#include <stdio.h>
#include <math.h>
#include <cstdio>
#include <cmath>

#include "gtkvideo.h"
#include "ticker.h"
#include "game.h"
#include <SDL.h>

#include <math.h>
#include <gtk/gtk.h>
#include <gtkgl/gtkglarea.h>
#include <GL/gl.h>

#include "gtkvideo.h"
#include "ticker.h"
#include "game.h"

volatile int quit = 0;

static gint main_quit(GtkWidget *widget, GdkEventExpose *event)


+ 2
- 2
src/gtkvideo.cpp View File

@@ -1,4 +1,6 @@

#include <cstdlib>

#include <gtk/gtk.h>
#include <gtkgl/gtkglarea.h>

@@ -13,8 +15,6 @@
# include <GL/gl.h>
#endif

#include <stdlib.h>

#include "gtkvideo.h"

/*


+ 0
- 1
src/map.cpp View File

@@ -2,7 +2,6 @@
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <malloc.h>
#include <ctype.h>

#include "map.h"


+ 3
- 3
src/test-map.cpp View File

@@ -1,9 +1,9 @@
// Test stuff

#include <SDL.h>
#include <cstdio>
#include <cmath>

#include <stdio.h>
#include <math.h>
#include <SDL.h>

#include "sdlvideo.h"
#include "game.h"


+ 30
- 15
src/ticker.cpp View File

@@ -3,8 +3,9 @@
* The ticker
*/

#include <cstdlib>
#include <cstdio>
#include <stdint.h>
#include <stdlib.h>

#include "ticker.h"
#include "asset.h"
@@ -19,19 +20,19 @@ static class TickerData

public:
TickerData() :
assets(0),
list(0),
nassets(0)
{
/* Nothing to do */
}

~TickerData()
{
free(assets);
if (nassets)
fprintf(stderr, "ERROR: still %i assets in ticker\n", nassets);
}

private:
Asset **assets;
Asset *list;
int nassets;
}
tickerdata;
@@ -44,23 +45,37 @@ static TickerData * const data = &tickerdata;

void Ticker::Register(Asset *asset)
{
int tmp = data->nassets++;
data->assets = (Asset **)realloc(data->assets,
data->nassets * sizeof(Asset *));
data->assets[tmp] = asset;

asset->index = tmp;
asset->next = data->list;
data->list = asset;
data->nassets++;
}

void Ticker::TickGame(float delta_time)
{
for (int i = 0; i < data->nassets; i++)
data->assets[i]->TickGame(delta_time);
/* Garbage collect objects that can be destroyed */
for (Asset *asset = data->list, *prev = NULL;
asset;
prev = asset, asset = asset->next)
if (asset->destroy)
{
if (prev)
prev->next = asset->next;
else
data->list = asset->next;

data->nassets--;
delete asset;
}

/* Tick objects for the game loop */
for (Asset *asset = data->list; asset; asset = asset->next)
asset->TickGame(delta_time);
}

void Ticker::TickRender(float delta_time)
{
for (int i = 0; i < data->nassets; i++)
data->assets[i]->TickRender(delta_time);
/* Tick objects for the render loop */
for (Asset *asset = data->list; asset; asset = asset->next)
asset->TickRender(delta_time);
}


+ 3
- 4
src/tiler.cpp View File

@@ -1,7 +1,7 @@

#include <cstring>
#include <cstdio>
#include <stdlib.h>
#include <cstdlib>

#include "tiler.h"
#include "tileset.h"
@@ -28,6 +28,8 @@ public:

~TilerData()
{
if (ntilesets)
fprintf(stderr, "ERROR: still %i tilesets in tiler\n", ntilesets);
free(tilesets);
}

@@ -81,10 +83,7 @@ void Tiler::Deregister(int id)
--id; /* ID 0 is for the empty tileset */

if (data->tilesets[id]->Unref() == 0)
{
delete data->tilesets[id];
data->tilesets[id] = NULL;
}
}

void Tiler::Render(uint32_t code, int x, int y)


+ 2
- 2
src/tileset.cpp View File

@@ -1,4 +1,6 @@

#include <cstdlib>

#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
@@ -13,8 +15,6 @@
#include <SDL.h>
#include <SDL_image.h>

#include <malloc.h>

#include "tileset.h"

/*


Loading…
Cancel
Save