Skip to content

Commit

Permalink
Use normal Animation system for GameStateLoad previews
Browse files Browse the repository at this point in the history
- When save slots are selected, the character preview will now run and
rotate at the same time.
- Since we're using the regular animation files for this, there is no
more need for the "preview" images that were previously only used in
GameStateLoad.
  • Loading branch information
dorkster committed Jun 2, 2017
1 parent 23bd531 commit 526c1d5
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 131 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Set (FLARE_SOURCES
./src/EventManager.cpp
./src/FileParser.cpp
./src/FontEngine.cpp
./src/GameSlotPreview.cpp
./src/GameState.cpp
./src/GameStateConfigBase.cpp
./src/GameStateConfigDesktop.cpp
Expand Down Expand Up @@ -221,6 +222,7 @@ Set (FLARE_HEADERS
./src/EventManager.h
./src/FileParser.h
./src/FontEngine.h
./src/GameSlotPreview.h
./src/GameState.h
./src/GameStateConfigBase.h
./src/GameStateConfigDesktop.h
Expand Down
1 change: 1 addition & 0 deletions flare-android-project/app/src/main/jni/src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
../../../../../../src/EventManager.cpp \
../../../../../../src/FileParser.cpp \
../../../../../../src/FontEngine.cpp \
../../../../../../src/GameSlotPreview.cpp \
../../../../../../src/GameState.cpp \
../../../../../../src/GameStateConfigBase.cpp \
../../../../../../src/GameStateConfigDesktop.cpp \
Expand Down
2 changes: 1 addition & 1 deletion mods/default/menus/gameload.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ level=24,36,left,top
class=24,52,left,top
map=24,68,left,top
slot_number=276,8,right,top
sprite=178,-24
sprite=240,74
loading_label=480,366,center,top
text_trim_boundary=178
199 changes: 199 additions & 0 deletions src/GameSlotPreview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
Copyright © 2011-2012 Clint Bellanger
Copyright © 2012 Igor Paliychuk
Copyright © 2012 Stefan Beller
Copyright © 2013 Henrik Andersson
Copyright © 2012-2016 Justin Jacobs
This file is part of FLARE.
FLARE 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 3 of the License, or (at your option) any later version.
FLARE 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
FLARE. If not, see http://www.gnu.org/licenses/
*/

/**
* class GameSlotPreview
*
* Contains logic and rendering routines for the previews in GameStateLoad.
*/

#include "AnimationManager.h"
#include "CommonIncludes.h"
#include "GameSlotPreview.h"
#include "FileParser.h"
#include "SharedResources.h"
#include "StatBlock.h"
#include "Utils.h"
#include "UtilsParsing.h"

GameSlotPreview::GameSlotPreview()
: stats(NULL)
{
// load the hero's animations from hero definition file
anim->increaseCount("animations/hero.txt");
animationSet = anim->getAnimationSet("animations/hero.txt");
activeAnimation = animationSet->getAnimation("");

loadLayerDefinitions();
}

/**
* Load avatar sprite layer definitions into vector.
*/
void GameSlotPreview::loadLayerDefinitions() {
layer_def = std::vector<std::vector<unsigned> >(8, std::vector<unsigned>());
layer_reference_order = std::vector<std::string>();

FileParser infile;
// @CLASS GameSlotPreview: Hero layers|Description of engine/hero_layers.txt
if (infile.open("engine/hero_layers.txt")) {
while(infile.next()) {
if (infile.key == "layer") {
// @ATTR layer|direction, list(string) : Direction, Layer name(s)|Defines the hero avatar sprite layer
unsigned dir = parse_direction(popFirstString(infile.val));
if (dir>7) {
infile.error("GameSlotPreview: Hero layer direction must be in range [0,7]");
mods->resetModConfig();
Exit(1);
}
std::string layer = popFirstString(infile.val);
while (layer != "") {
// check if already in layer_reference:
unsigned ref_pos;
for (ref_pos = 0; ref_pos < layer_reference_order.size(); ++ref_pos)
if (layer == layer_reference_order[ref_pos])
break;
if (ref_pos == layer_reference_order.size())
layer_reference_order.push_back(layer);
layer_def[dir].push_back(ref_pos);

layer = popFirstString(infile.val);
}
}
else {
infile.error("GameSlotPreview: '%s' is not a valid key.", infile.key.c_str());
}
}
infile.close();
}

// There are the positions of the items relative to layer_reference_order
// so if layer_reference_order=main,body,head,off
// and we got a layer=3,off,body,head,main
// then the layer_def[3] looks like (3,1,2,0)
}

void GameSlotPreview::loadGraphics(std::vector<std::string> _img_gfx) {
if (!stats)
return;

loadLayerDefinitions();

for (unsigned int i=0; i<animsets.size(); i++) {
if (animsets[i])
anim->decreaseCount(animsets[i]->getName());
delete anims[i];
}
animsets.clear();
anims.clear();

for (unsigned int i=0; i<_img_gfx.size(); i++) {
if (_img_gfx[i] != "") {
std::string name = "animations/avatar/"+stats->gfx_base+"/"+_img_gfx[i] +".txt";
anim->increaseCount(name);
animsets.push_back(anim->getAnimationSet(name));
animsets.back()->setParent(animationSet);
anims.push_back(animsets.back()->getAnimation(activeAnimation->getName()));
setAnimation("stance");
if(!anims.back()->syncTo(activeAnimation)) {
logError("GameSlotPreview: Error syncing animation in '%s' to 'animations/hero.txt'.", animsets.back()->getName().c_str());
}
}
else {
animsets.push_back(NULL);
anims.push_back(NULL);
}
}
anim->cleanUp();

setAnimation("stance");
}

void GameSlotPreview::logic() {
// handle animation
activeAnimation->advanceFrame();
for (unsigned i=0; i < anims.size(); i++) {
if (anims[i] != NULL)
anims[i]->advanceFrame();
}
}

void GameSlotPreview::setAnimation(std::string name) {
if (name == activeAnimation->getName())
return;

activeAnimation = animationSet->getAnimation(name);

for (unsigned i=0; i < animsets.size(); i++) {
delete anims[i];
if (animsets[i])
anims[i] = animsets[i]->getAnimation(name);
else
anims[i] = 0;
}
}

void GameSlotPreview::addRenders(std::vector<Renderable> &r) {
if (!stats)
return;

for (unsigned i = 0; i < layer_def[stats->direction].size(); ++i) {
unsigned index = layer_def[stats->direction][i];
if (anims[index]) {
Renderable ren = anims[index]->getCurrentFrame(stats->direction);
ren.prio = i+1;
r.push_back(ren);
}
}
}

void GameSlotPreview::setStatBlock(StatBlock *_stats) {
stats = _stats;
}

void GameSlotPreview::setPos(Point _pos) {
pos = _pos;
}

void GameSlotPreview::render() {
std::vector<Renderable> r;
addRenders(r);

for (size_t i = 0; i < r.size(); ++i) {
if (r[i].image) {
Rect dest;
dest.x = pos.x - r[i].offset.x;
dest.y = pos.y - r[i].offset.y;
render_device->render(r[i], dest);
}
}
}

GameSlotPreview::~GameSlotPreview() {
anim->decreaseCount("animations/hero.txt");

for (unsigned int i=0; i<animsets.size(); i++) {
if (animsets[i])
anim->decreaseCount(animsets[i]->getName());
delete anims[i];
}
anim->cleanUp();
}
68 changes: 68 additions & 0 deletions src/GameSlotPreview.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright © 2011-2012 Clint Bellanger
Copyright © 2012 Igor Paliychuk
Copyright © 2013 Henrik Andersson
Copyright © 2012-2016 Justin Jacobs
This file is part of FLARE.
FLARE 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 3 of the License, or (at your option) any later version.
FLARE 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
FLARE. If not, see http://www.gnu.org/licenses/
*/

/**
* class GameSlotPreview
*
* Contains logic and rendering routines for the previews in GameStateLoad.
*/

#ifndef AVATAR_GRAPHICS_H
#define AVATAR_GRAPHICS_H

#include "Animation.h"
#include "AnimationSet.h"
#include "CommonIncludes.h"
#include "SharedResources.h"
#include "Utils.h"

class StatBlock;

class GameSlotPreview {
private:
void loadLayerDefinitions();

StatBlock *stats;
Point pos;

std::vector<AnimationSet*> animsets; // hold the animations for all equipped items in the right order of drawing.
std::vector<Animation*> anims; // hold the animations for all equipped items in the right order of drawing.

public:
GameSlotPreview();
~GameSlotPreview();

void setAnimation(std::string name);
void setStatBlock(StatBlock *_stats);
void setPos(Point _pos);
void loadGraphics(std::vector<std::string> _img_gfx);
void logic();
void addRenders(std::vector<Renderable> &r);
void render();

Animation *activeAnimation;
AnimationSet *animationSet;

std::vector<std::string> layer_reference_order;
std::vector<std::vector<unsigned> > layer_def;
};

#endif

Loading

0 comments on commit 526c1d5

Please sign in to comment.