Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML2 Texture/Image loadFroPixels  (Read 2639 times)

0 Members and 1 Guest are viewing this topic.

Richy19

  • Full Member
  • ***
  • Posts: 190
    • View Profile
SFML2 Texture/Image loadFroPixels
« on: May 03, 2012, 06:49:31 pm »
Im trying to convert the libRocket SFML renderer from 1.6 to 2.0 and I have done more or less everyhthing apart from the texture creation, the included SFML renderer uses the old loadFromPixels but I just wanted to know how you would do this in 2.0

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML2 Texture/Image loadFroPixels
« Reply #1 on: May 03, 2012, 07:01:52 pm »
texture.create(width, height);
texture.update(pixels);
Or
sf::Image image;
image.create(width, height, pixels);
texture.loadFromImage(image);
Laurent Gomila - SFML developer

Richy19

  • Full Member
  • ***
  • Posts: 190
    • View Profile
Re: SFML2 Texture/Image loadFroPixels
« Reply #2 on: May 03, 2012, 07:45:21 pm »
Ok, So I got libRocket drawing, but there seems to be something weird going on with the resizing and input.
Also when the program is run I get some OGL errors:

Quote
libRocket/Samples/basic/sfml/bin/Debug$ ./Agamed
An internal OpenGL call failed in RenderTarget.cpp (244) : GL_STACK_UNDERFLOW, this command would cause a stack underflow
An internal OpenGL call failed in RenderTarget.cpp (245) : GL_STACK_UNDERFLOW, this command would cause a stack underflow
An internal OpenGL call failed in RenderTarget.cpp (247) : GL_STACK_UNDERFLOW, this command would cause a stack underflow
An internal OpenGL call failed in RenderTarget.cpp (249) : GL_STACK_UNDERFLOW, this command would cause a stack underflow
An internal OpenGL call failed in RenderTarget.cpp (251) : GL_STACK_UNDERFLOW, this command would cause a stack underflow
[Info] - Loaded font face Delicious Bold (from Delicious-Bold.otf).
[Info] - Loaded font face Delicious Bold Italic (from Delicious-BoldItalic.otf).
[Info] - Loaded font face Delicious Italic (from Delicious-Italic.otf).
[Info] - Loaded font face Delicious Roman (from Delicious-Roman.otf).
[Info] - Loaded font face Lacuna Regular Regular (from byte stream).
[Info] - Loaded font face Lacuna Italic Regular (from byte stream).

this is what the 2.0 code looks like:
RenderInterface:

Code: [Select]
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 Nuno Silva
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include <Rocket/Core/Core.h>
#include "RenderInterfaceSFML.h"
#include <GL/gl.h>

#define GL_CLAMP_TO_EDGE 0x812F

// If built with the GL Easy Extension library we can compile geometry to VBO's
// http://www.opengl.org/sdk/libs/GLee/
#ifdef ENABLE_GLEE
#include <GL/glee.h>

class RocketSFMLRendererGeometryHandler
{
public:
GLuint VertexID, IndexID;
int NumVertices;
Rocket::Core::TextureHandle Texture;

RocketSFMLRendererGeometryHandler() : VertexID(0), IndexID(0), Texture(0), NumVertices(0)
{
};

~RocketSFMLRendererGeometryHandler()
{
if(VertexID)
glDeleteBuffers(1, &VertexID);

if(IndexID)
glDeleteBuffers(1, &IndexID);

VertexID = IndexID = 0;
};
};

#endif

struct RocketSFMLRendererVertex
{
sf::Vector2f Position, TexCoord;
sf::Color Color;
};

RocketSFMLRenderer::RocketSFMLRenderer()
{
}

void RocketSFMLRenderer::SetWindow(sf::RenderWindow *Window)
{
MyWindow = Window;

Resize();
};

sf::RenderWindow *RocketSFMLRenderer::GetWindow()
{
return MyWindow;
};

void RocketSFMLRenderer::Resize()
{
MyWindow->setActive(true);


sf::View View(sf::FloatRect(0, 0, (float)MyWindow->getSize().x, (float)MyWindow->getSize().y));
MyWindow->setView(View);

MyWindow->popGLStates();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, MyWindow->getSize().x, MyWindow->getSize().y, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

glViewport(0, 0, MyWindow->getSize().x, MyWindow->getSize().y);
MyWindow->pushGLStates();
   
};

// Called by Rocket when it wants to render geometry that it does not wish to optimise.
void RocketSFMLRenderer::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
{
MyWindow->setActive();

glPushMatrix();
glTranslatef(translation.x, translation.y, 0);

std::vector<Rocket::Core::Vector2f> Positions(num_vertices);
std::vector<Rocket::Core::Colourb> Colors(num_vertices);
std::vector<Rocket::Core::Vector2f> TexCoords(num_vertices);

for(int  i = 0; i < num_vertices; i++)
{
Positions[i] = vertices[i].position;
Colors[i] = vertices[i].colour;
TexCoords[i] = vertices[i].tex_coord;
};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, &Positions[0]);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

