Browse Source

Implement lazy initialisation of TileSet objects.

legacy
Sam Hocevar sam 14 years ago
parent
commit
5a17077940
6 changed files with 87 additions and 113 deletions
  1. +1
    -1
      src/asset.h
  2. +6
    -3
      src/editor.cpp
  3. +29
    -83
      src/gtkvideo.cpp
  4. +5
    -2
      src/test-map.cpp
  5. +41
    -23
      src/tileset.cpp
  6. +5
    -1
      src/tileset.h

+ 1
- 1
src/asset.h View File

@@ -22,7 +22,7 @@ public:
virtual void Ref();
virtual int Unref();

private:
protected:
int index, ref, destroy;
};



+ 6
- 3
src/editor.cpp View File

@@ -4,6 +4,7 @@
#include <math.h>

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

#include <math.h>
@@ -56,8 +57,6 @@ int main(int argc, char **argv)
gtk_widget_show(GTK_WIDGET(glarea));
gtk_widget_show(GTK_WIDGET(window));

while (g_main_context_iteration(NULL, FALSE));
if (gtk_gl_area_make_current(GTK_GL_AREA(glarea))) fprintf(stderr, "OK\n");
Game *game = new Game("maps/testmap.tmx");

for (;;)
@@ -68,8 +67,12 @@ if (gtk_gl_area_make_current(GTK_GL_AREA(glarea))) fprintf(stderr, "OK\n");
if (quit)
break;

video->PreRender();
game->SetMouse(0, 0);
Ticker::TickGame(33.33333f);

video->PreRender();
Ticker::TickRender(33.33333f);

game->Render();
video->PostRender(33.33333f);
}


+ 29
- 83
src/gtkvideo.cpp View File

@@ -26,6 +26,30 @@ class GtkVideoData
friend class GtkVideo;

private:
void SetupView()
{
glViewport(0, 0, widget->allocation.width,
widget->allocation.height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, widget->allocation.width,
widget->allocation.height, 0, -1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

static gint init(GtkWidget *widget)
{
GtkVideoData *data = (GtkVideoData *)
@@ -34,32 +58,7 @@ private:
/* OpenGL functions can be called only if make_current returns true */
if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
{
glViewport(0, 0, widget->allocation.width,
widget->allocation.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,100, 100,0, -1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, data->widget->allocation.width,
data->widget->allocation.height, 0, -1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

data->SetupView();
}
return TRUE;
}
@@ -72,18 +71,6 @@ private:
if (event->count == 0 && data->drawing == 2
&& gtk_gl_area_make_current(GTK_GL_AREA(widget)))
{
#if 0
/* Draw simple triangle */
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glBegin(GL_TRIANGLES);
glVertex2f(10,10);
glVertex2f(10,90);
glVertex2f(90,90);
glEnd();
#endif

data->drawing = 0;

/* Swap backbuffer to front */
@@ -100,27 +87,7 @@ private:

if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
{
glViewport(0,0, widget->allocation.width,
widget->allocation.height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, data->widget->allocation.width,
data->widget->allocation.height, 0, -1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

data->SetupView();
}
return TRUE;
}
@@ -169,28 +136,7 @@ GtkVideo::GtkVideo(char const *title, int width, int height)
GTK_SIGNAL_FUNC(GtkVideoData::init), NULL);

// FIXME: is this needed?
gtk_widget_set_usize(GTK_WIDGET(data->widget), 100, 100);

/* Initialise OpenGL */
glViewport(0, 0, data->widget->allocation.width,
data->widget->allocation.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, data->widget->allocation.width,
data->widget->allocation.height, 0, -1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gtk_widget_set_usize(GTK_WIDGET(data->widget), 400, 300);
}

void *GtkVideo::GetWidget()
@@ -210,8 +156,8 @@ int GtkVideo::GetHeight() const

void GtkVideo::PreRender()
{
/// XXX: is this right?
gtk_gl_area_make_current(GTK_GL_AREA(data->widget));
/// XXX: is this right?
gtk_gl_area_make_current(GTK_GL_AREA(data->widget));

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();


+ 5
- 2
src/test-map.cpp View File

@@ -7,6 +7,7 @@

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

int main(int argc, char **argv)
{
@@ -15,13 +16,15 @@ int main(int argc, char **argv)

for (int done = 0; !done; )
{
video->PreRender();

/* Test stuff */
int mx, my;
SDL_GetMouseState(&mx, &my);
game->SetMouse(mx * (640 - 32) / 640, my * (480 - 32) / 480);

Ticker::TickGame(33.33333f);

video->PreRender();
Ticker::TickRender(33.33333f);
game->Render();
video->PostRender(33.33333f);



+ 41
- 23
src/tileset.cpp View File

@@ -31,7 +31,7 @@ private:
int ntiles;

SDL_Surface *img;
GLuint texture[1];
GLuint texture;
};

/*
@@ -45,6 +45,7 @@ TileSet::TileSet(char const *path)
data->tiles = NULL;
data->ntiles = 0;
data->img = NULL;
data->texture = 0;

for (char const *name = path; *name; name++)
if ((data->img = IMG_Load(name)))
@@ -55,26 +56,40 @@ TileSet::TileSet(char const *path)
SDL_Quit();
exit(1);
}

glGenTextures(1, data->texture);
glBindTexture(GL_TEXTURE_2D, data->texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}

TileSet::~TileSet()
{
glDeleteTextures(1, data->texture);

free(data->tiles);
free(data->name);
delete data;
}

void TileSet::TickRender(float delta_time)
{
Asset::TickRender(delta_time);

if (data->img)
{
glGenTextures(1, &data->texture);
glBindTexture(GL_TEXTURE_2D, data->texture);

glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

SDL_FreeSurface(data->img);
data->img = NULL;
}
else if (ref == 0)
{
glDeleteTextures(1, &data->texture);
destroy = 1;
}
}

char const *TileSet::GetName()
{
return data->name;
@@ -85,16 +100,19 @@ void TileSet::BlitTile(uint32_t id, int x, int y)
float tx = .0625f * (id & 0xf);
float ty = .0625f * ((id >> 4) & 0xf);

glBindTexture(GL_TEXTURE_2D, data->texture[0]);
glBegin(GL_QUADS);
glTexCoord2f(tx, ty);
glVertex2f(x, y);
glTexCoord2f(tx + .0625f, ty);
glVertex2f(x + 32, y);
glTexCoord2f(tx + .0625f, ty + .0625f);
glVertex2f(x + 32, y + 32);
glTexCoord2f(tx, ty + .0625f);
glVertex2f(x, y + 32);
glEnd();
if (!data->img)
{
glBindTexture(GL_TEXTURE_2D, data->texture);
glBegin(GL_QUADS);
glTexCoord2f(tx, ty);
glVertex2f(x, y);
glTexCoord2f(tx + .0625f, ty);
glVertex2f(x + 32, y);
glTexCoord2f(tx + .0625f, ty + .0625f);
glVertex2f(x + 32, y + 32);
glTexCoord2f(tx, ty + .0625f);
glVertex2f(x, y + 32);
glEnd();
}
}


+ 5
- 1
src/tileset.h View File

@@ -16,8 +16,12 @@ class TileSet : public Asset
{
public:
TileSet(char const *path);
~TileSet();
virtual ~TileSet();

/* Inherited from Asset */
virtual void TickRender(float delta_time);

/* New implementations */
char const *GetName();

void BlitTile(uint32_t id, int x, int y);


Loading…
Cancel
Save