Adding Input Example.
This commit is contained in:
parent
1332921174
commit
b0d14b91e0
5 changed files with 119 additions and 1 deletions
|
|
@ -24,7 +24,8 @@ assets = CopyDir(PathJoin(paths.build, paths.examples), "assets")
|
||||||
|
|
||||||
examples = BuildExamples(example_settings, {
|
examples = BuildExamples(example_settings, {
|
||||||
"text",
|
"text",
|
||||||
"events"
|
"events",
|
||||||
|
"input"
|
||||||
}, assets)
|
}, assets)
|
||||||
|
|
||||||
PseudoTarget("examples", examples)
|
PseudoTarget("examples", examples)
|
||||||
|
|
|
||||||
65
examples/input/InputExample.cpp
Normal file
65
examples/input/InputExample.cpp
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
#include <Spectre/Input/InputModule.h>
|
||||||
|
#include <Spectre/Input/Keyboard.h>
|
||||||
|
#include <Spectre/Input/Mouse.h>
|
||||||
|
|
||||||
|
#include <Spectre/Graphics/BatchRenderer2D.h>
|
||||||
|
#include <Spectre/System/Log.h>
|
||||||
|
#include "InputExample.h"
|
||||||
|
|
||||||
|
void InputExample::init()
|
||||||
|
{
|
||||||
|
m_renderer = new sp::BatchRenderer2D();
|
||||||
|
|
||||||
|
m_tux_texture.create("assets/textures/tux.png");
|
||||||
|
|
||||||
|
m_kb_sprite.setTexture(m_tux_texture);
|
||||||
|
m_kb_sprite.setColor(sp::Color::Green);
|
||||||
|
m_kb_sprite.setSize(sp::vec2f(100, 100));
|
||||||
|
|
||||||
|
m_mouse_sprite.setTexture(m_tux_texture);
|
||||||
|
m_mouse_sprite.setColor(sp::Color::Red);
|
||||||
|
m_mouse_sprite.setSize(sp::vec2f(100, 100));
|
||||||
|
m_mouse_sprite.setPosition(sp::vec2f(50, 50));
|
||||||
|
|
||||||
|
m_renderer->setCamera(m_camera);
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputExample::update(double dt)
|
||||||
|
{
|
||||||
|
sp::Keyboard* keyboard = getInput()->getKeyboard();
|
||||||
|
sp::Mouse* mouse = getInput()->getMouse();
|
||||||
|
double delta = 0.3 * dt;
|
||||||
|
|
||||||
|
// Handle keyboard input.
|
||||||
|
if (keyboard->isKeyDown(sp::Keyboard::Left)) {
|
||||||
|
m_kb_sprite.move(sp::vec2f(-delta, 0));
|
||||||
|
} else if (keyboard->isKeyDown(sp::Keyboard::Right)) {
|
||||||
|
m_kb_sprite.move(sp::vec2f( delta, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keyboard->isKeyDown(sp::Keyboard::Up)) {
|
||||||
|
m_kb_sprite.move(sp::vec2f(0, -delta));
|
||||||
|
} else if (keyboard->isKeyDown(sp::Keyboard::Down)) {
|
||||||
|
m_kb_sprite.move(sp::vec2f(0, delta));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle mouse input.
|
||||||
|
if (mouse->isButtonDown(sp::Mouse::Left)) {
|
||||||
|
m_mouse_sprite.setPosition(mouse->getPosition());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputExample::render()
|
||||||
|
{
|
||||||
|
sp::Graphics* g = getGraphics();
|
||||||
|
|
||||||
|
g->clearBuffer();
|
||||||
|
|
||||||
|
m_renderer->begin();
|
||||||
|
m_renderer->submit(m_mouse_sprite);
|
||||||
|
m_renderer->submit(m_kb_sprite);
|
||||||
|
m_renderer->render();
|
||||||
|
|
||||||
|
g->swapBuffers();
|
||||||
|
}
|
||||||
34
examples/input/InputExample.h
Normal file
34
examples/input/InputExample.h
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
|
||||||
|
#ifndef INPUT_EXAMPLE_H
|
||||||
|
#define INPUT_EXAMPLE_H
|
||||||
|
|
||||||
|
#include <Spectre/Scene/Camera2D.h>
|
||||||
|
#include <Spectre/Graphics/Texture.h>
|
||||||
|
#include <Spectre/Graphics/Sprite.h>
|
||||||
|
#include <Spectre/Game.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
class Renderer2D;
|
||||||
|
}
|
||||||
|
|
||||||
|
class InputExample : public sp::Game
|
||||||
|
{
|
||||||
|
protected :
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
|
void update(double dt);
|
||||||
|
|
||||||
|
void render();
|
||||||
|
|
||||||
|
private :
|
||||||
|
sp::Camera2D m_camera;
|
||||||
|
sp::Renderer2D *m_renderer;
|
||||||
|
|
||||||
|
sp::Sprite m_kb_sprite;
|
||||||
|
sp::Sprite m_mouse_sprite;
|
||||||
|
|
||||||
|
sp::Texture m_tux_texture;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* INPUT_EXAMPLE_H*/
|
||||||
7
examples/input/bam.lua
Normal file
7
examples/input/bam.lua
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
local base = PathDir(ModuleFilename())
|
||||||
|
|
||||||
|
src ={
|
||||||
|
base .. "/main.cpp",
|
||||||
|
base .. "/InputExample.cpp"
|
||||||
|
}
|
||||||
11
examples/input/main.cpp
Normal file
11
examples/input/main.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
#include "InputExample.h"
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
InputExample game;
|
||||||
|
|
||||||
|
game.run();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue