Browse Source

Initial commit

master
Macoy Madson 6 years ago
parent
commit
e9e2128446
44 changed files with 18954 additions and 0 deletions
  1. +52
    -0
      README.md
  2. +2
    -0
      README.md~
  3. +139
    -0
      animation/animation.cpp
  4. +60
    -0
      animation/animation.hpp
  5. +1394
    -0
      collision/collision.cpp
  6. +93
    -0
      collision/collision.hpp
  7. +331
    -0
      ept/eptParser.cpp
  8. +68
    -0
      ept/eptParser.hpp
  9. +29
    -0
      ept/eptSpecification.txt
  10. +195
    -0
      graphics/graphics.cpp
  11. +162
    -0
      graphics/graphics.cpp~
  12. +103
    -0
      graphics/graphics.hpp
  13. +91
    -0
      graphics/graphics.hpp~
  14. +379
    -0
      input/input.cpp
  15. +21
    -0
      input/input.cpp~
  16. +154
    -0
      input/input.hpp
  17. +15
    -0
      input/input.hpp~
  18. +136
    -0
      makefile
  19. +92
    -0
      math/math.cpp
  20. +36
    -0
      math/math.hpp
  21. +5
    -0
      network/network.cpp
  22. +24
    -0
      network/network.hpp
  23. +114
    -0
      profiler/profiler.cpp
  24. +44
    -0
      profiler/profiler.hpp
  25. +90
    -0
      sound/sound.cpp
  26. +40
    -0
      sound/sound.hpp
  27. +453
    -0
      tileMap/multilayerMap.cpp
  28. +150
    -0
      tileMap/multilayerMap.hpp
  29. +678
    -0
      tileMap/multilayerMapEditor.cpp
  30. +63
    -0
      tileMap/tileCamera.cpp
  31. +45
    -0
      tileMap/tileCamera.hpp
  32. +569
    -0
      tileMap/tileEditor.cpp
  33. +592
    -0
      tileMap/tileMap.cpp
  34. +161
    -0
      tileMap/tileMap.hpp
  35. +72
    -0
      tiledImporter/pugixml/pugiconfig.hpp
  36. +10639
    -0
      tiledImporter/pugixml/pugixml.cpp
  37. +1332
    -0
      tiledImporter/pugixml/pugixml.hpp
  38. +272
    -0
      tiledImporter/tiledImporter.cpp
  39. +15
    -0
      timer/timer.cpp
  40. +18
    -0
      timer/timer.hpp
  41. +8
    -0
      toLink.txt
  42. BIN
      tools/editorFont.ttf
  43. +9
    -0
      tools/mlme.desktop
  44. +9
    -0
      tools/tle.desktop

+ 52
- 0
README.md View File

@ -1,2 +1,54 @@
# base2.0
A simple game library and abstraction layer for SFML 2.2
------------------
Base
------------------
Setup
-----
You need SFML 2.0 installed if you have base2.0, or SFML 1.6 if you have base.
It's then really easy from there; just
make
make tools
sudo make install
sudo make installTools
If you want to clean up:
sudo make clean
sudo make cleanTools
Installing the library will result in the following:
/usr/include/base2.0 will contain all of the header files. To use them:
#include <base2.0/module/module.hpp>
/usr/lib will contain libbase2.0.a or libbase.a . Use these when linking like so:
g++ -o "myProj" myProj.o -lbase2.0 [or -lbase] -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system
The Tile Map Editor (tileEditor) binary will be added to /usr/bin
"tle.desktop" will be added to /usr/share/applications, which adds the Tile Map Editor to the Ubuntu Dash
Cleaning will undo all of these changes.
Basic Use (installed)
------
#include <base[or base2.0]/module/module.hpp>
To link:
g++ -o "myProj" myProj.o -lbase [or -lbase2.0] -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system
Basic Use (not installed)
------
#include "/path/to/base/module/module.hpp"
To link:
g++ -o "myProj" myProj.o "/path/to/base/lib/base.a" -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system
About
------
Base/Base2.0 are my game libraries and SFML abstraction layers. I've built it with the goal of a simple interface and maximum multimedia library independence. You are free to use Base in any way you want (but don't sell it).
Questions? Email me: macoymadson@gmail.com
Check out my website: http://augames.f11.us/

+ 2
- 0
README.md~ View File

@ -0,0 +1,2 @@
# base2.0
A simple game library and abstraction layer for SFML 2.2

+ 139
- 0
animation/animation.cpp View File

