Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/2d-recalc/src/lib/graphics/render2D/render_2d.cc @ 5711

Last change on this file since 5711 was 5381, checked in by bensch, 19 years ago

orxonox/branches/2d-recalc: some recalculations.. do not know it i will continue with this, as has certain disadvantages over the old approach… maybe later

File size: 3.6 KB
Line 
1/*
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_GRAPHICS
17
18#include "render_2d.h"
19#include "debug.h"
20
21#include "graphics_engine.h"
22#include "class_list.h"
23#include "list.h"
24#include "element_2d.h"
25
26#include <math.h>
27
28using namespace std;
29
30/**
31 * standard constructor
32 */
33Render2D::Render2D ()
34{
35   this->setClassID(CL_RENDER_2D, "Render2D");
36   this->setName("Render2D");
37
38   for (int i = 0; i < E2D_LAYER_COUNT; i++)
39     this->element2DList[i] = new tList<Element2D>;
40}
41
42/**
43 *  the singleton reference to this class
44 */
45Render2D* Render2D::singletonRef = NULL;
46
47/**
48 * standard deconstructor
49 */
50Render2D::~Render2D ()
51{
52  delete NullElement2D::getInstance();
53
54  for (int i = 0; i < E2D_LAYER_COUNT; i++)
55    delete this->element2DList[i];
56
57  Render2D::singletonRef = NULL;
58}
59
60
61/**
62 * registers a 2D-element to the 2D-Renderer
63 * @param element2D the element to registers
64 *
65 * do not use this function by yourself, because this is used by Element2D's constructor
66 */
67void Render2D::registerElement2D(Element2D* element2D)
68{
69  this->element2DList[(int)log2(E2D_DEFAULT_LAYER)]->add(element2D);
70}
71
72/**
73 * unregisters a 2D-element from the 2D-Renderer
74 * @param element2D The element to unregister
75 *
76 * do not use this function by yourself, because this is used by Element2D's destructor
77 */
78void Render2D::unregisterElement2D(Element2D* element2D)
79{
80  this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D);
81}
82
83
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 */
89void Render2D::moveToLayer(Element2D* element2D, E2D_LAYER to)
90{
91  // prevent switching Layers if either Render2D is not Instanciated or element2D is NULL
92  if (unlikely(element2D == NULL  || this == NULL))
93    return;
94  if(to > pow(2, E2D_LAYER_COUNT))
95  {
96    PRINTF(2)("trying to %s (%s) move to a to non-existing layer %d\n", element2D->getName(), element2D->getClassName() );
97    to = E2D_DEFAULT_LAYER;
98  }
99  if (element2D->getLayer() != to)
100  {
101    this->element2DList[(int)log2(element2D->getLayer())]->remove(element2D);
102    this->element2DList[(int)log2(to)]->add(element2D);
103  }
104}
105
106
107/**
108 * ticks all the 2d-elements
109 * @param dt the timestep since last dt
110 */
111void Render2D::tick(float dt)
112{
113  for (int i = 0; i < E2D_LAYER_COUNT; i++)
114  {
115    tIterator<Element2D>* iterator = this->element2DList[i]->getIterator();
116    Element2D* elem = iterator->firstElement();
117    while (elem != NULL)
118    {
119      if (elem->isActive())
120        elem->tick(dt);
121      elem = iterator->nextElement();
122    }
123    delete iterator;
124  }
125}
126
127/**
128 * renders all the Elements of the Render2D-engine
129 * @param layer the Layer to draw
130 */
131void Render2D::draw(unsigned int layer) const
132{
133  GraphicsEngine::enter2DMode();
134
135  int drawLayer = 1;
136
137  for (int i = 0; i < E2D_LAYER_COUNT; i++)
138  {
139    if (layer & drawLayer && this->element2DList[i]->getSize() > 0)
140    {
141      tIterator<Element2D>* iterator = this->element2DList[i]->getIterator();
142      Element2D* elem = iterator->firstElement();
143      while (elem != NULL)
144      {
145        if (elem->isVisible())
146          elem->draw();
147        elem = iterator->nextElement();
148      }
149      delete iterator;
150    }
151    drawLayer << 1;
152  }
153  GraphicsEngine::leave2DMode();
154}
Note: See TracBrowser for help on using the repository browser.