summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom <tom@ground-control>2016-05-02 15:39:22 -0500
committertom <tom@ground-control>2016-05-02 15:39:22 -0500
commit3a2f69b3f1082b2fbdf39bd2a4f7ad0020971eac (patch)
treec5961dcb6e1ccbdf5a478d97f7383028492140d4
parent1ddb996ac4a4fc78e5484acdadac2df95006b632 (diff)
spring cleaning, added framerate cap & started iterator replacement
-rw-r--r--Makefile2
-rw-r--r--inc/main.hpp1
-rw-r--r--inc/timer.hpp25
-rw-r--r--src/creature.cpp7
-rw-r--r--src/list.cpp21
-rw-r--r--src/main.cpp8
-rw-r--r--src/resource.cpp5
-rw-r--r--src/timer.cpp53
-rw-r--r--src/window.cpp8
9 files changed, 102 insertions, 28 deletions
diff --git a/Makefile b/Makefile
index ca9c1ee..626ddea 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-OBJS = src/*.cpp
+OBJS = src/*cpp
DEPS = inc/
CC = g++
diff --git a/inc/main.hpp b/inc/main.hpp
index c6f51e3..d20f7ef 100644
--- a/inc/main.hpp
+++ b/inc/main.hpp
@@ -8,5 +8,6 @@
#include "resource.hpp"
#include "list.hpp"
#include "location.hpp"
+#include "timer.hpp"
#endif
diff --git a/inc/timer.hpp b/inc/timer.hpp
new file mode 100644
index 0000000..1213311
--- /dev/null
+++ b/inc/timer.hpp
@@ -0,0 +1,25 @@
+#ifndef timer_h
+#define timer_h
+
+#include "window.hpp"
+
+class Timer
+{
+ public:
+ Timer();
+ void Start();
+ void Stop();
+ void Pause();
+ void unPause();
+ int getTicks();
+ bool isStarted(){return started;};
+ bool isPaused(){return paused;};
+
+ private:
+ int startTicks;
+ int pausedTicks;
+ bool paused;
+ bool started;
+};
+
+#endif
diff --git a/src/creature.cpp b/src/creature.cpp
index d43bc2a..10274b6 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -1,6 +1,6 @@
#include "creature.hpp"
-Creature::Creature(Window m, std::string s) //Constructor
+Creature::Creature(Window m, std::string s)
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
@@ -8,7 +8,6 @@ Creature::Creature(Window m, std::string s) //Constructor
maxHealth = 1000;
hunger = 0;
- //initializes random start coordinates for creature, target position is equivalent to it's position
yPosition=yTarget=rand()%800;
xPosition=xTarget=rand()%1200;
hasTarget = false;
@@ -19,7 +18,7 @@ Creature::Creature(Window m, std::string s) //Constructor
int Creature::Behavior()
{
- health-=1; //Decrements health each time a behavior is executed
+ health-=1;
this->Priority();
@@ -38,7 +37,7 @@ int Creature::Behavior()
void Creature::Priority()
{
- double d; // lol
+ double d;
// Gets location for closest resource
for(int i = 0; i < nR.size(); i++)
diff --git a/src/list.cpp b/src/list.cpp
index 379a113..464ac48 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -4,7 +4,7 @@ List::List(Window m) //Constructor
{
int i;
- for(i=0;i<3;i++)
+ for(i=0;i<10;i++)
{
Creature X(m,"img/Cbasic.png");
C.push_back(X);
@@ -25,22 +25,13 @@ void List::Place()
for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++)
it->Place();
- /*
- if(R.size()<15)
- {
- Resource Y(m,"img/Rbasic.png");
- R.push_back(Y);
- }
- */
- //places all resources
- for(int j = 0; j<R.size(); j++)
- {
- if(R[j].getAmount()<=0)
- R.erase(R.begin()+j);
+ for(vector<Resource>::iterator it = R.begin(); it!=R.end(); it++){
+ if(it->getAmount()<=0)
+ R.erase(it--);
else
- R[j].Place();
- }
+ it->Place();
+ }
}
void List::Behavior()
diff --git a/src/main.cpp b/src/main.cpp
index ed41895..25d4cc4 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -6,8 +6,12 @@ int main()
List L(main);
Event e;
+ Timer fps;
+
while(e.gRun())
{
+ fps.Start();
+
while(e.Poll())
{
if(e.gEventType() == SDL_QUIT)
@@ -22,7 +26,9 @@ int main()
L.Behavior();
main.Render();
- //SDL_Delay(10);
+
+ if(fps.getTicks() < (1000 / 60))
+ SDL_Delay((1000 / 60) - fps.getTicks());
}
main.Destroy();
diff --git a/src/resource.cpp b/src/resource.cpp
index 71a13e3..8f1a4cf 100644
--- a/src/resource.cpp
+++ b/src/resource.cpp
@@ -1,11 +1,10 @@
#include "resource.hpp"
-Resource::Resource(Window m, std::string s) //Constructor
+Resource::Resource(Window m, std::string s)
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
- //Initialized random position coordinates
yPosition = rand()%800;
xPosition = rand()%1200;
@@ -23,7 +22,7 @@ Resource::Resource(Window m, std::string s, Location z)
amount = 100;
}
-Location Resource::getLocation() //Returns resource object
+Location Resource::getLocation()
{
Location L(xPosition,yPosition,2);
return L;
diff --git a/src/timer.cpp b/src/timer.cpp
new file mode 100644
index 0000000..b59bec8
--- /dev/null
+++ b/src/timer.cpp
@@ -0,0 +1,53 @@
+#include "timer.hpp"
+
+Timer::Timer()
+{
+ startTicks = 0;
+ pausedTicks= 0;
+ paused = false;
+ started = false;
+}
+
+void Timer::Start()
+{
+ started = true;
+ paused = false;
+ startTicks = SDL_GetTicks();
+}
+
+void Timer::Stop()
+{
+ started =false;
+ paused = false;
+}
+
+void Timer::Pause()
+{
+ if ((started == true) && (paused == false )){
+ paused = true;
+ pausedTicks = SDL_GetTicks() - startTicks;
+ }
+}
+
+void Timer::unPause()
+{
+ if (paused)
+ {
+ paused = false;
+ startTicks = SDL_GetTicks() - pausedTicks;
+ pausedTicks = 0;
+ }
+}
+
+int Timer::getTicks()
+{
+ if(started)
+ {
+ if (paused)
+ return pausedTicks;
+ else
+ return SDL_GetTicks() - startTicks;
+ }
+ else
+ return 0;
+}
diff --git a/src/window.cpp b/src/window.cpp
index 66c7a4c..5df97cf 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -1,6 +1,6 @@
#include "window.hpp"
-Window::Window() //Constructor
+Window::Window()
{
SDL_Init(SDL_INIT_VIDEO);
main = SDL_CreateWindow("main",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,1280,800,SDL_WINDOW_SHOWN);
@@ -8,19 +8,19 @@ Window::Window() //Constructor
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
}
-void Window::Destroy() //Kills window
+void Window::Destroy()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(main);
SDL_Quit();
}
-void Window::Clear() //Clears renderer
+void Window::Clear()
{
SDL_RenderClear(renderer);
}
-void Window::Render() //Brings image forward
+void Window::Render()
{
SDL_RenderPresent(renderer);
}