@ -0,0 +1,139 @@
#ifndef ANIMATION_CPP
#define ANIMATION_CPP
#include "animation.hpp"
////////////////////////////////////////////////////
// Animation ///////////////////////////////////////
////////////////////////////////////////////////////
animation::animation()
{
sourceImage=NULL;
frameRate=0;
totalFrames=0; //starts at 1; if you have 3 different frames, put in 3
currentFrame=0;
currentTime.start();
isPlaying=true;
startX=0;
startY=0;
addX=0;
addY=0;
widthX=0;
widthY=0;
originX=originY=0;
rotation=0;
posX=posY=0;
}
void animation::setImage(sprite* newImage)
{
sourceImage=newImage;
}
void animation::setFrameRate(float newFrameRate)
{
frameRate=newFrameRate;
}
void animation::setTotalFrames(int newTotal)
{
totalFrames=newTotal;
}
void animation::setStartPosition(float x, float y)
{
startX=x;
startY=y;
}
void animation::setAddAmount(float x, float y)
{
addX=x;
addY=y;
}
void animation::setWidth(float x, float y)
{
widthX=x;
widthY=y;
}
void animation::setPosition(float newX, float newY)
{
posX=newX;
posY=newY;
}
void animation::setOrigin(float x, float y)
{
originX=x;
originY=y;
}
void animation::setRotation(float newRot)
{
rotation=newRot;
}
float animation::getX()
{
return posX;
}
float animation::getY()
{
return posY;
}
void animation::render(window* win)
{
float newX=0;
float newY=0;
//If the animation is playing, update the frames
if (isPlaying)
{
//If it's time to change frames
if (currentTime.getTime()>=frameRate)
{
//Change frames
if (currentFrame+1==totalFrames || currentFrame>totalFrames) currentFrame=0;
else ++currentFrame;
currentTime.start();
}
}
//The magic happens here; find the frame's top left corner
newX=startX + (addX * currentFrame);
newY=startY + (addY * currentFrame);
//Prepare the image
sourceImage->setSubRect(newX, newY, widthX, widthY);
sourceImage->setOrigin(originX, originY);
sourceImage->setRotation(rotation);
sourceImage->setPosition(posX, posY);
//Draw the image
win->draw(sourceImage);
//Just in case a subrect messes someone up down the line
sourceImage->clearSubRect();
}
//Starts the animation at zero
void animation::start()
{
currentFrame=0;
currentTime.start();
}
//Stops the animation on the first frame
void animation::stop()
{
isPlaying=false;
currentFrame=0;
}
//Pauses the animation on its current frame
void animation::pause()
{
isPlaying=false;
}
void animation::resume()
{
isPlaying=true;
}
void animation::configureFromEpt(eptGroup* currentAnimation)
{
frameRate=atof(currentAnimation->getAttribute("frameRate").c_str());
totalFrames=atoi(currentAnimation->getAttribute("totalFrames").c_str());
startX=atof(currentAnimation->getAttribute("startX").c_str());
startY=atof(currentAnimation->getAttribute("startY").c_str());
addX=atof(currentAnimation->getAttribute("addX").c_str());
addY=atof(currentAnimation->getAttribute("addY").c_str());
widthX=atof(currentAnimation->getAttribute("widthX").c_str());
widthY=atof(currentAnimation->getAttribute("widthY").c_str());
originX=atof(currentAnimation->getAttribute("originX").c_str());
originY=atof(currentAnimation->getAttribute("originY").c_str());
}
#endif

+ 60
- 0
animation/animation.hpp View File

@ -0,0 +1,60 @@
#ifndef ANIMATION_HPP
#define ANIMATION_HPP
#include "../graphics/graphics.hpp"
#include "../ept/eptParser.hpp"
#include "../timer/timer.hpp"
class animation
{
private:
sprite* sourceImage;
float frameRate;
int totalFrames;
int currentFrame;
timer currentTime;
bool isPlaying;
float startX;
float startY;
//This will be added for a new frame
float addX;
float addY;
float widthX;
float widthY;
//Mostly for rotation
float originX, originY;
float rotation;
float posX, posY;
public:
animation();
//All of these must be set (I recommend an EPT file for these!)
void setImage(sprite*);
void setFrameRate(float);
void setTotalFrames(int);
void setStartPosition(float, float);
void setAddAmount(float, float);
void setWidth(float, float);
void setOrigin(float, float);
void setRotation(float);
void setPosition(float, float);
float getX();
float getY();
void render(window*);
//Starts the animation at zero
void start();
//Stops the animation on the first frame
void stop();
//Pauses the animation on its current frame
void pause();
//Resume the animation
void resume();
//Use an EPT file to set up the animation (ultra convenient)
void configureFromEpt(eptGroup*);
};
#endif

+ 1394
- 0
collision/collision.cpp
File diff suppressed because it is too large
View File


+ 93
- 0
collision/collision.hpp View File

@ -0,0 +1,93 @@
#ifndef COLLISION_HPP
#define COLLISION_HPP
#include <vector>
#include "../math/math.hpp"
//////////////////////////////////////////////////////////////////////////////////
//Axis aligned bounding boxes //////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//NOTE: X & Y are assumed to be in the top left, so if there's offset you must adjust
struct vec2
{
float x;
float y;
};
class aabb
{
public:
float x, y, w, h;
aabb();
//Takes x, y, w, h
aabb(float, float, float, float);
void setPosition(float, float);
void resize(float, float);
float getX();
float getY();
float getWidth();
float getHeight();
};
bool isColliding(aabb *, aabb*);
//preventCollision takes two bounding boxes and a velocity vector and modifies
//the velocity in order to prevent the two bounding boxes from colliding. The
//new velocity is stored in newVelX and newVelY. It is assumed that the boxes
//never overlap (so they shouldn't start when overlapping). Rand is used when
//on a corner to prevent hitching. TOUCHING_TOLERANCE sets the tolerance of
//what is considered touching and what is considered overlapping (useful if
//there are hitches when sliding).
//Note that movingObj should have the bounds it would have BEFORE the translation.
//When you adjust the velocity, do not change the bounds - leave them until
//all collision checks are complete.
void preventCollision(aabb& movingObj, aabb& staticObj, float velX, float velY, float& newVelX, float& newVelY, const float TOUCHING_TOLERANCE, bool debug);
bool resolveCollision(aabb*, aabb*, vec2* velocity);
//////////////////////////////////////////////////////////////////////////////////
//Simple sphere-to-sphere testing //////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
class circleBound
{
public:
float x, y, r;
circleBound();
circleBound(float, float, float);
void setPosition(float, float);
void setRadius(float);
};
bool isColliding(circleBound *, circleBound*);
//Returns the manhattan distance from the first pair of coordinates to the second
//float manhattanTo(float, float, float, float); //See math
//////////////////////////////////////////////////////////////////////////////////
//SAT collision detection (ANY convex shape) /////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/**POTENTIAL IMPROVEMENTS
* -Optimize the resolve code, it's quite large
* -Clean up the function calls to the resolve code
* -Make a file load bounding boxes with XML or something**/
struct point;
class satBound
{
public:
std::vector< point> vertices;
std::vector< point> normals;
int totalVertices;
float x;
float y;
satBound();
//Pass in the number of vertices the bound needs (ex 3 for a triangle)
satBound(int);
//Change the number of vertices in the bound (the previous vertices will stay)
bool resetSize(unsigned int);
//Pass in x,y,index , where index starts at 0
void setVertex(float, float, int);
//Generates the normals for the bounding box. This is a heavier function so call sparingly
void generateNormals();
//Sets the bound's position to 0
void resetPosition();
//Translates the bound. It has to loop through & manipulate every vertex, so try to reduce usage as much as necessary
void setPosition(float, float);
};
/*True=colliding
* tarPos is assumed to be the same as tar->x and tar->y. If there was a collision tarPos will contain resolving position.
* resolveAlignedCollision() is when you want to only test against the X and Y axes
* resolveComplexCollision() is for testing non-axis-aligned bounds. It will test against all axes.*/
bool resolveAlignedCollision(satBound *tar, satBound *stat, point *tarPos, point *statPos);
bool resolveComplexCollision(satBound *tar, satBound *stat, point *tarPos, point *statPos);
#endif

