Skip to content
Snippets Groups Projects
Commit 5eb93829 authored by dg's avatar dg
Browse files

hande images

git-svn-id: http://svn.net-core.org/repos/t-engine4@13 51575b47-30f0-44d4-a5cc-537603b46e54
parent 704d2093
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ function _M:init(t)
self.uid = next_uid
__uids[self.uid] = self
self.image = t.image or nil
self.display = t.display or '.'
self.color_r = t.color_r or 0
self.color_g = t.color_g or 0
......
......@@ -4,6 +4,7 @@ module(..., package.seeall, class.make)
function _M:init(keyhandler)
self.key = keyhandler
self.level = nil
self.w, self.h = core.display.size()
end
function _M:setLevel(level)
......@@ -12,6 +13,7 @@ end
function _M:setCurrent()
core.game.set_current_game(self)
_M.current = self
end
function _M:display()
......
......@@ -11,7 +11,7 @@ function _M:init(w, h, max, fontname, fontsize, color, bgcolor)
self.surface = core.display.newSurface(w, h)
self.log = {}
getmetatable(self).__call = _M.call
self.max = max or 400
self.max = max or 4000
self.changed = true
end
......
......@@ -11,20 +11,28 @@ ACTOR = 20
displayOrder = { ACTOR, OBJECT, TERRAIN }
rememberDisplayOrder = { TERRAIN }
function _M:init(w, h)
self.tiles = Tiles.new(16, 16)
self.w, self.h = w, h
function _M:init(w, h, tile_w, tile_h)
self.tiles = Tiles.new(tile_w, tile_h)
self.w, self.h = math.floor(w / tile_w), math.floor(h / tile_h)
self.tile_w, self.tile_h = tile_w, tile_h
self.map = {}
self.lites = {}
self.seens = {}
self.remembers = {}
for i = 0, w * h - 1 do self.map[i] = {} end
getmetatable(self).__call = _M.call
setmetatable(self.lites, {__call = function(t, x, y, v) if v ~= nil then t[x + y * w] = v end return t[x + y * w] end})
setmetatable(self.seens, {__call = function(t, x, y, v) if v ~= nil then t[x + y * w] = v end return t[x + y * w] end})
setmetatable(self.remembers, {__call = function(t, x, y, v) if v ~= nil then t[x + y * w] = v end return t[x + y * w] end})
local mapbool = function(t, x, y, v)
if x < 0 or y < 0 or x >= self.w or y >= self.h then return end
if v ~= nil then
t[x + y * self.w] = v
end
return t[x + y * self.w]
end
setmetatable(self.lites, {__call = mapbool})
setmetatable(self.seens, {__call = mapbool})
setmetatable(self.remembers, {__call = mapbool})
self.surface = core.display.newSurface(w * 16, h * 16)
self.surface = core.display.newSurface(w, h)
self.fov = core.fov.new(_M.opaque, _M.apply, self)
self.fov_lite = core.fov.new(_M.opaque, _M.applyLite, self)
self.changed = true
......@@ -68,15 +76,14 @@ function _M:display()
e, si = nil, 1
z = i + j * self.w
order = displayOrder
if self.seens[z] or self.remembers[z] then
if not self.seens[z] then order = rememberDisplayOrder end
while not e and si <= #order do e = self(i, j, order[si]) si = si + 1 end
if e then
if self.seens[z] then
self.surface:merge(self.tiles:get(e.display, e.color_r, e.color_g, e.color_b, e.color_br, e.color_bg, e.color_bb), i * 16, j * 16)
self.surface:merge(self.tiles:get(e.display, e.color_r, e.color_g, e.color_b, e.color_br, e.color_bg, e.color_bb, e.image), i * self.tile_w, j * self.tile_h)
elseif self.remembers[z] then
self.surface:merge(self.tiles:get(e.display, e.color_r/3, e.color_g/3, e.color_b/3, e.color_br/3, e.color_bg/3, e.color_bb/3), i * 16, j * 16)
self.surface:merge(self.tiles:get(e.display, e.color_r/3, e.color_g/3, e.color_b/3, e.color_br/3, e.color_bg/3, e.color_bb/3, e.image), i * self.tile_w, j * self.tile_h)
end
end
end
......
......@@ -2,13 +2,16 @@ require "engine.class"
module(..., package.seeall, class.make)
prefix = "/data/gfx/"
use_images = true
function _M:init(w, h, fontname, fontsize)
self.w, self.h = w, h
self.font = core.display.newFont(fontname or "/data/font/VeraMono.ttf", fontsize or 14)
self.repo = {}
end
function _M:get(char, fr, fg, fb, br, bg, bb)
function _M:get(char, fr, fg, fb, br, bg, bb, image)
local fgidx = 65536 * fr + 256 * fg + fb
local bgidx
if br >= 0 and bg >= 0 and bb >= 0 then
......@@ -16,17 +19,25 @@ function _M:get(char, fr, fg, fb, br, bg, bb)
else
bgidx = "none"
end
if self.use_images and image then char = image end
if self.repo[char] and self.repo[char][fgidx] and self.repo[char][fgidx][bgidx] then
return self.repo[char][fgidx][bgidx]
else
local s = core.display.newSurface(self.w, self.h)
local s
if self.use_images and image then
s = core.display.loadImage(self.prefix..image)
else
s = core.display.newSurface(self.w, self.h)
if br >= 0 then
s:erase(br, bg, bb)
end
if br >= 0 then
s:erase(br, bg, bb)
end
local w, h = self.font:size(char)
s:drawString(self.font, char, (self.w - w) / 2, (self.h - h) / 2, fr, fg, fb)
local w, h = self.font:size(char)
s:drawString(self.font, char, (self.w - w) / 2, (self.h - h) / 2, fr, fg, fb)
end
self.repo[char] = self.repo[char] or {}
self.repo[char][fgidx] = self.repo[char][fgidx] or {}
......
......@@ -2,6 +2,7 @@
dofile("/engine/utils.lua")
require "engine.KeyCommand"
require "engine.Tiles"
-- Setup a default key handler
local key = engine.KeyCommand.new()
......@@ -20,6 +21,8 @@ if mod_def then
mod_def()
if not mod.name or not mod.short_name or not mod.version or not mod.starter then os.exit() end
engine.Tiles.prefix = "/"..mod.short_name.."/data/gfx/"
require(mod.starter)
else
os.exit()
......
......@@ -17,10 +17,10 @@ function _M:init()
self.tooltip = engine.Tooltip.new(nil, nil, {255,255,255}, {30,30,30})
self.log = engine.LogDisplay.new(400, 150, nil, nil, nil, {255,255,255}, {30,30,30})
self.log = engine.LogDisplay.new(self.w * 0.5, self.h * 0.20, nil, nil, nil, {255,255,255}, {30,30,30})
self.log("Welcome to #00FF00#Tales of Middle Earth!")
local map = Map.new(40, 20)
local map = Map.new(self.w, math.floor(self.h * 0.80), 16, 16)
-- map:liteAll(0, 0, map.w, map.h)
local floor = Entity.new{display='.', color_r=100, color_g=200, color_b=100, color_br=0, color_bg=50, color_bb=0}
......@@ -29,7 +29,7 @@ function _M:init()
local e3 = Entity.new{display='#', color_b=255, block_sight=true, block_move=true}
local e4 = e3:clone{color_r=255}
for i = 0, 39 do for j = 0, 19 do
for i = 0, map.w-1 do for j = 0, map.h-1 do
map(i, j, 1, floor)
end end
......@@ -46,7 +46,7 @@ function _M:init()
local level = Level.new(map)
self:setLevel(level)
self.player = Player.new(self, {name="player", display='@', color_r=230, color_g=230, color_b=230})
self.player = Player.new(self, {name="player", image='player.png', display='@', color_r=230, color_g=230, color_b=230})
self.player:move(4, 3, true)
level:addEntity(self.player)
......@@ -64,14 +64,21 @@ function _M:tick()
end
function _M:display()
self.log:display():toScreen(0, 16 * 20 + 5)
self.log:display():toScreen(0, self.h * 0.80)
if self.level and self.level.map then
self.level.map.seens(self.player.x, self.player.y, true)
self.level.map.lites(self.player.x, self.player.y, true)
self.level.map.remembers(self.player.x, self.player.y, true)
self.level.map.fov(self.player.x, self.player.y, 20)
self.level.map.fov_lite(self.player.x, self.player.y, 4)
local s = self.level.map:display()
if s then s:toScreen(0, 0) end
if s then
s:toScreen(0, 0)
end
local mx, my = core.mouse.get()
local tt = self.level.map:checkAllEntity(math.floor(mx / 16), math.floor(my / 16), "tooltip")
local tt = self.level.map:checkAllEntity(math.floor(mx / self.level.map.tile_w), math.floor(my / self.level.map.tile_h), "tooltip")
if tt then
self.tooltip:set(tt)
local t = self.tooltip:display()
......
......@@ -9,11 +9,6 @@ end
function _M:move(x, y, force)
local moved = tome.class.Actor.move(self, x, y, force)
if self.x and self.y then
self.game.level.map.fov(self.x, self.y, 20)
self.game.level.map.fov_lite(self.x, self.y, 4)
self.game.level.map.seens(self.x, self.y, true)
end
return moved
end
......
game/modules/tome/data/gfx/player.png

251 B

......@@ -189,6 +189,13 @@ static int sdl_fullscreen(lua_State *L)
return 0;
}
static int sdl_screen_size(lua_State *L)
{
lua_pushnumber(L, screen->w);
lua_pushnumber(L, screen->h);
return 2;
}
static int sdl_new_font(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
......@@ -284,6 +291,18 @@ static int sdl_new_surface(lua_State *L)
return 1;
}
static int sdl_load_image(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
SDL_Surface **s = (SDL_Surface**)lua_newuserdata(L, sizeof(SDL_Surface*));
auxiliar_setclass(L, "sdl{surface}", -1);
*s = IMG_Load_RW(PHYSFSRWOPS_openRead(name), TRUE);
return 1;
}
static int sdl_free_surface(lua_State *L)
{
SDL_Surface **s = (SDL_Surface**)auxiliar_checkclass(L, "sdl{surface}", 1);
......@@ -345,8 +364,10 @@ static int sdl_surface_merge(lua_State *L)
static const struct luaL_reg displaylib[] =
{
{"fullscreen", sdl_fullscreen},
{"size", sdl_screen_size},
{"newFont", sdl_new_font},
{"newSurface", sdl_new_surface},
{"loadImage", sdl_load_image},
{NULL, NULL},
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment