From f6f6d81c5634f659693914b7b74efcdd39ba5d4f Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 10 Jan 2017 09:49:32 -0600 Subject: -Replaced images with colored rectangles -Removed various functions and variables pertaining to such images -Started migrating all uses of Location to SDL_Rect (I would preferably completly remove Location.hpp) -Scaled down resolution to 1080x640 (I would like to make these global constants) --- Makefile | 2 +- exec | Bin 0 -> 362688 bytes img/Cbasic.png | Bin 3167 -> 0 bytes img/Cdead.png | Bin 2806 -> 0 bytes img/Rbasic.png | Bin 1185 -> 0 bytes inc/creature.hpp | 6 +++--- inc/entity.hpp | 18 ++++++++++-------- inc/resource.hpp | 4 ++-- src/creature.cpp | 23 +++++++++++++++-------- src/entity.cpp | 31 ++++++++++++++++++++----------- src/list.cpp | 7 ++++--- src/resource.cpp | 32 +++++++++++++++++--------------- src/window.cpp | 2 +- 13 files changed, 73 insertions(+), 52 deletions(-) create mode 100755 exec delete mode 100644 img/Cbasic.png delete mode 100644 img/Cdead.png delete mode 100644 img/Rbasic.png diff --git a/Makefile b/Makefile index 4fc413f..f05677a 100644 --- a/Makefile +++ b/Makefile @@ -8,4 +8,4 @@ COMPILER_FLAGS = -w -I$(DEPS) LINKER_FLAGS = -g -lm -lSDL2 -lSDL2_image -std=c++11 all : $(OBJS) - $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o natures + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o exec diff --git a/exec b/exec new file mode 100755 index 0000000..b21bd27 Binary files /dev/null and b/exec differ diff --git a/img/Cbasic.png b/img/Cbasic.png deleted file mode 100644 index 01ba395..0000000 Binary files a/img/Cbasic.png and /dev/null differ diff --git a/img/Cdead.png b/img/Cdead.png deleted file mode 100644 index 226c75b..0000000 Binary files a/img/Cdead.png and /dev/null differ diff --git a/img/Rbasic.png b/img/Rbasic.png deleted file mode 100644 index a625a84..0000000 Binary files a/img/Rbasic.png and /dev/null differ diff --git a/inc/creature.hpp b/inc/creature.hpp index 636d960..bc23652 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -8,14 +8,14 @@ class Creature: public Entity { public: - Creature(Window m, std::string s); + Creature(Window m, int size); void Behavior(); void Action(); void Priority(); void setTarget(); void Move(Location l); + void giveN(list n){N = n;}; - Location getLocation(){return L;}; double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; int getHealth(){return health;}; @@ -31,7 +31,7 @@ class Creature: public Entity bool hungry; int speed = 1; bool able; - int bestSense = 100; + int bestSense = 100; list N; Entity *target; diff --git a/inc/entity.hpp b/inc/entity.hpp index 540e7b9..62c14d4 100644 --- a/inc/entity.hpp +++ b/inc/entity.hpp @@ -7,19 +7,21 @@ class Entity { public: + void Init(Window m); + void Init(Window m, Location z); void Place(); - SDL_Texture* loadTexture(std::string path, Window main); - Location getLocation(){return L;}; + int getType(){return type;}; - virtual void eat(void) {}; - virtual int getAmount(void) {}; + Location getLocation(){return L;}; + SDL_Rect getRect(){return rect;}; + + virtual void eat(void){}; + virtual int getAmount(void){}; protected: - Location L; - int height, width; //Dimensions of image on window - int degrees = 0; int type = 0; - SDL_Texture* texture; + SDL_Rect rect; + Location L; SDL_Renderer* renderer; }; diff --git a/inc/resource.hpp b/inc/resource.hpp index 968c208..5f70585 100644 --- a/inc/resource.hpp +++ b/inc/resource.hpp @@ -7,8 +7,8 @@ class Resource: public Entity { public: - Resource(Window m, std::string s); - Resource(Window m, std::string s, Location z); + Resource(Window m, int size); + Resource(Window m, int size, Location z); void eat(); Location getLocation(){return L;}; diff --git a/src/creature.cpp b/src/creature.cpp index ead0b2d..7e9011a 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -1,16 +1,18 @@ #include "creature.hpp" -Creature::Creature(Window m, std::string s) +Creature::Creature(Window m, int size) { - texture = loadTexture(s, m); - renderer = m.getRenderer(); - health = 500; - maxHealth = 1000; - - L.y=rand()%800; - L.x=rand()%1200; + Init(m); type = 1; + + rect.h = rect.w = size; + + L.x = rect.x; + L.y = rect.y; + health = 500; + maxHealth = 1000; + hungry = false; hasTarget = false; wander = false; @@ -42,6 +44,9 @@ void Creature::Priority() void Creature::setTarget() { + if(hasTarget) + return; + for(list ::iterator it = N.begin(); it!=N.end(); it++){ if((*it)->getType() == 2 && hungry){ if(!hasTarget){ @@ -117,4 +122,6 @@ void Creature::Move(Location l) L.y-=speed; } } + rect.x = L.x; + rect.y = L.y; } diff --git a/src/entity.cpp b/src/entity.cpp index 4f60851..be3e792 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -1,18 +1,27 @@ #include "entity.hpp" -void Entity::Place() +void Entity::Init(Window m) +{ + renderer = m.getRenderer(); + rect.y = rand()%640; + rect.x = rand()%1080; +} + +void Entity::Init(Window m, Location z) { - SDL_Rect rect = {L.x, L.y, width/8, height/8}; - SDL_RenderCopyEx(renderer,texture,NULL,&rect,degrees,NULL,SDL_FLIP_NONE); + renderer = m.getRenderer(); + rect.y = z.y; + rect.x = z.x; } -SDL_Texture* Entity::loadTexture(string path, Window main) +void Entity::Place() { - SDL_Surface* surface = IMG_Load(path.c_str()); - SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 255, 255, 255)); - height = surface->h; - width = surface->w; - SDL_Texture* texture = SDL_CreateTextureFromSurface(main.getRenderer(),surface); - SDL_FreeSurface(surface); - return texture; + if(type == 1) + SDL_SetRenderDrawColor(renderer,255,0,255,255); + else + SDL_SetRenderDrawColor(renderer,0,255,0,255); + + SDL_RenderDrawRect(renderer, &rect); + + SDL_SetRenderDrawColor(renderer,0,0,0,255); } diff --git a/src/list.cpp b/src/list.cpp index e08531b..2fca4f4 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -5,12 +5,12 @@ List::List(Window m) int i; for(i=0;i<10;i++){ - Creature X(m,"img/Cbasic.png"); + Creature X(m,10); C.push_back(X); } for(i=0;i<100;i++){ - Resource Y(m,"img/Rbasic.png"); + Resource Y(m,5); R.push_back(Y); } @@ -41,7 +41,8 @@ void List::Behavior() if(it->getHealth()<=0){ Location z = it->getLocation(); - Resource r = Resource(main,"img/Cdead.png",z); + SDL_Rect rect = it->getRect(); + Resource r = Resource(main,rect.w,z); R.push_back(r); C.erase(it--); } diff --git a/src/resource.cpp b/src/resource.cpp index 08929bc..0c07be3 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -1,27 +1,29 @@ #include "resource.hpp" -Resource::Resource(Window m, std::string s) -{ - texture = loadTexture(s, m); - renderer = m.getRenderer(); - - L.y = rand()%800; - L.x = rand()%1200; +Resource::Resource(Window m, int size) +{ + Init(m); type = 2; + rect.h = rect.w = size; + + L.x = rect.x; + L.y = rect.y; + amount = 100; } -Resource::Resource(Window m, std::string s, Location z) +Resource::Resource(Window m, int size, Location z) { - texture = loadTexture(s, m); - renderer = m.getRenderer(); - - L.y = z.y; - L.x = z.x; + Init(m,z); type = 2; - - amount = 100; + + rect.h = rect.w = size; + + L.x = rect.x; + L.y = rect.y; + + amount = 200; } void Resource::eat() diff --git a/src/window.cpp b/src/window.cpp index 5df97cf..fccdef0 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,7 +3,7 @@ Window::Window() { SDL_Init(SDL_INIT_VIDEO); - main = SDL_CreateWindow("main",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,1280,800,SDL_WINDOW_SHOWN); + main = SDL_CreateWindow("main",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,1080,640,SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(main,-1,SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); } -- cgit v1.2.3