+ 331
- 0
ept/eptParser.cpp View File

@ -0,0 +1,331 @@
#ifndef EPT_PARSER_CPP
#define EPT_PARSER_CPP
#include <iostream>
#include <fstream>
#include <cstring> //Used for strstr()
#include <cstdlib> //atoi()
#include <sstream> //Used for int||floatToAttr() functions
#include "eptParser.hpp"
const int SUPPORTED_VERSION=1;
////////////////////////////////////////////////
std::string eptGroup::getAttribute(const std::string& attrName)
{
std::map<std::string, std::string>::iterator myIt=attributes.find(attrName);
if (myIt==attributes.end()) return "";
else return myIt->second;
}
void eptGroup::setAttribute(std::string name, std::string value)
{
attributes[name]=value;
}
std::string eptGroup::getAttributeFromIndex(int i, std::string& attributeName)
{
std::map<std::string, std::string>::iterator it=attributes.begin();
for (int n=0; n<i; ++n)
{
++it;
}
if (it !=attributes.end())
{
attributeName=it->first;
return it->second;
}
else
{
attributeName="";
}
return "";
}
int eptGroup::getTotalAttributes()
{
return attributes.size();
}
////////////////////////////////////////////////
eptFile::eptFile()
{
version=99;
}
eptFile::~eptFile()
{
//clearAllData();
}
eptGroup* eptFile::getGroup(const std::string& groupName)
{
eptGroup* returnValue=NULL;
std::map<std::string, eptGroup*>::iterator myIt=groups.find(groupName);
if (myIt!=groups.end()) returnValue=myIt->second;
return returnValue;
}
void eptFile::addGroup(const std::string& groupName, eptGroup* newGroup)
{
groups[groupName]=newGroup;
}
int eptFile::getVersion()
{
return version;
}
///???
void eptFile::clearAllData()
{
for (std::map<std::string, eptGroup*>::iterator groupIt=groups.begin(); groupIt !=groups.end(); ++groupIt)
{
delete groupIt->second;
//groupIt=groups.erase(groupIt);
}
}
void eptFile::setVersion(int newVersion)
{
version=newVersion;
}
//Shows how easy it is to dump EPT stuff
bool eptFile::dumpToFile(std::string fileName, std::string collectionName)
{
std::ofstream out;
out.open(fileName.c_str());
if (!out.is_open()) return false;
//Header information
out << "<" << collectionName << "." << version << ">\n\n";
//Groups
for (std::map<std::string, eptGroup*>::iterator myIt=groups.begin(); myIt!=groups.end(); ++myIt)
{
//Group name
out << myIt->first << ":\n";
//Attributes
for (std::map<std::string, std::string>::iterator attrIt=myIt->second->attributes.begin();
attrIt !=myIt->second->attributes.end(); ++attrIt)
{
out << "\t"<< attrIt->first << "=" << attrIt->second << ";\n";
}
}
out.close();
return true;
}
eptGroup* eptFile::getGroupFromIndex(int i, std::string& groupName)
{
std::map<std::string, eptGroup*>::iterator it=groups.begin();
for (int n=0; n<i; ++n)
{
++it;
}
if (it !=groups.end())
{
groupName=it->first;
return it->second;
}
else
{
groupName="";
}
return NULL;
}
std::string eptFile::getAttribute(const std::string& location)
{
unsigned int strPos = location.find(".");
eptGroup* group = getGroup(location.substr(0, strPos));
if (group)
{
return group->getAttribute(location.substr(strPos + 1, location.length() - 1));
}
return "";
}
////////////////////////////////////////////////
eptParser::~eptParser()
{
clearAllData();
}
eptFile* eptParser::getFile(const std::string& fileName)
{
eptFile* returnValue=NULL;
std::map<std::string, eptFile*>::iterator finder=files.find(fileName);
if (finder!=files.end()) returnValue=finder->second;
return returnValue;
}
//Ugh, file I/O...
//Now that I see this is so huge, the E part of EPT might be a bit of a misnomer...
bool eptParser::load(const char* fileName)
{
std::ifstream in;
in.open(fileName);
if (in.is_open())
{
//Get a new file to work with
eptFile* newFile=new eptFile;
std::string line;
//First line is file header
getline(in, line);
//Grab the collection/file name
unsigned int strPos=line.find(".");
if (strPos>line.length())
{
std::cout << "Incorrect header format; file failed to load (couldn't find '.')\n";
in.close();
return false;
}
std::string fileInternalName = line.substr(1, strPos-1);
//Check if the file already exists
std::map<std::string, eptFile*>::iterator search = files.find(fileInternalName);
if (search!=files.end())
{
std::cout << "WARNING: File '"<< fileName <<"'already loaded\n";
return true;
}
else
{
//Insert file into map
files[fileInternalName]=newFile;
}
//newFile=files.find(line.substr(1, strPos-1))->second;
//Set the file's version
unsigned int strPosB=line.find(">");
if (strPosB>line.length())
{
std::cout << "Incorrect header format; file failed to load (couldn't find '>')\n";
in.close();
return false;
}
int newVersion=atoi(line.substr(strPos+1, strPosB-1).c_str());
if (newVersion !=SUPPORTED_VERSION)
{
std::cout << "Version not supported (supports " << SUPPORTED_VERSION << "); file failed to load\n";
in.close();
return false;
}
newFile->setVersion(newVersion);
eptGroup* currentGroup=NULL;
//Go line by line and parse each line individually
while (in.good())
{
getline(in, line);
//If '//' is found, the line is a comment and should be ignored
if (line.length()==0 || strstr(line.c_str(), "//"))
{
if (!in.eof()) continue;
else break;
}
//Look for a new group definition
strPos=line.find(":");
if (strPos < line.length())
{
for (strPosB=0; strPosB!=line.length(); ++strPosB)
{
if (line[strPosB] !=' ' && line[strPosB] !='\t') break;
}
newFile->addGroup(line.substr(strPosB, strPos-strPosB), new eptGroup);
currentGroup=newFile->getGroup(line.substr(strPosB, strPos-strPosB));
continue;
}
//Look for an attribute definition (if there is no =, there is none)
strPos=line.find("=");
if (strPos>line.length()) continue;
//Trim any spaces etc.
for (strPosB=0; strPosB!=line.length(); ++strPosB)
{
if (line[strPosB] !=' ' && line[strPosB] !='\t') break;
}
std::string attrName=line.substr(strPosB, strPos-strPosB);
//Find the semicolon
strPosB=line.find(";");
while (strPosB>line.length())
{
//std::cout << "Attribute value does not terminate properly (forget the ';'?). Attr Ignored...\n";
//continue;
line+='\n';
std::string temp;
temp=line;
getline(in, line);
temp+=line;
line=temp;
strPosB=line.find(";");
if (!in.good())
{
std::cout << " EPT: Forget semicolon!\n";
return false;
}
}
currentGroup->setAttribute(attrName,line.substr(strPos + 1, strPosB - (strPos + 1)));
}
in.close();
return true;
}
else return false;
}
void eptParser::clearAllData()
{
}
void eptParser::addFile(std::string collectionName, eptFile* newFile)
{
files[collectionName]=newFile;
}
eptGroup* eptParser::getGroup(const std::string& location)
{
eptGroup* group=NULL;
unsigned int strPos=location.find(".");
eptFile* file=getFile(location.substr(0, strPos));
if (file)
{
std::string newLocation=location.substr(strPos+1, location.length()-1);
strPos=newLocation.find(".");
group=file->getGroup(newLocation.substr(0, strPos));
}
return group;
}
std::string eptParser::getAttribute(const std::string& location)
{
std::string returnString="";
unsigned int strPos=location.find(".");
eptFile* file=getFile(location.substr(0, strPos));
if (file)
{
std::string newLocation=location.substr(strPos+1, location.length()-1);
strPos=newLocation.find(".");
eptGroup* group=file->getGroup(newLocation.substr(0, strPos));
if (group)
{
newLocation=newLocation.substr(strPos+1, newLocation.length()-1);
returnString=group->getAttribute(newLocation);
}
}
return returnString;
}
///////////////////////////
/////Utility Functions/////
///////////////////////////
float attrToFloat(const std::string& attr)
{
if (attr.empty()) return 0;
return atof(attr.c_str());
}
int attrToInt(const std::string& attr)
{
if (attr.empty()) return 0;
return atoi(attr.c_str());
}
bool attrToBool(const std::string& attr)
{
if (attr.empty()) return false;
if (attr=="true") return true;
return false;
}
std::string floatToAttr(float in)
{
std::ostringstream s;
s << in;
return s.str();
}
std::string intToAttr(int in)
{
std::ostringstream s;
s << in;
return s.str();
}
#endif

