Implemented lua, shaking out latent bugs
This commit is contained in:
+14
-2
@@ -56,6 +56,16 @@ void Application::Init(int argc, char* argv[]) {
|
||||
|
||||
//set the hook for the renderer
|
||||
BaseScene::SetRenderer(renderer);
|
||||
|
||||
//setup lua
|
||||
lua = luaL_newstate();
|
||||
if (!lua) {
|
||||
std::ostringstream msg;
|
||||
msg << "Failed to create the lua state";
|
||||
throw(std::runtime_error(msg.str()));
|
||||
}
|
||||
|
||||
luaL_openlibs(lua);
|
||||
}
|
||||
|
||||
void Application::Proc() {
|
||||
@@ -103,7 +113,9 @@ void Application::Proc() {
|
||||
}
|
||||
|
||||
void Application::Quit() {
|
||||
//cleran up after the program
|
||||
lua_close(lua);
|
||||
|
||||
//clean up after the program
|
||||
BaseScene::SetRenderer(nullptr);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
@@ -125,7 +137,7 @@ void Application::ProcessSceneSignal(SceneSignal signal) {
|
||||
switch(signal) {
|
||||
case SceneSignal::FIRST:
|
||||
case SceneSignal::EXAMPLE_SCENE:
|
||||
activeScene = new ExampleScene();
|
||||
activeScene = new ExampleScene(lua);
|
||||
break;
|
||||
default: {
|
||||
std::ostringstream msg;
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "base_scene.hpp"
|
||||
#include "scene_signal.hpp"
|
||||
|
||||
#include "lua.hpp"
|
||||
#include "SDL2/SDL.h"
|
||||
|
||||
//TODO: do something with these
|
||||
@@ -50,4 +51,6 @@ private:
|
||||
//TODO: build a "window" class?
|
||||
SDL_Window* window = nullptr;
|
||||
SDL_Renderer* renderer = nullptr;
|
||||
|
||||
lua_State* lua = nullptr;
|
||||
};
|
||||
+32
-5
@@ -21,8 +21,24 @@
|
||||
*/
|
||||
#include "example_scene.hpp"
|
||||
|
||||
ExampleScene::ExampleScene() {
|
||||
//
|
||||
#include <iostream>
|
||||
|
||||
ExampleScene::ExampleScene(lua_State* L) {
|
||||
lua = L;
|
||||
|
||||
tileSheet.Load(GetRenderer(), "./rsc/terrain.bmp", 32, 32);
|
||||
|
||||
//set the pager's hook
|
||||
regionPager.SetLuaState(lua);
|
||||
|
||||
//load the file as a chunk
|
||||
luaL_loadfile(lua, "./rsc/startup.lua");
|
||||
|
||||
//push the pager as an arg
|
||||
lua_pushlightuserdata(lua, static_cast<void*>(®ionPager));
|
||||
|
||||
//run the function
|
||||
lua_pcall(lua, 1, LUA_MULTRET, 0);
|
||||
}
|
||||
|
||||
ExampleScene::~ExampleScene() {
|
||||
@@ -46,7 +62,9 @@ void ExampleScene::FrameEnd() {
|
||||
}
|
||||
|
||||
void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
|
||||
//
|
||||
for (std::list<Region>::iterator it = regionPager.GetContainer()->begin(); it != regionPager.GetContainer()->end(); it++) {
|
||||
tileSheet.DrawRegionTo(renderer, &(*it), camera.x, camera.y, camera.scale, camera.scale);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
@@ -54,11 +72,20 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
|
||||
//-------------------------
|
||||
|
||||
void ExampleScene::MouseMotion(SDL_MouseMotionEvent const& event) {
|
||||
//
|
||||
//right mouse button moves the camera
|
||||
if (event.state & SDL_BUTTON_RMASK) {
|
||||
camera.x -= event.xrel;
|
||||
camera.y -= event.yrel;
|
||||
}
|
||||
}
|
||||
|
||||
void ExampleScene::MouseButtonDown(SDL_MouseButtonEvent const& event) {
|
||||
//
|
||||
switch(event.button) {
|
||||
case SDL_BUTTON_LEFT:
|
||||
//change the selected tile
|
||||
regionPager.SetTile((event.x + camera.x) / 32, (event.y + camera.y) / 32, layer, selection);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ExampleScene::MouseButtonUp(SDL_MouseButtonEvent const& event) {
|
||||
|
||||
+18
-1
@@ -22,10 +22,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "base_scene.hpp"
|
||||
#include "region_pager_lua.hpp"
|
||||
#include "tile_sheet.hpp"
|
||||
|
||||
#include "lua.hpp"
|
||||
|
||||
class ExampleScene : public BaseScene {
|
||||
public:
|
||||
ExampleScene();
|
||||
ExampleScene(lua_State* L);
|
||||
~ExampleScene();
|
||||
|
||||
void RenderFrame(SDL_Renderer* renderer) override;
|
||||
@@ -43,4 +47,17 @@ private:
|
||||
void MouseWheel(SDL_MouseWheelEvent const& event) override;
|
||||
void KeyDown(SDL_KeyboardEvent const& event) override;
|
||||
void KeyUp(SDL_KeyboardEvent const& event) override;
|
||||
|
||||
//members
|
||||
lua_State* lua = nullptr;
|
||||
RegionPagerLua regionPager;
|
||||
TileSheet tileSheet;
|
||||
struct {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
double scale = 1.0;
|
||||
} camera;
|
||||
|
||||
int selection = 1;
|
||||
int layer = 0;
|
||||
};
|
||||
|
||||
@@ -104,6 +104,8 @@ static int unloadRegion(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//TODO: check that parameters are not null
|
||||
|
||||
static int setOnLoad(lua_State* L) {
|
||||
RegionPagerLua* pager = reinterpret_cast<RegionPagerLua*>(lua_touserdata(L, 1));
|
||||
luaL_unref(L, LUA_REGISTRYINDEX, pager->GetLoadReference());
|
||||
|
||||
@@ -28,6 +28,8 @@ RegionPagerBase::~RegionPagerBase() {
|
||||
UnloadAll();
|
||||
};
|
||||
|
||||
//TODO: add nullptr checks to the calls to GetRegion()
|
||||
|
||||
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
|
||||
Region* ptr = GetRegion(x, y);
|
||||
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
//NOTE: set the lua hook before use
|
||||
|
||||
class RegionPagerLua : public RegionPagerBase {
|
||||
public:
|
||||
RegionPagerLua() = default;
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $
|
||||
** Initialization of libraries for lua.c and other clients
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
/*
|
||||
** If you embed Lua in your program and need to open the standard
|
||||
** libraries, call luaL_openlibs in your program. If you need a
|
||||
** different set of libraries, copy this file to your project and edit
|
||||
** it to suit your needs.
|
||||
*/
|
||||
|
||||
|
||||
#define linit_c
|
||||
#define LUA_LIB
|
||||
|
||||
#include "lua.hpp"
|
||||
|
||||
#include "region_api.hpp"
|
||||
#include "region_pager_api.hpp"
|
||||
|
||||
/*
|
||||
** these libs are loaded by lua.c and are readily available to any Lua
|
||||
** program
|
||||
*/
|
||||
static const luaL_Reg loadedlibs[] = {
|
||||
{"_G", luaopen_base},
|
||||
{LUA_LOADLIBNAME, luaopen_package},
|
||||
{LUA_COLIBNAME, luaopen_coroutine},
|
||||
{LUA_TABLIBNAME, luaopen_table},
|
||||
{LUA_IOLIBNAME, luaopen_io},
|
||||
{LUA_OSLIBNAME, luaopen_os},
|
||||
{LUA_STRLIBNAME, luaopen_string},
|
||||
{LUA_BITLIBNAME, luaopen_bit32},
|
||||
{LUA_MATHLIBNAME, luaopen_math},
|
||||
{LUA_DBLIBNAME, luaopen_debug},
|
||||
|
||||
//custom stuff
|
||||
{TORTUGA_REGION_API, openRegionAPI},
|
||||
{TORTUGA_REGION_PAGER_API, openRegionPagerAPI},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
/*
|
||||
** these libs are preloaded and must be required before used
|
||||
*/
|
||||
static const luaL_Reg preloadedlibs[] = {
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
LUALIB_API void luaL_openlibs (lua_State *L) {
|
||||
const luaL_Reg *lib;
|
||||
/* call open functions from 'loadedlibs' and set results to global table */
|
||||
for (lib = loadedlibs; lib->func; lib++) {
|
||||
luaL_requiref(L, lib->name, lib->func, 1);
|
||||
lua_pop(L, 1); /* remove lib */
|
||||
}
|
||||
/* add open functions from 'preloadedlibs' into 'package.preload' table */
|
||||
luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
|
||||
for (lib = preloadedlibs; lib->func; lib++) {
|
||||
lua_pushcfunction(L, lib->func);
|
||||
lua_setfield(L, -2, lib->name);
|
||||
}
|
||||
lua_pop(L, 1); /* remove _PRELOAD table */
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
#NOTE: I know it's a wonky spot, sue me
|
||||
debug: export CXXFLAGS+=-g
|
||||
debug: clean all
|
||||
|
||||
#include directories
|
||||
INCLUDES+=. common libmap
|
||||
|
||||
@@ -8,6 +12,10 @@ ifeq ($(OS),Windows_NT)
|
||||
LIBS+=-lmingw32
|
||||
endif
|
||||
LIBS+=-lSDL2main -lSDL2 -lSDL2_image -llua
|
||||
ifeq ($(shell uname), Linux)
|
||||
#I don't know what this does, but Ubuntu needs it (dynamic linking for lua)
|
||||
LIBS+=-ldl
|
||||
endif
|
||||
|
||||
#flags
|
||||
CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
--args are: userdata RegionPagerLua;
|
||||
|
||||
print("Running startup script")
|
||||
|
||||
pager = ...
|
||||
|
||||
---[[
|
||||
|
||||
--BUG: RegionPagerLua fails without these
|
||||
region_pager.SetOnLoad(pager, function(r)
|
||||
print("Calling SetOnLoad's lambda")
|
||||
end)
|
||||
region_pager.SetOnSave(pager, function(r)
|
||||
print("Calling SetOnSave's lambda")
|
||||
end)
|
||||
region_pager.SetOnCreate(pager, function(r)
|
||||
print("Calling SetOnCreate's lambda")
|
||||
end)
|
||||
region_pager.SetOnUnload(pager, function(r)
|
||||
print("Calling SetOnUnload's lambda")
|
||||
end)
|
||||
|
||||
--]]
|
||||
|
||||
print("Finished startup script")
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 540 KiB |
Reference in New Issue
Block a user