render do main menu

This commit is contained in:
Eduardo Bart 2011-04-08 06:50:26 -03:00
parent 4c6d1269a0
commit 6e8df399c9
21 changed files with 243 additions and 61 deletions

View File

@ -56,6 +56,7 @@ SET(SOURCES
src/teststate.cpp src/teststate.cpp
# framework sources # framework sources
src/framework/borderedimage.cpp
src/framework/dispatcher.cpp src/framework/dispatcher.cpp
src/framework/framebuffer.cpp src/framework/framebuffer.cpp
src/framework/font.cpp src/framework/font.cpp

View File

@ -68,8 +68,6 @@ void Engine::run()
update(ticks, 0); update(ticks, 0);
lastUpdateTicks = ticks; lastUpdateTicks = ticks;
Point fpsPos(10,10);
while(!m_stopping) { while(!m_stopping) {
// poll platform events // poll platform events
Platform::poll(); Platform::poll();
@ -105,7 +103,9 @@ void Engine::run()
// render fps // render fps
if(m_calculateFps) { if(m_calculateFps) {
g_defaultFont->renderText(format("FPS: %d", fps), fpsPos); std::string fpsText = format("FPS: %d", fps);
Size textSize = g_defaultFont->calculateTextRectSize(fpsText);
g_defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - textSize.width() - 10, 10));
} }
// swap buffers // swap buffers
@ -136,7 +136,7 @@ void Engine::render()
g_graphics.beginRender(); g_graphics.beginRender();
if(m_currentState) if(m_currentState)
m_currentState->render(); m_currentState->render();
g_gui.render(); g_gui->render();
g_graphics.endRender(); g_graphics.endRender();
} }
@ -144,7 +144,7 @@ void Engine::update(int ticks, int elapsedTicks)
{ {
if(m_currentState) if(m_currentState)
m_currentState->update(ticks, elapsedTicks); m_currentState->update(ticks, elapsedTicks);
g_gui.update(ticks, elapsedTicks); g_gui->update(ticks, elapsedTicks);
} }
void Engine::onClose() void Engine::onClose()
@ -156,13 +156,16 @@ void Engine::onClose()
void Engine::onResize(const Size& size) void Engine::onResize(const Size& size)
{ {
g_graphics.resize(size); g_graphics.resize(size);
g_gui.resize(size); g_gui->resize(size);
if(m_currentState)
m_currentState->onResize(size);
} }
void Engine::onInputEvent(InputEvent *event) void Engine::onInputEvent(InputEvent *event)
{ {
// inputs goest to gui first // inputs goest to gui first
if(!g_gui.onInputEvent(event)) { if(!g_gui->onInputEvent(event)) {
// if gui didnt capture the input then goes to the state // if gui didnt capture the input then goes to the state
if(m_currentState) if(m_currentState)
m_currentState->onInputEvent(event); m_currentState->onInputEvent(event);

View File

@ -25,6 +25,8 @@
#ifndef GAMESTATE_H #ifndef GAMESTATE_H
#define GAMESTATE_H #define GAMESTATE_H
#include "size.h"
struct InputEvent; struct InputEvent;
class GameState class GameState
@ -38,6 +40,7 @@ public:
virtual void onClose() = 0; virtual void onClose() = 0;
virtual void onInputEvent(InputEvent *event) = 0; virtual void onInputEvent(InputEvent *event) = 0;
virtual void onResize(const Size& size) = 0;
virtual void render() = 0; virtual void render() = 0;
virtual void update(int ticks, int elapsedTicks) = 0; virtual void update(int ticks, int elapsedTicks) = 0;

View File

@ -180,6 +180,40 @@ void Graphics::_drawTexturedRect(const Rect& screenCoords, const Rect& textureCo
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top); glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
} }
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords, const Texture* texture, const Rect& texCoords)
{
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
glBegin(GL_QUADS);
_drawRepeatedTexturedRect(screenCoords, texCoords, texture->getSize());
glEnd();
}
void Graphics::_drawRepeatedTexturedRect(const Rect& screenCoords, const Rect& textureCoords, const Size& textureSize)
{
// render many repeated texture rects
Rect virtualScreenCoords(0,0,screenCoords.size());
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
for(int x = 0; x <= virtualScreenCoords.width(); x += textureCoords.width()) {
Rect partialCoords(x, y, textureCoords.size());
Rect partialTextureCoords = textureCoords;
// partialCoords to screenCoords bottomRight
if(partialCoords.bottom() > virtualScreenCoords.bottom()) {
partialTextureCoords.setBottom(partialTextureCoords.bottom() + (virtualScreenCoords.bottom() - partialCoords.bottom()));
partialCoords.setBottom(virtualScreenCoords.bottom());
}
if(partialCoords.right() > virtualScreenCoords.right()) {
partialTextureCoords.setRight(partialTextureCoords.right() + (virtualScreenCoords.right() - partialCoords.right()));
partialCoords.setRight(virtualScreenCoords.right());
}
partialCoords.translate(screenCoords.topLeft());
_drawTexturedRect(partialCoords, partialTextureCoords, textureSize);
}
}
}
void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color) void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)
{ {
glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_2D);
@ -250,3 +284,10 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in
resetColor(); resetColor();
} }
void Graphics::_drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth)
{
glEnd();
drawBoundingRect(screenCoords, color, innerLineWidth);
glBegin(GL_QUADS);
}

View File

@ -62,12 +62,15 @@ public:
// high level rendering // high level rendering
void drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords = Rect()); void drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords = Rect());
void drawRepeatedTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords);
void drawColoredRect(const Rect& screenCoords, const Color& color); void drawColoredRect(const Rect& screenCoords, const Color& color);
void drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth = 1); void drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth = 1);
// lower level rendering // lower level rendering
void _beginTextureRender(const Texture *texture); void _beginTextureRender(const Texture *texture);
void _drawTexturedRect(const Rect& screenCoords, const Rect& textureCoords, const Size& textureSize); void _drawTexturedRect(const Rect& screenCoords, const Rect& textureCoords, const Size& textureSize);
void _drawRepeatedTexturedRect(const Rect& screenCoords, const Rect& textureCoords, const Size& textureSize);
void _drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth = 1);
void _endTextureRender(); void _endTextureRender();
private: private:

View File

@ -44,18 +44,20 @@ TexturePtr Textures::get(const std::string& textureFile)
if(!texture) { // load texture if(!texture) { // load texture
// currently only png textures are supported // currently only png textures are supported
if(!boost::ends_with(textureFile, ".png")) { if(!boost::ends_with(textureFile, ".png")) {
logError("Unable to load texture %s, file format no supported.", textureFile.c_str()); logFatal("Unable to load texture %s, file format no supported.", textureFile.c_str());
return texture; return texture;
} }
uint fileSize; uint fileSize;
uchar *textureFileData = g_resources.loadFile(textureFile, &fileSize); uchar *textureFileData = g_resources.loadFile(textureFile, &fileSize);
if(!textureFileData) if(!textureFileData) {
logFatal("Unable to load texture %s, file could not be read.", textureFile.c_str());
return texture; return texture;
}
texture = TexturePtr(TextureLoader::loadPNG(textureFileData)); texture = TexturePtr(TextureLoader::loadPNG(textureFileData));
if(!texture) if(!texture)
logError("Unable to load texture %s, loading error.", textureFile.c_str()); logFatal("Unable to load texture %s, loading error.", textureFile.c_str());
delete[] textureFileData; delete[] textureFileData;
} }

View File

@ -23,4 +23,27 @@
#include "uibutton.h" #include "uibutton.h"
#include "../fonts.h"
#include "../font.h"
UIButton::UIButton(const std::string& text) :
m_text(text)
{
m_boderedImage = BorderedImagePtr(new BorderedImage("ui.png"));
m_boderedImage->setTexCoords(Rect(45,139,1,18),
Rect(130,139,1,18),
Rect(46,138,84,1),
Rect(46,157,84,1),
Rect(45,138,1,1),
Rect(130,138,1,1),
Rect(45,157,1,1),
Rect(130,157,1,1),
Rect(46,139,84,18));
}
void UIButton::render()
{
m_boderedImage->draw(m_rect);
g_fonts.get("tibia-8px-antialised")->renderText(m_text, m_rect, ALIGN_CENTER);
}

View File

