Oh sorry...
main.cpp
#include "LazySpace/LazySpace.hpp"
int main()
{
if(!ls::initGL()) return -1;
ls::update();
}
LazySpace.hpp
#ifndef LAZY_HEADER
#define LAZY_HEADER
#include "SFML/OpenGL.hpp"
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include <stdio.h>
#include "../Texture/Texture.hpp"
namespace ls
{
extern sf::Window* mainWin;
const int SCREEN_WIDTH = 400;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
bool initGL();
bool loadMedia();
void update();
void render();
}
#endif
LazySpace.cpp
#include "LazySpace.hpp"
namespace ls
{
sf::Window* mainWin;
sf::Image image;
Texture* tex;
bool initGL()
{
mainWin = new sf::Window(sf::VideoMode(800, 600), "This");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLuint error = glGetError();
if(error != GL_NO_ERROR)
{
printf("GL init error: %s\n", gluErrorString(error));
return false;
}
if(!loadMedia()) return false;
return true;
}
bool loadMedia()
{
// if(!image.loadFromFile("assets/smile.png"))
// {
// printf("CAN'T LOAD THIS THING");
// return false;
// }
GLubyte* pixels = new GLubyte[128*128];
for(int i = 0; i < 128*128; i++)
{
pixels[i] = 1;
}
tex = new Texture();
tex->loadTextureFromPixels32(pixels, 128, 128);
return true;
}
void update()
{
sf::Event ev;
while(mainWin->isOpen())
{
if(mainWin->pollEvent(ev))
{
if(ev.type == sf::Event::Closed) mainWin->close();
}
render();
}
}
void render()
{
// glClear(GL_COLOR_BUFFER_BIT);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
// glTranslatef(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0);
// glBegin(GL_QUADS);
// glTexCoord2f(0, 0); glVertex2i(50, 50);
// glVertex2i(50, -50);
// glVertex2i(-50, -50);
// glVertex2i(-50, 50);
// glEnd();
tex->render(SCREEN_WIDTH - tex->getTextureWidth() / 2, SCREEN_HEIGHT - tex->getTextureHeight() / 2);
mainWin->display();
}
};
Texture.hpp
#ifndef TEXTURE_H
#define TEXTURE_H
#include "../LazySpace/LazySpace.hpp"
namespace ls
{
class Texture
{
public:
Texture();
~Texture();
bool loadTextureFromPixels32(GLubyte* pixels, GLuint width, GLuint height);
void freeTexture();
void render(GLfloat x, GLfloat y);
GLuint getTextureID();
GLuint getTextureWidth();
GLuint getTextureHeight();
private:
GLuint mTextureID;
GLuint mTextureWidth;
GLuint mTextureHeight;
};
}
#endif
Texture.cpp
#include "Texture.hpp"
namespace ls
{
Texture::Texture()
{
mTextureID = 0;
mTextureWidth = 0;
mTextureHeight = 0;
}
Texture::~Texture()
{
freeTexture();
}
GLuint Texture::getTextureWidth()
{
return mTextureWidth;
}
GLuint Texture::getTextureHeight()
{
return mTextureHeight;
}
bool Texture::loadTextureFromPixels32(GLubyte* pixels, GLuint width, GLuint height)
{
freeTexture();
mTextureWidth = width;
mTextureHeight = height;
glGenTextures(1, &mTextureID);
glBindTexture(GL_TEXTURE_2D, mTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, NULL);
GLenum error = glGetError();
if(error != GL_NO_ERROR);
{
printf("Error loading texture from %p. %s\n", pixels, gluErrorString(error));
return false;
}
return true;
}
void Texture::freeTexture()
{
if(mTextureID != 0)
{
glDeleteTextures(1, &mTextureID);
mTextureID = 0;
}
mTextureWidth = 0;
mTextureHeight = 0;
}
void Texture::render(GLfloat x, GLfloat y)
{
if(mTextureID != 0)
{
glLoadIdentity();
glTranslatef(x, y, 0);
glBindTexture(GL_TEXTURE_2D, mTextureID);
}
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(mTextureWidth, 0, 0);
glTexCoord2f(1, 1); glVertex3f(mTextureWidth, mTextureHeight, 0);
glTexCoord2f(0, 1); glVertex3f(0, mTextureHeight, 0);
glEnd();
}
}
There... thats all of it.
btw, thanks so much for helping with this. I've used SFML for a few years and it's really nice to see that its creator is so involved
Thank you.