sf::Texture *image = (sf::Texture*)texture;

if(image)
{
image->bind();
}
else
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0);
};

glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glColor4f(1, 1, 1, 1);

glPopMatrix();
}

// Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
Rocket::Core::CompiledGeometryHandle RocketSFMLRenderer::CompileGeometry(Rocket::Core::Vertex* vertices,
   int num_vertices, int* indices,
   int num_indices,
   const Rocket::Core::TextureHandle texture)
{
#ifdef ENABLE_GLEE
MyWindow->SetActive();

if(!GLEE_VERSION_2_0)
return (Rocket::Core::CompiledGeometryHandle) NULL;

std::vector<RocketSFMLRendererVertex> Data(num_vertices);

for(unsigned long i = 0; i < Data.size(); i++)
{
Data[i].Position = *(sf::Vector2f*)&vertices[i].position;
Data[i].TexCoord = *(sf::Vector2f*)&vertices[i].tex_coord;
Data[i].Color = sf::Color(vertices[i].colour.red, vertices[i].colour.green,
vertices[i].colour.blue, vertices[i].colour.alpha);
};

RocketSFMLRendererGeometryHandler *Geometry = new RocketSFMLRendererGeometryHandler();
Geometry->NumVertices = num_indices;

glGenBuffers(1, &Geometry->VertexID);
glBindBuffer(GL_ARRAY_BUFFER, Geometry->VertexID);
glBufferData(GL_ARRAY_BUFFER, sizeof(RocketSFMLRendererVertex) * num_vertices, &Data[0],
GL_STATIC_DRAW);

glGenBuffers(1, &Geometry->IndexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Geometry->IndexID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * num_indices, indices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

Geometry->Texture = texture;

return (Rocket::Core::CompiledGeometryHandle)Geometry;
#else
return NULL;
#endif
}

// Called by Rocket when it wants to render application-compiled geometry.
void RocketSFMLRenderer::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
{
#ifdef ENABLE_GLEE
MyWindow->setActive();

RocketSFMLRendererGeometryHandler *RealGeometry = (RocketSFMLRendererGeometryHandler *)geometry;

glPushMatrix();
glTranslatef(translation.x, translation.y, 0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

sf::Texture *image = (sf::Texture *)RealGeometry->Texture;

if(image)
{
image->bind();
}
else
{
glBindTexture(GL_TEXTURE_2D, 0);
};

glEnable(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_COLOR_ARRAY);

#define BUFFER_OFFSET(x) ((char*)0 + x)

glBindBuffer(GL_ARRAY_BUFFER, RealGeometry->VertexID);
glVertexPointer(2, GL_FLOAT, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(0));
glTexCoordPointer(2, GL_FLOAT, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f)));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f[2])));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RealGeometry->IndexID);
glDrawElements(GL_TRIANGLES, RealGeometry->NumVertices, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

glBindBuffer(GL_ARRAY_BUFFER, 0);

glDisable(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_VERTEX_ARRAY);

glColor4f(1, 1, 1, 1);

glPopMatrix();
#else
ROCKET_ASSERT(false & "Not Implemented");
#endif
}

// Called by Rocket when it wants to release application-compiled geometry.
void RocketSFMLRenderer::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry)
{
#ifdef ENABLE_GLEE
MyWindow->SetActive();

delete (RocketSFMLRendererGeometryHandler *)geometry;
#else
ROCKET_ASSERT(false & "Not Implemented");
#endif
}

// Called by Rocket when it wants to enable or disable scissoring to clip content.
void RocketSFMLRenderer::EnableScissorRegion(bool enable)
{
MyWindow->setActive();

if (enable)
glEnable(GL_SCISSOR_TEST);
else
glDisable(GL_SCISSOR_TEST);
}

// Called by Rocket when it wants to change the scissor region.
void RocketSFMLRenderer::SetScissorRegion(int x, int y, int width, int height)
{
MyWindow->setActive();

glScissor(x, MyWindow->getSize().y - (y + height), width, height);
}

// Called by Rocket when a texture is required by the library.
bool RocketSFMLRenderer::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
{
MyWindow->setActive();

Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
Rocket::Core::FileHandle file_handle = file_interface->Open(source);
if (file_handle == NULL)
return false;

file_interface->Seek(file_handle, 0, SEEK_END);
size_t buffer_size = file_interface->Tell(file_handle);
file_interface->Seek(file_handle, 0, SEEK_SET);

char* buffer = new char[buffer_size];
file_interface->Read(buffer, buffer_size, file_handle);
file_interface->Close(file_handle);

sf::Texture *image = new sf::Texture();

if(!image->loadFromMemory(buffer, buffer_size))
{
delete buffer;
delete image;

return false;
};
delete buffer;

texture_handle = (Rocket::Core::TextureHandle) image;
texture_dimensions = Rocket::Core::Vector2i(image->getSize().x, image->getSize().y);

return true;
}

// Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
bool RocketSFMLRenderer::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
{
MyWindow->setActive();

sf::Texture *image = new sf::Texture();

image->create(source_dimensions.x, source_dimensions.y);
image->update(source);

texture_handle = (Rocket::Core::TextureHandle)image;

return true;
}

// Called by Rocket when a loaded texture is no longer required.
void RocketSFMLRenderer::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
{
MyWindow->setActive();

delete (sf::Texture *)texture_handle;
}

And the systemInterface:

Code: [Select]
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 Nuno Silva
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
#include <Rocket/Core.h>
#include "SystemInterfaceSFML.h"

int RocketSFMLSystemInterface::GetKeyModifiers(sf::Window *Window)
{
int Modifiers = 0;

if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RShift))
Modifiers |= Rocket::Core::Input::KM_SHIFT;

if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RControl))
Modifiers |= Rocket::Core::Input::KM_CTRL;

if(sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt))
Modifiers |= Rocket::Core::Input::KM_ALT;

return Modifiers;
};

Rocket::Core::Input::KeyIdentifier RocketSFMLSystemInterface::TranslateKey(sf::Keyboard::Key Key)
{
switch(Key)
{
case sf::Keyboard::A:
return Rocket::Core::Input::KI_A;
break;
case sf::Keyboard::B:
return Rocket::Core::Input::KI_B;
break;
case sf::Keyboard::C:
return Rocket::Core::Input::KI_C;
break;
case sf::Keyboard::D:
return Rocket::Core::Input::KI_D;
break;
case sf::Keyboard::E:
return Rocket::Core::Input::KI_E;
break;
case sf::Keyboard::F:
return Rocket::Core::Input::KI_F;
break;
case sf::Keyboard::G:
return Rocket::Core::Input::KI_G;
break;
case sf::Keyboard::H:
return Rocket::Core::Input::KI_H;
break;
case sf::Keyboard::I:
return Rocket::Core::Input::KI_I;
break;
case sf::Keyboard::J:
return Rocket::Core::Input::KI_J;
break;
case sf::Keyboard::K:
return Rocket::Core::Input::KI_K;
break;
case sf::Keyboard::L:
return Rocket::Core::Input::KI_L;
break;
case sf::Keyboard::M:
return Rocket::Core::Input::KI_M;
break;
case sf::Keyboard::N:
return Rocket::Core::Input::KI_N;
break;
case sf::Keyboard::O:
return Rocket::Core::Input::KI_O;
break;
case sf::Keyboard::P:
return Rocket::Core::Input::KI_P;
break;
case sf::Keyboard::Q:
return Rocket::Core::Input::KI_Q;
break;
case sf::Keyboard::R:
return Rocket::Core::Input::KI_R;
break;
case sf::Keyboard::S:
return Rocket::Core::Input::KI_S;
break;
case sf::Keyboard::T:
return Rocket::Core::Input::KI_T;
break;
case sf::Keyboard::U:
return Rocket::Core::Input::KI_U;
break;
case sf::Keyboard::V:
return Rocket::Core::Input::KI_V;
break;
case sf::Keyboard::W:
return Rocket::Core::Input::KI_W;
break;
case sf::Keyboard::X:
return Rocket::Core::Input::KI_X;
break;
case sf::Keyboard::Y:
return Rocket::Core::Input::KI_Y;
break;
case sf::Keyboard::Z:
return Rocket::Core::Input::KI_Z;
break;
case sf::Keyboard::Num0:
return Rocket::Core::Input::KI_0;
break;
case sf::Keyboard::Num1:
return Rocket::Core::Input::KI_1;
break;
case sf::Keyboard::Num2:
return Rocket::Core::Input::KI_2;
break;
case sf::Keyboard::Num3:
return Rocket::Core::Input::KI_3;
break;
case sf::Keyboard::Num4:
return Rocket::Core::Input::KI_4;
break;
case sf::Keyboard::Num5:
return Rocket::Core::Input::KI_5;
break;
case sf::Keyboard::Num6:
return Rocket::Core::Input::KI_6;
break;
case sf::Keyboard::Num7:
return Rocket::Core::Input::KI_7;
break;
case sf::Keyboard::Num8:
return Rocket::Core::Input::KI_8;
break;
case sf::Keyboard::Num9:
return Rocket::Core::Input::KI_9;
break;
case sf::Keyboard::Numpad0:
return Rocket::Core::Input::KI_NUMPAD0;
break;
case sf::Keyboard::Numpad1:
return Rocket::Core::Input::KI_NUMPAD1;
break;
case sf::Keyboard::Numpad2:
return Rocket::Core::Input::KI_NUMPAD2;
break;
case sf::Keyboard::Numpad3:
return Rocket::Core::Input::KI_NUMPAD3;
break;
case sf::Keyboard::Numpad4:
return Rocket::Core::Input::KI_NUMPAD4;
break;
case sf::Keyboard::Numpad5:
return Rocket::Core::Input::KI_NUMPAD5;
break;
case sf::Keyboard::Numpad6:
return Rocket::Core::Input::KI_NUMPAD6;
break;
case sf::Keyboard::Numpad7:
return Rocket::Core::Input::KI_NUMPAD7;
break;
case sf::Keyboard::Numpad8:
return Rocket::Core::Input::KI_NUMPAD8;
break;
case sf::Keyboard::Numpad9:
return Rocket::Core::Input::KI_NUMPAD9;
break;
case sf::Keyboard::Left:
return Rocket::Core::Input::KI_LEFT;
break;
case sf::Keyboard::Right:
return Rocket::Core::Input::KI_RIGHT;
break;
case sf::Keyboard::Up:
return Rocket::Core::Input::KI_UP;
break;
case sf::Keyboard::Down:
return Rocket::Core::Input::KI_DOWN;
break;
case sf::Keyboard::Add:
return Rocket::Core::Input::KI_ADD;
break;
case sf::Keyboard::Back:
return Rocket::Core::Input::KI_BACK;
break;
case sf::Keyboard::Delete:
return Rocket::Core::Input::KI_DELETE;
break;
case sf::Keyboard::Divide:
return Rocket::Core::Input::KI_DIVIDE;
break;
case sf::Keyboard::End:
return Rocket::Core::Input::KI_END;
break;
case sf::Keyboard::Escape:
return Rocket::Core::Input::KI_ESCAPE;
break;
case sf::Keyboard::F1:
return Rocket::Core::Input::KI_F1;
break;
case sf::Keyboard::F2:
return Rocket::Core::Input::KI_F2;
break;
case sf::Keyboard::F3:
return Rocket::Core::Input::KI_F3;
break;
case sf::Keyboard::F4:
return Rocket::Core::Input::KI_F4;
break;
case sf::Keyboard::F5:
return Rocket::Core::Input::KI_F5;
break;
case sf::Keyboard::F6:
return Rocket::Core::Input::KI_F6;
break;
case sf::Keyboard::F7:
return Rocket::Core::Input::KI_F7;
break;
case sf::Keyboard::F8:
return Rocket::Core::Input::KI_F8;
break;
case sf::Keyboard::F9:
return Rocket::Core::Input::KI_F9;
break;
case sf::Keyboard::F10:
return Rocket::Core::Input::KI_F10;
break;
case sf::Keyboard::F11:
return Rocket::Core::Input::KI_F11;
break;
case sf::Keyboard::F12:
return Rocket::Core::Input::KI_F12;
break;
case sf::Keyboard::F13:
return Rocket::Core::Input::KI_F13;
break;
case sf::Keyboard::F14:
return Rocket::Core::Input::KI_F14;
break;
case sf::Keyboard::F15:
return Rocket::Core::Input::KI_F15;
break;
case sf::Keyboard::Home:
return Rocket::Core::Input::KI_HOME;
break;
case sf::Keyboard::Insert:
return Rocket::Core::Input::KI_INSERT;
break;
case sf::Keyboard::LControl:
return Rocket::Core::Input::KI_LCONTROL;
break;
case sf::Keyboard::LShift:
return Rocket::Core::Input::KI_LSHIFT;
break;
case sf::Keyboard::Multiply:
return Rocket::Core::Input::KI_MULTIPLY;
break;
case sf::Keyboard::Pause:
return Rocket::Core::Input::KI_PAUSE;
break;
case sf::Keyboard::RControl:
return Rocket::Core::Input::KI_RCONTROL;
break;
case sf::Keyboard::Return:
return Rocket::Core::Input::KI_RETURN;
break;
case sf::Keyboard::RShift:
return Rocket::Core::Input::KI_RSHIFT;
break;
case sf::Keyboard::Space:
return Rocket::Core::Input::KI_SPACE;
break;
case sf::Keyboard::Subtract:
return Rocket::Core::Input::KI_SUBTRACT;
break;
case sf::Keyboard::Tab:
return Rocket::Core::Input::KI_TAB;
break;
};

return Rocket::Core::Input::KI_UNKNOWN;
};