+ 68
- 0
ept/eptParser.hpp View File

@ -0,0 +1,68 @@
#ifndef EPT_PARSER_HPP
#define EPT_PARSER_HPP
#include <string>
#include <map>
#include <vector>
//Should this be pointers? What would be fastest? (references?)
class eptFile;
class eptGroup
{
friend class eptFile;
protected:
std::map<std::string, std::string> attributes;
public:
std::string getAttribute(const std::string&);
void setAttribute(std::string, std::string);
//Allows looping through the map (ignores textual indices); Break with ""
//Second param is name
std::string getAttributeFromIndex(int, std::string&);
int getTotalAttributes();
};
class eptFile
{
private:
std::map<std::string, eptGroup*> groups;
int version;
public:
eptFile();
~eptFile();
eptGroup* getGroup(const std::string&);
//Pointer because groups can be large
void addGroup(const std::string&, eptGroup*);
int getVersion();
void setVersion(int);
void clearAllData();
//File name, then this file/collection's name
bool dumpToFile(std::string, std::string);
//Loop through all groups via an indice (second param is name)
//Break on NULL pointer
eptGroup* getGroupFromIndex(int, std::string&);
//Get an attribute via dot notation (eg. group.attribute)
std::string getAttribute(const std::string&);
};
class eptParser
{
private:
std::map<std::string, eptFile*> files;
public:
~eptParser();
eptFile* getFile(const std::string&);
eptGroup* getGroup(const std::string&);
void addFile(std::string, eptFile*);
bool load(const char*);
void clearAllData();
//Use dotted notation to get an attribute (safer)
//eg. files.ept loaded; "files.images.defaultSprite" will open
//eptFile files, find group images, and return attr defaultSprite
//PASS POINTER INSTEAD FOR EDITING? HOW?
std::string getAttribute(const std::string&);
};
//Some basic utility functions to make life easier
//Returns 0 if failure
float attrToFloat(const std::string&);
int attrToInt(const std::string&);
bool attrToBool(const std::string&);
std::string floatToAttr(float);
std::string intToAttr(int);
#endif

+ 29
- 0
ept/eptSpecification.txt View File

@ -0,0 +1,29 @@
----------------
EPT SPECIFICATION
----------------
-Format:
<fileName.version>
//Comment (SEE NOTE!)
groupName:
attribute=value;
-WARNING! Attributes are sorted in the map, meaning if you have any
order dependency, you must be conscious of the attribute's name!
-Value can be anything. Values are terminated by ";", so no quotation
marks are necessary or needed for strings. To EPT, ints, strings, etc.
are all the same thing as they are stored as mere strings.
-Multiline attributes are supported; everything will be extracted until the next semicolon
-Header is space sensitive
-Using : is NOT OK in an attribute definition! This is mainly a result of the
precendence I coded for, so it's probably not *that* hard to change if you need to
-NOTE! Comments use "//" and operate on the ENTIRE line! This means that
test=This is a test; //Here is an example attribute
will IGNORE THE ENTIRE LINE!
-It is NOT OK to use "." in a group definition (think about dotted notation). "." is OK
in an attribute name or definition.
-A group definition ends the instant a line contains a ':' (a new group definition)

