Code: Alles auswählen
#include "NLSandBox.hpp"
#include "NLApplication.hpp"
#include <boost/bind.hpp>
const WindowSettings g_fullscreen(1680, 1050, false, 32, 2, false);
const WindowSettings g_windowed(1280,800, false, 32, 2, false);
const bool g_framelimit = false;
NLApplication::NLApplication()
: m_controller(SystemController::getInstance()), m_text(NULL)
{
// Those names are important for the appdata-folder..
m_controller.setVendorName("Scorched Productions");
m_controller.setAppName("NLSandBox");
// Check for User-Directory.
if (!m_controller.userDataDirExists()){
m_controller.createUserDataDir();
}
// Create HTML-Log
m_controller.createLog(m_controller.getUserDataDir() + std::string("/") + std::string("NightLight.html"),
LL_VERBOSE, new HtmlWriter());
}
NLApplication::~NLApplication()
{
NLMessage("[Client] Resources (hopefully) freed. Client exits.");
}
void NLApplication::run()
{
// ----------------------------------------
// Construct Window
// ----------------------------------------
WindowSettings s;
Controller().copyDesktop(s);
s.fullscreen = true;
IPlatformWindow& window = (*m_controller.createWindow(s));
window.center();
//window.toggleFullScreen(true);
m_controller.getOpenGLContext()->setClearColor(Color4f(0.7f,0.7f,0.7f,1.0f));
m_controller.getOpenGLContext()->dumpOpenGLInfos(false);
// ----------------------------------------
// Load
// ----------------------------------------
m_controller.getResourceManager().loadResourceFile("../xml/examples.xml");
Shader* TexturedWithAlphaTest = m_controller.getResourceManager().getShader("TexturedWithAlphaTest");
Shader* TexturedWithAlphaTestAdditiveColor = m_controller.getResourceManager().getShader("TexturedWithAlphaTestAdditiveColor");
SpriteSheet* sheet = m_controller.getResourceManager().getSpriteSheet("SpriteSheet");
Font* fnt = m_controller.getResourceManager().getFont("NormalFont");
// ----------------------------------------
// Create Batcher
// ----------------------------------------
m_batcher = new SpriteBatcher(TexturedWithAlphaTest, sheet->getTexture());
// ----------------------------------------
// Create Animation
// ----------------------------------------
m_animation = new AnimatedSprite("../xml/animation.xml", glm::vec3(100,100, LayerConstants::BACKGROUND_LAYER1), glm::vec2(64,64), "AnimationTest");
m_batcher->insert(m_animation);
m_animation->play();
// ----------------------------------------
// Static Text
// ----------------------------------------
m_s_text = new StaticText(fnt, glm::vec3(10,300, LayerConstants::GUI_TEXT), TexturedWithAlphaTest, COLOR4F_BLUE, "I am a static text rendered on a texture");
// ----------------------------------------
// Create Sprites
// ----------------------------------------
TextureCoords* bohne = sheet->getTexCoordsByName("bohne_64");
for ( int i=0; i <= 1500; i++)
{
f32 x = Controller().getRandomReal(10, window.getSettings().width-32);
f32 y = Controller().getRandomReal(10, window.getSettings().height-32);
f32 rot = Controller().getRandomReal(0.0f, 360.0f);
MySprite* s = new MySprite(glm::vec2(32,32), glm::vec3(x, y, LayerConstants::DYNAMIC_LAYER2), bohne);
s->rotateAroundCenter(rot);
m_batcher->insert(s);
}
m_batcher->createBatch();
// ----------------------------------------
// Create dynamic Text
// ----------------------------------------
std::stringstream ss;
ss << "FPS: " << window.getFrameCounter().getFPS() << "\nsprites:" << m_batcher->getList().size()-1;
m_text = new DynamicText(fnt, glm::vec3(0,0, LayerConstants::GUI_TEXT), TexturedWithAlphaTestAdditiveColor, glm::vec4(0.7f, 0.0, 0.0, 0.0f), ss.str());
// ----------------------------------------
// Init done -> connect events
// ----------------------------------------
window.connectSignalKey(NLBindKeySlot(NLApplication, onKey));
window.connectSignalQuit(NLBindQuitSlot(NLApplication, onQuit));
window.connectSignalMouse(NLBindMouseSlot(NLApplication, onMouse));
m_controller.connectSignalRender(NLBindRenderSlot(NLApplication, onRender));
m_controller.getTimerController().connectSignalLogicUpdate(NLBindUpdateLogicSlot(NLApplication, onUpdate));
// ----------------------------------------
// Hide Mouse and enter EventLoop
// ----------------------------------------
Controller().enterLoop();
}
void NLApplication::onRender(u32 delta, u32 time)
{
m_batcher->renderObject();
m_text->renderObject();
m_s_text->renderObject(delta, time);
}
void NLApplication::onUpdate(u32 delta, u32 time)
{
std::stringstream ss;
ss << "FPS: " << Controller().getWindow()->getFrameCounter().getFPS() << "\nsprites:" << m_batcher->getList().size()-1;
m_text->setText(ss.str());
m_batcher->update(delta, time);
}
void NLApplication::onKey(const int key, const bool down)
{
if ( down == true )
{
switch(key)
{
case Key::F12:
{
m_controller.doScreenshot(m_controller.getUserDataDir() + std::string("/") + std::string("screenshot.png"));
break;
}
case Key::Q:
{
if (m_controller.getWindow()->ctrl() && down )
{
m_controller.getWindow()->closeWindow();
}
break;
}
}
}
}
bool NLApplication::onQuit()
{
std::stringstream ss;
ss << "Highest FPS: " << Controller().getWindow()->getFrameCounter().getHighestFPS();
Controller().printToLog(LL_MESSAGE, ss.str());
delete m_s_text;
delete m_batcher;
delete m_text;
return true;
}
class MySprite : public BatchedSprite
{
public:
MySprite(const glm::vec2& size, const glm::vec3& pos, const TextureCoords* coords)
: BatchedSprite(size, pos, coords)
{
}
virtual ~MySprite()
{
}
virtual void update(u32 delta, u32 time)
{
float speed = 0.1f;
float pos = ( delta * speed );
float angle = ( delta * speed );
glm::vec3 axis = glm::vec3(0,0,1);
if ( this->getPosition().x >= Controller().getScreenBoundingBox().getSize().x - 64 ) {
to_right = true;
}
else if ( this->getPosition().x <= 5 ){
to_right = false;
}
if (to_right){
pos = -pos;
axis = -axis;
}
this->translate(pos, 0);
this->rotateAroundCenter(angle, axis);
BatchedSprite::update(delta, time);
}
private:
bool to_right;
};