float RocketSFMLSystemInterface::GetElapsedTime()
{
return timer.getElapsedTime().asSeconds();
};

bool RocketSFMLSystemInterface::LogMessage(Rocket::Core::Log::Type type, const Rocket::Core::String& message)
{
std::string Type;

switch(type)
{
case Rocket::Core::Log::LT_ALWAYS:
Type = "[Always]";
break;
case Rocket::Core::Log::LT_ERROR:
Type = "[Error]";
break;
case Rocket::Core::Log::LT_ASSERT:
Type = "[Assert]";
break;
case Rocket::Core::Log::LT_WARNING:
Type = "[Warning]";
break;
case Rocket::Core::Log::LT_INFO:
Type = "[Info]";
break;
case Rocket::Core::Log::LT_DEBUG:
Type = "[Debug]";
break;
};

printf("%s - %s\n", Type.c_str(), message.CString());

return true;
};

And this is the original 1.6:
Code: [Select]
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 Nuno Silva
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

#include <Rocket/Core/Core.h>
#include "RenderInterfaceSFML.h"
#include <GL/gl.h>

#define GL_CLAMP_TO_EDGE 0x812F

// If built with the GL Easy Extension library we can compile geometry to VBO's
// http://www.opengl.org/sdk/libs/GLee/
#ifdef ENABLE_GLEE
#include <GL/glee.h>

