1 /***************************************************************************
3 * Copyright 2010 BMW Car IT GmbH
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 ****************************************************************************/
20 #include "WindowSystems/NullWindowSystem.h"
32 int NullWindowSystem::windowWidth= 1280;
35 int NullWindowSystem::windowHeight= 480;
38 int NullWindowSystem::resolutionWidth= 1280;
41 int NullWindowSystem::resolutionHeight= 480;
44 bool NullWindowSystem::m_initialized= false;
47 bool NullWindowSystem::m_success= false;
50 bool NullWindowSystem::debugMode= false;
53 void NullWindowSystem::printDebug(int posx,int posy){
54 // print stuff about layerlist
55 std::stringstream debugmessage;
56 debugmessage << "Layer: ID | X | Y | W | H | Al. \n";
58 std::list<Layer*> list = layerlist->getCurrentRenderOrder();
59 for(std::list<Layer*>::const_iterator currentLayer = list.begin();
60 currentLayer != list.end(); currentLayer++)
62 Rectangle dest = (*currentLayer)->getDestinationRegion();
63 debugmessage << " " << std::setw(4) << (*currentLayer)->getID() << " " << std::setw(3) << dest.x << " " << std::setw(3) << dest.y << " " << std::setw(3) << dest.width << " " << std::setw(3) << dest.height << " " << std::setw(3) << (*currentLayer)->opacity << "\n";
65 debugmessage << " Surface: ID |Al.| SVP: X | Y | W | H DVP: X | Y | W | H \n";
66 // loop the surfaces of within each layer
67 for(std::list<Surface*>::iterator current = (*currentLayer)->surfaces.begin();
68 current != (*currentLayer)->surfaces.end(); current++)
70 Rectangle src = (*current)->getSourceRegion();
71 Rectangle dest = (*current)->getDestinationRegion();
72 debugmessage << " " << std::setw(4) << (*current)->getID() << " " << std::setprecision(3) << (*current)->opacity<< " " << std::setw(3) << src.x << " " << std::setw(3) << src.y << " " << std::setw(3) << src.width << " " << std::setw(3) << src.height << " " << std::setw(3) << dest.x << " " << std::setw(3) << dest.y << " " << std::setw(3) << dest.width << " " << std::setw(3) << dest.height << "\n";
75 LOG_INFO("NullWindowSystem",debugmessage.str());
78 float TimeCounter, LastFrameTimeCounter, DT, prevTime = 0.0, FPS;
79 struct timeval tv, tv0;
80 int Frame = 1, FramesPerFPS;
82 void UpdateTimeCounter() {
83 LastFrameTimeCounter = TimeCounter;
84 gettimeofday(&tv, NULL);
85 TimeCounter = (float)(tv.tv_sec-tv0.tv_sec) + 0.000001*((float)(tv.tv_usec-tv0.tv_usec));
86 DT = TimeCounter - LastFrameTimeCounter; }
91 if((Frame%FramesPerFPS) == 0) {
92 FPS = ((float)(FramesPerFPS)) / (TimeCounter-prevTime);
93 prevTime = TimeCounter; } }
96 void NullWindowSystem::Redraw()
98 // LOG_INFO("NullWindowSystem","redraw");
99 // draw all the layers
100 graphicSystem->clearBackground();
101 std::list<Layer*> layers = layerlist->getCurrentRenderOrder();
102 for(std::list<Layer*>::const_iterator current = layers.begin(); current != layers.end(); current++){
103 Layer* currentLayer = (Layer*)*current;
104 if ((*currentLayer).visibility && (*currentLayer).opacity>0.0f){
106 // glRotated(-90*currentLayer->getOrientation(),0,0,1.0);
107 // draw all the surfaces of the layer
108 std::list<Surface*> surfaces = currentLayer->surfaces;
109 for(std::list<Surface*>::const_iterator currentS = surfaces.begin(); currentS != surfaces.end(); currentS++){
110 if ((*currentS)->visibility && (*currentS)->opacity>0.0f){
111 Surface* currentSurface = (Surface*)*currentS;
112 PlatformSurface* nativeSurface = (PlatformSurface*)currentSurface->platform;
113 if (NULL==nativeSurface)
115 LOG_INFO("NullWindowSystem","creating native surface for new window");
116 // this surface does not have a native platform surface attached yet!
117 currentSurface->platform = graphicSystem->m_binder->createPlatformSurface(currentSurface);
118 currentSurface->OriginalSourceWidth=256;
119 currentSurface->OriginalSourceHeight=256;
120 graphicSystem->m_binder->createClientBuffer(currentSurface);
121 LOG_INFO("NullWindowSystem","created native surface for new window");
123 graphicSystem->drawSurface(currentLayer,currentSurface);
132 printDebug(200,windowHeight-40);
136 char floatStringBuffer[256];
137 sprintf(floatStringBuffer, "FPS: %f", FPS);
138 if ((Frame % 1000 ) == 0)
140 LOG_INFO("NullWindowSystem",floatStringBuffer);
143 graphicSystem->swapBuffers();
149 void* NullWindowSystem::EventLoop(void * ptr)
151 LOG_DEBUG("NullWindowSystem", "Enter thread");
152 NullWindowSystem *windowsys = (NullWindowSystem *) ptr;
157 // then init graphiclib
160 m_success = windowsys->graphicSystem->init(&display,&window,windowHeight,windowWidth);
162 m_initialized = true;
164 // // get instance of Renderer for this thread
166 // X11WindowSystem *renderer = (X11WindowSystem *) ptr;
167 // if (renderer != NULL)
170 // // initialize the renderer (OpenGL etc)
172 // // run the main event loop while rendering
173 bool running = windowsys->m_success;
174 gettimeofday(&tv0, NULL);
178 defaultLayer = windowsys->layerlist->createLayer();
179 defaultLayer->setOpacity(1.0);
180 defaultLayer->setDestinationRegion(Rectangle(0,0,windowsys->resolutionWidth,windowsys->resolutionHeight));
181 defaultLayer->setSourceRegion(Rectangle(0,0,windowsys->resolutionWidth,windowsys->resolutionHeight));
182 windowsys->layerlist->getCurrentRenderOrder().push_back(defaultLayer);
184 LOG_DEBUG("NullWindowSystem", "Enter render loop");
189 LOG_INFO("NullWindowSystem", "Renderer thread finished");
194 bool NullWindowSystem::start(int displayWidth, int displayHeight, const char* DisplayName){
195 LOG_INFO("NullWindowSystem", "Starting / Creating thread");
196 pthread_t renderThread;
197 NullWindowSystem *renderer = this;
198 resolutionWidth = displayWidth;
199 resolutionHeight = displayHeight;
200 int status = pthread_create( &renderThread, NULL, NullWindowSystem::EventLoop, (void*) renderer);
205 while ( m_initialized == false )
208 LOG_INFO("NullWindowSystem","Waiting start complete " << m_initialized);
210 LOG_INFO("NullWindowSystem","Start complete " << m_initialized << " success " << m_success);