[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 | /** |
---|
[4838] | 30 | * standard constructor |
---|
| 31 | */ |
---|
[4839] | 32 | Render2D::Render2D () |
---|
[3655] | 33 | { |
---|
[4839] | 34 | this->setClassID(CL_RENDER_2D, "Render2D"); |
---|
| 35 | this->setName("Render2D"); |
---|
[3655] | 36 | |
---|
[4862] | 37 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 38 | this->element2DList[i] = new tList<Element2D>; |
---|
[3655] | 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
[4838] | 42 | * the singleton reference to this class |
---|
| 43 | */ |
---|
[4839] | 44 | Render2D* Render2D::singletonRef = NULL; |
---|
[3655] | 45 | |
---|
| 46 | /** |
---|
[4840] | 47 | * standard deconstructor |
---|
[4838] | 48 | */ |
---|
[4839] | 49 | Render2D::~Render2D () |
---|
[3655] | 50 | { |
---|
[5286] | 51 | delete NullElement2D::getInstance(); |
---|
| 52 | |
---|
[4862] | 53 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 54 | delete this->element2DList[i]; |
---|
[4840] | 55 | |
---|
[4839] | 56 | Render2D::singletonRef = NULL; |
---|
[3655] | 57 | } |
---|
[4840] | 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * registers a 2D-element to the 2D-Renderer |
---|
| 62 | * @param element2D the element to registers |
---|
[4848] | 63 | * |
---|
| 64 | * do not use this function by yourself, because this is used by Element2D's constructor |
---|
[4840] | 65 | */ |
---|
| 66 | void Render2D::registerElement2D(Element2D* element2D) |
---|
| 67 | { |
---|
[5382] | 68 | if (likely(element2D != NULL)) |
---|
| 69 | this->element2DList[(int)log2(E2D_DEFAULT_LAYER)]->add(element2D); |
---|
[4840] | 70 | } |
---|
| 71 | |
---|
[5382] | 72 | |
---|
[4840] | 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 | { |
---|
[5382] | 92 | if (element2D == NULL) |
---|
| 93 | return; |
---|
| 94 | |
---|
| 95 | if (unlikely(pow(2, E2D_LAYER_COUNT ) > to)) |
---|
| 96 | to = E2D_DEFAULT_LAYER; |
---|
[5078] | 97 | if (element2D->getLayer() != to) |
---|
| 98 | { |
---|
| 99 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
| 100 | this->element2DList[(int)log2(to)]->add(element2D); |
---|
| 101 | } |
---|
[4862] | 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * ticks all the 2d-elements |
---|
| 107 | * @param dt the timestep since last dt |
---|
| 108 | */ |
---|
[4849] | 109 | void Render2D::tick(float dt) |
---|
| 110 | { |
---|
[4862] | 111 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4849] | 112 | { |
---|
[4862] | 113 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 114 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 115 | while (elem != NULL) |
---|
| 116 | { |
---|
[5068] | 117 | if (elem->isActive()) |
---|
| 118 | elem->tick(dt); |
---|
[4862] | 119 | elem = iterator->nextElement(); |
---|
| 120 | } |
---|
| 121 | delete iterator; |
---|
[4849] | 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * renders all the Elements of the Render2D-engine |
---|
[4955] | 127 | * @param layer the Layer to draw |
---|
[4849] | 128 | */ |
---|
[4862] | 129 | void Render2D::draw(unsigned int layer) const |
---|
[4847] | 130 | { |
---|
[4848] | 131 | GraphicsEngine::enter2DMode(); |
---|
| 132 | |
---|
[4862] | 133 | int drawLayer = 1; |
---|
| 134 | |
---|
| 135 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4847] | 136 | { |
---|
[4862] | 137 | if (layer & drawLayer && this->element2DList[i]->getSize() > 0) |
---|
| 138 | { |
---|
| 139 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 140 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 141 | while (elem != NULL) |
---|
| 142 | { |
---|
| 143 | if (elem->isVisible()) |
---|
| 144 | elem->draw(); |
---|
| 145 | elem = iterator->nextElement(); |
---|
| 146 | } |
---|
| 147 | delete iterator; |
---|
| 148 | } |
---|
| 149 | drawLayer << 1; |
---|
[4847] | 150 | } |
---|
[4848] | 151 | GraphicsEngine::leave2DMode(); |
---|
[4847] | 152 | } |
---|