Component rendering now uses OpenGL translation.

You don't need to take into account offset when rendering anymore.
This commit is contained in:
Aloshi 2013-06-02 14:34:50 -05:00
parent 1cef2f5433
commit 24512c0c9f
9 changed files with 74 additions and 25 deletions

View file

@ -1,6 +1,7 @@
#include "GuiComponent.h" #include "GuiComponent.h"
#include "Window.h" #include "Window.h"
#include "Log.h" #include "Log.h"
#include "Renderer.h"
GuiComponent::GuiComponent(Window* window) : mWindow(window), mParent(NULL) GuiComponent::GuiComponent(Window* window) : mWindow(window), mParent(NULL)
{ {
@ -34,6 +35,15 @@ void GuiComponent::update(int deltaTime)
} }
void GuiComponent::render() void GuiComponent::render()
{
Renderer::translate(mOffset);
onRender();
Renderer::translate(-mOffset);
}
void GuiComponent::onRender()
{ {
for(unsigned int i = 0; i < getChildCount(); i++) for(unsigned int i = 0; i < getChildCount(); i++)
{ {
@ -57,16 +67,7 @@ void GuiComponent::deinit()
} }
} }
void GuiComponent::setParent(GuiComponent* parent) //Offset stuff.
{
mParent = parent;
}
GuiComponent* GuiComponent::getParent()
{
return mParent;
}
Vector2i GuiComponent::getGlobalOffset() Vector2i GuiComponent::getGlobalOffset()
{ {
if(mParent) if(mParent)
@ -91,6 +92,7 @@ void GuiComponent::setOffset(int x, int y)
mOffset.y = y; mOffset.y = y;
} }
//Children stuff.
void GuiComponent::addChild(GuiComponent* cmp) void GuiComponent::addChild(GuiComponent* cmp)
{ {
mChildren.push_back(cmp); mChildren.push_back(cmp);
@ -134,3 +136,13 @@ GuiComponent* GuiComponent::getChild(unsigned int i)
{ {
return mChildren.at(i); return mChildren.at(i);
} }
void GuiComponent::setParent(GuiComponent* parent)
{
mParent = parent;
}
GuiComponent* GuiComponent::getParent()
{
return mParent;
}

View file

@ -12,11 +12,21 @@ public:
GuiComponent(Window* window); GuiComponent(Window* window);
virtual ~GuiComponent(); virtual ~GuiComponent();
//Called when input is received.
//Return true if the input is consumed, false if it should continue to be passed to other children. //Return true if the input is consumed, false if it should continue to be passed to other children.
virtual bool input(InputConfig* config, Input input); virtual bool input(InputConfig* config, Input input);
//Called when time passes. Default implementation also calls update(deltaTime) on children - so you should probably call GuiComponent::update(deltaTime) at some point.
virtual void update(int deltaTime); virtual void update(int deltaTime);
//Called when it's time to render. Translates the OpenGL matrix, calls onRender() (which renders children), then un-translates the OpenGL matrix.
//You probably don't need to override this, but instead want the protected method onRender.
virtual void render(); virtual void render();
//Called when the Renderer initializes. Passes to children.
virtual void init(); virtual void init();
//Called when the Renderer deinitializes. Passes to children.
virtual void deinit(); virtual void deinit();
Vector2i getGlobalOffset(); Vector2i getGlobalOffset();
@ -34,6 +44,9 @@ public:
GuiComponent* getChild(unsigned int i); GuiComponent* getChild(unsigned int i);
protected: protected:
//Default implementation just renders children - you should probably always call GuiComponent::onRender at some point in your custom onRender.
virtual void onRender();
Window* mWindow; Window* mWindow;
GuiComponent* mParent; GuiComponent* mParent;
Vector2i mOffset; Vector2i mOffset;

View file

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "Vector2.h"
#include "platform.h" #include "platform.h"
#include GLHEADER #include GLHEADER
//#include "Font.h" //#include "Font.h"
@ -28,8 +29,12 @@ namespace Renderer
Font* getDefaultFont(FontSize size); Font* getDefaultFont(FontSize size);
void buildGLColorArray(GLubyte* ptr, unsigned int color, unsigned int vertCount); void buildGLColorArray(GLubyte* ptr, unsigned int color, unsigned int vertCount);
//drawing commands //graphics commands
void swapBuffers(); void swapBuffers();
void translatef(float x, float y);
void translate(Vector2i offset);
void drawRect(int x, int y, int w, int h, unsigned int color); void drawRect(int x, int y, int w, int h, unsigned int color);
void drawText(std::string text, int x, int y, unsigned int color, Font* font); void drawText(std::string text, int x, int y, unsigned int color, Font* font);
void drawCenteredText(std::string text, int xOffset, int y, unsigned int color, Font* font); void drawCenteredText(std::string text, int xOffset, int y, unsigned int color, Font* font);

View file

@ -26,6 +26,16 @@ namespace Renderer {
} }
} }
void translatef(float x, float y)
{
glTranslatef(x, y, 0);
}
void translate(Vector2i offset)
{
translatef((float)offset.x, (float)offset.y);
}
void drawRect(int x, int y, int w, int h, unsigned int color) void drawRect(int x, int y, int w, int h, unsigned int color)
{ {
#ifdef USE_OPENGL_ES #ifdef USE_OPENGL_ES

View file

@ -88,7 +88,7 @@ namespace Renderer
LOG(LogInfo) << "Created surface successfully."; LOG(LogInfo) << "Created surface successfully.";
//hide mouse cursor //hide mouse cursor
initialCursorState = SDL_ShowCursor(0); initialCursorState = SDL_ShowCursor(0) == 1;
return true; return true;
} }