class RocketSFMLRendererGeometryHandler
{
public:
GLuint VertexID, IndexID;
int NumVertices;
Rocket::Core::TextureHandle Texture;

RocketSFMLRendererGeometryHandler() : VertexID(0), IndexID(0), Texture(0), NumVertices(0)
{
};

~RocketSFMLRendererGeometryHandler()
{
if(VertexID)
glDeleteBuffers(1, &VertexID);

if(IndexID)
glDeleteBuffers(1, &IndexID);

VertexID = IndexID = 0;
};
};

#endif

struct RocketSFMLRendererVertex
{
sf::Vector2f Position, TexCoord;
sf::Color Color;
};

RocketSFMLRenderer::RocketSFMLRenderer()
{
}

void RocketSFMLRenderer::SetWindow(sf::RenderWindow *Window)
{
MyWindow = Window;

Resize();
};

sf::RenderWindow *RocketSFMLRenderer::GetWindow()
{
return MyWindow;
};

void RocketSFMLRenderer::Resize()
{
MyWindow->SetActive(true);
MyWindow->PreserveOpenGLStates(true);

static sf::View View;
View.SetFromRect(sf::FloatRect(0, (float)MyWindow->GetWidth(), (float)MyWindow->GetHeight(), 0));
MyWindow->SetView(View);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, MyWindow->GetWidth(), MyWindow->GetHeight(), 0, -1, 1);
glMatrixMode(GL_MODELVIEW);

glViewport(0, 0, MyWindow->GetWidth(), MyWindow->GetHeight());
};

// Called by Rocket when it wants to render geometry that it does not wish to optimise.
void RocketSFMLRenderer::RenderGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, const Rocket::Core::TextureHandle texture, const Rocket::Core::Vector2f& translation)
{
MyWindow->SetActive();

glPushMatrix();
glTranslatef(translation.x, translation.y, 0);

std::vector<Rocket::Core::Vector2f> Positions(num_vertices);
std::vector<Rocket::Core::Colourb> Colors(num_vertices);
std::vector<Rocket::Core::Vector2f> TexCoords(num_vertices);

for(int  i = 0; i < num_vertices; i++)
{
Positions[i] = vertices[i].position;
Colors[i] = vertices[i].colour;
TexCoords[i] = vertices[i].tex_coord;
};

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, &Positions[0]);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, &Colors[0]);
glTexCoordPointer(2, GL_FLOAT, 0, &TexCoords[0]);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

sf::Image *image = (sf::Image *)texture;

if(image)
{
image->Bind();
}
else
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0);
};

glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, indices);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glColor4f(1, 1, 1, 1);

glPopMatrix();
}

// Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
Rocket::Core::CompiledGeometryHandle RocketSFMLRenderer::CompileGeometry(Rocket::Core::Vertex* vertices,
   int num_vertices, int* indices,
   int num_indices,
   const Rocket::Core::TextureHandle texture)
{
#ifdef ENABLE_GLEE
MyWindow->SetActive();

if(!GLEE_VERSION_2_0)
return (Rocket::Core::CompiledGeometryHandle) NULL;

std::vector<RocketSFMLRendererVertex> Data(num_vertices);

for(unsigned long i = 0; i < Data.size(); i++)
{
Data[i].Position = *(sf::Vector2f*)&vertices[i].position;
Data[i].TexCoord = *(sf::Vector2f*)&vertices[i].tex_coord;
Data[i].Color = sf::Color(vertices[i].colour.red, vertices[i].colour.green,
vertices[i].colour.blue, vertices[i].colour.alpha);
};

RocketSFMLRendererGeometryHandler *Geometry = new RocketSFMLRendererGeometryHandler();
Geometry->NumVertices = num_indices;

glGenBuffers(1, &Geometry->VertexID);
glBindBuffer(GL_ARRAY_BUFFER, Geometry->VertexID);
glBufferData(GL_ARRAY_BUFFER, sizeof(RocketSFMLRendererVertex) * num_vertices, &Data[0],
GL_STATIC_DRAW);

glGenBuffers(1, &Geometry->IndexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Geometry->IndexID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * num_indices, indices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, 0);

Geometry->Texture = texture;

return (Rocket::Core::CompiledGeometryHandle)Geometry;
#else
return NULL;
#endif
}