@ -27,13 +27,22 @@
#include "../prerequisites.h" #include "../prerequisites.h"
#include "uielement.h" #include "uielement.h"
#include "../borderedimage.h"
class UIButton : public UIElement class UIButton : public UIElement
{ {
public: public:
UIButton(UIContainerPtr parent) : UIElement(parent) { } UIButton(const std::string& text);
void render();
virtual UI::ControlType getControlType() const { return UI::Button; } virtual UI::ControlType getControlType() const { return UI::Button; }
private:
std::string m_text;
BorderedImagePtr m_boderedImage;
}; };
typedef std::shared_ptr<UIButton> UIButtonPtr;
#endif // UIBUTTON_H #endif // UIBUTTON_H

View File

@ -24,7 +24,7 @@
#include "uicontainer.h" #include "uicontainer.h"
UIContainer g_gui; UIContainerPtr g_gui(new UIContainer);
void UIContainer::addChild(UIElementPtr child) void UIContainer::addChild(UIElementPtr child)
{ {
@ -60,7 +60,7 @@ void UIContainer::setRect(const Rect& rect)
{ {
// update children rect // update children rect
for(auto it = m_children.begin(); it != m_children.end(); ++it) { for(auto it = m_children.begin(); it != m_children.end(); ++it) {
UIElementPtr child = (*it)->asUIElement(); const UIElementPtr& child = (*it);
// transforme child rect // transforme child rect
Rect childRect = child->getRect(); Rect childRect = child->getRect();
@ -94,6 +94,24 @@ void UIContainer::moveTo(const Point& pos)
} }
void UIContainer::render()
{
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
const UIElementPtr& child = (*it);
child->render();
}
}
void UIContainer::update(int ticks, int elapsedTicks)
{
}
bool UIContainer::onInputEvent(InputEvent* event)
{
return false;
}
void UIContainer::setActiveElement(UIElementPtr activeElement) void UIContainer::setActiveElement(UIElementPtr activeElement)
{ {

View File

@ -33,7 +33,7 @@
class UIContainer : public UIElement class UIContainer : public UIElement
{ {
public: public:
UIContainer(UIContainerPtr parent = UIContainerPtr()) : UIElement(parent) { } UIContainer() : UIElement() { }
virtual ~UIContainer() { } virtual ~UIContainer() { }
virtual void addChild(UIElementPtr child); virtual void addChild(UIElementPtr child);
@ -45,15 +45,16 @@ public:
virtual void move(const Point& trans); virtual void move(const Point& trans);
virtual void moveTo(const Point& pos); virtual void moveTo(const Point& pos);
virtual void render() { } virtual void render();
virtual void update(int ticks, int elapsedTicks) { } virtual void update(int ticks, int elapsedTicks);
virtual bool onInputEvent(InputEvent *event) { return false; } virtual bool onInputEvent(InputEvent *event);
virtual void setActiveElement(UIElementPtr activeElement); virtual void setActiveElement(UIElementPtr activeElement);
UIElementPtr getActiveElement() const { return m_activeElement; } UIElementPtr getActiveElement() const { return m_activeElement; }
virtual UI::ControlType getControlType() const { return UI::Container; } virtual UI::ControlType getControlType() const { return UI::Container; }
UIContainerPtr asUIContainer() { return UIContainerPtr(this); }
UIContainerPtr asUIContainer() { return std::static_pointer_cast<UIContainer>(shared_from_this()); }
protected: protected:
std::list<UIElementPtr> m_children; std::list<UIElementPtr> m_children;
@ -63,6 +64,6 @@ private:
void onMove(const Point& pos); void onMove(const Point& pos);
}; };
extern UIContainer g_gui; extern UIContainerPtr g_gui;
#endif // UICONTAINER_H #endif // UICONTAINER_H

View File

@ -23,14 +23,3 @@
#include "uielement.h" #include "uielement.h"
#include "uicontainer.h"
UIElement::UIElement(UIContainerPtr parent) :
m_visible(true),
m_active(true)
{
if(parent)
parent->addChild(asUIElement());
}

View File

@ -36,10 +36,10 @@ typedef std::shared_ptr<UIContainer> UIContainerPtr;
class UIElement; class UIElement;
typedef std::shared_ptr<UIElement> UIElementPtr; typedef std::shared_ptr<UIElement> UIElementPtr;
class UIElement class UIElement : public std::enable_shared_from_this<UIElement>
{ {
public: public:
UIElement(UIContainerPtr parent); UIElement() { }
virtual ~UIElement() { } virtual ~UIElement() { }
virtual void render() { } virtual void render() { }
@ -63,7 +63,7 @@ public:
virtual UI::ControlType getControlType() const { return UI::Element; } virtual UI::ControlType getControlType() const { return UI::Element; }
UIElementPtr asUIElement() { return UIElementPtr(this); } UIElementPtr asUIElement() { return shared_from_this(); }
protected: protected:
UI::ControlType m_type; UI::ControlType m_type;

View File

@ -31,7 +31,7 @@
class UILabel : public UIElement class UILabel : public UIElement
{ {
public: public:
UILabel(UIContainerPtr parent) : UIElement(parent) { } UILabel() { }
virtual UI::ControlType getControlType() const { return UI::Label; } virtual UI::ControlType getControlType() const { return UI::Label; }
}; };

View File

@ -24,3 +24,23 @@
#include "uipanel.h" #include "uipanel.h"
UIPanel::UIPanel()
{
m_boderedImage = BorderedImagePtr(new BorderedImage("ui.png"));
m_boderedImage->setTexCoords(Rect(0,214,5,32),
Rect(6,214,5,32),
Rect(43,214,32,5),
Rect(43,220,32,5),
Rect(43,225,5,5),
Rect(49,225,5,5),
Rect(43,230,5,5),
Rect(48,231,5,5),
Rect(11,214,32,32));
}
void UIPanel::render()
{
m_boderedImage->draw(m_rect);
UIContainer::render();
}

View File

@ -27,13 +27,21 @@
#include "../prerequisites.h" #include "../prerequisites.h"
#include "uicontainer.h" #include "uicontainer.h"
#include "../borderedimage.h"
class UIPanel : public UIContainer class UIPanel : public UIContainer
{ {
public: public:
UIPanel(UIContainerPtr parent) : UIContainer(parent) { } UIPanel();
void render();
virtual UI::ControlType getControlType() const { return UI::Panel; } virtual UI::ControlType getControlType() const { return UI::Panel; }
private:
BorderedImagePtr m_boderedImage;
}; };
typedef std::shared_ptr<UIPanel> UIPanelPtr;
#endif // UIPANEL_H #endif // UIPANEL_H

View File

@ -569,6 +569,10 @@ bool Platform::createWindow(int x, int y, int width, int height, int minWidth, i
x11.width = width; x11.width = width;
x11.height = height; x11.height = height;
x11.maximizeOnFirstShow = maximized; x11.maximizeOnFirstShow = maximized;
// call first onResize
g_engine.onResize(Size(width, height));
return true; return true;
} }

View File

@ -28,6 +28,7 @@
#include "framework/platform.h" #include "framework/platform.h"
#include "menustate.h" #include "menustate.h"
#include "teststate.h" #include "teststate.h"
#include "framework/dispatcher.h"
/// Catches signals so we can exit nicely /// Catches signals so we can exit nicely
void signal_handler(int sig) void signal_handler(int sig)
@ -111,7 +112,9 @@ int main(int argc, const char *argv[])
{ {
std::shared_ptr<MenuState> initialState(new MenuState); std::shared_ptr<MenuState> initialState(new MenuState);
//std::shared_ptr<TestState> initialState(new TestState); //std::shared_ptr<TestState> initialState(new TestState);
g_engine.changeState(initialState.get()); g_dispatcher.addTask([&initialState]{
g_engine.changeState(initialState.get());
});
Platform::showWindow(); Platform::showWindow();
//Platform::hideMouseCursor(); //Platform::hideMouseCursor();

View File

@ -34,25 +34,15 @@
#include "framework/dispatcher.h" #include "framework/dispatcher.h"
#include "framework/ui/ui.h" #include "framework/ui/ui.h"
#include "framework/net/connections.h" #include "framework/net/connections.h"
#include "framework/borderedimage.h"
void MenuState::onEnter() void MenuState::onEnter()
{ {
m_background = g_textures.get("background.png"); m_background = g_textures.get("background.png");
m_background->enableBilinearFilter(); m_background->enableBilinearFilter();
/* createMainMenu();
UIPanelPtr panel(new UIPanel);
panel.setAnchorsLeft(g_gui.left());
panel.setAnchorsBottom(g_gui.right());
panel.setMarginBottom(10);
panel.setMarginLeft(10);
panel.setSize(Size(100, 100));
UIButtonPtr button(new UIButton);
button.setAnchorsHorizontalCenter(panel.horizontalCenter());
button.setTop
g_gui.addChild(panel);
*/
} }
void MenuState::onLeave() void MenuState::onLeave()
@ -70,6 +60,11 @@ void MenuState::onInputEvent(InputEvent* event)
} }
void MenuState::onResize(const Size& size)
{
recalculateMenuPanelPosition();
}
void MenuState::render() void MenuState::render()
{ {
// render background // render background
@ -89,3 +84,50 @@ void MenuState::update(int ticks, int elapsedTicks)
{ {
} }
void MenuState::createMainMenu()
{
UIButtonPtr button;
int y = 16;
m_menuPanel = UIPanelPtr(new UIPanel);
recalculateMenuPanelPosition();
button = UIButtonPtr(new UIButton("Enter Game"));
button->setRect(Rect(16, y, 86, 20));
m_menuPanel->addChild(button);
y += 30;
button = UIButtonPtr(new UIButton("Access Account"));
button->setRect(Rect(16, y, 86, 20));
m_menuPanel->addChild(button);
y += 30;
button = UIButtonPtr(new UIButton("Options"));
button->setRect(Rect(16, y, 86, 20));
m_menuPanel->addChild(button);
y += 30;
button = UIButtonPtr(new UIButton("Info"));
button->setRect(Rect(16, y, 86, 20));
m_menuPanel->addChild(button);
y += 30;
button = UIButtonPtr(new UIButton("Exit Game"));
button->setRect(Rect(16, y, 86, 20));
m_menuPanel->addChild(button);
y += 30;
g_gui->addChild(m_menuPanel);
}
void MenuState::recalculateMenuPanelPosition()
{
if(m_menuPanel) {
// calculate panel rect
Size panelSize = Size(117, 171);
Rect panelRect = Rect(0, 0, panelSize);
panelRect.moveBottomLeft(Point(60, g_graphics.getScreenSize().height() - 70));
m_menuPanel->setRect(panelRect);
}
}

View File

@ -28,6 +28,7 @@
#include "framework/gamestate.h" #include "framework/gamestate.h"
#include "framework/texture.h" #include "framework/texture.h"
#include "framework/net/connection.h" #include "framework/net/connection.h"
#include "framework/ui/uipanel.h"
class MenuState : public GameState class MenuState : public GameState
{ {
@ -35,16 +36,21 @@ class MenuState : public GameState
public: public:
MenuState() { } MenuState() { }
virtual void onEnter(); void onEnter();
virtual void onLeave(); void onLeave();
virtual void onClose(); void onClose();
virtual void onInputEvent(InputEvent *event); void onInputEvent(InputEvent *event);
void onResize(const Size& size);
virtual void render(); void render();
virtual void update(int ticks, int elapsedTicks); void update(int ticks, int elapsedTicks);
private: private:
void createMainMenu();
void recalculateMenuPanelPosition();
UIPanelPtr m_menuPanel;
TexturePtr m_background; TexturePtr m_background;
}; };

View File

@ -48,6 +48,11 @@ void TestState::onInputEvent(InputEvent* event)
} }
void TestState::onResize(const Size& size)
{
}
void TestState::render() void TestState::render()
{ {

View File

@ -32,14 +32,15 @@ class TestState : public GameState
public: public:
TestState() { } TestState() { }
virtual void onEnter(); void onEnter();
virtual void onLeave(); void onLeave();
virtual void onClose(); void onClose();
virtual void onInputEvent(InputEvent *event); void onInputEvent(InputEvent *event);
void onResize(const Size& size);
virtual void render(); void render();
virtual void update(int ticks, int elapsedTicks); void update(int ticks, int elapsedTicks);
}; };
#endif // TESTSTATE_H #endif // TESTSTATE_H