Skip to content

Commit

Permalink
Add background, sprites to battle scene
Browse files Browse the repository at this point in the history
  • Loading branch information
glynnc committed Feb 10, 2011
1 parent 2188c2e commit 0aedf9d
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 6 deletions.
124 changes: 124 additions & 0 deletions src/background.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/////////////////////////////////////////////////////////////////////////////
// This file is part of EasyRPG Player.
//
// EasyRPG Player 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.
//
// EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <string>
#include "data.h"
#include "rpg_terrain.h"
#include "baseui.h"
#include "graphics.h"
#include "cache.h"
#include "background.h"

////////////////////////////////////////////////////////////
Background::Background(const std::string& name) :
ID(Graphics::drawable_id++), zobj(NULL), visible(true),
bg_screen(NULL), bg_hscroll(0), bg_vscroll(0), bg_x(0), bg_y(0),
fg_screen(NULL), fg_hscroll(0), fg_vscroll(0), fg_x(0), fg_y(0) {

zobj = Graphics::RegisterZObj(0, ID);
Graphics::RegisterDrawable(ID, this);

bg_screen = BitmapScreen::CreateBitmapScreen(Cache::Backdrop(name));
}

Background::Background(int terrain_id) :
ID(Graphics::drawable_id++), zobj(NULL), visible(true),
bg_screen(NULL), bg_hscroll(0), bg_vscroll(0), bg_x(0), bg_y(0),
fg_screen(NULL), fg_hscroll(0), fg_vscroll(0), fg_x(0), fg_y(0) {

zobj = Graphics::RegisterZObj(0, ID);
Graphics::RegisterDrawable(ID, this);

const RPG::Terrain& terrain = Data::terrains[terrain_id - 1];

if (terrain.background_type == 0) {
bg_screen = BitmapScreen::CreateBitmapScreen(Cache::Backdrop(terrain.background_name));
return;
}

bg_screen = BitmapScreen::CreateBitmapScreen(Cache::Frame(terrain.background_a_name));
bg_hscroll = terrain.background_a_scrollh ? terrain.background_a_scrollh_speed : 0;
bg_vscroll = terrain.background_a_scrollv ? terrain.background_a_scrollv_speed : 0;

if (terrain.background_b) {
fg_screen = BitmapScreen::CreateBitmapScreen(Cache::Frame(terrain.background_b_name));
fg_hscroll = terrain.background_b_scrollh ? terrain.background_b_scrollh_speed : 0;
fg_vscroll = terrain.background_b_scrollv ? terrain.background_b_scrollv_speed : 0;
}
}

////////////////////////////////////////////////////////////
Background::~Background() {
Graphics::RemoveZObj(ID);
Graphics::RemoveDrawable(ID);
if (bg_screen != NULL)
delete bg_screen;
if (fg_screen != NULL)
delete fg_screen;
}

////////////////////////////////////////////////////////////
int Background::GetZ() const {
return z;
}

unsigned long Background::GetId() const {
return ID;
}

DrawableType Background::GetType() const {
return type;
}

////////////////////////////////////////////////////////////
void Background::Update(int& rate, int& value) {
int step =
(rate > 0) ? 1 << rate :
(rate < 0) ? 1 << -rate :
0;
value += step;
}

void Background::Update() {
Update(bg_hscroll, bg_x);
Update(bg_vscroll, bg_y);
Update(fg_hscroll, fg_x);
Update(fg_vscroll, fg_y);
}

int Background::Scale(int x) {
return x > 0 ? x / 64 : -(-x / 64);
}

void Background::Draw(int z_order) {
if (!visible)
return;

if (bg_screen)
bg_screen->BlitScreenTiled(bg_screen->GetBitmap()->GetRect(),
DisplayUi->GetDisplaySurface()->GetRect(),
Scale(bg_x), Scale(bg_y));

if (fg_screen != NULL)
fg_screen->BlitScreenTiled(bg_screen->GetBitmap()->GetRect(),
DisplayUi->GetDisplaySurface()->GetRect(),
Scale(fg_x), Scale(fg_y));
}

67 changes: 67 additions & 0 deletions src/background.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/////////////////////////////////////////////////////////////////////////////
// This file is part of EasyRPG Player.
//
// EasyRPG Player 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.
//
// EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////

#ifndef _EASYRPG_BACKGROUND_H_
#define _EASYRPG_BACKGROUND_H_

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <string>
#include "drawable.h"
#include "zobj.h"
#include "bitmap_screen.h"

////////////////////////////////////////////////////////////
class Background : public Drawable {
public:
Background(const std::string& name);
Background(int terrain_id);
virtual ~Background();

void Draw(int z_order);
void Update();

unsigned long GetId() const;
int GetZ() const;
DrawableType GetType() const;

private:
static const int z = -1000;
static const DrawableType type = TypeBackground;

static void Update(int& rate, int& value);
static int Scale(int x);

unsigned long ID;
ZObj* zobj;
bool visible;

BitmapScreen* bg_screen;
int bg_hscroll;
int bg_vscroll;
int bg_x;
int bg_y;
BitmapScreen* fg_screen;
int fg_hscroll;
int fg_vscroll;
int fg_x;
int fg_y;
};

#endif

1 change: 1 addition & 0 deletions src/drawable.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum DrawableType {
TypeTilemap,
TypeSprite,
TypePlane,
TypeBackground,
TypeDefault
};

Expand Down
101 changes: 99 additions & 2 deletions src/scene_battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "output.h"
#include "player.h"
#include "sprite.h"
#include "cache.h"
#include "game_system.h"
#include "game_temp.h"
#include "game_party.h"
Expand All @@ -33,6 +34,17 @@ Scene_Battle::Scene_Battle() {
Scene::type = Scene::Battle;
}

////////////////////////////////////////////////////////////
Scene_Battle::~Scene_Battle() {
delete help_window;
delete options_window;
delete status_window;
delete command_window;
delete item_window;
delete skill_window;
delete background;
}

////////////////////////////////////////////////////////////
void Scene_Battle::Start() {
if (Player::battle_test_flag) {
Expand All @@ -41,6 +53,65 @@ void Scene_Battle::Start() {
}
}

cycle = 0;

if (!Game_Temp::battle_background.empty())
background = new Background(Game_Temp::battle_background);
else
background = new Background(Game_Temp::battle_terrain_id);

troop = &Data::troops[Game_Temp::battle_troop_id - 1];

for (std::vector<RPG::TroopMember>::const_iterator it = troop->members.begin();
it != troop->members.end();
it++) {
const RPG::TroopMember& mem = *it;
const RPG::Enemy& enemy = Data::enemies[mem.ID - 1];
Bitmap* graphic = Cache::Monster(enemy.battler_name);
bool hue_change = enemy.battler_hue != 0;
if (hue_change) {
Surface* new_graphic = Surface::CreateSurface(graphic->GetWidth(), graphic->GetHeight());
new_graphic->HueChangeBlit(0, 0, graphic, graphic->GetRect(), enemy.battler_hue);
delete graphic;
graphic = new_graphic;
}

Sprite* sprite = new Sprite();
sprite->SetBitmap(graphic, hue_change);
sprite->SetOx(graphic->GetWidth() / 2);
sprite->SetOy(graphic->GetHeight() / 2);
sprite->SetX(mem.x);
sprite->SetY(mem.y);
sprite->SetZ(mem.y);
sprite->SetVisible(!mem.invisible);

enemy_sprites.push_back(sprite);
}

if (Player::engine == Player::EngineRpg2k3) {
const std::vector<Game_Actor*>& actors = Game_Party::GetActors();
for (std::vector<Game_Actor*>::const_iterator it = actors.begin();
it != actors.end();
it++) {
const Game_Actor* actor = *it;
const RPG::Actor& rpg_actor = Data::actors[actor->GetId() - 1];
const RPG::BattlerAnimation& anim = Data::battleranimations[rpg_actor.battler_animation - 1];
const RPG::BattlerAnimationExtension& ext = anim.base_data[0];
Bitmap* graphic = Cache::BattleCharset(ext.battler_name);

Sprite* sprite = new Sprite();
sprite->SetBitmap(graphic);
sprite->SetSrcRect(Rect(0, ext.battler_index * 48, 48, 48));
sprite->SetOx(24);
sprite->SetOy(24);
sprite->SetX(rpg_actor.battle_x);
sprite->SetY(rpg_actor.battle_y);
sprite->SetZ(rpg_actor.battle_y);

actor_sprites.push_back(sprite);
}
}

help_window = new Window_Help(0, 0, 320, 32);
help_window->SetVisible(false);

Expand All @@ -54,8 +125,8 @@ void Scene_Battle::Start() {
status_window = new Window_BattleStatus();

std::vector<std::string> commands;
commands.push_back(Data::terms.command_attack);
commands.push_back(Data::terms.command_defend);
commands.push_back(!Data::terms.command_attack.empty() ? Data::terms.command_attack : "Attack");
commands.push_back(!Data::terms.command_defend.empty() ? Data::terms.command_defend : "Defend");
commands.push_back(Data::terms.command_item);
commands.push_back(Data::terms.command_skill);

Expand Down Expand Up @@ -142,6 +213,8 @@ void Scene_Battle::Update() {
status_window->Update();
command_window->Update();

cycle++;

if (Input::IsTriggered(Input::DECISION)) {
Game_System::SePlay(Data::system.decision_se);
switch (state) {
Expand Down Expand Up @@ -228,6 +301,30 @@ void Scene_Battle::Update() {
default:
break;
}

for (int i = 0; i < (int) troop->members.size(); i++) {
const RPG::TroopMember& mem = troop->members[i];
const RPG::Enemy& enemy = Data::enemies[mem.ID - 1];
if (!enemy.levitate)
continue;
Sprite* sprite = enemy_sprites[i];
int y = (int) (3 * sin(cycle / 20.0));
sprite->SetY(mem.y + y);
sprite->SetZ(mem.y + y);
}

if (Player::engine == Player::EngineRpg2k3) {
for (int i = 0; i < (int) actors.size(); i++) {
const Game_Actor* actor = actors[i];
const RPG::Actor& rpg_actor = Data::actors[actor->GetId() - 1];
const RPG::BattlerAnimation& anim = Data::battleranimations[rpg_actor.battler_animation - 1];
const RPG::BattlerAnimationExtension& ext = anim.base_data[0];
Sprite* sprite = actor_sprites[i];
static const int frames[] = {0,1,2,1};
int frame = frames[(cycle / 20) % 4];
sprite->SetSrcRect(Rect(frame * 48, ext.battler_index * 48, 48, 48));
}
}
}

////////////////////////////////////////////////////////////
Expand Down
12 changes: 8 additions & 4 deletions src/scene_battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// Headers
////////////////////////////////////////////////////////////
#include "scene.h"
#include "background.h"
#include "window_help.h"
#include "window_item.h"
#include "window_skill.h"
Expand All @@ -31,15 +32,12 @@
////////////////////////////////////////////////////////////
/// Scene_Battle class.
/// Manages the battles.
/// @todo Not implemented. Just a stub class for battle test
////////////////////////////////////////////////////////////
class Scene_Battle : public Scene {

public:
////////////////////////////////////////////////////////
/// Constructor.
////////////////////////////////////////////////////////
Scene_Battle();
~Scene_Battle();

void Start();
void Update();
Expand All @@ -58,13 +56,19 @@ class Scene_Battle : public Scene {

private:
State state;
int cycle;

Window_Help* help_window;
Window_BattleCommand* options_window;
Window_BattleStatus* status_window;
Window_BattleCommand* command_window;
Window_Item* item_window;
Window_Skill* skill_window;
Background* background;

const RPG::Troop* troop;
std::vector<Sprite*> enemy_sprites;
std::vector<Sprite*> actor_sprites;
};

#endif

0 comments on commit 0aedf9d

Please sign in to comment.