Skip to content

Commit

Permalink
Refactor: Moved AnimGroups from ClientResources to libdoomsday
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jul 28, 2016
1 parent 89e4d00 commit 41bb3e1
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 86 deletions.
28 changes: 0 additions & 28 deletions doomsday/apps/client/include/resource/clientresources.h
Expand Up @@ -411,34 +411,6 @@ class ClientResources : public Resources
*/
void releaseFontGLTexturesByScheme(de::String schemeName);

#endif // __CLIENT__

/**
* Returns the total number of animation/precache groups.
*/
de::dint animGroupCount();

/**
* Destroys all the animation groups.
*/
void clearAllAnimGroups();

/**
* Construct a new animation group.
*
* @param flags @ref animationGroupFlags
*/
res::AnimGroup &newAnimGroup(de::dint flags);

/**
* Returns the AnimGroup associated with @a uniqueId (1-based); otherwise @c 0.
*/
res::AnimGroup *animGroup(de::dint uniqueId);

res::AnimGroup *animGroupForTexture(res::TextureManifest const &textureManifest);

#ifdef __CLIENT__

/**
* Prepare resources for the current Map.
*/
Expand Down
3 changes: 2 additions & 1 deletion doomsday/apps/client/src/def_main.cpp
Expand Up @@ -45,6 +45,7 @@
#include <doomsday/filesys/fs_main.h>
#include <doomsday/filesys/fs_util.h>
#include <doomsday/resource/manifest.h>
#include <doomsday/resource/animgroups.h>
#include <doomsday/res/Bundles>
#include <doomsday/res/Textures>
#include <doomsday/world/Materials>
Expand Down Expand Up @@ -708,7 +709,7 @@ static void generateMaterialDefForTexture(res::TextureManifest const &manifest)
st0.set("texture", texUri.compose());

