static int screen_compare(lua_State *L) { Screen *screen; SDL_Surface *real; SDL_Surface *tmp; SDL_Surface *expected; int is_equal; int size; int bpp_real; int bpp_expected; screen = check_screen(L, 1); real = screen->screen; bpp_real = real->format->BytesPerPixel; tmp = IMG_Load(lua_tostring(L, 2)); /* convert to screen format */ expected = SDL_DisplayFormat(tmp); bpp_expected = expected->format->BytesPerPixel; SDL_FreeSurface(tmp); bpp_real = expected->format->BytesPerPixel; assert(expected); assert(bpp_real == bpp_expected); assert(real->w == expected->w); assert(real->h == expected->h); size = expected->w * expected->h * bpp_expected; is_equal = compare_mem(real->pixels, expected->pixels, size); SDL_FreeSurface(expected); lua_pushboolean(L, is_equal); return 1; }
static int screen_init(lua_State *L) { Screen *screen; int w, h, bpp; screen = check_screen(L, 1); if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); /* TODO: Don't die, just return error */ exit(1); } lua_getfield(L, 2, "y"); h = lua_tointeger(L, -1); lua_getfield(L, 2, "x"); w = lua_tointeger(L, -1); bpp = lua_tointeger(L, 3); screen->screen = SDL_SetVideoMode( w, h, bpp, SDL_RESIZABLE); SDL_WM_SetCaption("Marauder_rl", NULL); if (!screen) { fprintf(stderr, "SDL_SetVideoMode() error: %s\n", SDL_GetError()); exit(1); } IMG_Init(IMG_INIT_PNG); init_colors(screen); init_characters(screen); screen->font = build_font(loadimg("img/font_8x12.png"), 8, 12); return 0; }
static int screen_set_map_offset(lua_State *L) { Screen *screen = check_screen(L, 1); lua_getfield(L, 2, "y"); screen->offset_y = lua_tointeger(L, -1) - 1; lua_getfield(L, 2, "x"); screen->offset_x = lua_tointeger(L, -1) - 1; return 0; }
static int screen_close(lua_State *L) { Screen *screen; screen = check_screen(L, -1); SDL_Quit(); (void)screen; /* TODO: free memory */ return 0; }
static int screen_get_map_offset(lua_State *L) { Screen *screen = check_screen(L, 1); lua_newtable(L); lua_pushinteger(L, screen->offset_y + 1); lua_setfield(L, -2, "y"); lua_pushinteger(L, screen->offset_x + 1); lua_setfield(L, -2, "x"); return 1; }
static int screen_delay(lua_State *L) { Screen *screen; int args_count = lua_gettop(L); int time; (void)screen; assert(args_count == 2); screen = check_screen(L, 1); time = lua_tointeger(L, 2); SDL_Delay(time); return 0; }
static int screen_move(lua_State *L) { Screen *screen; int args_count = lua_gettop(L); assert(args_count == 2); screen = check_screen(L, 1); lua_getfield(L, 2, "y"); screen->cursor_y = lua_tointeger(L, -1) - 1; lua_getfield(L, 2, "x"); screen->cursor_x = lua_tointeger(L, -1) - 1; assert(screen->cursor_x >= 0); assert(screen->cursor_y >= 0); return 0; }
/* TODO */ static int screen_draw_text(lua_State *L) { Vec2i pos; Screen *screen; int args_count = lua_gettop(L); assert(args_count == 3); assert(lua_istable(L, 2) && lua_isstring(L, 3)); lua_getfield(L, 2, "y"); pos.y = lua_tointeger(L, -1) - 1; lua_getfield(L, 2, "x"); pos.x = lua_tointeger(L, -1) - 1; screen = check_screen(L, 1); render_text(screen->screen, &screen->font, lua_tostring(L, 3), pos); return 0; }
static int screen_draw_symbol(lua_State *L) { Screen *screen; SDL_Surface *surface; int sx, sy; int args_count = lua_gettop(L); int char_code; assert(args_count == 2); screen = check_screen(L, 1); char_code = lua_tointeger(L, 2); surface = screen->characters[char_code]; sx = screen->offset_x + 25 * screen->cursor_x; sy = screen->offset_y + 25 * screen->cursor_y; draw_image(screen, sx, sy, surface); return 0; }
static int screen_tile_to_pixel(lua_State *L) { Screen *screen; Vec2i pos; screen = check_screen(L, 1); lua_getfield(L, 2, "y"); pos.y = lua_tointeger(L, -1) - 1; lua_getfield(L, 2, "x"); pos.x = lua_tointeger(L, -1) - 1; pos.y = screen->offset_y + pos.y * 25; pos.x = screen->offset_x + pos.x * 25; lua_newtable(L); lua_pushinteger(L, pos.y); lua_setfield(L, -2, "y"); lua_pushinteger(L, pos.x); lua_setfield(L, -2, "x"); return 1; }
/* TODO: Color */ static int screen_draw_line(lua_State *L) { Screen *screen; int x0, y0, x1, y1; int args_count = lua_gettop(L); assert(args_count == 3); screen = check_screen(L, 1); lua_getfield(L, 2, "y"); y0 = lua_tointeger(L, -1) - 1; lua_getfield(L, 2, "x"); x0 = lua_tointeger(L, -1) - 1; lua_getfield(L, 3, "y"); y1 = lua_tointeger(L, -1) - 1; lua_getfield(L, 3, "x"); x1 = lua_tointeger(L, -1) - 1; bresenham_line(screen->screen, x0, y0, x1, y1, screen->colors.white); return 0; }
int main(int argc, char *argv[]) { int err = 0; initscr(); if (!check_screen()) { fprintf(stderr, "Your terminal is not big enough.\n"); err = 1; } else { init(); boot(); } endwin(); return err; }
static int screen_get_char(lua_State *L) { Screen *screen; int char_code = -1; SDL_Event e; screen = check_screen(L, -1); while (char_code == -1) { while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_KEYUP: { char_code = e.key.keysym.sym; break; } #if 1 /* TODO: Move out here? */ case SDL_QUIT: { /* done = true; */ printf("case SDL_QUIT\n"); exit(0); break; } case SDL_VIDEORESIZE: { screen->screen = SDL_SetVideoMode( e.resize.w, e.resize.h, 32, SDL_RESIZABLE); /* TODO: Redraw */ break; } #endif } } SDL_Delay(10); } assert(char_code > 0); assert(char_code < 128); lua_pushinteger(L, char_code); return 1; }
static int screen_refresh(lua_State *L) { Screen *screen; screen = check_screen(L, -1); SDL_Flip(screen->screen); return 0; }
/* TODO: Add color */ static int screen_clear(lua_State *L) { Screen *screen; screen = check_screen(L, -1); draw_bg(screen, screen->colors.grey); return 0; }