From 2c12d19204aa198bf8537bcdb137b40f0c7317e9 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 25 Sep 2016 18:38:34 +0200 Subject: Initial --- AmpelJonas/AmpelJonas.vcxproj | 175 ++++++++++++++++++++++++++++++++ AmpelJonas/AmpelJonas.vcxproj.filters | 83 +++++++++++++++ AmpelJonas/cCar.cpp | 84 ++++++++++++++++ AmpelJonas/cCar.h | 22 ++++ AmpelJonas/cCrossroad.cpp | 177 ++++++++++++++++++++++++++++++++ AmpelJonas/cCrossroad.h | 18 ++++ AmpelJonas/cObject.cpp | 59 +++++++++++ AmpelJonas/cObject.h | 32 ++++++ AmpelJonas/cObjectHandler.cpp | 63 ++++++++++++ AmpelJonas/cObjectHandler.h | 32 ++++++ AmpelJonas/cPeasant.cpp | 51 ++++++++++ AmpelJonas/cPeasant.h | 20 ++++ AmpelJonas/cPeasantTrafficLight.cpp | 84 ++++++++++++++++ AmpelJonas/cPeasantTrafficLight.h | 30 ++++++ AmpelJonas/cRender.cpp | 183 ++++++++++++++++++++++++++++++++++ AmpelJonas/cRender.h | 86 ++++++++++++++++ AmpelJonas/cTrafficLight.cpp | 139 ++++++++++++++++++++++++++ AmpelJonas/cTrafficLight.h | 27 +++++ AmpelJonas/main.cpp | 14 +++ AmpelJonas/stdafx.cpp | 8 ++ AmpelJonas/stdafx.h | 24 +++++ AmpelJonas/targetver.h | 8 ++ 22 files changed, 1419 insertions(+) create mode 100644 AmpelJonas/AmpelJonas.vcxproj create mode 100644 AmpelJonas/AmpelJonas.vcxproj.filters create mode 100644 AmpelJonas/cCar.cpp create mode 100644 AmpelJonas/cCar.h create mode 100644 AmpelJonas/cCrossroad.cpp create mode 100644 AmpelJonas/cCrossroad.h create mode 100644 AmpelJonas/cObject.cpp create mode 100644 AmpelJonas/cObject.h create mode 100644 AmpelJonas/cObjectHandler.cpp create mode 100644 AmpelJonas/cObjectHandler.h create mode 100644 AmpelJonas/cPeasant.cpp create mode 100644 AmpelJonas/cPeasant.h create mode 100644 AmpelJonas/cPeasantTrafficLight.cpp create mode 100644 AmpelJonas/cPeasantTrafficLight.h create mode 100644 AmpelJonas/cRender.cpp create mode 100644 AmpelJonas/cRender.h create mode 100644 AmpelJonas/cTrafficLight.cpp create mode 100644 AmpelJonas/cTrafficLight.h create mode 100644 AmpelJonas/main.cpp create mode 100644 AmpelJonas/stdafx.cpp create mode 100644 AmpelJonas/stdafx.h create mode 100644 AmpelJonas/targetver.h diff --git a/AmpelJonas/AmpelJonas.vcxproj b/AmpelJonas/AmpelJonas.vcxproj new file mode 100644 index 0000000..771a41c --- /dev/null +++ b/AmpelJonas/AmpelJonas.vcxproj @@ -0,0 +1,175 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {91D16C6D-C1C1-4FF5-AA46-AAB7E7B2B2B1} + Win32Proj + AmpelJonas + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/AmpelJonas/AmpelJonas.vcxproj.filters b/AmpelJonas/AmpelJonas.vcxproj.filters new file mode 100644 index 0000000..5afdbac --- /dev/null +++ b/AmpelJonas/AmpelJonas.vcxproj.filters @@ -0,0 +1,83 @@ + + + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {136559fc-b520-4b29-b0e8-3c65612b3f81} + + + {ca493b7f-1c64-4d61-8eb5-f8f92504d57d} + + + {66d5bf72-191b-41f8-82bc-153596a427de} + + + {41cd36d9-535a-4f7e-a947-79dd5136ed9d} + + + {37e4a21a-712c-4c0d-8cf7-fa21a20424cb} + + + {c887f383-8151-41d2-a9e0-ccdeb35b9538} + + + + + Engine\Header + + + Engine\Header + + + Engine\Header + + + Game\Headers + + + + + Game\Headers + + + Game\Headers + + + Game\Headers + + + Game\Headers + + + + + Engine\Source + + + Engine\Source + + + Engine\Source + + + Game\Source + + + + + Game\Source + + + Game\Source + + + Game\Source + + + Game\Source + + + \ No newline at end of file diff --git a/AmpelJonas/cCar.cpp b/AmpelJonas/cCar.cpp new file mode 100644 index 0000000..212cd7c --- /dev/null +++ b/AmpelJonas/cCar.cpp @@ -0,0 +1,84 @@ +#include "cCar.h" + +cCar::cCar(int _direction, int *_phase) +{ + pPhase = _phase; + iDirection = _direction; + if (iDirection ==_CAR_DIR_NORTH) { //South or north + car = new cObject(4, 4); + car->setPosition(_SIZE_X_ / 2 - 4, - 4 ); + car->drawRectangle('+', 'X', sPos{ 0,0 }, sPos{3,3}, _COL_RED, _COL_RED); + } + else if (iDirection == _CAR_DIR_SOUTH) { //South or north + car = new cObject(4, 4); + car->setPosition(_SIZE_X_ / 2 +1, _SIZE_Y_); + car->drawRectangle('+', 'X', sPos{ 0,0 }, sPos{ 3,3 }, _COL_RED, _COL_RED); + } + else if(iDirection == _CAR_DIR_WEST){ + car = new cObject(6, 3); + car->setPosition( -6, _SIZE_Y_ / 2 + 1); + car->drawRectangle('+', ' ', sPos{ 0,0 }, sPos{5, 2}, _COL_BLUE, _COL_BLUE); + car->drawText("AUTO", sPos{ 1,1 }, _COL_BLUE); + } + else if (iDirection == _CAR_DIR_EAST) + { + car = new cObject(6, 3); + car->setPosition(_SIZE_X_, _SIZE_Y_ / 2 -3); + car->drawRectangle('+', ' ', sPos{ 0,0 }, sPos{ 5, 2 }, _COL_BLUE, _COL_BLUE); + car->drawText("AUTO", sPos{ 1,1 }, _COL_BLUE); + } +} + +cCar::~cCar() +{ + delete car; +} + +cObject *cCar::getObject() +{ + return car; +} + +void cCar::drive() +{ + sPos oldPos; + switch(iDirection) + { + case _CAR_DIR_EAST: + oldPos = car->getPosition(); + if (!((*pPhase != 5 && *pPhase != 4) && oldPos.x == (_SIZE_X_ / 2) + 11)){ + car->setPosition(oldPos.x - 1, oldPos.y); + } + if (oldPos.x < -6) { + car->setPosition(_SIZE_X_, oldPos.y); + } + break; + case _CAR_DIR_WEST: + oldPos = car->getPosition(); + if (!((*pPhase != 5 && *pPhase != 4) && oldPos.x == (_SIZE_X_ / 2) - 15)){ + car->setPosition(oldPos.x + 1, oldPos.y); + } + if (oldPos.x > _SIZE_X_) { + car->setPosition(-6 , oldPos.y); + } + break; + case _CAR_DIR_NORTH: + oldPos = car->getPosition(); + if (!((*pPhase != 0 && *pPhase != 9 && *pPhase != -1) && oldPos.y == (_SIZE_Y_ / 2) - 11)) { + car->setPosition(oldPos.x, oldPos.y + 1); + } + if (oldPos.y > _SIZE_Y_) { + car->setPosition(oldPos.x, -4); + } + break; + case _CAR_DIR_SOUTH: + oldPos = car->getPosition(); + if (!((*pPhase != 0 && *pPhase != 9 && *pPhase != -1) && oldPos.y == (_SIZE_Y_ / 2) + 8)) { + car->setPosition(oldPos.x, oldPos.y - 1); + } + if (oldPos.y < -4) { + car->setPosition(oldPos.x, _SIZE_Y_); + } + break; + } +} \ No newline at end of file diff --git a/AmpelJonas/cCar.h b/AmpelJonas/cCar.h new file mode 100644 index 0000000..c505079 --- /dev/null +++ b/AmpelJonas/cCar.h @@ -0,0 +1,22 @@ +#pragma once +#include "stdafx.h" + +#define _CAR_DIR_SOUTH 2 +#define _CAR_DIR_NORTH 4 +#define _CAR_DIR_EAST 1 +#define _CAR_DIR_WEST 3 + +class cCar +{ +public: + cCar(int _direction, int *_phase); + ~cCar(); + + cObject *getObject(); + + void drive(); +private: + cObject *car; + int iDirection; + int *pPhase; +}; \ No newline at end of file diff --git a/AmpelJonas/cCrossroad.cpp b/AmpelJonas/cCrossroad.cpp new file mode 100644 index 0000000..10718f6 --- /dev/null +++ b/AmpelJonas/cCrossroad.cpp @@ -0,0 +1,177 @@ +#include "cCrossroad.h" + +cCrossroad::cCrossroad() +{ + render = new cRender(' ', _COL_GREEN, _SIZE_X_, _SIZE_Y_); + //handler = new cObjectHandler(render); +} + +cCrossroad::~cCrossroad() +{ + delete render; + //delete handler; +} + +void cCrossroad::run() +{ + int iWait = 4; + int iPhase = -1; + int iFrames = 0; + + cObjectHandler handler(render); + cObject *backround = new cObject(_SIZE_X_, _SIZE_Y_); + + //Objects + cTrafficLight tlNorth(_TL_NORTH_, _TL_COL_GREEN_); + cTrafficLight tlSouth(_TL_SOUTH_, _TL_COL_GREEN_); + cTrafficLight tlEast(_TL_EAST_, _TL_COL_RED_); + cTrafficLight tlWest(_TL_WEST_, _TL_COL_RED_); + + cPeasantTrafficLight ptl1(_CTL_1, _CTL_COL_GREEN); + cPeasantTrafficLight ptl2(_CTL_2, _CTL_COL_RED); + cPeasantTrafficLight ptl3(_CTL_3, _CTL_COL_RED); + cPeasantTrafficLight ptl4(_CTL_4, _CTL_COL_GREEN); + cPeasantTrafficLight ptl5(_CTL_5, _CTL_COL_GREEN); + cPeasantTrafficLight ptl6(_CTL_6, _CTL_COL_RED); + cPeasantTrafficLight ptl7(_CTL_7, _CTL_COL_RED); + cPeasantTrafficLight ptl8(_CTL_8, _CTL_COL_GREEN); + + cCar carSouth(_CAR_DIR_SOUTH, &iPhase); + cCar carNorth(_CAR_DIR_NORTH, &iPhase); + cCar carEast(_CAR_DIR_EAST, &iPhase); + cCar carWest(_CAR_DIR_WEST, &iPhase); + + cPeasant peasantNorth(_PE_DIR_NORTH, &iPhase); + cPeasant peasantWest(_PE_DIR_WEST, &iPhase); + + //init + backround->setPosition(0, 0); + backround->drawRectangle('°', '°', sPos{ 0, _SIZE_Y_ / 2 - 4 }, sPos{ _SIZE_X_ - 1, _SIZE_Y_ / 2 + 4 }, _COL_DEFAULT, _COL_DEFAULT); + backround->drawRectangle('°', '°', sPos{ _SIZE_X_ / 2 - 6, 0 }, sPos{ _SIZE_X_ / 2 + 6, _SIZE_Y_ }, _COL_DEFAULT, _COL_DEFAULT); + + //add objects to handler + handler.createObject(backround); + + handler.createObject(tlNorth.getObject()); + handler.createObject(tlSouth.getObject()); + handler.createObject(tlEast.getObject()); + handler.createObject(tlWest.getObject()); + + handler.createObject(ptl1.getObject()); + handler.createObject(ptl2.getObject()); + handler.createObject(ptl3.getObject()); + handler.createObject(ptl4.getObject()); + handler.createObject(ptl5.getObject()); + handler.createObject(ptl6.getObject()); + handler.createObject(ptl7.getObject()); + handler.createObject(ptl8.getObject()); + + handler.createObject(carSouth.getObject()); + handler.createObject(carNorth.getObject()); + handler.createObject(carEast.getObject()); + handler.createObject(carWest.getObject()); + + handler.createObject(peasantNorth.getObject()); + handler.createObject(peasantWest.getObject()); + + handler.write(); + render->render(); + + //main Loop + + //Starting with vertical Green, horizontal red + + while(1) + { + if (iWait <= 0) + { + iPhase++; + switch (iPhase) + { + case 0: //ptls to red + ptl1.next(); + ptl4.next(); + ptl5.next(); + ptl8.next(); + iWait = 4; + break; + case 1: //switch to yellow + tlNorth.next(); + tlSouth.next(); + iWait = 4; + break; + case 2: //to red + tlNorth.next(); + tlSouth.next(); + iWait = 4; + break; + case 3: //Yellow-Red + tlEast.next(); + tlWest.next(); + iWait = 4; + break; + case 4: //Green + tlEast.next(); + tlWest.next(); + ptl2.next(); + ptl3.next(); + ptl7.next(); + ptl6.next(); + iWait = 10; + break; + case 5: //vert ptls to red + ptl2.next(); + ptl3.next(); + ptl7.next(); + ptl6.next(); + iWait = 4; + break; + case 6: //Yellow-red + tlEast.next(); + tlWest.next(); + iWait = 4; + break; + case 7: //red + tlEast.next(); + tlWest.next(); + iWait = 4; + break; + case 8: //yellow-red + tlNorth.next(); + tlSouth.next(); + iWait = 4; + break; + case 9: //green + tlNorth.next(); + tlSouth.next(); + ptl1.next(); + ptl4.next(); + ptl5.next(); + ptl8.next(); + iPhase = -1; + iWait = 10; + break; + } + } + + + carWest.drive(); + carEast.drive(); + carNorth.drive(); + carSouth.drive(); + + if (iFrames % 2 == 0) { //Run every 2nd frame + peasantNorth.run(); + peasantWest.run(); + } + + + + handler.write(); + render->render(); + + iWait--; + iFrames++; + backround->drawText(to_string(iFrames) , sPos{ 0,0 }, _COL_DEFAULT); //Framecounter + } +} \ No newline at end of file diff --git a/AmpelJonas/cCrossroad.h b/AmpelJonas/cCrossroad.h new file mode 100644 index 0000000..37bec28 --- /dev/null +++ b/AmpelJonas/cCrossroad.h @@ -0,0 +1,18 @@ +#pragma once +#include "stdafx.h" + +#define _SIZE_X_ 150 +#define _SIZE_Y_ 50 + + +class cCrossroad +{ +public: + cCrossroad(void); + ~cCrossroad(void); + + void run(void); +private: + cRender *render; + //cObjectHandler *handler; +}; \ No newline at end of file diff --git a/AmpelJonas/cObject.cpp b/AmpelJonas/cObject.cpp new file mode 100644 index 0000000..8b18e41 --- /dev/null +++ b/AmpelJonas/cObject.cpp @@ -0,0 +1,59 @@ +#include "cObject.h" + +cObject::cObject(int _sx, int _sy) +{ + bBlockRender = true; //Block inherited render capabilities of parent + + sizeX = _sx; + sizeY = _sy; + + //Initialize 2D array + cScreen = (char**) malloc(sizeof *cScreen * _sx); + for (int i = 0; i < _sx; i++) + cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy); + + wColor = (WORD**)malloc(sizeof *wColor * _sx); + for (int i = 0; i < _sx; i++) + wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy); + + for (int i = 0; i < sizeY; i++) { + for (int o = 0; o < sizeX; o++) { + cScreen[o][i] = NULL; + wColor[o][i] = _COL_DEFAULT; + } + } +} + +cObject::~cObject() +{ + for (int i = 0; i < sizeX; i++) { + free(cScreen[i]); + free(wColor[i]); + } + + free(cScreen); + free(wColor); +} + +sPos cObject::getPosition() +{ + return pos; +} + +void cObject::setPosition(sPos _pos) +{ + pos = _pos; +} + + +void cObject::setPosition(int _x, int _y) +{ + pos.x = _x; + pos.y = _y; +} + + +sObject cObject::getObject() +{ + return sObject{pos, wColor, cScreen, sizeX, sizeY}; +} diff --git a/AmpelJonas/cObject.h b/AmpelJonas/cObject.h new file mode 100644 index 0000000..abdba3c --- /dev/null +++ b/AmpelJonas/cObject.h @@ -0,0 +1,32 @@ +#pragma once +#include "stdafx.h" + +struct sObject + +{ + sPos pos; + WORD **wColor; + char **cScreen; + int sizeX; + int sizeY; +}; + +class cObject : public cRender +{ +public: + cObject(int _sx, int _sy); + //_sx : SizeX + //_sy : SizeY + ~cObject(); + + sPos getPosition(); + void setPosition(sPos _pos); + void setPosition(int _x, int _y); + sObject getObject(); + +private: + //wColor, cScreen, sizeX and sizeY are inherited from cRender + sPos pos; + +}; + diff --git a/AmpelJonas/cObjectHandler.cpp b/AmpelJonas/cObjectHandler.cpp new file mode 100644 index 0000000..a5c5884 --- /dev/null +++ b/AmpelJonas/cObjectHandler.cpp @@ -0,0 +1,63 @@ +#include "cObjectHandler.h" + +cObjectHandler::cObjectHandler(cRender *_render) +{ + render = _render; +} + +int cObjectHandler::createObject(cObject *_object) +{ + objects.push_back(_object); + return objects.size() - 1; +} + +int cObjectHandler::moveObject(int _object, sPos _pos, int _mode) +{ + if (_object >= objects.size()) //prevent segmentation faults + return 1; + + if (!objects[_object]) + return 1; + + sPos objPosition = objects[_object]->getPosition(); + + if (_mode == _MOVE_RELATIVE) + objects[_object]->setPosition(sPos{ objPosition.x + _pos.x, objPosition.y + _pos.y }); + else if (_mode == _MOVE_ABSOULUTE) + objects[_object]->setPosition(_pos); + + return 0; +} + +int cObjectHandler::destroyObject(int _object) +{ + delete objects[_object]; + objects[_object] = NULL; + + return 0; +} + +int cObjectHandler::write() +{ + render->clear(); + + for (int i = 0; i < objects.size(); i++) + { + if (objects[i]) // Check if objects[i] is existent + { + //Draw every Object + sObject obj = objects[i]->getObject(); //get Object #i + + for (int o = 0; o < obj.sizeY; o++) { //y axis + for (int p = 0; p < obj.sizeX; p++) { //x axis + if (obj.cScreen[p][o]) { //Dont overwrite empty pixels + sPos pos{ obj.pos.x + p, obj.pos.y + o }; + render->drawPoint(obj.cScreen[p][o], pos, true, obj.wColor[p][o]); + } + } + } + } + } + + return 0; +} \ No newline at end of file diff --git a/AmpelJonas/cObjectHandler.h b/AmpelJonas/cObjectHandler.h new file mode 100644 index 0000000..47598f2 --- /dev/null +++ b/AmpelJonas/cObjectHandler.h @@ -0,0 +1,32 @@ +#pragma once +#include "stdafx.h" + +//movemodes +#define _MOVE_RELATIVE 0 +#define _MOVE_ABSOULUTE 1 + +class cObject; //Circular dependency break (Bad practice. I Know.) + +class cObjectHandler +{ +public: + cObjectHandler(cRender *_render); + + int createObject(cObject *_object); + //Adds _object to managed objects vector + //returns Identifier for newly created vector + + int moveObject(int _object, sPos _pos, int _mode); + //Alters position of _object by _pos either relative to old position or Absolute + //Depending on selected _mode (_MOVE_RELATIVE / _MOVE_ABSOLUTE). + + int destroyObject(int _object); + //removes _object from vector after deleting it + + int write(); + //writes all objects in objects[] to render buffer + +private: + vector objects; + cRender *render; +}; \ No newline at end of file diff --git a/AmpelJonas/cPeasant.cpp b/AmpelJonas/cPeasant.cpp new file mode 100644 index 0000000..280c16f --- /dev/null +++ b/AmpelJonas/cPeasant.cpp @@ -0,0 +1,51 @@ +#include "cPeasant.h" + +cPeasant::cPeasant(int _direction, int *_phase) +{ + pPhase = _phase; + iDirection = _direction; + pe = new cObject(1, 1); + pe->drawPoint(203, sPos{0,0}, true, _COL_YELLOW); + + switch (iDirection) + { + case _PE_DIR_NORTH: + pe->setPosition(_SIZE_X_ / 2 - 7, -1); + break; + case _PE_DIR_WEST: + pe->setPosition(-1, _SIZE_Y_ / 2 - 5); + break; + } +} + +cPeasant::~cPeasant() +{ + delete pe; +} + +cObject *cPeasant::getObject() +{ + return pe; +} + +void cPeasant::run() +{ + sPos oldPos = pe->getPosition(); + switch (iDirection) + { + case _PE_DIR_NORTH: + if(!(oldPos.y == _SIZE_Y_ / 2 - 5 && *pPhase != -1)) + pe->setPosition(oldPos.x, oldPos.y + 1); + + if(oldPos.y > _SIZE_Y_) + pe->setPosition(oldPos.x, -1); + break; + case _PE_DIR_WEST: + if (!(oldPos.x == _SIZE_X_ / 2 - 7 && *pPhase != 4)) + pe->setPosition(oldPos.x + 1, oldPos.y); + + if(oldPos.x > _SIZE_X_) + pe->setPosition(-1, oldPos.y); + break; + } +} \ No newline at end of file diff --git a/AmpelJonas/cPeasant.h b/AmpelJonas/cPeasant.h new file mode 100644 index 0000000..b8f448e --- /dev/null +++ b/AmpelJonas/cPeasant.h @@ -0,0 +1,20 @@ +#pragma once +#include "stdafx.h" + +#define _PE_DIR_NORTH 0 +#define _PE_DIR_WEST 1 + +class cPeasant +{ +public: + cPeasant(int _direction, int *_phase); + ~cPeasant(); + + cObject *getObject(); + + void run(); +private: + cObject *pe; + int iDirection; + int *pPhase; +}; \ No newline at end of file diff --git a/AmpelJonas/cPeasantTrafficLight.cpp b/AmpelJonas/cPeasantTrafficLight.cpp new file mode 100644 index 0000000..6e348ab --- /dev/null +++ b/AmpelJonas/cPeasantTrafficLight.cpp @@ -0,0 +1,84 @@ +#include "cPeasantTrafficLight.h" + +cPeasantTrafficLight::cPeasantTrafficLight(int _type, int _startphase) +{ + iPhase = _startphase; + iPhase == _CTL_COL_GREEN ? iPhase = _CTL_COL_RED : iPhase = _CTL_COL_GREEN; //switch arround bc switch occurs before lights are switched + iType = _type; + + _type % 2 == 0 ? tl = new cObject(1, 2) : tl = new cObject(2, 1); + + //tl->setPosition(_SIZE_X_ / 2, _SIZE_Y_ / 2); + + switch (_type) + { + case _CTL_1: + tl->setPosition(_SIZE_X_ / 2 - 9, _SIZE_Y_ / 2 - 6); + break; + case _CTL_4: + tl->setPosition(_SIZE_X_ / 2 + 10, _SIZE_Y_ / 2 - 6); + break; + case _CTL_5: + tl->setPosition(_SIZE_X_ / 2 + 10, _SIZE_Y_ / 2 + 5); + break; + case _CTL_8: + tl->setPosition(_SIZE_X_ / 2 - 9, _SIZE_Y_ / 2 + 5); + break; + case _CTL_2: + tl->setPosition(_SIZE_X_ / 2 - 8, _SIZE_Y_ / 2 - 7); + break; + case _CTL_3: + tl->setPosition(_SIZE_X_ / 2 + 7, _SIZE_Y_ / 2 - 7); + break; + case _CTL_6: + tl->setPosition(_SIZE_X_ / 2 + 7, _SIZE_Y_ / 2 + 7); + break; + case _CTL_7: + tl->setPosition(_SIZE_X_ / 2 - 8, _SIZE_Y_ / 2 + 7); + break; + } + + next(); +} + +cPeasantTrafficLight::~cPeasantTrafficLight() +{ + delete tl; +} + +void cPeasantTrafficLight::next() +{ + iPhase == _CTL_COL_GREEN ? iPhase = _CTL_COL_RED : iPhase = _CTL_COL_GREEN; + switch (iType) + { + case _CTL_1: + case _CTL_4: + tl->drawPoint('o', sPos{ 0,0 }, true, _COL_DEFAULT); + tl->drawPoint('o', sPos{ 0,1 }, true, _COL_DEFAULT); + iPhase == _CTL_COL_GREEN ? tl->drawPoint('o', sPos{ 0,1 }, true, _COL_GREEN) : tl->drawPoint('o', sPos{ 0,0 }, true, _COL_RED); + break; + case _CTL_5: + case _CTL_8: + tl->drawPoint('o', sPos{ 0,0 }, true, _COL_DEFAULT); + tl->drawPoint('o', sPos{ 0,1 }, true, _COL_DEFAULT); + iPhase == _CTL_COL_GREEN ? tl->drawPoint('o', sPos{ 0,0 }, true, _COL_GREEN) : tl->drawPoint('o', sPos{ 0,1 }, true, _COL_RED); + break; + case _CTL_2: + case _CTL_7: + tl->drawPoint('o', sPos{ 0,0 }, true, _COL_DEFAULT); + tl->drawPoint('o', sPos{ 1,0 }, true, _COL_DEFAULT); + iPhase == _CTL_COL_GREEN ? tl->drawPoint('o', sPos{ 1,0 }, true, _COL_GREEN) : tl->drawPoint('o', sPos{ 0,0 }, true, _COL_RED); + break; + case _CTL_6: + case _CTL_3: + tl->drawPoint('o', sPos{ 0,0 }, true, _COL_DEFAULT); + tl->drawPoint('o', sPos{ 1,0 }, true, _COL_DEFAULT); + iPhase == _CTL_COL_GREEN ? tl->drawPoint('o', sPos{ 0,0 }, true, _COL_GREEN) : tl->drawPoint('o', sPos{ 1,0 }, true, _COL_RED); + break; + } +} + +cObject *cPeasantTrafficLight::getObject() +{ + return tl; +} \ No newline at end of file diff --git a/AmpelJonas/cPeasantTrafficLight.h b/AmpelJonas/cPeasantTrafficLight.h new file mode 100644 index 0000000..88ec99b --- /dev/null +++ b/AmpelJonas/cPeasantTrafficLight.h @@ -0,0 +1,30 @@ +#pragma once +#include "stdafx.h" + +#define _CTL_COL_RED 0 +#define _CTL_COL_GREEN 1 + +//Counted clockwise, starting with the upper tl of west lane +#define _CTL_1 2 +#define _CTL_2 1 +#define _CTL_3 3 +#define _CTL_4 4 +#define _CTL_5 6 +#define _CTL_6 5 +#define _CTL_7 7 +#define _CTL_8 8 + +class cPeasantTrafficLight +{ +public: + cPeasantTrafficLight(int _type, int _startphase); + ~cPeasantTrafficLight(); + + void next(); + + cObject *getObject(); +private: + cObject *tl; + int iPhase; + int iType; +}; \ No newline at end of file diff --git a/AmpelJonas/cRender.cpp b/AmpelJonas/cRender.cpp new file mode 100644 index 0000000..7df1320 --- /dev/null +++ b/AmpelJonas/cRender.cpp @@ -0,0 +1,183 @@ +#include "cRender.h" + +cRender::cRender(char _backound, WORD _color, int _sx, int _sy) +{ + bBlockRender = false; //If this Constructor is used, this instance is not inherited, thus render() doesn't need to be blocked + + hstdout = GetStdHandle(STD_OUTPUT_HANDLE); //get handle + GetConsoleScreenBufferInfo(hstdout, &csbi); //get current console settings + wDefColor = csbi.wAttributes; //Get default console color + + SetConsoleWindowSize(_sx + 1, _sy + 1); //set the windows size to _sx * _sy (+1 so no scrolling accurs) + + cBackound = _backound; + wBackColor = _color; + + sizeX = _sx; + sizeY = _sy; + + //Initialize 2D array + cScreen = (char**)malloc(sizeof *cScreen * _sx); + for (int i = 0; i < _sx; i++) + cScreen[i] = (char*)malloc(sizeof *cScreen[i] * _sy); + + wColor = (WORD**)malloc(sizeof *wColor * _sx); + for (int i = 0; i < _sx; i++) + wColor[i] = (WORD*)malloc(sizeof *wColor[i] * _sy); + + clear(); //Init backround array +} + +cRender::cRender() {} + +cRender::~cRender() +{ + //Free allocated memory + for (int i = 0; i < sizeX; i++) { + free(cScreen[i]); + free(wColor[i]); + } + + free(cScreen); + free(wColor); +} + +int cRender::drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color) +{ + if (_pos.x >= sizeX || _pos.y >= sizeY || _pos.x < 0 || _pos.y < 0) + return _ERR_COORDINATES_INVALID_; + + if (cScreen[_pos.x][_pos.y] != cBackound && _overrideCollision != true) //detect Collsision + return _COLLISION_; + + cScreen[_pos.x][_pos.y] = _c; + if (_color == _COL_DEFAULT) //_COL_DEFAULT is NOT a proper colorcode! + wColor[_pos.x][_pos.y] = wDefColor; + else + wColor[_pos.x][_pos.y] = _color; + + return 0; +} + +int cRender::drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision, WORD _color) +{ + if (_pos1.x == _pos2.x) { //Horizontal line + for (int i = _pos1.y; i <= _pos2.y; i++) + { + drawPoint(_c, sPos{_pos1.x, i}, _overrideCollision, _color); + } + } + else if (_pos1.y == _pos2.y) { //Vertical line + for (int i = _pos1.x; i <= _pos2.x; i++) + { + drawPoint(_c, sPos{ i, _pos1.y }, _overrideCollision, _color); + } + } + else { //Diagonal Line + int dX = _pos1.x - _pos2.x; + int dY = _pos1.y - _pos2.y; + float fGradient = (float)dY / (float)dX; + + for (int i = 0; i <= abs(dX); i++) + { + drawPoint(_c, sPos{i + _pos1.x, (int)(i * fGradient + _pos1.y + 0.5)}, _overrideCollision, _color); //+0.5 for rounding error + } + } + + return 0; +} + +int cRender::drawText(string _s, sPos _pos, WORD _color) +{ + for (int i = 0; i < _s.length(); i++) + { + drawPoint(_s[i], sPos{ i + _pos.x,_pos.y }, true, _color); + } + return 0; +} + +int cRender::drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor) +{ + //Draw the four outside lines + drawLine(_border, _pos1, sPos{ _pos1.x, _pos2.y }, true, _borderColor); + drawLine(_border, _pos1, sPos{ _pos2.x, _pos1.y }, true, _borderColor); + drawLine(_border, sPos{ _pos1.x, _pos2.y }, _pos2, true, _borderColor); + drawLine(_border, sPos{ _pos2.x, _pos1.y }, _pos2, true, _borderColor); + + //Fill rectangle if _fill isn't NULL + if (_fill) { + for (int i = _pos1.y + 1; i < _pos2.y; i++) { + for (int o = _pos1.x + 1; o < _pos2.x; o++) { + drawPoint(_fill, sPos{ o,i }, true, _fillColor); + } + } + } + + return 0; +} + +int cRender::render(void) +{ + SetConsoleCursorPosition(hstdout, COORD{ 0,0 }); //Set cursor position to be able to alter the image without the effect of it shifting to the top + + if (bBlockRender) + return _ERR_RENDER_BLOCKED_BY_CHILD_; + + for (int i = 0; i < sizeY; i++) { + for (int o = 0; o < sizeX; o++) { + SetConsoleTextAttribute(hstdout, wColor[o][i] | _COL_INTENSITY); + cout << cScreen[o][i]; + } + cout << endl; //New Line Feed + } + return 0; +} + +int cRender::clear(void) +{ + for (int i = 0; i < sizeY; i++) { + for (int o = 0; o < sizeX; o++) { + cScreen[o][i] = cBackound; + wColor[o][i] = wBackColor; + } + } + return 0; +} + +//Source: http://www.cplusplus.com/forum/windows/121444/ +int cRender::SetConsoleWindowSize(int x, int y) +{ + HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); + + if (h == INVALID_HANDLE_VALUE) + return 1; + + CONSOLE_SCREEN_BUFFER_INFO bufferInfo; + if (!GetConsoleScreenBufferInfo(h, &bufferInfo)) + return 1; + + SMALL_RECT& winInfo = bufferInfo.srWindow; + COORD windowSize = { winInfo.Right - winInfo.Left + 1, winInfo.Bottom - winInfo.Top + 1 }; + + if (windowSize.X > x || windowSize.Y > y) + { + // window size needs to be adjusted before the buffer size can be reduced. + SMALL_RECT info = + { + 0, 0, + x < windowSize.X ? x - 1 : windowSize.X - 1, + y < windowSize.Y ? y - 1 : windowSize.Y - 1 + }; + + if (!SetConsoleWindowInfo(h, TRUE, &info)) + return 1; + } + + COORD size = { x, y }; + if (!SetConsoleScreenBufferSize(h, size)) + return 1; + + SMALL_RECT info = { 0, 0, x - 1, y - 1 }; + if (!SetConsoleWindowInfo(h, TRUE, &info)) + return 1; +} \ No newline at end of file diff --git a/AmpelJonas/cRender.h b/AmpelJonas/cRender.h new file mode 100644 index 0000000..dadae40 --- /dev/null +++ b/AmpelJonas/cRender.h @@ -0,0 +1,86 @@ +#pragma once + +#include +#include +#include +#include + + +//errors +#define _OK_ 0 +#define _ERR_ 1 +#define _ERR_COORDINATES_INVALID_ 2 +#define _ERR_RENDER_BLOCKED_BY_CHILD_ 3 + +#define _COLLISION_ 255 + +//Colors +#define _COL_BLACK 0x00 +#define _COL_BLUE 0x01 +#define _COL_GREEN 0x02 +#define _COL_YELLOW 0x0E +#define _COL_RED 0x04 +#define _COL_WHITE 0x0F +#define _COL_DARK_WHITE 0x07 +#define _COL_INTENSITY 0x08 +#define _COL_DEFAULT 0xFF + +using namespace std; + +struct sPos +{ + int x; + int y; +}; + +class cRender +{ +public: + cRender(char _backound, WORD _color, int _sx, int _sy); + //Constructor + //sets cBackround[][] to _backround & wColor[][] to _color + //Resizes console window + + ~cRender(); + //frees allocated memory + + int drawPoint(char _c, sPos _pos, bool _overrideCollision, WORD _color); + //Draws _c @ _pos + //Returns _COLLOSION_ if _pos is already set to !cBackround && _overrideCollision isnt set + + int drawLine(char _c, sPos _pos1, sPos _pos2, bool _overrideCollision, WORD _color); + //x Value of pos1 MUSTNT exceed the x value of pos2! + + int drawText(string _s, sPos _pos, WORD _color); + //Draws Text _s @ _pos + //First char is @ _pos + + int drawRectangle(char _border, char _fill, sPos _pos1, sPos _pos2, WORD _borderColor, WORD _fillColor); + //x Value of pos1 MUSTNT exceed the x value of pos2! + + int render(void); + //Prints cScreen + + int clear(void); + //clears cScreen + wColor + + +protected: + cRender(); //Empty Constructor for being inheritable + + bool bBlockRender; //Used by children to block render function + + char **cScreen; //Pixel Map + WORD **wColor; //Color Map + + char cBackound; //Default backround + WORD wBackColor; + int sizeX, sizeY; + + HANDLE hstdout; + CONSOLE_SCREEN_BUFFER_INFO csbi; + WORD wDefColor; //Default Color +private: + int SetConsoleWindowSize(int x, int y); + //Slightly adapted from: http://www.cplusplus.com/forum/windows/121444/ +}; \ No newline at end of file diff --git a/AmpelJonas/cTrafficLight.cpp b/AmpelJonas/cTrafficLight.cpp new file mode 100644 index 0000000..9844803 --- /dev/null +++ b/AmpelJonas/cTrafficLight.cpp @@ -0,0 +1,139 @@ +#include "cTrafficLight.h" + +cTrafficLight::cTrafficLight(int _type, int _startphase) +{ + iPhase = _startphase - 1; //-1 because iPhase is incremented before switch statement + iType = _type; + if (_type == _TL_SOUTH_ ) { + tl = new cObject(3, 5); + tl->setPosition(_SIZE_X_ / 2 + 10, _SIZE_Y_ / 2 + 7); + tl->drawRectangle('#', NULL, sPos{ 0,0 }, sPos{ 2,4 }, _COL_WHITE, NULL); + } + else if (_type == _TL_NORTH_) { + tl = new cObject(3, 5); + tl->setPosition(_SIZE_X_ / 2 - 11, _SIZE_Y_ / 2 - 11); + tl->drawRectangle('#', NULL, sPos{ 0,0 }, sPos{ 2,4 }, _COL_WHITE, NULL); + } + else if (_type == _TL_EAST_) { + tl = new cObject(5, 3); + tl->setPosition(_SIZE_X_ / 2 + 10, _SIZE_Y_ / 2 - 9); + tl->drawRectangle('#', NULL, sPos{ 0,0 }, sPos{ 4,2 }, _COL_WHITE, NULL); + } + else if (_type == _TL_WEST_) { + tl = new cObject(5, 3); + tl->setPosition(_SIZE_X_ / 2 - 13, _SIZE_Y_ / 2 + 7); + tl->drawRectangle('#', NULL, sPos{ 0,0 }, sPos{ 4,2 }, _COL_WHITE, NULL); + } + + next(); +} + +cTrafficLight::~cTrafficLight() +{ + delete tl; +} + +void cTrafficLight::next() +{ + iPhase >= _TL_COL_RED_YELLOW_ ? iPhase = _TL_COL_GREEN_ : iPhase++; + + switch(iPhase) + { + case _TL_COL_GREEN_: + if (iType == _TL_SOUTH_) { + + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_GREEN); + } + else if (iType == _TL_NORTH_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_GREEN); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_DEFAULT); + } + else if (iType == _TL_EAST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_GREEN); + } + else if (iType == _TL_WEST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_GREEN); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_DEFAULT); + } + break; + case _TL_COL_YELLOW_: + if (iType == _TL_SOUTH_) { + + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_DEFAULT); + } + else if (iType == _TL_NORTH_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_DEFAULT); + } + else if (iType == _TL_EAST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_DEFAULT); + } + else if (iType == _TL_WEST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_DEFAULT); + } + break; + case _TL_COL_RED_: + if (iType == _TL_SOUTH_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_RED); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_DEFAULT); + } + else if (iType == _TL_NORTH_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_RED); + } + else if (iType == _TL_EAST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_RED); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_DEFAULT); + } + else if (iType == _TL_WEST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_RED); + } + break; + case _TL_COL_RED_YELLOW_: + if (iType == _TL_SOUTH_) { + + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_RED); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_DEFAULT); + } + else if (iType == _TL_NORTH_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 1,2 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 1,3 }, true, _COL_RED); + } + else if (iType == _TL_EAST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_RED); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_DEFAULT); + } + else if (iType == _TL_WEST_) { + tl->drawPoint('0', sPos{ 1,1 }, true, _COL_DEFAULT); + tl->drawPoint('0', sPos{ 2,1 }, true, _COL_YELLOW); + tl->drawPoint('0', sPos{ 3,1 }, true, _COL_RED); + } + break; + } +} + +cObject *cTrafficLight::getObject() +{ + return tl; +} \ No newline at end of file diff --git a/AmpelJonas/cTrafficLight.h b/AmpelJonas/cTrafficLight.h new file mode 100644 index 0000000..c17cdb7 --- /dev/null +++ b/AmpelJonas/cTrafficLight.h @@ -0,0 +1,27 @@ +#pragma once +#define _TL_SOUTH_ 0 +#define _TL_NORTH_ 1 +#define _TL_EAST_ 2 +#define _TL_WEST_ 3 + +#define _TL_COL_GREEN_ 0 +#define _TL_COL_YELLOW_ 1 +#define _TL_COL_RED_ 2 +#define _TL_COL_RED_YELLOW_ 3 + +#include "stdafx.h" + +class cTrafficLight +{ +public: + cTrafficLight(int _type, int _startphase); + ~cTrafficLight(); + + void next(); + + cObject *getObject(); +private: + cObject *tl; + int iPhase; + int iType; +}; \ No newline at end of file diff --git a/AmpelJonas/main.cpp b/AmpelJonas/main.cpp new file mode 100644 index 0000000..1b463c1 --- /dev/null +++ b/AmpelJonas/main.cpp @@ -0,0 +1,14 @@ +#include "stdafx.h" + +int main() +{ + cCrossroad *crossroad; + crossroad = new cCrossroad; + crossroad->run(); + delete crossroad; + + cout << endl; + cin.get(); + + return 0; +} \ No newline at end of file diff --git a/AmpelJonas/stdafx.cpp b/AmpelJonas/stdafx.cpp new file mode 100644 index 0000000..b50f832 --- /dev/null +++ b/AmpelJonas/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : Quelldatei, die nur die Standard-Includes einbindet. +// AmpelJonas.pch ist der vorkompilierte Header. +// stdafx.obj enthält die vorkompilierten Typinformationen. + +#include "stdafx.h" + +// TODO: Auf zusätzliche Header verweisen, die in STDAFX.H +// und nicht in dieser Datei erforderlich sind. diff --git a/AmpelJonas/stdafx.h b/AmpelJonas/stdafx.h new file mode 100644 index 0000000..9c52c37 --- /dev/null +++ b/AmpelJonas/stdafx.h @@ -0,0 +1,24 @@ +// stdafx.h: Includedatei für Standardsystem-Includedateien +// oder häufig verwendete projektspezifische Includedateien, +// die nur in unregelmäßigen Abständen geändert werden. +// + +#pragma once +//#include "targetver.h" + +#include +#include +#include +#include + + +#include "cRender.h" +#include "cObject.h" +#include "cObjectHandler.h" +#include "cCrossroad.h" +#include "cTrafficLight.h" +#include "cPeasantTrafficLight.h" +#include "cCar.h" +#include "cPeasant.h" + +using namespace std; \ No newline at end of file diff --git a/AmpelJonas/targetver.h b/AmpelJonas/targetver.h new file mode 100644 index 0000000..a765413 --- /dev/null +++ b/AmpelJonas/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Durch Einbeziehen von"SDKDDKVer.h" wird die höchste verfügbare Windows-Plattform definiert. + +// Wenn Sie die Anwendung für eine frühere Windows-Plattform erstellen möchten, schließen Sie "WinSDKVer.h" ein, und +// legen Sie das _WIN32_WINNT-Makro auf die zu unterstützende Plattform fest, bevor Sie "SDKDDKVer.h" einschließen. + +#include -- cgit v1.2.3