// Is there an animation for this?
res::AnimGroup const *anim = resSys().animGroupForTexture(manifest);
res::AnimGroup const *anim = res::AnimGroups::get().animGroupForTexture(manifest);
if (anim && anim->frameCount() > 1)
{
// Determine the start frame.
Expand Down
5 changes: 3 additions & 2 deletions doomsday/apps/client/src/resource/base/api_resource.cpp
Expand Up @@ -28,6 +28,7 @@
#endif

#include <doomsday/resource/colorpalettes.h>
#include <doomsday/resource/animgroups.h>
#include <doomsday/res/Textures>

using namespace de;
Expand Down Expand Up @@ -63,7 +64,7 @@ DENG_EXTERN_C int Textures_UniqueId(uri_s const *uri)
#undef R_CreateAnimGroup
DENG_EXTERN_C int R_CreateAnimGroup(int flags)
{
return App_ResourceSystem().newAnimGroup(flags & ~AGF_PRECACHE).id();
return res::AnimGroups::get().newAnimGroup(flags & ~AGF_PRECACHE).id();
}

#undef R_AddAnimGroupFrame
Expand All @@ -76,7 +77,7 @@ DENG_EXTERN_C void R_AddAnimGroupFrame(int groupId, uri_s const *textureUri_, in

try
{
if(AnimGroup *group = App_ResourceSystem().animGroup(groupId))
if(res::AnimGroup *group = res::AnimGroups::get().animGroup(groupId))
{
group->newFrame(res::Textures::get().textureManifest(textureUri), tics, randomTics);
}
Expand Down
52 changes: 1 addition & 51 deletions doomsday/apps/client/src/resource/base/clientresources.cpp
Expand Up @@ -425,9 +425,6 @@ DENG2_PIMPL(ClientResources)
, DENG2_OBSERVES(res::ColorPalette, ColorTableChange)
#endif
{
typedef QList<res::AnimGroup *> AnimGroups;
AnimGroups animGroups;

typedef QHash<lumpnum_t, rawtex_t *> RawTextureHash;
RawTextureHash rawTexHash;

Expand Down Expand Up @@ -549,7 +546,6 @@ DENG2_PIMPL(ClientResources)
{
convertSavegameTasks.waitForDone();

self.clearAllAnimGroups();
#ifdef __CLIENT__
self.clearAllFontSchemes();
clearFontManifests();
Expand Down Expand Up @@ -2079,7 +2075,7 @@ void ClientResources::clear()
R_ShutdownSvgs();
#endif
clearAllRuntimeResources();
clearAllAnimGroups();
animGroups().clearAllAnimGroups();
}

void ClientResources::clearAllResources()
Expand Down Expand Up @@ -2874,52 +2870,6 @@ void ClientResources::setModelDefFrame(FrameModelDef &modef, dint frame)

#endif // __CLIENT__

void ClientResources::clearAllAnimGroups()
{
qDeleteAll(d->animGroups);
d->animGroups.clear();
}

dint ClientResources::animGroupCount()
{
return d->animGroups.count();
}

res::AnimGroup &ClientResources::newAnimGroup(dint flags)
{
LOG_AS("ResourceSystem");
dint const uniqueId = d->animGroups.count() + 1; // 1-based.
// Allocating one by one is inefficient but it doesn't really matter.
d->animGroups.append(new res::AnimGroup(uniqueId, flags));
return *d->animGroups.last();
}

res::AnimGroup *ClientResources::animGroup(dint uniqueId)
{
LOG_AS("ClientResources::animGroup");
if (uniqueId > 0 && uniqueId <= d->animGroups.count())
{
return d->animGroups.at(uniqueId - 1);
}
LOGDEV_RES_WARNING("Invalid group #%i, returning NULL") << uniqueId;
return nullptr;
}

res::AnimGroup *ClientResources::animGroupForTexture(res::TextureManifest const &textureManifest)
{
// Group ids are 1-based.
// Search backwards to allow patching.
for (dint i = animGroupCount(); i > 0; i--)
{
res::AnimGroup *group = animGroup(i);
if (group->hasFrameFor(textureManifest))
{
return group;
}
}
return nullptr; // Not found.
}

struct SpriteFrameDef
{
bool mirrored = false;
Expand Down
Expand Up @@ -31,13 +31,13 @@ namespace res {
*
* @ingroup resource
*/
class AnimGroup
class LIBDOOMSDAY_PUBLIC AnimGroup
{
public:
/**
* A single frame in the animation.
*/
struct Frame
struct LIBDOOMSDAY_PUBLIC Frame
{
public:
/**
Expand Down
70 changes: 70 additions & 0 deletions doomsday/apps/libdoomsday/include/doomsday/resource/animgroups.h
@@ -0,0 +1,70 @@
/** @file animgroups.h
*
* @authors Copyright © 2005-2015 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDOOMSDAY_RESOURCE_ANIMGROUPS_H
#define LIBDOOMSDAY_RESOURCE_ANIMGROUPS_H

#include "../libdoomsday.h"
#include <de/libcore.h>
#include "animgroup.h"

namespace res {

class AnimGroup;
class TextureManifest;

class LIBDOOMSDAY_PUBLIC AnimGroups
{
public:
static AnimGroups &get();

public:
AnimGroups();

/**
* Returns the total number of animation/precache groups.
*/
de::dint animGroupCount();

/**
* Destroys all the animation groups.
*/
void clearAllAnimGroups();

/**
* Construct a new animation group.
*
* @param flags @ref animationGroupFlags
*/
AnimGroup &newAnimGroup(de::dint flags);

/**
* Returns the AnimGroup associated with @a uniqueId (1-based); otherwise @c 0.
*/
AnimGroup *animGroup(de::dint uniqueId);

AnimGroup *animGroupForTexture(TextureManifest const &textureManifest);

private:
DENG2_PRIVATE(d)
};

} // namespace res

#endif // LIBDOOMSDAY_RESOURCE_ANIMGROUPS_H
Expand Up @@ -31,6 +31,7 @@ namespace res
class MapManifests;
class ColorPalettes;
class Textures;
class AnimGroups;
}

/**
Expand Down Expand Up @@ -91,6 +92,9 @@ class LIBDOOMSDAY_PUBLIC Resources : public de::System
res::Textures & textures();
res::Textures const & textures() const;

res::AnimGroups & animGroups();
res::AnimGroups const & animGroups() const;

private:
DENG2_PRIVATE(d)
};
Expand Down
4 changes: 2 additions & 2 deletions doomsday/apps/libdoomsday/src/resource/animgroup.cpp
Expand Up @@ -66,9 +66,9 @@ int AnimGroup::flags() const

bool AnimGroup::hasFrameFor(TextureManifest const &textureManifest) const
{
foreach(Frame *frame, d->frames)
foreach (Frame *frame, d->frames)
{
if(&frame->textureManifest() == &textureManifest)
if (&frame->textureManifest() == &textureManifest)
return true;
}
return false;
Expand Down
98 changes: 98 additions & 0 deletions doomsday/apps/libdoomsday/src/resource/animgroups.cpp
@@ -0,0 +1,98 @@
/** @file animgroups.cpp
*
* @authors Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "doomsday/resource/animgroups.h"
#include "doomsday/resource/resources.h"

#include <de/Log>
#include <QList>

namespace res {

using namespace de;

DENG2_PIMPL_NOREF(AnimGroups)
{
QList<res::AnimGroup *> animGroups;

~Impl()
{
clearAllAnimGroups();
}

void clearAllAnimGroups()
{
qDeleteAll(animGroups);
animGroups.clear();
}
};

AnimGroups &AnimGroups::get()
{
return Resources::get().animGroups();
}

AnimGroups::AnimGroups() : d(new Impl)
{}

dint AnimGroups::animGroupCount()
{
return d->animGroups.count();
}

void AnimGroups::clearAllAnimGroups()
{
d->clearAllAnimGroups();
}

res::AnimGroup &AnimGroups::newAnimGroup(dint flags)
{
LOG_AS("AnimGroups");
dint const uniqueId = d->animGroups.count() + 1; // 1-based.
// Allocating one by one is inefficient but it doesn't really matter.
d->animGroups.append(new res::AnimGroup(uniqueId, flags));
return *d->animGroups.last();
}

res::AnimGroup *AnimGroups::animGroup(dint uniqueId)
{
LOG_AS("AnimGroups::animGroup");
if (uniqueId > 0 && uniqueId <= d->animGroups.count())
{
return d->animGroups.at(uniqueId - 1);
}
LOGDEV_RES_WARNING("Invalid group #%i, returning NULL") << uniqueId;
return nullptr;
}

res::AnimGroup *AnimGroups::animGroupForTexture(res::TextureManifest const &textureManifest)
{
// Group ids are 1-based.
// Search backwards to allow patching.
for (dint i = animGroupCount(); i > 0; i--)
{
res::AnimGroup *group = animGroup(i);
if (group->hasFrameFor(textureManifest))
{
return group;
}
}
return nullptr; // Not found.
}

} // namespace res

0 comments on commit 41bb3e1

Please sign in to comment.