Skip to content
Browse files

Trying to load multiple textures but can't get it working

  • Loading branch information...
1 parent 1190a80 commit 5e77babc381a18535a3cc8abe173c6b1f0ac181e @RachelJMorris committed
Showing with 85 additions and 22 deletions.
  1. +2 −2 Geometry.cpp
  2. +3 −1 Geometry.h
  3. +28 −0 Texture.cpp
  4. +21 −0 Texture.h
  5. +31 −19 main.cpp
View
4 Geometry.cpp
@@ -31,12 +31,12 @@ void Geometry::Setup( const sf::Vector3f& pos, const sf::Vector3f& dim, const sf
m_lstPoints.push_back( sf::Vector3f( pos.x, pos.y + dim.y, pos.z + dim.z ) );
}
-void Geometry::Draw( const GLuint& texture )
+void Geometry::Draw( const Texture& texture )
{
glEnable( GL_TEXTURE_2D );
glBegin( GL_QUADS );
- glBindTexture( GL_TEXTURE_2D, texture );
+ glBindTexture( GL_TEXTURE_2D, texture.m_texture );
DrawXY();
DrawXZ();
View
4 Geometry.h
@@ -1,6 +1,8 @@
#ifndef _GEOMETRY
#define _GEOMETRY
+#include "Texture.h"
+
#include <SFML/Graphics.hpp>
#include <vector>
@@ -14,7 +16,7 @@ class Geometry
Geometry();
Geometry( const sf::Vector3f& pos, const sf::Vector3f& dim, const sf::Vector3i& );
void Setup( const sf::Vector3f& pos, const sf::Vector3f& dim, const sf::Vector3i& );
- void Draw( const GLuint& texture );
+ void Draw( const Texture& texture );
void CheckCursor( const sf::Vector3f& cursor );
private:
View
28 Texture.cpp
@@ -0,0 +1,28 @@
+#include "Texture.h"
+
+namespace ursa
+{
+
+void Texture::Load( const std::string& filename )
+{
+ sf::Image image;
+ image.LoadFromFile( filename );
+ image.SetSmooth( false );
+
+ glGenTextures( 1, &m_texture );
+ glBindTexture( GL_TEXTURE_2D, m_texture );
+ gluBuild2DMipmaps( GL_TEXTURE_2D,
+ GL_RGBA,
+ image.GetWidth(),
+ image.GetHeight(),
+ GL_RGBA,
+ GL_UNSIGNED_BYTE,
+ image.GetPixelsPtr() );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
+ m_dimensions.x = image.GetWidth();
+ m_dimensions.y = image.GetHeight();
+
+}
+
+}
View
21 Texture.h
@@ -0,0 +1,21 @@
+#ifndef _TEXTURE
+#define _TEXTURE
+
+#include <SFML/Graphics.hpp>
+
+#include <string>
+
+namespace ursa
+{
+
+class Texture
+{
+ public:
+ void Load( const std::string& filename );
+ GLuint m_texture;
+ sf::Vector2f m_dimensions;
+};
+
+}
+
+#endif
View
50 main.cpp
@@ -7,9 +7,12 @@
#include "Camera.h"
#include "Input.h"
#include "Geometry.h"
+#include "Texture.h"
using namespace std;
+void LoadTextures( std::vector<ursa::Texture>& lstTextures );
+
int main()
{
ursa::Application app;
@@ -17,6 +20,9 @@ int main()
camera.Setup( sf::Vector3f( -50, 30, -115 ), sf::Vector3f( 52, 0, 0 ) );
ursa::Input input;
+ std::vector<ursa::Texture> lstTextures;
+ LoadTextures( lstTextures );
+
const int mapWidth = 10;
const int mapDepth = 10;
@@ -30,24 +36,7 @@ int main()
}
}
- sf::Vector3f cursor;
- cursor.x = 0;
- cursor.y = 0;
- cursor.z = 0;
-
- sf::Image imgTxt;
- imgTxt.LoadFromFile( "assets/brick.png" );
- imgTxt.SetSmooth( false );
-
- GLuint texture;
- glGenTextures( 1, &texture );
- glBindTexture( GL_TEXTURE_2D, texture );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
- imgTxt.GetWidth(), imgTxt.GetHeight(),
- 0, GL_RGBA, GL_UNSIGNED_BYTE, imgTxt.GetPixelsPtr() );
-
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
+ sf::Vector3f cursor(0, 0, 0);
while ( !app.Quit() )
{
@@ -81,7 +70,7 @@ int main()
for ( int i = 0; i < lstBoxes.size(); i++ )
{
- lstBoxes[i].Draw( texture );
+ lstBoxes[i].Draw( lstTextures[i%5] );
}
app.FinishDraw();
@@ -89,3 +78,26 @@ int main()
return 0;
}
+
+void LoadTextures( std::vector<ursa::Texture>& lstTextures )
+{
+ ursa::Texture txtGrass;
+ txtGrass.Load( "assets/grass.png" );
+ lstTextures.push_back( txtGrass );
+
+ ursa::Texture txtBrick;
+ txtBrick.Load( "assets/brick.png" );
+ lstTextures.push_back( txtBrick );
+
+ ursa::Texture txtWood;
+ txtWood.Load( "assets/wood.png" );
+ lstTextures.push_back( txtWood );
+
+ ursa::Texture txtSand;
+ txtSand.Load( "assets/sand.png" );
+ lstTextures.push_back( txtSand );
+
+ ursa::Texture txtWater;
+ txtWater.Load( "assets/water.png" );
+ lstTextures.push_back( txtWater );
+}

0 comments on commit 5e77bab

Please sign in to comment.
Something went wrong with that request. Please try again.