+ 195
- 0
graphics/graphics.cpp View File

@ -0,0 +1,195 @@
#ifndef GRAPHICS_CPP
#define GRAPHICS_CPP
#include "graphics.hpp"
#include <iostream>
///////////////////////////////////////////////
// Window //////////////////////////////////
///////////////////////////////////////////////
window::window(int width,int height, std::string title)
{
isClearing=true;
win=new sf::RenderWindow(sf::VideoMode(width, height), title, sf::Style::Close);
//win->setVerticalSyncEnabled(false);
//win->setFramerateLimit(45);
focused=true;
time.start();
}
//Close & delete the window
window::~window()
{
win->close();
delete win;
}
void window::draw(sprite * spr)
{
if (spr) win->draw(spr->spr);
}
void window::draw(text *txt)
{
if (txt) win->draw(txt->str);
}
void window::update()
{
time.start();
win->display();
if (isClearing) win->clear(background);
}
///Doesn't work
float window::getFrameTime()
{
//return win->GetFrameTime(); //Looks like it's deprecated; let's just use a timer then
return time.getTime();
}
//This is a sloppy function because it does a lot more then tell whether or
//not the window should close, but it works well in this condition
bool window::shouldClose()
{
sf::Event event;
while (win->pollEvent(event))
{
if (event.type==sf::Event::Closed) return true;
if (event.type==sf::Event::Resized)
{
//std::cout << "Window resized to " << event.size.width << " , " << event.size.height << "\n";
//win->setSize(sf::Vector2u(event.size.width, event.size.height));
}
if (event.type==sf::Event::GainedFocus)
{
focused=true;
}
if (event.type==sf::Event::LostFocus)
{
focused=false;
}
}
return false;
}
bool window::isFocused()
{
return focused;
}
void window::setBackgroundColor(char r, char g, char b, char a)
{
background.r=r;
background.g=g;
background.b=b;
background.a=a;
}
//If you don't want to clear the window after updating (at all)
void window::shouldClear(bool newState)
{
isClearing=newState;
}
//Dimensions
unsigned int window::getWidth()
{
return win->getSize().x;
}
unsigned int window::getHeight()
{
return win->getSize().y;
}
//If you need the SFML window itself
sf::RenderWindow* window::getBase()
{
return win;
}
///////////////////////////////////////////////
// Text ///////////////////////////////////////
///////////////////////////////////////////////
bool text::loadFont(const char* fileName)
{
if (!font.loadFromFile(fileName)) return false;
else
{
str.setFont(font);
return true;
}
}
void text::setText(std::string newText)
{
str.setString(newText);
}
void text::setPosition(float newX, float newY)
{
str.setPosition(newX, newY);
}
float text::getX()
{
return str.getPosition().x;
}
float text::getY()
{
return str.getPosition().y;
}
void text::setSize(unsigned int size)
{
str.setCharacterSize(size);
}
void text::setColor(char r, char g, char b, char a)
{
str.setColor(sf::Color(r,g,b,a));
}
sf::Text* text::getBase()
{
return &str;
}
///////////////////////////////////////////////
// Sprite /////////////////////////////////////
///////////////////////////////////////////////
bool sprite::load(const char* filename)
{
if (!srcImage.loadFromFile(filename)) return false;
spr.setTexture(srcImage);
return true;
}
void sprite::setPosition(float newX, float newY)
{
spr.setPosition(newX, newY);
}
void sprite::addVector(float newX, float newY)
{
spr.setPosition(newX+getX(), newY+getY());
}
float sprite::getX()
{
return spr.getPosition().x;
}
float sprite::getY()
{
return spr.getPosition().y;
}
unsigned int sprite::getWidth()
{
return srcImage.getSize().x;
}
unsigned int sprite::getHeight()
{
return srcImage.getSize().y;
}
void sprite::setRotation(float rotation)
{
spr.setRotation(rotation);
}
void sprite::setOrigin(float x, float y)
{
spr.setOrigin(x, y);
}
void sprite::setSmooth(bool useSmooth)
{
srcImage.setSmooth(useSmooth);
}
void sprite::setSubRect(int x, int y, int wx, int hy)
{
spr.setTextureRect(sf::Rect<int>(x,y,wx,hy));
}
void sprite::clearSubRect()
{
setSubRect(0, 0, getWidth(), getHeight());
}
sf::Sprite* sprite::getBase()
{
return &spr;
}
#endif

+ 162
- 0
graphics/graphics.cpp~ View File

@ -0,0 +1,162 @@
#include "graphics.hpp"
//#include <iostream>
///////////////////////////////////////////////
// Window //////////////////////////////////
///////////////////////////////////////////////
window::window(int width,int height, std::string title)
{
isClearing=true;
win=new sf::RenderWindow(sf::VideoMode(width, height), title);
time.start();
}
//Close & delete the window
window::~window()
{
win->close();
delete win;
}
void window::draw(sprite * spr)
{
win->draw(spr->spr);
}
void window::draw(text *txt)
{
win->draw(txt->str);
}
void window::update()
{
win->display();
if (isClearing) win->clear(background);
time.start();
}
float window::getFrameTime()
{
//return win->GetFrameTime(); //Looks like it's deprecated; let's just use a timer then
return time.getTime();
}
bool window::shouldClose()
{
sf::Event event;
while (win->pollEvent(event))
{
if (event.type==sf::Event::Closed) return true;
}
return false;
}
///DOESN'T WORK
bool window::isFocused()
{
//std::cout << "Preparing to test events...\n";
sf::Event event;
while (win->pollEvent(event))
{
//std::cout << "An event was detected\n";
if (event.type==sf::Event::LostFocus) return false;
if (event.type==sf::Event::GainedFocus) return true;
}
//std::cout << "No focus events, returning true...\n";
return true;
}
void window::setBackgroundColor(char r, char g, char b, char a)
{
background.r=r;
background.g=g;
background.b=b;
background.a=a;
}
//If you don't want to clear the window after updating (at all)
void window::shouldClear(bool newState)
{
isClearing=newState;
}
//Dimensions
unsigned int window::getWidth()
{
return win->getSize().x;
}
unsigned int window::getHeight()
{
return win->getSize().y;
}
//If you need the SFML window itself
sf::RenderWindow* window::getBase()
{
return win;
}
///////////////////////////////////////////////
// Text ///////////////////////////////////////
///////////////////////////////////////////////
void text::setText(std::string newText)
{
str.setString(newText);
}
void text::setPosition(float newX, float newY)
{
str.setPosition(newX, newY);
}
float text::getX()
{
return str.getPosition().x;
}
float text::getY()
{
return str.getPosition().y;
}
void text::setSize(unsigned int size)
{
str.setCharacterSize(size);
}
void text::setColor(char r, char g, char b, char a)
{
str.setColor(sf::Color(r,g,b,a));
}
sf::Text* text::getBase()
{
return &str;
}
///////////////////////////////////////////////
// Sprite /////////////////////////////////////
///////////////////////////////////////////////
bool sprite::load(const char* filename)
{
if (!srcImage.loadFromFile(filename)) return false;
spr.setImage(srcImage);
return true;
}
void sprite::setPosition(float newX, float newY)
{
spr.setPosition(newX, newY);
}
void sprite::addVector(float newX, float newY)
{
spr.setPosition(newX+getX(), newY+getY());
}
float sprite::getX()
{
return spr.getPosition().x;
}
float sprite::getY()
{
return spr.getPosition().y;
}
void sprite::setRotation(float rotation)
{
spr.setRotation(rotation);
}
void sprite::setOrigin(float x, float y)
{
spr.setOrigin(x, y);
}
void sprite::setSmooth(bool useSmooth)
{
srcImage.setSmooth(useSmooth);
}
void sprite::setSubRect(int x, int y, int wx, int hy)
{
spr.setTextureRect(sf::Rect<int>(x,y,wx,hy));
}
sf::Sprite* sprite::getBase()
{
return &spr;
}

+ 103
- 0
graphics/graphics.hpp View File

@ -0,0 +1,103 @@
/*This is the graphics layer for the game. It is meant to make it easy to switch out the graphics
* library at a later time, if necessary. It also makes it a little easier to handle scripting language
* bindings, if they are eventually added. Note that for now I won't be adding primitives like rectangles
* or lines. They will be added if needed.
* */
#ifndef GRAPHICS_HPP
#define GRAPHICS_HPP
#include <string>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "../timer/timer.hpp" //Useful for getframetime
//To link:
//g++ -o "%e" "%f" -lsfml-graphics -lsfml-window -lsfml-system
class window;
//Renderable text
class text
{
friend class window;
private:
sf::Text str;
sf::Font font;
public:
void setText(std::string);
bool loadFont(const char*);
void setPosition(float newX, float newY);
float getX();
float getY();
void setSize(unsigned int);
void setColor(char r, char g, char b, char a);
//Returns the actual element used for rendering
sf::Text* getBase();
};
//A single renderable image
class sprite
{
friend class window;
private:
sf::Texture srcImage;
sf::Sprite spr;
public:
//Load and configure the sprite (false=failed)
bool load (const char* filename);
//Set the sprite's new position
void setPosition(float, float);
//Add a vector-esque value to the position
void addVector(float, float);
//Get the sprite's current position
float getX();
float getY();
//Rotate the sprite
void setRotation(float rotation);
//Change the origin
void setOrigin(float,float);
//Good for tilemap tiles
void setSmooth(bool);
//Returns the width and height of the image
unsigned int getWidth();
unsigned int getHeight();
//Splits a part of the sprite out
//Takes the top left corner coordinates and a width and height (both bases)
void setSubRect(int,int,int,int);
//Back to whole sprite
void clearSubRect();
//Return a direct pointer to the base sprite (lib dependant)
sf::Sprite* getBase();
};
//The core of the graphics, the window.
class window
{
private:
sf::RenderWindow *win;
sf::Color background;
bool isClearing;
bool focused;
timer time;
public:
window(int width,int height, std::string title);
//Close & delete the window
~window();
//Various drawing functions
void draw(sprite * spr);
void draw(text *txt);
//Update the window
void update();
//Test if the window should close
bool shouldClose();
//If the window is focused (true) or buried (false)
bool isFocused();
//Time since last frame
float getFrameTime();
//Window configuration & management
void setBackgroundColor(char, char, char, char);
//If you don't want to clear the window after updating (at all)
void shouldClear(bool newState);
//Get the dimensions of the window
unsigned int getWidth();
unsigned int getHeight();
//Return a direct pointer to the base (library dependant)
sf::RenderWindow* getBase();
};
#endif

+ 91
- 0
graphics/graphics.hpp~ View File

@ -0,0 +1,91 @@
/*This is the graphics layer for the game. It is meant to make it easy to switch out the graphics
* library at a later time, if necessary. It also makes it a little easier to handle scripting language
* bindings, if they are eventually added. Note that for now I won't be adding primitives like rectangles
* or lines. They will be added if needed.
* */
#include <string>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "../timer/timer.cpp" //Useful for getframetime
//To link:
//g++ -o "%e" "%f" -lsfml-graphics -lsfml-window -lsfml-system
class window;
//Renderable text
class text
{
friend class window;
private:
sf::Text str;
public:
void setText(std::string newText);
void setPosition(float newX, float newY);
float getX();
float getY();
void setSize(unsigned int);
void setColor(char r, char g, char b, char a);
//Returns the actual element used for rendering
sf::Text* getBase();
};
//A single renderable image
class sprite
{
friend class window;
private:
sf::Image srcImage;
sf::Sprite spr;
public:
//Load and configure the sprite (false=failed)
bool load (const char* filename);
//Set the sprite's new position
void setPosition(float, float);
//Add a vector-esque value to the position
void addVector(float, float);
//Get the sprite's current position
float getX();
float getY();
//Rotate the sprite
void setRotation(float rotation);
//Change the origin
void setOrigin(float,float);
//Good for tilemap tiles
void setSmooth(bool);
//Splits a part of the sprite out
void setSubRect(int,int,int,int);
//Return a direct pointer to the base sprite (lib dependant)
sf::Sprite* getBase();
};
//The core of the graphics, the window.
class window
{
private:
sf::RenderWindow *win;
sf::Color background;
bool isClearing;
timer time;
public:
window(int width,int height, std::string title) ;
//Close & delete the window
~window();
//Various drawing functions
void draw(sprite * spr);
void draw(text *txt);
//Update the window
void update();
//Test if the window should close
bool shouldClose();
//If the window is focused (true) or buried (false)
bool isFocused();
//Time since last frame
float getFrameTime();
//Window configuration & management
void setBackgroundColor(char, char, char, char);
//If you don't want to clear the window after updating (at all)
void shouldClear(bool newState);
//Get the dimensions of the window
unsigned int getWidth();
unsigned int getHeight();
//Return a direct pointer to the base (library dependant)
sf::RenderWindow* getBase();
};

+ 379
- 0
input/input.cpp View File

