• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
texturemanager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Henry de Valence <hdevalence@gmail.com>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY
12  or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 
19 */
20 
21 #include "texturemanager.h"
22 #include "skymap.h"
23 #include "kstars.h"
24 
25 #include <kstandarddirs.h>
26 
27 #ifdef HAVE_OPENGL
28 # include <QGLWidget>
29 #endif
30 
31 // We returning reference to image. We refer to this image when search
32 // for image fails
33 const static QImage emptyImage;
34 
35 TextureManager* TextureManager::m_p;
36 
37 
38 TextureManager *TextureManager::Create() {
39  if( !m_p )
40  m_p = new TextureManager();
41  return m_p;
42 }
43 
44 const QImage& TextureManager::getImage(const QString& name)
45 {
46  Create();
47  if(name == "")
48  return emptyImage;
49  CacheIter it = findTexture( name );
50  if( it != m_p->m_textures.constEnd() ) {
51  return *it;
52  } else {
53  return emptyImage;
54  }
55 }
56 
57 // FIXME: should be cache images which are not found?
58 TextureManager::CacheIter TextureManager::findTexture(const QString& name)
59 {
60  Create();
61  // Lookup in cache first
62  CacheIter it = m_p->m_textures.constFind( name );
63  if( it != m_p->m_textures.constEnd() ) {
64  return it;
65  } else {
66  // Try to load from file in 'textures' subdirectory
67  QString filename = KStandardDirs::locate("appdata",QString("textures/%1.png").arg(name));
68  if( !filename.isNull() ) {
69  return (TextureManager::CacheIter)m_p->m_textures.insert( name, QImage(filename,"PNG") );
70  } else {
71  // Try to load from file in main data directory
72  filename = KStandardDirs::locate("appdata",QString("%1.png").arg(name));
73  if( !filename.isNull() ) {
74  return (TextureManager::CacheIter)m_p->m_textures.insert( name, QImage(filename,"PNG") );
75  } else {
76  return m_p->m_textures.constEnd();
77  }
78  }
79  }
80 }
81 
82 #ifdef HAVE_OPENGL
83 static void bindImage(const QImage& img, QGLWidget* cxt)
84 {
85  GLuint tid = cxt->bindTexture(img, GL_TEXTURE_2D, GL_RGBA, QGLContext::DefaultBindOption);
86  glBindTexture(GL_TEXTURE_2D, tid);
87 }
88 
89 // FIXME: should we check that image have appropriate size as bindFromImage do?
90 void TextureManager::bindTexture(const QString& name, QGLWidget* cxt)
91 {
92  Create();
93  Q_ASSERT( "Must be called only with valid GL context" && cxt );
94 
95  CacheIter it = findTexture( name );
96  if( it != m_p->m_textures.constEnd() )
97  bindImage( *it, cxt );
98 }
99 
100 void TextureManager::bindFromImage(const QImage& image, QGLWidget* cxt)
101 {
102  Create();
103  Q_ASSERT( "Must be called only with valid GL context" && cxt );
104 
105  if ( image.width() != image.height() || ( image.width() & ( image.width() - 1 ) ) ) {
106  // Compute texture size
107  int longest = qMax( image.width(), image.height() );
108  int tex_size = 2;
109  while ( tex_size < longest ) {
110  tex_size *= 2;
111  }
112  // FIXME: Check if Qt does this for us already. [Note that it does scale to the nearest power of two]
113  bindImage(
114  image.scaled( tex_size, tex_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ),
115  cxt );
116  } else {
117  bindImage( image, cxt );
118  }
119 }
120 #endif
121 
122 TextureManager::TextureManager(QObject* parent): QObject(parent)
123 {}
TextureManager
a singleton class to manage texture loading/retrieval
Definition: texturemanager.h:33
QObject
TextureManager::getImage
static const QImage & getImage(const QString &name)
Return texture image.
Definition: texturemanager.cpp:44
texturemanager.h
TextureManager::Create
static TextureManager * Create()
Create the instance of TextureManager.
Definition: texturemanager.cpp:38
skymap.h
QGLWidget
emptyImage
static const QImage emptyImage
Definition: texturemanager.cpp:33
kstars.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal