[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 | |
---|
[5317] | 25 | #include "shader.h" |
---|
| 26 | |
---|
[4862] | 27 | #include <math.h> |
---|
| 28 | |
---|
[3655] | 29 | using namespace std; |
---|
| 30 | |
---|
| 31 | |
---|
[4839] | 32 | |
---|
[3655] | 33 | /** |
---|
[4838] | 34 | * standard constructor |
---|
| 35 | */ |
---|
[4839] | 36 | Render2D::Render2D () |
---|
[3655] | 37 | { |
---|
[4839] | 38 | this->setClassID(CL_RENDER_2D, "Render2D"); |
---|
| 39 | this->setName("Render2D"); |
---|
[3655] | 40 | |
---|
[4862] | 41 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 42 | this->element2DList[i] = new tList<Element2D>; |
---|
[3655] | 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
[4838] | 46 | * the singleton reference to this class |
---|
| 47 | */ |
---|
[4839] | 48 | Render2D* Render2D::singletonRef = NULL; |
---|
[3655] | 49 | |
---|
| 50 | /** |
---|
[4840] | 51 | * standard deconstructor |
---|
[4838] | 52 | */ |
---|
[4839] | 53 | Render2D::~Render2D () |
---|
[3655] | 54 | { |
---|
[5286] | 55 | delete NullElement2D::getInstance(); |
---|
| 56 | |
---|
[4862] | 57 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
| 58 | delete this->element2DList[i]; |
---|
[4840] | 59 | |
---|
[4839] | 60 | Render2D::singletonRef = NULL; |
---|
[3655] | 61 | } |
---|
[4840] | 62 | |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * registers a 2D-element to the 2D-Renderer |
---|
| 66 | * @param element2D the element to registers |
---|
[4848] | 67 | * |
---|
| 68 | * do not use this function by yourself, because this is used by Element2D's constructor |
---|
[4840] | 69 | */ |
---|
| 70 | void Render2D::registerElement2D(Element2D* element2D) |
---|
| 71 | { |
---|
[4862] | 72 | this->element2DList[(int)log2(E2D_DEFAULT_LAYER)]->add(element2D); |
---|
[4840] | 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * unregisters a 2D-element from the 2D-Renderer |
---|
| 77 | * @param element2D The element to unregister |
---|
[4848] | 78 | * |
---|
| 79 | * do not use this function by yourself, because this is used by Element2D's destructor |
---|
[4840] | 80 | */ |
---|
| 81 | void Render2D::unregisterElement2D(Element2D* element2D) |
---|
| 82 | { |
---|
[4862] | 83 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
[4840] | 84 | } |
---|
[4847] | 85 | |
---|
| 86 | |
---|
[4862] | 87 | /** |
---|
| 88 | * moves an 2D-Element to another Layer |
---|
| 89 | * @param element2D the Element to move |
---|
| 90 | * @param to which layer to move it to. |
---|
| 91 | */ |
---|
| 92 | void Render2D::moveToLayer(Element2D* element2D, E2D_LAYER to) |
---|
| 93 | { |
---|
[5078] | 94 | if (element2D->getLayer() != to) |
---|
| 95 | { |
---|
| 96 | this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D); |
---|
| 97 | this->element2DList[(int)log2(to)]->add(element2D); |
---|
| 98 | } |
---|
[4862] | 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * ticks all the 2d-elements |
---|
| 104 | * @param dt the timestep since last dt |
---|
| 105 | */ |
---|
[4849] | 106 | void Render2D::tick(float dt) |
---|
| 107 | { |
---|
[4862] | 108 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4849] | 109 | { |
---|
[4862] | 110 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 111 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 112 | while (elem != NULL) |
---|
| 113 | { |
---|
[5068] | 114 | if (elem->isActive()) |
---|
| 115 | elem->tick(dt); |
---|
[4862] | 116 | elem = iterator->nextElement(); |
---|
| 117 | } |
---|
| 118 | delete iterator; |
---|
[4849] | 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * renders all the Elements of the Render2D-engine |
---|
[4955] | 124 | * @param layer the Layer to draw |
---|
[4849] | 125 | */ |
---|
[4862] | 126 | void Render2D::draw(unsigned int layer) const |
---|
[4847] | 127 | { |
---|
[5317] | 128 | Shader::suspendShader(); |
---|
| 129 | |
---|
[4848] | 130 | GraphicsEngine::enter2DMode(); |
---|
| 131 | |
---|
[4862] | 132 | int drawLayer = 1; |
---|
| 133 | |
---|
| 134 | for (int i = 0; i < E2D_LAYER_COUNT; i++) |
---|
[4847] | 135 | { |
---|
[4862] | 136 | if (layer & drawLayer && this->element2DList[i]->getSize() > 0) |
---|
| 137 | { |
---|
| 138 | tIterator<Element2D>* iterator = this->element2DList[i]->getIterator(); |
---|
[5115] | 139 | Element2D* elem = iterator->firstElement(); |
---|
[4862] | 140 | while (elem != NULL) |
---|
| 141 | { |
---|
| 142 | if (elem->isVisible()) |
---|
| 143 | elem->draw(); |
---|
| 144 | elem = iterator->nextElement(); |
---|
| 145 | } |
---|
| 146 | delete iterator; |
---|
| 147 | } |
---|
| 148 | drawLayer << 1; |
---|
[4847] | 149 | } |
---|
[4848] | 150 | GraphicsEngine::leave2DMode(); |
---|
[4862] | 151 | |
---|
[5317] | 152 | Shader::restoreShader(); |
---|
[4847] | 153 | } |
---|