2019-12-28 12:16:32 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
#include "api/api.h"
|
|
|
|
#include "renderer.h"
|
2019-12-31 11:49:07 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
2019-12-31 11:49:07 +01:00
|
|
|
#elif __linux__
|
|
|
|
#include <unistd.h>
|
|
|
|
#elif __APPLE__
|
|
|
|
#include <mach-o/dyld.h>
|
2019-12-28 12:16:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
SDL_Window *window;
|
|
|
|
|
2019-12-31 11:49:07 +01:00
|
|
|
|
|
|
|
static double get_scale(void) {
|
|
|
|
float dpi;
|
|
|
|
SDL_GetDisplayDPI(0, NULL, &dpi, NULL);
|
|
|
|
#if _WIN32
|
|
|
|
return dpi / 96.0;
|
|
|
|
#elif __APPLE__
|
2020-05-10 20:31:09 +02:00
|
|
|
return 1.0; /* dpi / 72.0; */
|
2019-12-31 11:49:07 +01:00
|
|
|
#else
|
|
|
|
return 1.0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-17 10:36:46 +02:00
|
|
|
static void get_exe_filename(char *buf, int sz) {
|
2019-12-31 11:49:07 +01:00
|
|
|
#if _WIN32
|
|
|
|
int len = GetModuleFileName(NULL, buf, sz - 1);
|
|
|
|
buf[len] = '\0';
|
|
|
|
#elif __linux__
|
|
|
|
char path[512];
|
|
|
|
sprintf(path, "/proc/%d/exe", getpid());
|
|
|
|
int len = readlink(path, buf, sz - 1);
|
|
|
|
buf[len] = '\0';
|
|
|
|
#elif __APPLE__
|
|
|
|
unsigned size = sz;
|
|
|
|
_NSGetExecutablePath(buf, &size);
|
|
|
|
#else
|
2020-05-17 10:36:46 +02:00
|
|
|
strcpy(buf, "./lite");
|
2019-12-31 11:49:07 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-06 20:49:34 +02:00
|
|
|
static void init_window_icon(void) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include "../icon.inl"
|
|
|
|
(void) icon_rgba_len; /* unused */
|
|
|
|
SDL_Surface *surf = SDL_CreateRGBSurfaceFrom(
|
|
|
|
icon_rgba, 64, 64,
|
|
|
|
32, 64 * 4,
|
|
|
|
0x000000ff,
|
|
|
|
0x0000ff00,
|
|
|
|
0x00ff0000,
|
|
|
|
0xff000000);
|
|
|
|
SDL_SetWindowIcon(window, surf);
|
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-12-31 11:49:07 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
HINSTANCE lib = LoadLibrary("user32.dll");
|
|
|
|
int (*SetProcessDPIAware)() = (void*) GetProcAddress(lib, "SetProcessDPIAware");
|
|
|
|
SetProcessDPIAware();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
|
|
|
|
SDL_EnableScreenSaver();
|
|
|
|
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
#if SDL_VERSION_ATLEAST(2, 0, 5)
|
|
|
|
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_DisplayMode dm;
|
|
|
|
SDL_GetCurrentDisplayMode(0, &dm);
|
|
|
|
|
|
|
|
window = SDL_CreateWindow(
|
2020-05-14 23:28:22 +02:00
|
|
|
"", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, dm.w * 0.8, dm.h * 0.8,
|
|
|
|
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
|
2020-05-06 20:49:34 +02:00
|
|
|
init_window_icon();
|
2019-12-28 12:16:32 +01:00
|
|
|
ren_init(window);
|
|
|
|
|
|
|
|
|
|
|
|
lua_State *L = luaL_newstate();
|
|
|
|
luaL_openlibs(L);
|
|
|
|
api_load_libs(L);
|
|
|
|
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
lua_pushstring(L, argv[i]);
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
2020-04-25 10:57:35 +02:00
|
|
|
lua_setglobal(L, "ARGS");
|
|
|
|
|
2020-05-24 14:52:10 +02:00
|
|
|
lua_pushstring(L, "1.05");
|
2020-04-25 10:57:35 +02:00
|
|
|
lua_setglobal(L, "VERSION");
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-04-23 21:03:14 +02:00
|
|
|
lua_pushstring(L, SDL_GetPlatform());
|
2020-04-25 10:57:35 +02:00
|
|
|
lua_setglobal(L, "PLATFORM");
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2019-12-31 11:49:07 +01:00
|
|
|
lua_pushnumber(L, get_scale());
|
2020-04-25 10:57:35 +02:00
|
|
|
lua_setglobal(L, "SCALE");
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-05-17 10:36:46 +02:00
|
|
|
char exename[2048];
|
|
|
|
get_exe_filename(exename, sizeof(exename));
|
|
|
|
lua_pushstring(L, exename);
|
|
|
|
lua_setglobal(L, "EXEFILE");
|
2019-12-31 11:49:07 +01:00
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
(void) luaL_dostring(L,
|
|
|
|
"local core\n"
|
|
|
|
"xpcall(function()\n"
|
2020-04-25 10:57:35 +02:00
|
|
|
" SCALE = tonumber(os.getenv(\"LITE_SCALE\")) or SCALE\n"
|
|
|
|
" PATHSEP = package.config:sub(1, 1)\n"
|
2020-05-17 17:58:32 +02:00
|
|
|
" EXEDIR = EXEFILE:match(\"^(.+)[/\\\\].*$\")\n"
|
2020-04-25 10:57:35 +02:00
|
|
|
" package.path = EXEDIR .. '/data/?.lua;' .. package.path\n"
|
|
|
|
" package.path = EXEDIR .. '/data/?/init.lua;' .. package.path\n"
|
2019-12-28 12:16:32 +01:00
|
|
|
" core = require('core')\n"
|
|
|
|
" core.init()\n"
|
|
|
|
" core.run()\n"
|
|
|
|
"end, function(err)\n"
|
|
|
|
" print('Error: ' .. tostring(err))\n"
|
|
|
|
" print(debug.traceback(nil, 2))\n"
|
|
|
|
" if core and core.on_error then\n"
|
|
|
|
" pcall(core.on_error, err)\n"
|
|
|
|
" end\n"
|
|
|
|
" os.exit(1)\n"
|
|
|
|
"end)");
|
|
|
|
|
|
|
|
|
|
|
|
lua_close(L);
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|