[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 | { |
---|
[5286] | 53 | delete NullElement2D::getInstance(); |
---|
| 54 | |
---|
[4862] | 55 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 56 | delete this->element2DList[i]; |
---|
[4840] | 57 | |
---|
[4839] | 58 | Render2D::singletonRef = NULL; |
---|
[3655] | 59 | } |
---|
[4840] | 60 | |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * registers a 2D-element to the 2D-Renderer |
---|
| 64 | * @param element2D the element to registers |
---|
[4848] | 65 | * |
---|
| 66 | * do not use this function by yourself, because this is used by Element2D's constructor |
---|
[4840] | 67 | */ |
---|
| 68 | void Render2D::registerElement2D(Element2D* element2D) |
---|
| 69 | { |
---|
[4862] | 70 | this->element2DList[(int)log2(E2D_DEFAULT_LAYER)]->add(element2D); |
---|
[4840] | 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * unregisters a 2D-element from the 2D-Renderer |
---|
| 75 | * @param element2D The element to unregister |
---|
[4848] | 76 | * |
---|
| 77 | * do not use this function by yourself, because this is used by Element2D's destructor |
---|
[4840] | 78 | */ |
---|
| 79 | void Render2D::unregisterElement2D(Element2D* element2D) |
---|
| 80 | { |
---|
[4862] | 81 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
[4840] | 82 | } |
---|
[4847] | 83 | |
---|
| 84 | |
---|
[4862] | 85 | /** |
---|
| 86 | * moves an 2D-Element to another Layer |
---|
| 87 | * @param element2D the Element to move |
---|
| 88 | * @param to which layer to move it to. |
---|
| 89 | */ |
---|
| 90 | void Render2D::moveToLayer(Element2D* element2D, E2D_LAYER to) |
---|
| 91 | { |
---|
[5078] | 92 | if (element2D->getLayer() != to) |
---|
| 93 | { |
---|
| 94 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
| 95 | this->element2DList[(int)log2(to)]->add(element2D); |
---|
| 96 | } |
---|
[4862] | 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * ticks all the 2d-elements |
---|
| 102 | * @param dt the timestep since last dt |
---|
| 103 | */ |
---|
[4849] | 104 | void Render2D::tick(float dt) |
---|
| 105 | { |
---|
[4862] | 106 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4849] | 107 | { |
---|
[4862] | 108 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 109 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 110 | while (elem != NULL) |
---|
| 111 | { |
---|
[5068] | 112 | if (elem->isActive()) |
---|
| 113 | elem->tick(dt); |
---|
[4862] | 114 | elem = iterator->nextElement(); |
---|
| 115 | } |
---|
| 116 | delete iterator; |
---|
[4849] | 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * renders all the Elements of the Render2D-engine |
---|
[4955] | 122 | * @param layer the Layer to draw |
---|
[4849] | 123 | */ |
---|
[4862] | 124 | void Render2D::draw(unsigned int layer) const |
---|
[4847] | 125 | { |
---|
[4848] | 126 | GraphicsEngine::enter2DMode(); |
---|
| 127 | |
---|
[4862] | 128 | int drawLayer = 1; |
---|
| 129 | |
---|
| 130 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4847] | 131 | { |
---|
[4862] | 132 | if (layer & drawLayer && this->element2DList[i]->getSize() > 0) |
---|
| 133 | { |
---|
| 134 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 135 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 136 | while (elem != NULL) |
---|
| 137 | { |
---|
| 138 | if (elem->isVisible()) |
---|
| 139 | elem->draw(); |
---|
| 140 | elem = iterator->nextElement(); |
---|
| 141 | } |
---|
| 142 | delete iterator; |
---|
| 143 | } |
---|
| 144 | drawLayer << 1; |
---|
[4847] | 145 | } |
---|
[4848] | 146 | GraphicsEngine::leave2DMode(); |
---|
[4862] | 147 | |
---|
[4847] | 148 | } |
---|