✨ pressed?, just-pressed and mouse-x, mouse-y functions.
parent
cd0d7bfea0
commit
1b72ba842c
75
src/Game.cpp
75
src/Game.cpp
|
@ -1,5 +1,7 @@
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include "Game.hpp"
|
#include "Game.hpp"
|
||||||
|
#include "SDL_events.h"
|
||||||
|
#include "SDL_keyboard.h"
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
namespace d2
|
namespace d2
|
||||||
|
@ -105,4 +107,77 @@ namespace d2
|
||||||
m_scenes.pop_back();
|
m_scenes.pop_back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Game::is_key_pressed(std::string const& keyname) const
|
||||||
|
{
|
||||||
|
SDL_Keycode key = SDL_GetKeyFromName(keyname.c_str());
|
||||||
|
SDL_Scancode scan = SDL_GetScancodeFromKey(key);
|
||||||
|
Uint8 const* keys = SDL_GetKeyboardState(nullptr);
|
||||||
|
|
||||||
|
return keys[scan];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Game::is_key_just_pressed(std::string const& keyname) const
|
||||||
|
{
|
||||||
|
static bool prev = false;
|
||||||
|
|
||||||
|
bool pressed = is_key_pressed(keyname);
|
||||||
|
|
||||||
|
if (pressed != prev)
|
||||||
|
{
|
||||||
|
prev = pressed;
|
||||||
|
return pressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Game::is_mouse_pressed(std::string const& btn_name) const
|
||||||
|
{
|
||||||
|
Uint32 btns = SDL_GetMouseState(nullptr, nullptr);
|
||||||
|
|
||||||
|
if (btn_name == "left")
|
||||||
|
{
|
||||||
|
return btns & SDL_BUTTON(SDL_BUTTON_LEFT);
|
||||||
|
}
|
||||||
|
else if (btn_name == "middle")
|
||||||
|
{
|
||||||
|
return btns & SDL_BUTTON(SDL_BUTTON_MIDDLE);
|
||||||
|
}
|
||||||
|
else if (btn_name == "right")
|
||||||
|
{
|
||||||
|
return btns & SDL_BUTTON(SDL_BUTTON_RIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw game_error {"Unknown mosue button '" + btn_name + "'."};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Game::is_mouse_just_pressed(std::string const& btn_name) const
|
||||||
|
{
|
||||||
|
static bool prev = false;
|
||||||
|
bool new_val = is_mouse_pressed(btn_name);
|
||||||
|
|
||||||
|
if (new_val != prev)
|
||||||
|
{
|
||||||
|
prev = new_val;
|
||||||
|
return new_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Game::mouse_x() const
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
SDL_GetMouseState(&x, nullptr);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Game::mouse_y() const
|
||||||
|
{
|
||||||
|
int y;
|
||||||
|
SDL_GetMouseState(&y, nullptr);
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,14 @@ namespace d2
|
||||||
void push(Args... args);
|
void push(Args... args);
|
||||||
void pop();
|
void pop();
|
||||||
|
|
||||||
|
bool is_key_pressed(std::string const& keyname) const;
|
||||||
|
bool is_key_just_pressed(std::string const& keyname) const;
|
||||||
|
|
||||||
|
bool is_mouse_pressed(std::string const& btn_name) const;
|
||||||
|
bool is_mouse_just_pressed(std::string const& btn_name) const;
|
||||||
|
int mouse_x() const;
|
||||||
|
int mouse_y() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_Window* m_window = nullptr;
|
SDL_Window* m_window = nullptr;
|
||||||
SDL_GLContext m_context;
|
SDL_GLContext m_context;
|
||||||
|
|
117
src/Script.cpp
117
src/Script.cpp
|
@ -10,6 +10,11 @@ namespace d2
|
||||||
scm_init_guile();
|
scm_init_guile();
|
||||||
scm_c_define_gsubr("winconf", 3, 0, 0,
|
scm_c_define_gsubr("winconf", 3, 0, 0,
|
||||||
reinterpret_cast<void*>(&fn_winconf));
|
reinterpret_cast<void*>(&fn_winconf));
|
||||||
|
// Drawing
|
||||||
|
// -------
|
||||||
|
scm_c_define_gsubr("rect", 4, 0, 0, reinterpret_cast<void*>(&fn_rect));
|
||||||
|
scm_c_define_gsubr("color!", 4, 0, 0,
|
||||||
|
reinterpret_cast<void*>(&fn_set_color));
|
||||||
|
|
||||||
// Scenes
|
// Scenes
|
||||||
// ------
|
// ------
|
||||||
|
@ -19,11 +24,19 @@ namespace d2
|
||||||
scm_c_define_gsubr("scene:pop", 0, 0, 0,
|
scm_c_define_gsubr("scene:pop", 0, 0, 0,
|
||||||
reinterpret_cast<void*>(&fn_scene__pop));
|
reinterpret_cast<void*>(&fn_scene__pop));
|
||||||
|
|
||||||
// Drawing
|
// Inputs
|
||||||
// -------
|
// ------
|
||||||
scm_c_define_gsubr("rect", 4, 0, 0, reinterpret_cast<void*>(&fn_rect));
|
scm_c_define_gsubr("pressed?", 2, 0, 0,
|
||||||
scm_c_define_gsubr("color!", 4, 0, 0,
|
reinterpret_cast<void*>(&fn_is_pressed));
|
||||||
reinterpret_cast<void*>(&fn_set_color));
|
scm_c_define_gsubr("just-pressed?", 2, 0, 0,
|
||||||
|
reinterpret_cast<void*>(&fn_is_just_pressed));
|
||||||
|
|
||||||
|
scm_c_define_gsubr("mouse-x", 0, 0, 0,
|
||||||
|
reinterpret_cast<void*>(&fn_mouse_x));
|
||||||
|
scm_c_define_gsubr("mouse-y", 0, 0, 0,
|
||||||
|
reinterpret_cast<void*>(&fn_mouse_y));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static*/ void Script::terminate()
|
/*static*/ void Script::terminate()
|
||||||
|
@ -56,27 +69,7 @@ namespace d2
|
||||||
return SCM_BOOL_T;
|
return SCM_BOOL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static*/ SCM Script::fn_scene__push(SCM scm_function, SCM scm_ctx)
|
/*static*/ SCM Script::fn_rect(SCM scm_x, SCM scm_y, SCM scm_w, SCM scm_h)
|
||||||
{
|
|
||||||
if (scm_is_null(scm_ctx))
|
|
||||||
{
|
|
||||||
Script::game->push<ScriptScene>(scm_function);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Script::game->push<ScriptScene>(scm_function, scm_ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
return SCM_BOOL_T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*static*/ SCM Script::fn_scene__pop()
|
|
||||||
{
|
|
||||||
Script::game->pop();
|
|
||||||
return SCM_BOOL_T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*static*/ SCM Script::fn_rect(SCM scm_x, SCM scm_y, SCM scm_w, SCM scm_h)
|
|
||||||
{
|
{
|
||||||
float x = scm_to_double(scm_x);
|
float x = scm_to_double(scm_x);
|
||||||
float y = scm_to_double(scm_y);
|
float y = scm_to_double(scm_y);
|
||||||
|
@ -116,4 +109,76 @@ namespace d2
|
||||||
|
|
||||||
return SCM_BOOL_T;
|
return SCM_BOOL_T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*static*/ SCM Script::fn_scene__push(SCM scm_function, SCM scm_ctx)
|
||||||
|
{
|
||||||
|
if (scm_is_null(scm_ctx))
|
||||||
|
{
|
||||||
|
Script::game->push<ScriptScene>(scm_function);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Script::game->push<ScriptScene>(scm_function, scm_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
return SCM_BOOL_T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ SCM Script::fn_scene__pop()
|
||||||
|
{
|
||||||
|
Script::game->pop();
|
||||||
|
return SCM_BOOL_T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ SCM Script::fn_is_pressed(SCM scm_device, SCM scm_keyname)
|
||||||
|
{
|
||||||
|
std::string device =
|
||||||
|
scm_to_locale_string(scm_symbol_to_string(scm_device));
|
||||||
|
std::string keyname = scm_to_locale_string(scm_keyname);
|
||||||
|
|
||||||
|
if (device == "key")
|
||||||
|
{
|
||||||
|
bool result = Script::game->is_key_pressed(keyname);
|
||||||
|
return scm_from_bool(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device == "mouse")
|
||||||
|
{
|
||||||
|
bool result = Script::game->is_mouse_pressed(keyname);
|
||||||
|
return scm_from_bool(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw script_error {"Unknown device '" + device + "'."};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ SCM Script::fn_is_just_pressed(SCM scm_device, SCM scm_keyname)
|
||||||
|
{
|
||||||
|
std::string device =
|
||||||
|
scm_to_locale_string(scm_symbol_to_string(scm_device));
|
||||||
|
std::string keyname = scm_to_locale_string(scm_keyname);
|
||||||
|
|
||||||
|
if (device == "key")
|
||||||
|
{
|
||||||
|
bool result = Script::game->is_key_just_pressed(keyname);
|
||||||
|
return scm_from_bool(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (device == "mouse")
|
||||||
|
{
|
||||||
|
bool result = Script::game->is_mouse_just_pressed(keyname);
|
||||||
|
return scm_from_bool(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw script_error {"Unknown device '" + device + "'."};
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ SCM Script::fn_mouse_x()
|
||||||
|
{
|
||||||
|
return scm_from_int32(Script::game->mouse_x());
|
||||||
|
}
|
||||||
|
|
||||||
|
/*static*/ SCM Script::fn_mouse_y()
|
||||||
|
{
|
||||||
|
return scm_from_int32(Script::game->mouse_y());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
namespace d2
|
namespace d2
|
||||||
{
|
{
|
||||||
|
D2_ERROR(script_error);
|
||||||
|
|
||||||
struct Script
|
struct Script
|
||||||
{
|
{
|
||||||
static void init();
|
static void init();
|
||||||
|
@ -21,6 +23,12 @@ namespace d2
|
||||||
static SCM fn_scene__push(SCM scm_function, SCM scm_ctx);
|
static SCM fn_scene__push(SCM scm_function, SCM scm_ctx);
|
||||||
static SCM fn_scene__pop();
|
static SCM fn_scene__pop();
|
||||||
|
|
||||||
|
static SCM fn_is_pressed(SCM scm_device, SCM scm_keyname);
|
||||||
|
static SCM fn_is_just_pressed(SCM scm_device, SCM scm_keyname);
|
||||||
|
|
||||||
|
static SCM fn_mouse_x();
|
||||||
|
static SCM fn_mouse_y();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::unique_ptr<Game> game;
|
static std::unique_ptr<Game> game;
|
||||||
static glm::vec4 color;
|
static glm::vec4 color;
|
||||||
|
|
Loading…
Reference in New Issue