Hello again. I've got some of the base functionality behind my program in place, and I've run into something I don't know how to get around.
My program doesn't have a steady speed. I'm using the clock functionality to have it run the animations at a certain speed regardless of framerate. However, it will pause for long periods, pump twitch back and forth between the two poses I'm testing with, then pause again.
I thought I'd been using efficient code so far. Though even if I don't, this computer can run skyrim on full graphics settings while ripping a DVD and converting fifty some odd media files without even a slight hangup in speed in any program.
Not sure what's causing it, but I'll copy and paste the bits that are being used currently.
main.cpp#include "global.h"
int main()
{
gameSystem.gameLoop();
return 0;
}
global.h#ifndef _GLOBAL_H
#define _GLOBAL_H
#include "game.h"
extern game gameSystem;
#endif
global.cpp#include "global.h"
game gameSystem;
game.h#ifndef _GAME_H
#define _GAME_H
#include <vector>
#include "graphic.h"
#include "gameSprite.h"
//#include "input.h"
//#include "setting.h"
class game
{
public:
game();
void gameLoop();
graphic graphics;
std::vector<gameSprite> sprites;
//std::vector<input> inputs;
//setting settings;
};
#endif
(since I'm not using input or settings at the moment, I commented them out so if their functions were causing the lag, they wouldn't be used. It didn't change anything)
game.cpp#include <vector>
#include <iostream>
#include "game.h"
#include <SFML/Graphics.hpp>
game::game()
{
sprites.push_back(gameSprite("img/arsh.png",4,4));
}
void game::gameLoop()
{
while(graphics.gameWindow.isOpen())
{
sf::Event Event;
while(graphics.gameWindow.pollEvent(Event))
{
switch(Event.type)
{
case sf::Event::Closed:
std::cout << "The window was closed." << std::endl;
graphics.gameWindow.close();
break;
default:
break;
}
graphics.gameWindow.clear(sf::Color(0,255,255));
// Draw all the sprites!
for (std::size_t i=0; i<sprites.size(); i++) {
sprites[i].update();
graphics.gameWindow.draw(sprites[i].getSprite());
}
graphics.gameWindow.display();
}
}
std::cout << "Loop has ended." << std::endl;
}
graphic.h#ifndef _GRAPHIC_H
#define _GRAPHIC_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
class graphic
{
public:
graphic();
bool windowStayOpen();
void changeWindowSize(int width, int height);
sf::RenderWindow gameWindow;
private:
int numWindows;
int numViewports;
};
#endif
graphic.cpp#include <iostream>
#include "graphic.h"
graphic::graphic()
{
sf::VideoMode VMode(800, 600, 32);
gameWindow.create(VMode,"test",sf::Style::Close);
//gameWindow.setFramerateLimit(40);
std::cout << "Graphics engine been made ^.^" << std::endl;
}
void graphic::changeWindowSize(int width, int height)
{
}
gameSprite.h#ifndef _GAMESPRITE_H
#define _GAMESPRITE_H
#include <SFML/Graphics.hpp>
#include <vector>
#include <utility>
#include <string>
#include <iostream>
using namespace std;
class gameSprite
{
public:
gameSprite(string SpriteSheet, int Rows = 1, int Columns = 1, int x = 0, int y = 0);
void update();
sf::Sprite getSprite();
void changeSpriteSheet(string newSheet);
private:
void setFrame(int row = 1, int column = 1);
sf::Sprite sprite;
sf::Texture texture;
string spriteSheet;
int rows;
int columns;
int frameWidth;
int frameHeight;
sf::IntRect animFrame;
vector< pair<pair<int,int>, int> > frameBuffer;
int currentFrame;
sf::Clock timer;
sf::Time elapsedTime;
};
#endif
gameSprite.cpp#include <SFML/Graphics.hpp>
#include <vector>
#include <utility>
#include <string>
#include <iostream>
using namespace std;
#include "gameSprite.h"
#include "global.h"
gameSprite::gameSprite(string SpriteSheet, int Rows, int Columns, int x, int y)
{
spriteSheet = SpriteSheet;
rows = Rows;
columns = Columns;
texture.loadFromFile(spriteSheet);
frameWidth = texture.getSize().x / rows;
frameHeight = texture.getSize().y / columns;
animFrame.width = frameWidth;
animFrame.height = frameHeight;
setFrame(0,0);
pair<pair<int,int>, int> tempFrameBuff ((make_pair(0,0)),.9);
pair<pair<int,int>, int> tempFrameBuff2 ((make_pair(0,1)),.9);
frameBuffer.push_back(tempFrameBuff);
frameBuffer.push_back(tempFrameBuff2);
currentFrame = 0;
}
void gameSprite::update()
{
elapsedTime = timer.getElapsedTime();
if (elapsedTime.asSeconds() >= frameBuffer[currentFrame].second)
{
if(currentFrame + 1 < frameBuffer.size()) {
currentFrame += 1;
} else {
currentFrame = 0;
}
cout << currentFrame << endl;
setFrame ( (frameBuffer[currentFrame].first).first, (frameBuffer[currentFrame].first).second );
cout << "The time has elapsed!" << endl;
timer.restart();
}
}
sf::Sprite gameSprite::getSprite()
{
sprite.setTexture(texture);
return sprite;
}
void gameSprite::changeSpriteSheet(string newSheet)
{
spriteSheet = newSheet;
}
void gameSprite::setFrame(int row, int column)
{
cout << "row: " << row << "column: " << column << endl;
//std::cout << row << std::endl;
//std::cout << frameWidth << std::endl;
animFrame.left = column * frameWidth;
animFrame.top = row * frameHeight;
sprite.setTextureRect(animFrame);
}
Sorry for that massive wall of text x.x If anyone could help that'd be awesome. I don't know why it's running in bursts like this. I'm using SFML 2 btw.