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

allow window to dynamic resize

protection against floating point widths


git-svn-id: http://svn.net-core.org/repos/t-engine4@587 51575b47-30f0-44d4-a5cc-537603b46e54
parent 3bd3af9e
No related branches found
No related tags found
No related merge requests found
......@@ -30,9 +30,9 @@ tiles = engine.Tiles.new(16, 16)
--- Create a buttons list
function _M:init(list, x, y, w, h, font, separator)
self.separator = separator or 20
self.w, self.h = w, h
self.display_x = x or (game.w - self.w) / 2
self.display_y = y or (game.h - self.h) / 2
self.w, self.h = math.floor(w), math.floor(h)
self.display_x = math.floor(x or (game.w - self.w) / 2)
self.display_y = math.floor(y or (game.h - self.h) / 2)
self.font = font
self.list = list
if not font then self.font = core.display.newFont("/data/font/VeraBd.ttf", 16) end
......
......@@ -48,9 +48,9 @@ end
--- Create a Dialog
function _M:init(title, w, h, x, y, alpha, font)
self.title = title
self.w, self.h = w, h
self.display_x = x or (game.w - self.w) / 2
self.display_y = y or (game.h - self.h) / 2
self.w, self.h = math.floor(w), math.floor(h)
self.display_x = math.floor(x or (game.w - self.w) / 2)
self.display_y = math.floor(y or (game.h - self.h) / 2)
self.font = font
if not font then self.font = core.display.newFont("/data/font/Vera.ttf", 12) end
self.font_h = self.font:lineSkip()
......
......@@ -200,6 +200,7 @@ end
--- Called when screen resolution changes
function _M:onResolutionChange()
self.w, self.h = core.display.size()
end
--- Requests the game to save
......
......@@ -32,14 +32,14 @@ end
--- Resize the display area
function _M:resize(x, y, w, h)
self.display_x, self.display_y = x, y
self.w, self.h = w, h
self.display_x, self.display_y = math.floor(x), math.floor(y)
self.w, self.h = math.floor(w), math.floor(h)
self.surface = core.display.newSurface(w, h)
if self.actor then self.actor.changed = true end
local cw, ch = self.font:size(" ")
self.font_w = cw
self.max_char_w = math.floor(w / self.font_w)
self.max_char_w = math.min(127, math.floor(w / self.font_w))
end
local page_to_hotkey = {"", "SECOND_", "THIRD_"}
......
......@@ -26,8 +26,8 @@ module(..., package.seeall, class.make)
function _M:init(x, y, w, h, max, fontname, fontsize, color, bgcolor)
self.color = color or {255,255,255}
self.bgcolor = bgcolor or {0,0,0}
self.display_x, self.display_y = x, y
self.w, self.h = w, h
self.display_x, self.display_y = math.floor(x), math.floor(y)
self.w, self.h = math.floor(w), math.floor(h)
self.font = core.display.newFont(fontname or "/data/font/Vera.ttf", fontsize or 12)
self.font_h = self.font:lineSkip()
self.surface = core.display.newSurface(w, h)
......@@ -40,8 +40,8 @@ end
--- Resize the display area
function _M:resize(x, y, w, h)
self.display_x, self.display_y = x, y
self.w, self.h = w, h
self.display_x, self.display_y = math.floor(x), math.floor(y)
self.w, self.h = math.floor(w), math.floor(h)
self.surface = core.display.newSurface(w, h)
self.changed = true
end
......
......@@ -30,8 +30,8 @@ BAD = 3
function _M:init(x, y, w, h, max, fontname, fontsize, color, bgcolor)
self.color = color or {255,255,255}
self.bgcolor = bgcolor or {0,0,0}
self.display_x, self.display_y = x, y
self.w, self.h = w, h
self.display_x, self.display_y = math.floor(x), math.floor(y)
self.w, self.h = math.floor(w), math.floor(h)
self.font = core.display.newFont(fontname or "/data/font/Vera.ttf", fontsize or 16)
self.font_h = self.font:lineSkip()
self.surface = core.display.newSurface(w, h)
......@@ -44,8 +44,8 @@ end
--- Resize the display area
function _M:resize(x, y, w, h)
self.display_x, self.display_y = x, y
self.w, self.h = w, h
self.display_x, self.display_y = math.floor(x), math.floor(y)
self.w, self.h = math.floor(w), math.floor(h)
self.surface = core.display.newSurface(w, h)
self.changed = true
end
......
......@@ -53,8 +53,8 @@ color_obscure = { 0.6, 0.6, 0.6, 1 }
-- @param fontsize font parameters, can be nil
function _M:setViewPort(x, y, w, h, tile_w, tile_h, fontname, fontsize, multidisplay)
self.multidisplay = multidisplay
self.display_x, self.display_y = x, y
self.viewport = {width=w, height=h, mwidth=math.floor(w/tile_w), mheight=math.floor(h/tile_h)}
self.display_x, self.display_y = math.floor(x), math.floor(y)
self.viewport = {width=math.floor(w), height=math.floor(h), mwidth=math.floor(w/tile_w), mheight=math.floor(h/tile_h)}
self.tile_w, self.tile_h = tile_w, tile_h
self.fontname, self.fontsize = fontname, fontsize
self:resetTiles()
......
......@@ -843,17 +843,8 @@ static int sdl_set_window_size(lua_State *L)
int w = luaL_checknumber(L, 1);
int h = luaL_checknumber(L, 2);
bool fullscreen = lua_toboolean(L, 3);
int flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWSURFACE;
if (fullscreen) flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWSURFACE | SDL_FULLSCREEN;
screen = SDL_SetVideoMode(w, h, 32, flags);
if (screen==NULL) {
printf("error opening screen: %s\n", SDL_GetError());
return 0;
}
resizeWindow(screen->w, screen->h);
do_resize(w, h, fullscreen);
lua_pushboolean(L, TRUE);
return 1;
......
......@@ -386,6 +386,21 @@ int resizeWindow(int width, int height)
return( TRUE );
}
void do_resize(int w, int h, bool fullscreen)
{
int flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWSURFACE | SDL_RESIZABLE;
if (fullscreen) flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWSURFACE | SDL_FULLSCREEN;
screen = SDL_SetVideoMode(w, h, 32, flags);
if (screen==NULL) {
printf("error opening screen: %s\n", SDL_GetError());
return 0;
}
resizeWindow(screen->w, screen->h);
}
/**
* Program entry point.
*/
......@@ -467,7 +482,8 @@ int main(int argc, char *argv[])
SDL_WM_SetIcon(IMG_Load_RW(PHYSFSRWOPS_openRead("/data/gfx/te4-icon.png"), TRUE), NULL);
screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWSURFACE);
// screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_HWSURFACE | SDL_RESIZABLE);
do_resize(WIDTH, HEIGHT, FALSE);
if (screen==NULL) {
printf("error opening screen: %s\n", SDL_GetError());
return;
......@@ -509,6 +525,22 @@ int main(int argc, char *argv[])
{
switch(event.type)
{
case SDL_VIDEORESIZE:
printf("resize %d x %d\n", event.resize.w, event.resize.h);
do_resize(event.resize.w, event.resize.h, FALSE);
if (current_game != LUA_NOREF)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
lua_pushstring(L, "onResolutionChange");
lua_gettable(L, -2);
lua_remove(L, -2);
lua_rawgeti(L, LUA_REGISTRYINDEX, current_game);
docall(L, 1, 0);
}
break;
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONUP:
case SDL_KEYDOWN:
......
......@@ -22,6 +22,7 @@
#define _MAIN_H_
extern int resizeWindow(int width, int height);
extern void do_resize(int w, int h, bool fullscreen);
#endif
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