@ -0,0 +1,379 @@
#ifndef INPUT_CPP
#define INPUT_CPP
#include "input.hpp"
inputManager::inputManager(sf::RenderWindow *newWin)
{
win=newWin;
}
inputManager::inputManager(window *newWin)
{
win=newWin->getBase();
}
inputManager::inputManager()
{
win=NULL;
}
void inputManager::setWindow(sf::RenderWindow *newWin)
{
win=newWin;
}
void inputManager::setWindow(window *newWin)
{
win=newWin->getBase();
}
int inputManager::getMouseX()
{
return sf::Mouse::getPosition(*win).x;
}
int inputManager::getMouseY()
{
return sf::Mouse::getPosition(*win).y;
}
bool inputManager::isPressed(sf::Keyboard::Key key)
{
return sf::Keyboard::isKeyPressed(key);
}
bool inputManager::isPressed(inputCode::keyCode key)
{
return sf::Keyboard::isKeyPressed(convertKeyCode(key));
}
bool inputManager::isMousePressed(sf::Mouse::Button button)
{
return sf::Mouse::isButtonPressed(button);
}
bool inputManager::isMousePressed(inputCode::mouseCode button)
{
return sf::Mouse::isButtonPressed(convertMouseCode(button));
}
sf::Keyboard::Key inputManager::convertKeyCode(inputCode::keyCode key)
{
sf::Keyboard::Key newKey=sf::Keyboard::Unknown;
//:(
switch(key)
{
case inputCode::A:
newKey=sf::Keyboard::A;
break;
case inputCode::B:
newKey=sf::Keyboard::B;
break;
case inputCode::C:
newKey=sf::Keyboard::C;
break;
case inputCode::D:
newKey=sf::Keyboard::D;
break;
case inputCode::E:
newKey=sf::Keyboard::E;
break;
case inputCode::F:
newKey=sf::Keyboard::F;
break;
case inputCode::G:
newKey=sf::Keyboard::G;
break;
case inputCode::H:
newKey=sf::Keyboard::H;
break;
case inputCode::I:
newKey=sf::Keyboard::I;
break; ///< The I key
case inputCode::J:
newKey=sf::Keyboard::J;
break; ///< The J key
case inputCode::K:
newKey=sf::Keyboard::K;
break; ///< The K key
case inputCode::L:
newKey=sf::Keyboard::L;
break; ///< The L key
case inputCode::M:
newKey=sf::Keyboard::M;
break; ///< The M key
case inputCode::N:
newKey=sf::Keyboard::N;
break; ///< The N key
case inputCode::O:
newKey=sf::Keyboard::O;
break; ///< The O key
case inputCode::P:
newKey=sf::Keyboard::P;
break; ///< The P key
case inputCode::Q:
newKey=sf::Keyboard::Q;
break; ///< The Q key
case inputCode::R:
newKey=sf::Keyboard::R;
break; ///< The R key
case inputCode::S:
newKey=sf::Keyboard::S;
break; ///< The S key
case inputCode::T:
newKey=sf::Keyboard::T;
break; ///< The T key
case inputCode::U:
newKey=sf::Keyboard::U;
break; ///< The U key
case inputCode::V:
newKey=sf::Keyboard::V;
break;///< The V key
case inputCode::W:
newKey=sf::Keyboard::W;
break; ///< The W key
case inputCode::X:
newKey=sf::Keyboard::X;
break; ///< The X key
case inputCode::Y:
newKey=sf::Keyboard::Y;
break; ///< The Y key
case inputCode::Z:
newKey=sf::Keyboard::Z;
break; ///< The Z key
case inputCode::Num0:
newKey=sf::Keyboard::Num0;
break; ///< The 0 key
case inputCode::Num1:
newKey=sf::Keyboard::Num1;
break; ///< The 1 key
case inputCode::Num2:
newKey=sf::Keyboard::Num2;
break; ///< The 2 key
case inputCode::Num3:
newKey=sf::Keyboard::Num3;
break; ///< The 3 key
case inputCode::Num4:
newKey=sf::Keyboard::Num4;
break; ///< The 4 key
case inputCode::Num5:
newKey=sf::Keyboard::Num5;
break; ///< The 5 key
case inputCode::Num6:
newKey=sf::Keyboard::Num6;
break; ///< The 6 key
case inputCode::Num7:
newKey=sf::Keyboard::Num7;
break; ///< The 7 key
case inputCode::Num8:
newKey=sf::Keyboard::Num8;
break; ///< The 8 key
case inputCode::Num9:
newKey=sf::Keyboard::Num9;
break; ///< The 9 key
case inputCode::Escape:
newKey=sf::Keyboard::Escape;
break; ///< The Escape key
case inputCode::LControl:
newKey=sf::Keyboard::LControl;
break; ///< The left Control key
case inputCode::LShift:
newKey=sf::Keyboard::LShift;
break; ///< The left Shift key
case inputCode::LAlt:
newKey=sf::Keyboard::LAlt;
break; ///< The left Alt key
case inputCode::LSystem:
newKey=sf::Keyboard::LSystem;
break; ///< The left OS specific key: window (Windows and Linux): apple (MacOS X): ...
case inputCode::RControl:
newKey=sf::Keyboard::RControl;
break; ///< The right Control key
case inputCode::RShift:
newKey=sf::Keyboard::RShift;
break; ///< The right Shift key
case inputCode::RAlt:
newKey=sf::Keyboard::RAlt;
break; ///< The right Alt key
case inputCode::RSystem:
newKey=sf::Keyboard::RSystem;
break; ///< The right OS specific key: window (Windows and Linux): apple (MacOS X): ...
case inputCode::Menu:
newKey=sf::Keyboard::Menu;
break; ///< The Menu key
case inputCode::LBracket:
newKey=sf::Keyboard::LBracket;
break; ///< The [ key
case inputCode::RBracket:
newKey=sf::Keyboard::RBracket;
break; ///< The ] key
case inputCode::SemiColon:
newKey=sf::Keyboard::SemiColon;
break; ///< The ; key
case inputCode::Comma:
newKey=sf::Keyboard::Comma;
break; ///< The : key
case inputCode::Period:
newKey=sf::Keyboard::Period;
break; ///< The . key
case inputCode::Quote:
newKey=sf::Keyboard::Quote;
break; ///< The ' key
case inputCode::Slash:
newKey=sf::Keyboard::Slash;
break; ///< The / key
case inputCode::BackSlash:
newKey=sf::Keyboard::BackSlash;
break; ///< The \ key
case inputCode::Tilde:
newKey=sf::Keyboard::Tilde;
break; ///< The ~ key
case inputCode::Equal:
newKey=sf::Keyboard::Equal;
break; ///< The = key
case inputCode::Dash:
newKey=sf::Keyboard::Dash;
break; ///< The - key
case inputCode::Space:
newKey=sf::Keyboard::Space;
break; ///< The Space key
case inputCode::Return:
newKey=sf::Keyboard::Return;
break; ///< The Return key
case inputCode::BackSpace:
newKey=sf::Keyboard::BackSpace;
break; ///< The Backspace key
case inputCode::Tab:
newKey=sf::Keyboard::Tab;
break; ///< The Tabulation key
case inputCode::PageUp:
newKey=sf::Keyboard::PageUp;
break; ///< The Page up key
case inputCode::PageDown:
newKey=sf::Keyboard::PageDown;
break; ///< The Page down key
case inputCode::End:
newKey=sf::Keyboard::End;
break; ///< The End key
case inputCode::Home:
newKey=sf::Keyboard::Home;
break;
case inputCode::Insert:
newKey=sf::Keyboard::Insert;
break; ///< The Insert key
case inputCode::Delete:
newKey=sf::Keyboard::Delete;
break; ///< The Delete key
case inputCode::Add:
newKey=sf::Keyboard::Add;
break; ///< The + key
case inputCode::Subtract:
newKey=sf::Keyboard::Subtract;
break; ///< The - key
case inputCode::Multiply:
newKey=sf::Keyboard::Multiply;
break; ///< The * key
case inputCode::Divide:
newKey=sf::Keyboard::Divide;
break; ///< The / key
case inputCode::Left:
newKey=sf::Keyboard::Left;
break; ///< Left arrow
case inputCode::Right:
newKey=sf::Keyboard::Right;
break; ///< Right arrow
case inputCode::Up:
newKey=sf::Keyboard::Up;
break; ///< Up arrow
case inputCode::Down:
newKey=sf::Keyboard::Down;
break; ///< Down arrow
case inputCode::Numpad0:
newKey=sf::Keyboard::Numpad0;
break; ///< The numpad 0 key
case inputCode::Numpad1:
newKey=sf::Keyboard::Numpad1;
break; ///< The numpad 1 key
case inputCode::Numpad2:
newKey=sf::Keyboard::Numpad2;
break; ///< The numpad 2 key
case inputCode::Numpad3:
newKey=sf::Keyboard::Numpad3;
break; ///< The numpad 3 key
case inputCode::Numpad4:
newKey=sf::Keyboard::Numpad4;
break; ///< The numpad 4 key
case inputCode::Numpad5:
newKey=sf::Keyboard::Numpad5;
break; ///< The numpad 5 key
case inputCode::Numpad6:
newKey=sf::Keyboard::Numpad6;
break; ///< The numpad 6 key
case inputCode::Numpad7:
newKey=sf::Keyboard::Numpad7;
break; ///< The numpad 7 key
case inputCode::Numpad8:
newKey=sf::Keyboard::Numpad8;
break; ///< The numpad 8 key
case inputCode::Numpad9:
newKey=sf::Keyboard::Numpad9;
break; ///< The numpad 9 key
case inputCode::F1:
newKey=sf::Keyboard::F1;
break; ///< The F1 key
case inputCode::F2:
newKey=sf::Keyboard::F2;
break; ///< The F2 key
case inputCode::F3:
newKey=sf::Keyboard::F3;
break; ///< The F3 key
case inputCode::F4:
newKey=sf::Keyboard::F4;
break; ///< The F4 key
case inputCode::F5:
newKey=sf::Keyboard::F5;
break; ///< The F5 key
case inputCode::F6:
newKey=sf::Keyboard::F6;
break; ///< The F6 key
case inputCode::F7:
newKey=sf::Keyboard::F7;
break; ///< The F7 key
case inputCode::F8:
newKey=sf::Keyboard::F8;
break; ///< The F8 key
case inputCode::F9:
newKey=sf::Keyboard::F9;
break; ///< The F9 key
case inputCode::F10:
newKey=sf::Keyboard::F10;
break; ///< The F10 key
case inputCode::F11:
newKey=sf::Keyboard::F11;
break; ///< The F11 key
case