// Called by Rocket when it wants to render application-compiled geometry.
void RocketSFMLRenderer::RenderCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry, const Rocket::Core::Vector2f& translation)
{
#ifdef ENABLE_GLEE
MyWindow->SetActive();

RocketSFMLRendererGeometryHandler *RealGeometry = (RocketSFMLRendererGeometryHandler *)geometry;

glPushMatrix();
glTranslatef(translation.x, translation.y, 0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

sf::Image *image = (sf::Image *)RealGeometry->Texture;

if(image)
{
image->Bind();
}
else
{
glBindTexture(GL_TEXTURE_2D, 0);
};

glEnable(GL_VERTEX_ARRAY);
glEnable(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_COLOR_ARRAY);

#define BUFFER_OFFSET(x) ((char*)0 + x)

glBindBuffer(GL_ARRAY_BUFFER, RealGeometry->VertexID);
glVertexPointer(2, GL_FLOAT, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(0));
glTexCoordPointer(2, GL_FLOAT, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f)));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(RocketSFMLRendererVertex), BUFFER_OFFSET(sizeof(sf::Vector2f[2])));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, RealGeometry->IndexID);
glDrawElements(GL_TRIANGLES, RealGeometry->NumVertices, GL_UNSIGNED_INT, BUFFER_OFFSET(0));

glBindBuffer(GL_ARRAY_BUFFER, 0);

glDisable(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_VERTEX_ARRAY);

glColor4f(1, 1, 1, 1);

glPopMatrix();
#else
ROCKET_ASSERT(false & "Not Implemented");
#endif
}

// Called by Rocket when it wants to release application-compiled geometry.
void RocketSFMLRenderer::ReleaseCompiledGeometry(Rocket::Core::CompiledGeometryHandle geometry)
{
#ifdef ENABLE_GLEE
MyWindow->SetActive();

delete (RocketSFMLRendererGeometryHandler *)geometry;
#else
ROCKET_ASSERT(false & "Not Implemented");
#endif
}

// Called by Rocket when it wants to enable or disable scissoring to clip content.
void RocketSFMLRenderer::EnableScissorRegion(bool enable)
{
MyWindow->SetActive();

if (enable)
glEnable(GL_SCISSOR_TEST);
else
glDisable(GL_SCISSOR_TEST);
}

// Called by Rocket when it wants to change the scissor region.
void RocketSFMLRenderer::SetScissorRegion(int x, int y, int width, int height)
{
MyWindow->SetActive();

glScissor(x, MyWindow->GetHeight() - (y + height), width, height);
}

// Called by Rocket when a texture is required by the library.
bool RocketSFMLRenderer::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source)
{
MyWindow->SetActive();

Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
Rocket::Core::FileHandle file_handle = file_interface->Open(source);
if (file_handle == NULL)
return false;

file_interface->Seek(file_handle, 0, SEEK_END);
size_t buffer_size = file_interface->Tell(file_handle);
file_interface->Seek(file_handle, 0, SEEK_SET);

char* buffer = new char[buffer_size];
file_interface->Read(buffer, buffer_size, file_handle);
file_interface->Close(file_handle);

sf::Image *image = new sf::Image();

if(!image->LoadFromMemory(buffer, buffer_size))
{
delete buffer;
delete image;

return false;
};
delete buffer;

texture_handle = (Rocket::Core::TextureHandle) image;
texture_dimensions = Rocket::Core::Vector2i(image->GetWidth(), image->GetHeight());

return true;
}

// Called by Rocket when a texture is required to be built from an internally-generated sequence of pixels.
bool RocketSFMLRenderer::GenerateTexture(Rocket::Core::TextureHandle& texture_handle, const Rocket::Core::byte* source, const Rocket::Core::Vector2i& source_dimensions)
{
MyWindow->SetActive();

sf::Image *image = new sf::Image();

if(!image->LoadFromPixels(source_dimensions.x, source_dimensions.y, source))
{
delete image;

return false;
};

texture_handle = (Rocket::Core::TextureHandle)image;

return true;
}

// Called by Rocket when a loaded texture is no longer required.
void RocketSFMLRenderer::ReleaseTexture(Rocket::Core::TextureHandle texture_handle)
{
MyWindow->SetActive();

delete (sf::Image *)texture_handle;
}

and

