Permalink
Browse files

RGJ06 kinda fixed

  • Loading branch information...
1 parent d824992 commit 614450fa3e6071297079d4cb9c543e22639f4b43 @svenstaro committed Oct 21, 2011
View
@@ -9,12 +9,12 @@ Background::Background(const std::string& type, float speed) {
mScale = 1.f;
- mSprite.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage(type));
+ mSprite1.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture(type));
- mSprite2.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage(type));
+ mSprite2.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture(type));
mSprite2.FlipX( true ); // background isn't really tileable, so let's make it!
- mSprite3.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage(type));
+ mSprite3.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture(type));
}
Background::~Background() {}
@@ -23,7 +23,7 @@ void Background::Update(float time_diff) {
if(mPosition.x < -1600) {
mPosition.x = 0;
}
- mSprite.SetPosition(mPosition.x, mPosition.y);
+ mSprite1.SetPosition(mPosition.x, mPosition.y);
mSprite2.SetPosition(mPosition.x + 800, mPosition.y);
@@ -35,7 +35,7 @@ void Background::Update(float time_diff) {
}
void Background::Draw(sf::RenderTarget* target) {
- target->Draw(mSprite);
+ target->Draw(mSprite1);
target->Draw(mSprite2);
target->Draw(mSprite3);
View
@@ -15,7 +15,7 @@ class Background: public Entity {
void Draw(sf::RenderTarget* target);
private:
- sf::Sprite mSprite;
+ sf::Sprite mSprite1;
sf::Sprite mSprite2;
sf::Sprite mSprite3;
float mSpeed;
View
@@ -8,7 +8,7 @@ Blob::Blob() {
mType = "blob";
mMarkedForRemoval = false;
- mSprite.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage("photon"));
+ mSprite.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture("photon"));
mSprite.SetOrigin(mSprite.GetSize().x / 2, mSprite.GetSize().y / 2);
mSprite.SetBlendMode(sf::Blend::Add);
mSprite.SetColor(sf::Color(100,200,255));
View
@@ -9,15 +9,15 @@ Enemy::Enemy() {
mScale = 1.f;
- mSprite.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage("photon"));
+ mSprite.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture("photon"));
mSprite.SetOrigin(mSprite.GetSize().x / 2, mSprite.GetSize().y / 2);
mSprite.SetBlendMode(sf::Blend::Add);
mSprite.SetColor(sf::Color(255,0,0));
mScale = 0.7f;
- int rand_x = sf::Randomizer::Random(0, 800);
- int rand_y = sf::Randomizer::Random(0, 600);
- int border = sf::Randomizer::Random(0,3);
+ int rand_x = GameApp::Random(0, 800);
+ int rand_y = GameApp::Random(0, 600);
+ int border = GameApp::Random(0,3);
if(border == 0)
rand_x = 0;
else if(border == 1)
@@ -55,7 +55,7 @@ Enemy::Enemy() {
void Enemy::GeneratePhotons() {
int lvl = GameApp::get_mutable_instance().GetWorld().GetLevel();
- int e = sf::Randomizer::Random(1,lvl);
+ int e = GameApp::Random(1,lvl);
for(int i = 0; i < e; ++i) {
GameApp::get_mutable_instance().GetWorld().AddPhoton(this);
View
@@ -9,13 +9,13 @@ void GameApp::Initialize() {
std::cout << "Antialiasing level: " << mRenderWindow.GetSettings().AntialiasingLevel << std::endl;
mRenderWindow.ShowMouseCursor(false);
- mResourceManager.AddImage("../gfx/", "particle.png", 20, 20);
- mResourceManager.AddImage("../gfx/", "blob.png", 100, 100);
- mResourceManager.AddImage("../gfx/", "enemy.png", 100, 100);
- mResourceManager.AddImage("../gfx/", "player.png", 200, 200);
- mResourceManager.AddImage("../gfx/", "photon.png", 200, 200);
- mResourceManager.AddImage("../gfx/", "background.png", 800, 600);
- mResourceManager.AddImage("../gfx/", "foreground.png", 800, 600);
+ mResourceManager.AddTexture("../gfx/", "particle.png", 20, 20);
+ mResourceManager.AddTexture("../gfx/", "blob.png", 100, 100);
+ mResourceManager.AddTexture("../gfx/", "enemy.png", 100, 100);
+ mResourceManager.AddTexture("../gfx/", "player.png", 200, 200);
+ mResourceManager.AddTexture("../gfx/", "photon.png", 200, 200);
+ mResourceManager.AddTexture("../gfx/", "background.png", 800, 600);
+ mResourceManager.AddTexture("../gfx/", "foreground.png", 800, 600);
mResourceManager.AddSoundBuffer("../snd/", "death.ogg");
mResourceManager.AddSoundBuffer("../snd/", "killenemy.ogg");
@@ -39,6 +39,7 @@ void GameApp::Run() {
HandleEvents();
float time_delta = mClock.GetElapsedTime();
+ time_delta /= 1000;
mClock.Reset();
time_budget += time_delta;
@@ -56,17 +57,17 @@ void GameApp::HandleEvents() {
while(mRenderWindow.PollEvent(event)) {
if((event.Type == sf::Event::Closed) ||
(event.Type == sf::Event::KeyPressed &&
- event.Key.Code == sf::Key::Escape)) {
+ event.Key.Code == sf::Keyboard::Escape)) {
mRenderWindow.Close();
}
//handle event
- mWorld.HandleEvent(event, mRenderWindow.GetInput());
+ mWorld.HandleEvent(event);
}
}
void GameApp::Update(float frame_time) {
- mWorld.Update(frame_time, mRenderWindow.GetInput());
+ mWorld.Update(frame_time);
}
void GameApp::Draw() {
@@ -82,3 +83,11 @@ ResourceManager* const GameApp::GetResourceManagerPtr() {
World& GameApp::GetWorld() {
return mWorld;
}
+
+float GameApp::Random(float min, float max) {
+ return static_cast<float>(rand()) / RAND_MAX * (max - min) + min;
+}
+int GameApp::Random(int min, int max) {
+ return rand() % (max - min + 1) + min;
+}
+
View
@@ -20,6 +20,9 @@ class GameApp : public boost::serialization::singleton<GameApp> {
ResourceManager* const GetResourceManagerPtr();
World& GetWorld();
+ static float Random(float min, float max);
+ static int Random(int min, int max);
+
private:
void HandleEvents();
void Update(float frame_time);
View
@@ -43,7 +43,7 @@ Particle::Particle(const Vector2D& position,
Particle::~Particle() {}
void Particle::Initialize() {
- mSprite.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage(mImageName));
+ mSprite.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture(mImageName));
mSprite.SetOrigin(mSprite.GetSize().x / 2, mSprite.GetSize().y / 2);
mSprite.SetBlendMode(mBlendMode);
}
@@ -2,6 +2,7 @@
#include <boost/foreach.hpp>
#include "ParticleEmitter.hpp"
+#include "GameApp.hpp"
ParticleEmitter::ParticleEmitter(const Vector2D& position_offset,
const float rotation_offset,
@@ -68,7 +69,7 @@ void ParticleEmitter::Update(float time_diff,
if(emit) {
while(mTimeSinceLastParticle >= 1.f / mRate) {
- float spread = sf::Randomizer::Random(-mSpread/2, mSpread/2);
+ float spread = GameApp::Random(-mSpread/2, mSpread/2);
Vector2D spread_vector = mDirection;
spread_vector.Normalize();
spread_vector.Rotate(Vector2D::deg2Rad(spread));
View
@@ -6,13 +6,13 @@ Photon::Photon(Vector2D pos) {
mType = "photon";
mMarkedForRemoval = false;
- mSprite.SetImage(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetImage("photon"));
- mSprite.SetOrigin(mSprite.GetImage()->GetWidth() / 2.f, mSprite.GetImage()->GetHeight() / 2.f);
+ mSprite.SetTexture(GameApp::get_mutable_instance().GetResourceManagerPtr()->GetTexture("photon"));
+ mSprite.SetOrigin(mSprite.GetTexture()->GetWidth() / 2.f, mSprite.GetTexture()->GetHeight() / 2.f);
mSprite.SetBlendMode(sf::Blend::Add);
- SetPosition(pos + Vector2D(sf::Randomizer::Random(-30.f,30.f), sf::Randomizer::Random(-30.f,30.f)));
- SetSpeed(sf::Randomizer::Random(40.f,80.f));
- SetDirection(Vector2D(sf::Randomizer::Random(-1.f,1.f), sf::Randomizer::Random(-1.f,1.f)));
+ SetPosition(pos + Vector2D(GameApp::Random(-30.f,30.f), GameApp::Random(-30.f,30.f)));
+ SetSpeed(GameApp::Random(40.f,80.f));
+ SetDirection(Vector2D(GameApp::Random(-1.f,1.f), GameApp::Random(-1.f,1.f)));
mTarget = NULL;
}
@@ -63,7 +63,7 @@ void Photon::CheckOpponentCollision() {
int m_e = w.GetEnergyOfEntity(GetTarget()); // my energy
int p_e = w.GetEnergyOfEntity(p->GetTarget()); // other's energy
bool other_is_player = p->GetTarget()->GetType() == "player";
- if( m_e > p_e || other_is_player || (m_e == p_e && sf::Randomizer::Random(0,1) == 1) ) {
+ if( m_e > p_e || other_is_player || (m_e == p_e && GameApp::Random(0,1) == 1) ) {
// I am stronger, delete the other one
p->MarkForRemoval();
return;
@@ -6,58 +6,58 @@ ResourceManager::ResourceManager() {}
ResourceManager::~ResourceManager() {}
-bool ResourceManager::AddImage(const boost::filesystem::path& path,
+bool ResourceManager::AddTexture(const boost::filesystem::path& path,
const std::string& imgname,
const float width,
const float height) {
if(!boost::filesystem::is_regular_file(path/imgname)) {
- std::cerr << "Tried loading image path '" << (path/imgname).string() << "' but this image path doesn't exist!" << std::endl;
+ std::cerr << "Tried loading texture path '" << (path/imgname).string() << "' but this texture path doesn't exist!" << std::endl;
exit(1);
}
// create Original file path
std::string originalFile = (path / imgname).string();
// if the optional param key is not given, use the basename as key
- std::string image_key = boost::filesystem::basename(originalFile);
+ std::string texture_key = boost::filesystem::basename(originalFile);
// Create Cache Paths
//boost::filesystem::path cacheDir = (path / "cached").string();
- //std::string cacheFile = (cacheDir / image_key ).string()+".png";
+ //std::string cacheFile = (cacheDir / texture_key ).string()+".png";
- // if an image with that key already exists in the dictionary, return
- if(mImages.count(image_key) != 0) {
+ // if an texture with that key already exists in the dictionary, return
+ if(mTextures.count(texture_key) != 0) {
return true;
}
- sf::Image sfimage;
- bool success = sfimage.LoadFromFile(originalFile);
+ sf::Texture sftexture;
+ bool success = sftexture.LoadFromFile(originalFile);
if(!success) {
- std::cerr << "Image " << originalFile << " does not exists. Exit. "<< std::endl;
+ std::cerr << "Texture " << originalFile << " does not exists. Exit. "<< std::endl;
exit(1);
}
/*bool cache = true;
if(boost::filesystem::is_regular_file(cacheFile)) {
// Load cached file
- bool success = sfimage.LoadFromFile(cacheFile);
- if (success && (int)sfimage.GetHeight() == (int)size.x && (int)sfimage.GetWidth() == (int)size.y) {
+ bool success = sftexture.LoadFromFile(cacheFile);
+ if (success && (int)sftexture.GetHeight() == (int)size.x && (int)sftexture.GetWidth() == (int)size.y) {
cache = false;
- std::cout << "Image " << originalFile << " already exists. Not caching. "<< std::endl;
+ std::cout << "Texture " << originalFile << " already exists. Not caching. "<< std::endl;
} else if (success) {
- std::cout << "Image " << originalFile << " does not exist in the resolution "
- << size.x << "x" << size.y << " but in " << sfimage.GetHeight() << "x" << sfimage.GetWidth() << "." << std::endl;
+ std::cout << "Texture " << originalFile << " does not exist in the resolution "
+ << size.x << "x" << size.y << " but in " << sftexture.GetHeight() << "x" << sftexture.GetWidth() << "." << std::endl;
}
}
if(cache){
- std::cout << ":: Caching image " << originalFile << std::endl;
+ std::cout << ":: Caching texture " << originalFile << std::endl;
// Create cache directory
boost::filesystem::create_directory(cacheDir.string());
- // Load, convert and save image (originalFile > cacheFile)
+ // Load, convert and save texture (originalFile > cacheFile)
Magick::Image mimage;
mimage.backgroundColor(Magick::Color(0, 0, 0, 65535));
mimage.density(Magick::Geometry(144, 144));
@@ -69,32 +69,32 @@ bool ResourceManager::AddImage(const boost::filesystem::path& path,
mimage.write(cacheFile);
// Load cached file
- sfimage.LoadFromFile(cacheFile);
+ sftexture.LoadFromFile(cacheFile);
}
*/
- sfimage.SetSmooth(true);
+ sftexture.SetSmooth(true);
- std::cout << " Added image: "<<image_key << std::endl;
- // Save loaded Image in Dictionary
- mImages[image_key] = sfimage;
+ std::cout << " Added texture: "<<texture_key << std::endl;
+ // Save loaded Texture in Dictionary
+ mTextures[texture_key] = sftexture;
return true;
}
-const sf::Image& ResourceManager::GetImage(const std::string& img) {
- if(mImages.count(img) >= 1) {
- return mImages[img];
+const sf::Texture& ResourceManager::GetTexture(const std::string& img) {
+ if(mTextures.count(img) >= 1) {
+ return mTextures[img];
} else {
- std::cerr << "Tried getting image '"<<img<<"' but this image doesn't exist!"<<std::endl;
+ std::cerr << "Tried getting texture '"<<img<<"' but this texture doesn't exist!"<<std::endl;
exit(1);
}
}
-const std::vector<std::string> ResourceManager::GetImageKeys() {
+const std::vector<std::string> ResourceManager::GetTextureKeys() {
std::vector<std::string> keys;
- for(auto iter = mImages.begin(); iter != mImages.end(); iter++) {
+ for(auto iter = mTextures.begin(); iter != mTextures.end(); iter++) {
if (iter->first != "view_border") {
keys.push_back(iter->first);
}
@@ -19,10 +19,10 @@ class ResourceManager {
~ResourceManager();
- bool AddImage(const boost::filesystem::path& path, const std::string& imgname,
+ bool AddTexture(const boost::filesystem::path& path, const std::string& imgname,
const float width, const float height);
- const sf::Image& GetImage(const std::string& filename);
- const std::vector<std::string> GetImageKeys();
+ const sf::Texture& GetTexture(const std::string& filename);
+ const std::vector<std::string> GetTextureKeys();
bool AddSoundBuffer(const boost::filesystem::path& path, const std::string& sound, const std::string& key="");
const sf::SoundBuffer& GetSoundBuffer(const std::string& sound);
@@ -34,7 +34,7 @@ class ResourceManager {
void StopSound(std::string key);
void StopSounds();
private:
- boost::ptr_map<std::string, sf::Image> mImages;
+ boost::ptr_map<std::string, sf::Texture> mTextures;
boost::ptr_map<std::string, sf::Font> mFonts;
boost::ptr_map<std::string, sf::SoundBuffer> mSoundBuffers;
boost::ptr_map<std::string, sf::Sound> mSounds;
View
@@ -60,14 +60,15 @@ void World::Reset() {
mScore = 0;
}
-void World::HandleEvent(const sf::Event& event, const sf::Input& input) {
+void World::HandleEvent(const sf::Event& event) {
auto mouse_partsys = GetEntityByName<ParticleSystem>("mouse_partsys");
- mouse_partsys->SetPosition(Vector2D(input.GetMouseX(), input.GetMouseY()));
+ mouse_partsys->SetPosition(Vector2D(sf::Mouse::GetPosition().x,
+ sf::Mouse::GetPosition().y));
if(event.Type == sf::Event::KeyPressed) {
- if(event.Key.Code == sf::Key::D) {
+ if(event.Key.Code == sf::Keyboard::D) {
mDebug = !mDebug;
- } else if(event.Key.Code == sf::Key::Space) {
+ } else if(event.Key.Code == sf::Keyboard::Space) {
AddPhoton(GetPlayer());
}
}
@@ -108,11 +109,12 @@ void World::HandleEvent(const sf::Event& event, const sf::Input& input) {
}*/
}
-void World::Update(float time_diff, const sf::Input& input) {
+void World::Update(float time_diff) {
mTimeSinceLastPhotonTransfered += time_diff;
- if(input.IsMouseButtonDown(sf::Mouse::Left)) {
- Vector2D mp(input.GetMouseX(), input.GetMouseY());
+ if(sf::Mouse::IsButtonPressed(sf::Mouse::Left)) {
+ Vector2D mp(sf::Mouse::GetPosition().x,
+ sf::Mouse::GetPosition().y);
int player_energy = GetEnergyOfEntity(GetPlayer());
if(PointWithinPlayerRange(mp) && mCurrentBlob != NULL && player_energy > 0) {
if(mTimeSinceLastPhotonTransfered > (3.0 / (player_energy + GetEnergyOfEntity(mCurrentBlob)) )) {
View
@@ -22,8 +22,8 @@ class World {
void Initialize();
void Reset();
- void HandleEvent(const sf::Event& event, const sf::Input& input);
- void Update(float time_diff, const sf::Input& input);
+ void HandleEvent(const sf::Event& event);
+ void Update(float time_diff);
void Draw(sf::RenderTarget* target);
void AddEntity(Entity* e);

0 comments on commit 614450f

Please sign in to comment.