summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Thomas <lupine@nick.lupine.me.uk>2007-04-04 14:10:27 +0100
committerNicholas Thomas <lupine@nick.lupine.me.uk>2007-04-04 14:10:27 +0100
commitdbc52f670788884713b788436ed1a1d1a93ac1a8 (patch)
tree1565fd369aa64296930df4405720e34ced22517d
parent94238b22a14b27102b297fc059ffa26f43bbd3c3 (diff)
downloadplugin-serenity-dbc52f670788884713b788436ed1a1d1a93ac1a8.tar.gz
plugin-serenity-dbc52f670788884713b788436ed1a1d1a93ac1a8.tar.bz2
More general fixing; texture handling in a sane manner
-rw-r--r--serenity.c117
1 files changed, 84 insertions, 33 deletions
diff --git a/serenity.c b/serenity.c
index 7060ec1..19c0be1 100644
--- a/serenity.c
+++ b/serenity.c
@@ -54,8 +54,6 @@ Don't like it? GTFO ;)
#define NUM_OPTIONS(d) (sizeof ((d)->opt) / sizeof (CompOption))
-static int displayPrivateIndex;
-
typedef struct _SerenityTexture
{
CompTexture *t;
@@ -63,9 +61,12 @@ typedef struct _SerenityTexture
unsigned int width;
Bool loaded;
GLuint list;
+ char *filename;
} SerenityTexture;
+static int displayPrivateIndex;
+
typedef struct _SerenityDisplay
{
CompDisplay *d;
@@ -78,28 +79,45 @@ typedef struct _SerenityScreen
PreparePaintScreenProc preparePaintScreen;
PaintScreenProc paintScreen;
- int object_x; //Top-left corner of the current position of our image
- int object_y;
+ SerenityTexture *currentTex; // What we're drawing right now.
+ int object_x; //Top-left corner of the current position of our image
+ int object_y; //
unsigned int numTextures;
SerenityTexture *textures;
} SerenityScreen;
+// Where all the logic gets done
static void serenityPreparePaintScreen(CompScreen * s, int msSinceLastPaint);
+// Where all the drawing gets done
static Bool serenityPaintScreen(CompScreen * s, const ScreenPaintAttrib * sAttrib, const CompTransform *transform, Region region,
int output, unsigned int mask);
+// Allocates space for numTextures and initialises each texture to the ground state
static Bool serenityInitTextures(SerenityScreen *ss, unsigned int numTextures);
+// Destroys all textures for the screen and does all cleanup.
static void serenityFiniTextures(SerenityScreen *ss);
+//creates a glList for drawing and assigns it to tex->list
static void serenityCreateList(SerenityTexture *tex);
+// Reloads all textures for the given screen
+static void serenityDoTextures(SerenityScreen *ss);
+// Assigns SerenityScreen->currentTex to a random SerenityScreen->textures. Only selects between loaded textures
+static Bool serenitySelectRandomTexture(SerenityScreen *ss);
+// Reloads all textures for the screen whenever the stringlist changes
static void serenityScreenOptionChanged(CompScreen *s, CompOption *opt, SerenityScreenOptions num);
+// TODO: Apply movement, etc here
static void serenityPreparePaintScreen(CompScreen * s, int msSinceLastPaint)
{
+ SERENITY_SCREEN(s);
+ if (ss->numTextures == 0)
+ return;
+ if (ss->currentTex == NULL)
+ serenitySelectRandomTexture(ss);
}
-// TODO: Apply movement, etc here
+// TODO: Draw the texture here
static Bool serenityPaintScreen(CompScreen * s,
const ScreenPaintAttrib * sAttrib,
const CompTransform *transform,
@@ -137,6 +155,7 @@ static Bool serenityInitTextures(SerenityScreen *ss, unsigned int numTextures)
static void serenityFiniTextures(SerenityScreen *ss)
{
unsigned int n;
+ ss->currentTex = NULL;
for (n = 0; n < ss->numTextures; n++)
{
ss->textures[n].loaded = FALSE;
@@ -144,6 +163,9 @@ static void serenityFiniTextures(SerenityScreen *ss)
ss->textures[n].height = 0;
ss->textures[n].width = 0;
};
+ if (ss->textures != NULL)
+ free(ss->textures);
+ ss->numTextures = 0;
}
// TODO: Check I don't fail at making lists.
@@ -166,38 +188,66 @@ static void serenityCreateList(SerenityTexture *tex)
glEnd();
glEndList();
}
-// TODO: Draw the texture here
-static void serenityScreenOptionChanged(CompScreen *s, CompOption *opt, SerenityScreenOptions num)
+
+static void serenityDoTextures(SerenityScreen *ss)
{
unsigned int n;
- SERENITY_SCREEN(s);
- switch (num)
- {
- case SerenityScreenOptionSerenityTextures:
- {
- serenityFiniTextures(ss); // First, destroy everything
- if (serenityInitTextures(ss, opt->value.list.nValue)) // Now recreate everything
- for(n = 0; n < ss->numTextures; n++)
+ CompOption *opt = serenityGetSerenityTexturesOption(ss->s);
+
+ serenityFiniTextures(ss);
+ if (serenityInitTextures(ss, opt->value.list.nValue))
+ for(n = 0; n < ss->numTextures; n++)
+ {
+ SerenityTexture *cTex = &ss->textures[n];
+ cTex->filename = opt->value.list.value[n].s;
+ if (!readImageToTexture(ss->s,
+ cTex->t,
+ cTex->filename,
+ &cTex->width,
+ &cTex->height))
+ // Error condition
+ printf("[serenity]: failed to load image %s\n", opt->value.list.value[n].s);
+ else
{
- SerenityTexture *cTex = &ss->textures[n];
- if (!readImageToTexture(s,
- cTex->t,
- opt->value.list.value[n].s,
- &cTex->width,
- &cTex->height))
- // Error condition
- {
- }
- else
- {
- serenityCreateList(cTex);
- ss->textures[n].loaded = TRUE;
- };
+ serenityCreateList(cTex);
+ ss->textures[n].loaded = TRUE;
};
+ };
+}
+
+static Bool serenitySelectRandomTexture(SerenityScreen *ss)
+{
+ unsigned int n;
+ SerenityTexture *activeTextures[ss->numTextures];
+ unsigned int numActiveTextures = 0;
- };
- break;
- default: break;
+ for (n = 0; n < ss->numTextures; n++)
+ if ((&ss->textures[n])->loaded)
+ {
+ activeTextures[numActiveTextures] = &ss->textures[n];
+ numActiveTextures++;
+ };
+
+ if (numActiveTextures == 0)
+ {
+ ss->currentTex = NULL;
+ return FALSE;
+ };
+
+ ss->currentTex = activeTextures[(int)(rand() / (((double)RAND_MAX + 1) / numActiveTextures))];
+ return TRUE;
+}
+
+static void serenityScreenOptionChanged(CompScreen *s, CompOption *opt, SerenityScreenOptions num)
+{
+ SERENITY_SCREEN(s);
+ switch (num)
+ {
+ case SerenityScreenOptionSerenityTextures:
+ serenityDoTextures(ss);
+ break;
+ default:
+ break;
}
}
@@ -258,7 +308,7 @@ static Bool serenityInitScreen(CompPlugin * p, CompScreen * s)
serenitySetSerenityTexturesNotify(s, serenityScreenOptionChanged);
ss->object_x = 0;
ss->object_y = 0;
-
+ serenityDoTextures(ss);
return TRUE;
}
@@ -267,6 +317,7 @@ static void serenityFiniScreen(CompPlugin * p, CompScreen * s)
SERENITY_SCREEN(s);
UNWRAP(ss, s, preparePaintScreen);
UNWRAP(ss, s, paintScreen);
+ serenityFiniTextures(ss);
free(ss);
}