Code: [Select]
/*
 * This source file is part of libRocket, the HTML/CSS Interface Middleware
 *
 * For the latest information, see http://www.librocket.com
 *
 * Copyright (c) 2008-2010 Nuno Silva
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
#include <Rocket/Core.h>
#include "SystemInterfaceSFML.h"

int RocketSFMLSystemInterface::GetKeyModifiers(sf::Window *Window)
{
int Modifiers = 0;

if(Window->GetInput().IsKeyDown(sf::Key::LShift) ||
Window->GetInput().IsKeyDown(sf::Key::RShift))
Modifiers |= Rocket::Core::Input::KM_SHIFT;

if(Window->GetInput().IsKeyDown(sf::Key::LControl) ||
Window->GetInput().IsKeyDown(sf::Key::RControl))
Modifiers |= Rocket::Core::Input::KM_CTRL;

if(Window->GetInput().IsKeyDown(sf::Key::LAlt) ||
Window->GetInput().IsKeyDown(sf::Key::RAlt))
Modifiers |= Rocket::Core::Input::KM_ALT;

return Modifiers;
};

Rocket::Core::Input::KeyIdentifier RocketSFMLSystemInterface::TranslateKey(sf::Key::Code Key)
{
switch(Key)
{
case sf::Key::A:
return Rocket::Core::Input::KI_A;
break;
case sf::Key::B:
return Rocket::Core::Input::KI_B;
break;
case sf::Key::C:
return Rocket::Core::Input::KI_C;
break;
case sf::Key::D:
return Rocket::Core::Input::KI_D;
break;
case sf::Key::E:
return Rocket::Core::Input::KI_E;
break;
case sf::Key::F:
return Rocket::Core::Input::KI_F;
break;
case sf::Key::G:
return Rocket::Core::Input::KI_G;
break;
case sf::Key::H:
return Rocket::Core::Input::KI_H;
break;
case sf::Key::I:
return Rocket::Core::Input::KI_I;
break;
case sf::Key::J:
return Rocket::Core::Input::KI_J;
break;
case sf::Key::K:
return Rocket::Core::Input::KI_K;
break;
case sf::Key::L:
return Rocket::Core::Input::KI_L;
break;
case sf::Key::M:
return Rocket::Core::Input::KI_M;
break;
case sf::Key::N:
return Rocket::Core::Input::KI_N;
break;
case sf::Key::O:
return Rocket::Core::Input::KI_O;
break;
case sf::Key::P:
return Rocket::Core::Input::KI_P;
break;
case sf::Key::Q:
return Rocket::Core::Input::KI_Q;
break;
case sf::Key::R:
return Rocket::Core::Input::KI_R;
break;
case sf::Key::S:
return Rocket::Core::Input::KI_S;
break;
case sf::Key::T:
return Rocket::Core::Input::KI_T;
break;
case sf::Key::U:
return Rocket::Core::Input::KI_U;
break;
case sf::Key::V:
return Rocket::Core::Input::KI_V;
break;
case sf::Key::W:
return Rocket::Core::Input::KI_W;
break;
case sf::Key::X:
return Rocket::Core::Input::KI_X;
break;
case sf::Key::Y:
return Rocket::Core::Input::KI_Y;
break;
case sf::Key::Z:
return Rocket::Core::Input::KI_Z;
break;
case sf::Key::Num0:
return Rocket::Core::Input::KI_0;
break;
case sf::Key::Num1:
return Rocket::Core::Input::KI_1;
break;
case sf::Key::Num2:
return Rocket::Core::Input::KI_2;
break;
case sf::Key::Num3:
return Rocket::Core::Input::KI_3;
break;
case sf::Key::Num4:
return Rocket::Core::Input::KI_4;
break;
case sf::Key::Num5:
return Rocket::Core::Input::KI_5;
break;
case sf::Key::Num6:
return Rocket::Core::Input::KI_6;
break;
case sf::Key::Num7:
return Rocket::Core::Input::KI_7;
break;
case sf::Key::Num8:
return Rocket::Core::Input::KI_8;
break;
case sf::Key::Num9:
return Rocket::Core::Input::KI_9;
break;
case sf::Key::Numpad0:
return Rocket::Core::Input::KI_NUMPAD0;
break;
case sf::Key::Numpad1:
return Rocket::Core::Input::KI_NUMPAD1;
break;
case sf::Key::Numpad2:
return Rocket::Core::Input::KI_NUMPAD2;
break;
case sf::Key::Numpad3:
return Rocket::Core::Input::KI_NUMPAD3;
break;
case sf::Key::Numpad4:
return Rocket::Core::Input::KI_NUMPAD4;
break;
case sf::Key::Numpad5:
return Rocket::Core::Input::KI_NUMPAD5;
break;
case sf::Key::Numpad6:
return Rocket::Core::Input::KI_NUMPAD6;
break;
case sf::Key::Numpad7:
return Rocket::Core::Input::KI_NUMPAD7;
break;
case sf::Key::Numpad8:
return Rocket::Core::Input::KI_NUMPAD8;
break;
case sf::Key::Numpad9:
return Rocket::Core::Input::KI_NUMPAD9;
break;
case sf::Key::Left:
return Rocket::Core::Input::KI_LEFT;
break;
case sf::Key::Right:
return Rocket::Core::Input::KI_RIGHT;
break;
case sf::Key::Up:
return Rocket::Core::Input::KI_UP;
break;
case sf::Key::Down:
return Rocket::Core::Input::KI_DOWN;
break;
case sf::Key::Add:
return Rocket::Core::Input::KI_ADD;
break;
case sf::Key::Back:
return Rocket::Core::Input::KI_BACK;
break;
case sf::Key::Delete:
return Rocket::Core::Input::KI_DELETE;
break;
case sf::Key::Divide:
return Rocket::Core::Input::KI_DIVIDE;
break;
case sf::Key::End:
return Rocket::Core::Input::KI_END;
break;
case sf::Key::Escape:
return Rocket::Core::Input::KI_ESCAPE;
break;
case sf::Key::F1:
return Rocket::Core::Input::KI_F1;
break;
case sf::Key::F2:
return Rocket::Core::Input::KI_F2;
break;
case sf::Key::F3:
return Rocket::Core::Input::KI_F3;
break;
case sf::Key::F4:
return Rocket::Core::Input::KI_F4;
break;
case sf::Key::F5:
return Rocket::Core::Input::KI_F5;
break;
case sf::Key::F6:
return Rocket::Core::Input::KI_F6;
break;
case sf::Key::F7:
return Rocket::Core::Input::KI_F7;
break;
case sf::Key::F8:
return Rocket::Core::Input::KI_F8;
break;
case sf::Key::F9:
return Rocket::Core::Input::KI_F9;
break;
case sf::Key::F10:
return Rocket::Core::Input::KI_F10;
break;
case sf::Key::F11:
return Rocket::Core::Input::KI_F11;
break;
case sf::Key::F12:
return Rocket::Core::Input::KI_F12;
break;
case sf::Key::F13:
return Rocket::Core::Input::KI_F13;
break;
case sf::Key::F14:
return Rocket::Core::Input::KI_F14;
break;
case sf::Key::F15:
return Rocket::Core::Input::KI_F15;
break;
case sf::Key::Home:
return Rocket::Core::Input::KI_HOME;
break;
case sf::Key::Insert:
return Rocket::Core::Input::KI_INSERT;
break;
case sf::Key::LControl:
return Rocket::Core::Input::KI_LCONTROL;
break;
case sf::Key::LShift:
return Rocket::Core::Input::KI_LSHIFT;
break;
case sf::Key::Multiply:
return Rocket::Core::Input::KI_MULTIPLY;
break;
case sf::Key::Pause:
return Rocket::Core::Input::KI_PAUSE;
break;
case sf::Key::RControl:
return Rocket::Core::Input::KI_RCONTROL;
break;
case sf::Key::Return:
return Rocket::Core::Input::KI_RETURN;
break;
case sf::Key::RShift:
return Rocket::Core::Input::KI_RSHIFT;
break;
case sf::Key::Space:
return Rocket::Core::Input::KI_SPACE;
break;
case sf::Key::Subtract:
return Rocket::Core::Input::KI_SUBTRACT;
break;
case sf::Key::Tab:
return Rocket::Core::Input::KI_TAB;
break;
};

return Rocket::Core::Input::KI_UNKNOWN;
};

float RocketSFMLSystemInterface::GetElapsedTime()
{
return timer.GetElapsedTime();
};

bool RocketSFMLSystemInterface::LogMessage(Rocket::Core::Log::Type type, const Rocket::Core::String& message)
{
std::string Type;

switch(type)
{
case Rocket::Core::Log::LT_ALWAYS:
Type = "[Always]";
break;
case Rocket::Core::Log::LT_ERROR:
Type = "[Error]";
break;
case Rocket::Core::Log::LT_ASSERT:
Type = "[Assert]";
break;
case Rocket::Core::Log::LT_WARNING:
Type = "[Warning]";
break;
case Rocket::Core::Log::LT_INFO:
Type = "[Info]";
break;
case Rocket::Core::Log::LT_DEBUG:
Type = "[Debug]";
break;
};

printf("%s - %s\n", Type.c_str(), message.CString());

return true;
};

I cant seem to see any thing else that needs chaning but maybe a new set of eyes might be able to spot something.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML2 Texture/Image loadFroPixels
« Reply #3 on: May 03, 2012, 07:57:10 pm »
You can't pop states before pushing them (in RocketSFMLRenderer::Resize) ;)
Laurent Gomila - SFML developer

 

anything