Skip to content

Commit

Permalink
Opengl/Direct3D specifics, File wrapper
Browse files Browse the repository at this point in the history
NEW: opengl and direct3d functions are wrapped, hope CMake is written
properly for this (haven't tested on Windows)

NEW: File wrapper. Users should probably use Lua's native file API, but
who knows.
  • Loading branch information
gilzoide committed Nov 9, 2016
1 parent 8e248bb commit 5834db6
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 70 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ include (${SWIG_USE_FILE})
find_package (Lua REQUIRED)

option (ALLEGRO_UNSTABLE "Include Allegro Unstable API" OFF)
option (ALLEGRO_WIN32_OPENGL "Include OpenGL specific API on Windows" OFF)

add_subdirectory (src)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Require, use, have fun!

```lua
local al = require 'lallegro'
assert (al.init (), 'Failed to initialize Allegro =/')
assert (al.init ())

local disp = al.create_display (800, 600)
al.clear_to_color (al.map_rgb (0, 0, 0))
Expand Down
51 changes: 29 additions & 22 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
# SWIG stuff
find_path (ALLEGRO_INCLUDE_PATH allegro5/allegro.h)
set (CMAKE_SWIG_FLAGS -I${ALLEGRO_INCLUDE_PATH})
# First, find Allegro
include (FindAllegro5.cmake)

# make SWIG find the headers
set (CMAKE_SWIG_FLAGS -I${ALLEGRO5_INCLUDE_DIR})
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_PROJECT_NAME})
# make SWIG use the same type runtime for all modules
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSWIG_TYPE_TABLE=${CMAKE_PROJECT_NAME}")

# Include Allegro Unstable API?
if (${ALLEGRO_UNSTABLE})
set (CMAKE_SWIG_FLAGS ${CMAKE_SWIG_FLAGS} -DALLEGRO_UNSTABLE)
set (CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -DALLEGRO_UNSTABLE)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DALLEGRO_UNSTABLE")
endif (${ALLEGRO_UNSTABLE})

swig_add_module (core lua core.i)
swig_link_libraries (core allegro)

# Wraps an addon, as they all follow the same name convention =]
function (swig_please_wrap_it addon)
swig_add_module (${addon} lua ${addon}.i)
swig_link_libraries (${addon} allegro allegro_${addon})
endfunction ()
# General modules
set (addons audio acodec color font ttf image memfile dialog physfs primitives video)
foreach (mod ${addons})
swig_add_module (${mod} lua ${mod}.i)
swig_link_libraries (${mod} allegro allegro_${mod})
endforeach (mod)

swig_please_wrap_it (audio)
swig_please_wrap_it (acodec)
swig_please_wrap_it (color)
swig_please_wrap_it (font)
swig_please_wrap_it (ttf)
swig_please_wrap_it (image)
swig_please_wrap_it (memfile)
swig_please_wrap_it (dialog)
swig_please_wrap_it (physfs)
swig_please_wrap_it (primitives)
swig_please_wrap_it (video)
# Platform specific modules: Direct3D or OpenGL?
if (${WIN32})
set (platform_specific direct3d)
# Include opengl specific functions on Windows?
if (${ALLEGRO_WIN32_OPENGL})
set (platform_specific "${platform_specific} opengl")
endif(${ALLEGRO_WIN32_OPENGL})
else ()
set (platform_specific opengl)
endif(${WIN32})
foreach (mod ${platform_specific})
swig_add_module (${mod} lua ${mod}.i)
swig_link_libraries (${mod} allegro)
endforeach (mod)

# Copy Lua files
file (GLOB luaSrc *.lua)
file (COPY ${luaSrc} DESTINATION ${CMAKE_PROJECT_NAME})

## Install ##
set (LUA_VER "${LUA_VERSION_MAJOR}.${LUA_VERSION_MINOR}")
install (TARGETS core audio acodec color font ttf image memfile dialog physfs
primitives video
install (TARGETS core ${addons} ${platform_specific}
LIBRARY DESTINATION lib/lua/${LUA_VER}/${CMAKE_PROJECT_NAME})
install (FILES ${luaSrc}
DESTINATION share/lua/${LUA_VER}/${CMAKE_PROJECT_NAME})
76 changes: 42 additions & 34 deletions src/Config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
--]]

--- @classmod lallegro.Config
-- ALLEGRO_CONFIG wrapper metatable, a GC enabled and iterable object. One can
-- use sandboxed Lua files as config, but this might be useful for some.
-- ALLEGRO_CONFIG wrapper metatable, a GC enabled object, with nice methods and
-- iterators. One can use sandboxed Lua files as config, but this might be
-- useful for some.

local al = require 'lallegro.core'

local Config = {}
-- Let Config objects call the methods
Config.__index = Config

--------------------------------------------------------------------------------
-- Interface functions
-- @section wrapper
--------------------------------------------------------------------------------

--- Garbage collector should destroy the wrapped ALLEGRO_CONFIG.
--
-- If you plan to destroy the ALLEGRO_CONFIG manually, first `extract` it, as
Expand All @@ -33,6 +39,30 @@ function Config:__gc ()
al.destroy_config (self.data)
end

--- Wraps a ALLEGRO_CONFIG on a Config object
--
-- @param al_cfg ALLEGRO_CONFIG to be wrapped
--
-- @return Config object
function Config.wrap (al_cfg)
return al_cfg and setmetatable ({ data = al_cfg }, Config)
end

--- Extracts the wrapped ALLEGRO_CONFIG, so that it will not be GCed.
--
-- This sets the inner ALLEGRO_CONFIG pointer as `NULL`, so it is not safe to
-- call any other methods after that.
function Config:extract ()
local al_cfg = self.data
self.data = nil
return al_cfg
end

--------------------------------------------------------------------------------
-- Getters
-- @section getters
--------------------------------------------------------------------------------

--- Get a configuration value.
--
-- This is a wrapper for al_get_config_value.
Expand All @@ -46,6 +76,11 @@ function Config:get (section, key)
return al.get_config_value (self.data, section, key)
end

--- Get the configuration as a Lua table
--
-- The global section is stored at index "global"
-- TODO

--------------------------------------------------------------------------------
-- Editing
-- @section edit
Expand Down Expand Up @@ -111,18 +146,15 @@ function Config:merge (other)
return Config.wrap (al.merge_config (self.data, other.data))
end

--- Merge this configuration into `other` one.
--
-- Note that in Allegro, the second argument is merged into the first one, but
-- here it is the opposite.
--- Merge `add` configuration into this one.
--
-- This is a wrapper for `al_merge_config_into`
--
-- @usage add:merge_into (master)
-- @usage master:merge_into (add)
--
-- @tparam Config other Configuration to merge into
function Config:merge_into (other)
al.merge_config_into (other.data, self.data)
-- @tparam Config add Configuration to merge into
function Config:merge_into (add)
al.merge_config_into (self.data, add.data)
end

--- Alias for `merge`, so you can merge Configs with Lua's `..` operator
Expand Down Expand Up @@ -188,30 +220,6 @@ function Config:iterate ()
return coroutine.wrap (the_iterator), self
end

--------------------------------------------------------------------------------
-- Interface functions
-- @section wrapper
--------------------------------------------------------------------------------

--- Wraps a ALLEGRO_CONFIG on a Config object
--
-- @param al_cfg ALLEGRO_CONFIG to be wrapped
--
-- @return Config object
function Config.wrap (al_cfg)
return al_cfg and setmetatable ({ data = al_cfg }, Config)
end

--- Extracts the wrapped ALLEGRO_CONFIG, so that it will not be GCed.
--
-- This sets the inner ALLEGRO_CONFIG pointer as `NULL`, so it is not safe to
-- call any other methods after that.
function Config:extract ()
local al_cfg = self.data
self.data = nil
return al_cfg
end

--------------------------------------------------------------------------------
-- Load/Save
-- @section loadsave
Expand Down
Loading

0 comments on commit 5834db6

Please sign in to comment.