View file

@ -261,7 +261,7 @@ void ImageComponent::setFlipY(bool flip)
mFlipY = flip; mFlipY = flip;
} }
void ImageComponent::render() void ImageComponent::onRender()
{ {
if(mTextureID && getOpacity() > 0) if(mTextureID && getOpacity() > 0)
{ {
@ -274,14 +274,16 @@ void ImageComponent::render()
float yCount = ((float)mResizeHeight/mHeight); float yCount = ((float)mResizeHeight/mHeight);
Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6); Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6);
buildImageArray(getOffset().x, getOffset().y, points, texs, xCount, yCount); buildImageArray(0, 0, points, texs, xCount, yCount);
}else{ }else{
Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6); Renderer::buildGLColorArray(colors, 0xFFFFFF00 | (getOpacity()), 6);
buildImageArray(getOffset().x, getOffset().y, points, texs); buildImageArray(0, 0, points, texs);
} }
drawImageArray(points, texs, colors, 6); drawImageArray(points, texs, colors, 6);
} }
GuiComponent::onRender();
} }
void ImageComponent::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs, float px, float py) void ImageComponent::buildImageArray(int posX, int posY, GLfloat* points, GLfloat* texs, float px, float py)

View file

@ -31,8 +31,6 @@ public:
bool hasImage(); bool hasImage();
void render();
//Image textures will be deleted on renderer deinitialization, and recreated on reinitialization (if mPath is not empty). //Image textures will be deleted on renderer deinitialization, and recreated on reinitialization (if mPath is not empty).
void init(); void init();
void deinit(); void deinit();
@ -40,6 +38,9 @@ public:
unsigned char getOpacity(); unsigned char getOpacity();
void setOpacity(unsigned char opacity); void setOpacity(unsigned char opacity);
protected:
void onRender();
private: private:
unsigned int mResizeWidth, mResizeHeight; unsigned int mResizeWidth, mResizeHeight;
float mOriginX, mOriginY; float mOriginX, mOriginY;

View file

@ -21,7 +21,6 @@ public:
bool input(InputConfig* config, Input input); bool input(InputConfig* config, Input input);
void update(int deltaTime); void update(int deltaTime);
void render();
void addObject(std::string name, T obj, unsigned int color = 0xFF0000); void addObject(std::string name, T obj, unsigned int color = 0xFF0000);
void clear(); void clear();
@ -44,6 +43,9 @@ public:
void setFont(Font* f); void setFont(Font* f);
protected:
void onRender();
private: private:
static const int SCROLLDELAY = 507; static const int SCROLLDELAY = 507;
static const int SCROLLTIME = 200; static const int SCROLLTIME = 200;
@ -96,9 +98,9 @@ TextListComponent<T>::~TextListComponent()
} }
template <typename T> template <typename T>
void TextListComponent<T>::render() void TextListComponent<T>::onRender()
{ {
const int cutoff = getOffset().y; const int cutoff = 0;
const int entrySize = mFont->getHeight() + 5; const int entrySize = mFont->getHeight() + 5;
int startEntry = 0; int startEntry = 0;
@ -133,18 +135,20 @@ void TextListComponent<T>::render()
//draw selector bar //draw selector bar
if(mSelection == i) if(mSelection == i)
{ {
Renderer::drawRect(getOffset().x, y, Renderer::getScreenWidth(), mFont->getHeight(), mSelectorColor); Renderer::drawRect(0, y, Renderer::getScreenWidth(), mFont->getHeight(), mSelectorColor);
} }
ListRow row = mRowVector.at((unsigned int)i); ListRow row = mRowVector.at((unsigned int)i);
if(mDrawCentered) if(mDrawCentered)
Renderer::drawCenteredText(row.name, getOffset().x + mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont); Renderer::drawCenteredText(row.name, mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont);
else else
Renderer::drawText(row.name, getOffset().x + mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont); Renderer::drawText(row.name, mTextOffsetX, y, (mSelection == i && mSelectedTextColorOverride != 0) ? mSelectedTextColorOverride : row.color, mFont);
y += entrySize; y += entrySize;
} }
GuiComponent::onRender();
} }
template <typename T> template <typename T>
@ -189,7 +193,7 @@ bool TextListComponent<T>::input(InputConfig* config, Input input)
} }
} }
return false; return GuiComponent::input(config, input);
} }
template <typename T> template <typename T>
@ -228,6 +232,8 @@ void TextListComponent<T>::update(int deltaTime)
} }
} }
} }
GuiComponent::update(deltaTime);
} }
template <typename T> template <typename T>

View file

@ -9,7 +9,7 @@
#include "../AudioManager.h" #include "../AudioManager.h"
#include "../Font.h" #include "../Font.h"
//This class loads an XML-defined list of Guis. //This class loads an XML-defined list of GuiComponents.
class ThemeComponent : public GuiComponent class ThemeComponent : public GuiComponent
{ {
public: public: