From 3a2f69b3f1082b2fbdf39bd2a4f7ad0020971eac Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 2 May 2016 15:39:22 -0500 Subject: spring cleaning, added framerate cap & started iterator replacement --- src/creature.cpp | 7 +++---- src/list.cpp | 21 ++++++--------------- src/main.cpp | 8 +++++++- src/resource.cpp | 5 ++--- src/timer.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/window.cpp | 8 ++++---- 6 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 src/timer.cpp (limited to 'src') 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::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::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); } -- cgit v1.2.3