render do main menu
This commit is contained in:
parent
4c6d1269a0
commit
6e8df399c9
|
@ -56,6 +56,7 @@ SET(SOURCES
|
|||
src/teststate.cpp
|
||||
|
||||
# framework sources
|
||||
src/framework/borderedimage.cpp
|
||||
src/framework/dispatcher.cpp
|
||||
src/framework/framebuffer.cpp
|
||||
src/framework/font.cpp
|
||||
|
|
|
@ -68,8 +68,6 @@ void Engine::run()
|
|||
update(ticks, 0);
|
||||
lastUpdateTicks = ticks;
|
||||
|
||||
Point fpsPos(10,10);
|
||||
|
||||
while(!m_stopping) {
|
||||
// poll platform events
|
||||
Platform::poll();
|
||||
|
@ -105,7 +103,9 @@ void Engine::run()
|
|||
|
||||
// render fps
|
||||
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
|
||||
|
@ -136,7 +136,7 @@ void Engine::render()
|
|||
g_graphics.beginRender();
|
||||
if(m_currentState)
|
||||
m_currentState->render();
|
||||
g_gui.render();
|
||||
g_gui->render();
|
||||
g_graphics.endRender();
|
||||
}
|
||||
|
||||
|
@ -144,7 +144,7 @@ void Engine::update(int ticks, int elapsedTicks)
|
|||
{
|
||||
if(m_currentState)
|
||||
m_currentState->update(ticks, elapsedTicks);
|
||||
g_gui.update(ticks, elapsedTicks);
|
||||
g_gui->update(ticks, elapsedTicks);
|
||||
}
|
||||
|
||||
void Engine::onClose()
|
||||
|
@ -156,13 +156,16 @@ void Engine::onClose()
|
|||
void Engine::onResize(const Size& 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)
|
||||
{
|
||||
// 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(m_currentState)
|
||||
m_currentState->onInputEvent(event);
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#ifndef GAMESTATE_H
|
||||
#define GAMESTATE_H
|
||||
|
||||
#include "size.h"
|
||||
|
||||
struct InputEvent;
|
||||
|
||||
class GameState
|
||||
|
@ -38,6 +40,7 @@ public:
|
|||
|
||||
virtual void onClose() = 0;
|
||||
virtual void onInputEvent(InputEvent *event) = 0;
|
||||
virtual void onResize(const Size& size) = 0;
|
||||
|
||||
virtual void render() = 0;
|
||||
virtual void update(int ticks, int elapsedTicks) = 0;
|
||||
|
|
|
@ -180,6 +180,40 @@ void Graphics::_drawTexturedRect(const Rect& screenCoords, const Rect& textureCo
|
|||
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)
|
||||
{
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
@ -250,3 +284,10 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in
|
|||
|
||||
resetColor();
|
||||
}
|
||||
|
||||
void Graphics::_drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth)
|
||||
{
|
||||
glEnd();
|
||||
drawBoundingRect(screenCoords, color, innerLineWidth);
|
||||
glBegin(GL_QUADS);
|
||||
}
|
||||
|
|
|
@ -62,12 +62,15 @@ public:
|
|||
|
||||
// high level rendering
|
||||
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 drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth = 1);
|
||||
|
||||
// lower level rendering
|
||||
void _beginTextureRender(const Texture *texture);
|
||||
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();
|
||||
|
||||
private:
|
||||
|
|
|
@ -44,18 +44,20 @@ TexturePtr Textures::get(const std::string& textureFile)
|
|||
if(!texture) { // load texture
|
||||
// currently only png textures are supported
|
||||
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;
|
||||
}
|
||||
|
||||
uint 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;
|
||||
}
|
||||
|
||||
texture = TexturePtr(TextureLoader::loadPNG(textureFileData));
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,4 +23,27 @@
|
|||
|
||||
|
||||
#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);
|
||||
}
|
||||
|
|
|
@ -27,13 +27,22 @@
|
|||
|
||||
#include "../prerequisites.h"
|
||||
#include "uielement.h"
|
||||
#include "../borderedimage.h"
|
||||
|
||||
class UIButton : public UIElement
|
||||
{
|
||||
public:
|
||||
UIButton(UIContainerPtr parent) : UIElement(parent) { }
|
||||
UIButton(const std::string& text);
|
||||
|
||||
void render();
|
||||
|
||||
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
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
|
||||
#include "uicontainer.h"
|
||||
|
||||
UIContainer g_gui;
|
||||
UIContainerPtr g_gui(new UIContainer);
|
||||
|
||||
void UIContainer::addChild(UIElementPtr child)
|
||||
{
|
||||
|
@ -60,7 +60,7 @@ void UIContainer::setRect(const Rect& rect)
|
|||
{
|
||||
// update children rect
|
||||
for(auto it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
UIElementPtr child = (*it)->asUIElement();
|
||||
const UIElementPtr& child = (*it);
|
||||
|
||||
// transforme child rect
|
||||
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)
|
||||
{
|
||||
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
class UIContainer : public UIElement
|
||||
{
|
||||
public:
|
||||
UIContainer(UIContainerPtr parent = UIContainerPtr()) : UIElement(parent) { }
|
||||
UIContainer() : UIElement() { }
|
||||
virtual ~UIContainer() { }
|
||||
|
||||
virtual void addChild(UIElementPtr child);
|
||||
|
@ -45,15 +45,16 @@ public:
|
|||
virtual void move(const Point& trans);
|
||||
virtual void moveTo(const Point& pos);
|
||||
|
||||
virtual void render() { }
|
||||
virtual void update(int ticks, int elapsedTicks) { }
|
||||
virtual bool onInputEvent(InputEvent *event) { return false; }
|
||||
virtual void render();
|
||||
virtual void update(int ticks, int elapsedTicks);
|
||||
virtual bool onInputEvent(InputEvent *event);
|
||||
|
||||
virtual void setActiveElement(UIElementPtr activeElement);
|
||||
UIElementPtr getActiveElement() const { return m_activeElement; }
|
||||
|
||||
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:
|
||||
std::list<UIElementPtr> m_children;
|
||||
|
@ -63,6 +64,6 @@ private:
|
|||
void onMove(const Point& pos);
|
||||
};
|
||||
|
||||
extern UIContainer g_gui;
|
||||
extern UIContainerPtr g_gui;
|
||||
|
||||
#endif // UICONTAINER_H
|
||||
|
|
|
@ -23,14 +23,3 @@
|
|||
|
||||
|
||||
#include "uielement.h"
|
||||
#include "uicontainer.h"
|
||||
|
||||
UIElement::UIElement(UIContainerPtr parent) :
|
||||
m_visible(true),
|
||||
m_active(true)
|
||||
{
|
||||
if(parent)
|
||||
parent->addChild(asUIElement());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -36,10 +36,10 @@ typedef std::shared_ptr<UIContainer> UIContainerPtr;
|
|||
class UIElement;
|
||||
typedef std::shared_ptr<UIElement> UIElementPtr;
|
||||
|
||||
class UIElement
|
||||
class UIElement : public std::enable_shared_from_this<UIElement>
|
||||
{
|
||||
public:
|
||||
UIElement(UIContainerPtr parent);
|
||||
UIElement() { }
|
||||
virtual ~UIElement() { }
|
||||
|
||||
virtual void render() { }
|
||||
|
@ -63,7 +63,7 @@ public:
|
|||
|
||||
virtual UI::ControlType getControlType() const { return UI::Element; }
|
||||
|
||||
UIElementPtr asUIElement() { return UIElementPtr(this); }
|
||||
UIElementPtr asUIElement() { return shared_from_this(); }
|
||||
|
||||
protected:
|
||||
UI::ControlType m_type;
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
class UILabel : public UIElement
|
||||
{
|
||||
public:
|
||||
UILabel(UIContainerPtr parent) : UIElement(parent) { }
|
||||
UILabel() { }
|
||||
|
||||
virtual UI::ControlType getControlType() const { return UI::Label; }
|
||||
};
|
||||
|
|
|
@ -24,3 +24,23 @@
|
|||
|
||||
#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();
|
||||
}
|
||||
|
|
|
@ -27,13 +27,21 @@
|
|||
|
||||
#include "../prerequisites.h"
|
||||
#include "uicontainer.h"
|
||||
#include "../borderedimage.h"
|
||||
|
||||
class UIPanel : public UIContainer
|
||||
{
|
||||
public:
|
||||
UIPanel(UIContainerPtr parent) : UIContainer(parent) { }
|
||||
UIPanel();
|
||||
|
||||
void render();
|
||||
|
||||
virtual UI::ControlType getControlType() const { return UI::Panel; }
|
||||
|
||||
private:
|
||||
BorderedImagePtr m_boderedImage;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<UIPanel> UIPanelPtr;
|
||||
|
||||
#endif // UIPANEL_H
|
||||
|
|
|
@ -569,6 +569,10 @@ bool Platform::createWindow(int x, int y, int width, int height, int minWidth, i
|
|||
x11.width = width;
|
||||
x11.height = height;
|
||||
x11.maximizeOnFirstShow = maximized;
|
||||
|
||||
// call first onResize
|
||||
g_engine.onResize(Size(width, height));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "framework/platform.h"
|
||||
#include "menustate.h"
|
||||
#include "teststate.h"
|
||||
#include "framework/dispatcher.h"
|
||||
|
||||
/// Catches signals so we can exit nicely
|
||||
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<TestState> initialState(new TestState);
|
||||
g_dispatcher.addTask([&initialState]{
|
||||
g_engine.changeState(initialState.get());
|
||||
});
|
||||
|
||||
Platform::showWindow();
|
||||
//Platform::hideMouseCursor();
|
||||
|
|
|
@ -34,25 +34,15 @@
|
|||
#include "framework/dispatcher.h"
|
||||
#include "framework/ui/ui.h"
|
||||
#include "framework/net/connections.h"
|
||||
#include "framework/borderedimage.h"
|
||||
|
||||
|
||||
void MenuState::onEnter()
|
||||
{
|
||||
m_background = g_textures.get("background.png");
|
||||
m_background->enableBilinearFilter();
|
||||
|
||||
/*
|
||||
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);
|
||||
*/
|
||||
createMainMenu();
|
||||
}
|
||||
|
||||
void MenuState::onLeave()
|
||||
|
@ -70,6 +60,11 @@ void MenuState::onInputEvent(InputEvent* event)
|
|||
|
||||
}
|
||||
|
||||
void MenuState::onResize(const Size& size)
|
||||
{
|
||||
recalculateMenuPanelPosition();
|
||||
}
|
||||
|
||||
void MenuState::render()
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
}
|
|
@ -28,6 +28,7 @@
|
|||
#include "framework/gamestate.h"
|
||||
#include "framework/texture.h"
|
||||
#include "framework/net/connection.h"
|
||||
#include "framework/ui/uipanel.h"
|
||||
|
||||
class MenuState : public GameState
|
||||
{
|
||||
|
@ -35,16 +36,21 @@ class MenuState : public GameState
|
|||
public:
|
||||
MenuState() { }
|
||||
|
||||
virtual void onEnter();
|
||||
virtual void onLeave();
|
||||
void onEnter();
|
||||
void onLeave();
|
||||
|
||||
virtual void onClose();
|
||||
virtual void onInputEvent(InputEvent *event);
|
||||
void onClose();
|
||||
void onInputEvent(InputEvent *event);
|
||||
void onResize(const Size& size);
|
||||
|
||||
virtual void render();
|
||||
virtual void update(int ticks, int elapsedTicks);
|
||||
void render();
|
||||
void update(int ticks, int elapsedTicks);
|
||||
|
||||
private:
|
||||
void createMainMenu();
|
||||
void recalculateMenuPanelPosition();
|
||||
|
||||
UIPanelPtr m_menuPanel;
|
||||
TexturePtr m_background;
|
||||
};
|
||||
|
||||
|
|
|
@ -48,6 +48,11 @@ void TestState::onInputEvent(InputEvent* event)
|
|||
|
||||
}
|
||||
|
||||
void TestState::onResize(const Size& size)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TestState::render()
|
||||
{
|
||||
|
||||
|
|
|
@ -32,14 +32,15 @@ class TestState : public GameState
|
|||
public:
|
||||
TestState() { }
|
||||
|
||||
virtual void onEnter();
|
||||
virtual void onLeave();
|
||||
void onEnter();
|
||||
void onLeave();
|
||||
|
||||
virtual void onClose();
|
||||
virtual void onInputEvent(InputEvent *event);
|
||||
void onClose();
|
||||
void onInputEvent(InputEvent *event);
|
||||
void onResize(const Size& size);
|
||||
|
||||
virtual void render();
|
||||
virtual void update(int ticks, int elapsedTicks);
|
||||
void render();
|
||||
void update(int ticks, int elapsedTicks);
|
||||
};
|
||||
|
||||
#endif // TESTSTATE_H
|
||||
|
|
Loading…
Reference in New Issue