[4744] | 1 | /* |
---|
[3655] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: ... |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
| 17 | |
---|
[4839] | 18 | #include "render_2d.h" |
---|
[3655] | 19 | |
---|
[4840] | 20 | #include "graphics_engine.h" |
---|
| 21 | #include "class_list.h" |
---|
| 22 | #include "list.h" |
---|
| 23 | #include "element_2d.h" |
---|
| 24 | |
---|
[4862] | 25 | #include <math.h> |
---|
| 26 | |
---|
[3655] | 27 | using namespace std; |
---|
| 28 | |
---|
| 29 | |
---|
[4839] | 30 | |
---|
[3655] | 31 | /** |
---|
[4838] | 32 | * standard constructor |
---|
| 33 | */ |
---|
[4839] | 34 | Render2D::Render2D () |
---|
[3655] | 35 | { |
---|
[4839] | 36 | this->setClassID(CL_RENDER_2D, "Render2D"); |
---|
| 37 | this->setName("Render2D"); |
---|
[3655] | 38 | |
---|
[4862] | 39 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 40 | this->element2DList[i] = new tList<Element2D>; |
---|
[3655] | 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
[4838] | 44 | * the singleton reference to this class |
---|
| 45 | */ |
---|
[4839] | 46 | Render2D* Render2D::singletonRef = NULL; |
---|
[3655] | 47 | |
---|
| 48 | /** |
---|
[4840] | 49 | * standard deconstructor |
---|
[4838] | 50 | */ |
---|
[4839] | 51 | Render2D::~Render2D () |
---|
[3655] | 52 | { |
---|
[4862] | 53 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 54 | delete this->element2DList[i]; |
---|
[4840] | 55 | |
---|
[5117] | 56 | delete NullElement2D::getInstance(); |
---|
[4839] | 57 | Render2D::singletonRef = NULL; |
---|
[3655] | 58 | } |
---|
[4840] | 59 | |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * registers a 2D-element to the 2D-Renderer |
---|
| 63 | * @param element2D the element to registers |
---|
[4848] | 64 | * |
---|
| 65 | * do not use this function by yourself, because this is used by Element2D's constructor |
---|
[4840] | 66 | */ |
---|
| 67 | void Render2D::registerElement2D(Element2D* element2D) |
---|
| 68 | { |
---|
[4862] | 69 | this->element2DList[(int)log2(E2D_DEFAULT_LAYER)]->add(element2D); |
---|
[4840] | 70 | } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * unregisters a 2D-element from the 2D-Renderer |
---|
| 74 | * @param element2D The element to unregister |
---|
[4848] | 75 | * |
---|
| 76 | * do not use this function by yourself, because this is used by Element2D's destructor |
---|
[4840] | 77 | */ |
---|
| 78 | void Render2D::unregisterElement2D(Element2D* element2D) |
---|
| 79 | { |
---|
[4862] | 80 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
[4840] | 81 | } |
---|
[4847] | 82 | |
---|
| 83 | |
---|
[4862] | 84 | /** |
---|
| 85 | * moves an 2D-Element to another Layer |
---|
| 86 | * @param element2D the Element to move |
---|
| 87 | * @param to which layer to move it to. |
---|
| 88 | */ |
---|
| 89 | void Render2D::moveToLayer(Element2D* element2D, E2D_LAYER to) |
---|
| 90 | { |
---|
[5078] | 91 | if (element2D->getLayer() != to) |
---|
| 92 | { |
---|
| 93 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
| 94 | this->element2DList[(int)log2(to)]->add(element2D); |
---|
| 95 | } |
---|
[4862] | 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * ticks all the 2d-elements |
---|
| 101 | * @param dt the timestep since last dt |
---|
| 102 | */ |
---|
[4849] | 103 | void Render2D::tick(float dt) |
---|
| 104 | { |
---|
[4862] | 105 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4849] | 106 | { |
---|
[4862] | 107 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 108 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 109 | while (elem != NULL) |
---|
| 110 | { |
---|
[5068] | 111 | if (elem->isActive()) |
---|
| 112 | elem->tick(dt); |
---|
[4862] | 113 | elem = iterator->nextElement(); |
---|
| 114 | } |
---|
| 115 | delete iterator; |
---|
[4849] | 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /** |
---|
| 120 | * renders all the Elements of the Render2D-engine |
---|
[4955] | 121 | * @param layer the Layer to draw |
---|
[4849] | 122 | */ |
---|
[4862] | 123 | void Render2D::draw(unsigned int layer) const |
---|
[4847] | 124 | { |
---|
[4848] | 125 | GraphicsEngine::enter2DMode(); |
---|
| 126 | |
---|
[4862] | 127 | int drawLayer = 1; |
---|
| 128 | |
---|
| 129 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4847] | 130 | { |
---|
[4862] | 131 | if (layer & drawLayer && this->element2DList[i]->getSize() > 0) |
---|
| 132 | { |
---|
| 133 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 134 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 135 | while (elem != NULL) |
---|
| 136 | { |
---|
| 137 | if (elem->isVisible()) |
---|
| 138 | elem->draw(); |
---|
| 139 | elem = iterator->nextElement(); |
---|
| 140 | } |
---|
| 141 | delete iterator; |
---|
| 142 | } |
---|
| 143 | drawLayer << 1; |
---|
[4847] | 144 | } |
---|
[4848] | 145 | GraphicsEngine::leave2DMode(); |
---|
[4862] | 146 | |
---|
[4847] | 147 | } |
---|