Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ echo compiling (windows)...
windres res.rc -O coff -o res.res
gcc src/*.c src/api/*.c src/lib/lua52/*.c src/lib/stb/*.c^
-O3 -s -std=gnu11 -fno-strict-aliasing -Isrc -DLUA_USE_POPEN^
-Iwinlib/SDL2-2.0.10/x86_64-w64-mingw32/include^
-lmingw32 -lm -lSDL2main -lSDL2 -Lwinlib/SDL2-2.0.10/x86_64-w64-mingw32/lib^
-Iwinlib/SDL3-3.2.16/x86_64-w64-mingw32/include^
-lmingw32 -lm -lSDL3 -Lwinlib/SDL3-3.2.16/x86_64-w64-mingw32/lib^
-mwindows res.res^
-o lite.exe

Expand Down
8 changes: 4 additions & 4 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/bin/bash

cflags="-Wall -O3 -g -std=gnu11 -fno-strict-aliasing -Isrc"
lflags="-lSDL2 -lm"
lflags="-lSDL3 -lm"

if [[ $* == *windows* ]]; then
platform="windows"
outfile="lite.exe"
compiler="x86_64-w64-mingw32-gcc"
cflags="$cflags -DLUA_USE_POPEN -Iwinlib/SDL2-2.0.10/x86_64-w64-mingw32/include"
lflags="$lflags -Lwinlib/SDL2-2.0.10/x86_64-w64-mingw32/lib"
lflags="-lmingw32 -lSDL2main $lflags -mwindows -o $outfile res.res"
cflags="$cflags -DLUA_USE_POPEN -Iwinlib/SDL3-3.2.16/x86_64-w64-mingw32/include"
lflags="$lflags -Lwinlib/SDL3-3.2.16/x86_64-w64-mingw32/lib"
lflags="-lmingw32 -lSDL3 $lflags -mwindows -o $outfile res.res"
x86_64-w64-mingw32-windres res.rc -O coff -o res.res
else
platform="unix"
Expand Down
6 changes: 3 additions & 3 deletions build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
./build.sh release windows
./build.sh release
rm lite.zip 2>/dev/null
cp winlib/SDL2-2.0.10/x86_64-w64-mingw32/bin/SDL2.dll SDL2.dll
cp winlib/SDL3-3.2.16/x86_64-w64-mingw32/bin/SDL3.dll SDL3.dll
strip lite
strip lite.exe
strip SDL2.dll
zip lite.zip lite lite.exe SDL2.dll data -r
strip SDL3.dll
zip lite.zip lite lite.exe SDL3.dll data -r

103 changes: 53 additions & 50 deletions src/api/system.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#include <stdbool.h>
#include <ctype.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "api.h"
#include "rencache.h"
#ifdef _WIN32
Expand Down Expand Up @@ -46,55 +47,22 @@ static int f_poll_event(lua_State *L) {
}

switch (e.type) {
case SDL_QUIT:
lua_pushstring(L, "quit");
return 1;

case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_RESIZED) {
lua_pushstring(L, "resized");
lua_pushnumber(L, e.window.data1);
lua_pushnumber(L, e.window.data2);
return 3;
} else if (e.window.event == SDL_WINDOWEVENT_EXPOSED) {
rencache_invalidate();
lua_pushstring(L, "exposed");
return 1;
}
/* on some systems, when alt-tabbing to the window SDL will queue up
** several KEYDOWN events for the `tab` key; we flush all keydown
** events on focus so these are discarded */
if (e.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
SDL_FlushEvent(SDL_KEYDOWN);
}
goto top;

case SDL_DROPFILE:
SDL_GetGlobalMouseState(&mx, &my);
SDL_GetWindowPosition(window, &wx, &wy);
lua_pushstring(L, "filedropped");
lua_pushstring(L, e.drop.file);
lua_pushnumber(L, mx - wx);
lua_pushnumber(L, my - wy);
SDL_free(e.drop.file);
return 4;

case SDL_KEYDOWN:
case SDL_EVENT_KEY_DOWN:
lua_pushstring(L, "keypressed");
lua_pushstring(L, key_name(buf, e.key.keysym.sym));
lua_pushstring(L, key_name(buf, e.key.key));
return 2;

case SDL_KEYUP:
case SDL_EVENT_KEY_UP:
lua_pushstring(L, "keyreleased");
lua_pushstring(L, key_name(buf, e.key.keysym.sym));
lua_pushstring(L, key_name(buf, e.key.key));
return 2;

case SDL_TEXTINPUT:
case SDL_EVENT_TEXT_INPUT:
lua_pushstring(L, "textinput");
lua_pushstring(L, e.text.text);
return 2;

case SDL_MOUSEBUTTONDOWN:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
if (e.button.button == 1) { SDL_CaptureMouse(1); }
lua_pushstring(L, "mousepressed");
lua_pushstring(L, button_name(e.button.button));
Expand All @@ -103,27 +71,62 @@ static int f_poll_event(lua_State *L) {
lua_pushnumber(L, e.button.clicks);
return 5;

case SDL_MOUSEBUTTONUP:
case SDL_EVENT_MOUSE_BUTTON_UP:
if (e.button.button == 1) { SDL_CaptureMouse(0); }
lua_pushstring(L, "mousereleased");
lua_pushstring(L, button_name(e.button.button));
lua_pushnumber(L, e.button.x);
lua_pushnumber(L, e.button.y);
return 4;

case SDL_MOUSEMOTION:
case SDL_EVENT_MOUSE_MOTION:
lua_pushstring(L, "mousemoved");
lua_pushnumber(L, e.motion.x);
lua_pushnumber(L, e.motion.y);
lua_pushnumber(L, e.motion.xrel);
lua_pushnumber(L, e.motion.yrel);
return 5;

case SDL_MOUSEWHEEL:
case SDL_EVENT_MOUSE_WHEEL:
lua_pushstring(L, "mousewheel");
lua_pushnumber(L, e.wheel.y);
return 2;

case SDL_EVENT_QUIT:
lua_pushstring(L, "quit");
return 1;

case SDL_EVENT_WINDOW_RESIZED:
lua_pushstring(L, "resized");
lua_pushnumber(L, e.window.data1);
lua_pushnumber(L, e.window.data2);
return 3;

case SDL_EVENT_WINDOW_EXPOSED:
rencache_invalidate();
lua_pushstring(L, "exposed");
return 1;

/* on some systems, when alt-tabbing to the window SDL will queue up
* * several KEYDOWN events for the `tab` key; we flush all keydown
** events on focus so these are discarded */
case SDL_EVENT_WINDOW_FOCUS_GAINED:
SDL_FlushEvent(SDL_EVENT_KEY_DOWN);
goto top;

case SDL_EVENT_DROP_FILE:
{
float f32_mx, f32_my;
SDL_GetGlobalMouseState(&f32_mx, &f32_my);
mx = (int)(f32_mx); my = (int)(f32_my);
SDL_GetWindowPosition(window, &wx, &wy);
lua_pushstring(L, "filedropped");
lua_pushstring(L, e.drop.data);
lua_pushnumber(L, mx - wx);
lua_pushnumber(L, my - wy);
return 4;
}

default:
goto top;
}
Expand All @@ -139,7 +142,7 @@ static int f_wait_event(lua_State *L) {
}


static SDL_Cursor* cursor_cache[SDL_SYSTEM_CURSOR_HAND + 1];
static SDL_Cursor* cursor_cache[SDL_SYSTEM_CURSOR_POINTER + 1];

static const char *cursor_opts[] = {
"arrow",
Expand All @@ -151,11 +154,11 @@ static const char *cursor_opts[] = {
};

static const int cursor_enums[] = {
SDL_SYSTEM_CURSOR_ARROW,
SDL_SYSTEM_CURSOR_IBEAM,
SDL_SYSTEM_CURSOR_SIZEWE,
SDL_SYSTEM_CURSOR_SIZENS,
SDL_SYSTEM_CURSOR_HAND
SDL_SYSTEM_CURSOR_DEFAULT,
SDL_SYSTEM_CURSOR_TEXT,
SDL_SYSTEM_CURSOR_EW_RESIZE,
SDL_SYSTEM_CURSOR_NS_RESIZE,
SDL_SYSTEM_CURSOR_POINTER
};

static int f_set_cursor(lua_State *L) {
Expand Down Expand Up @@ -184,7 +187,7 @@ enum { WIN_NORMAL, WIN_MAXIMIZED, WIN_FULLSCREEN };
static int f_set_window_mode(lua_State *L) {
int n = luaL_checkoption(L, 1, "normal", window_opts);
SDL_SetWindowFullscreen(window,
n == WIN_FULLSCREEN ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
n == WIN_FULLSCREEN ? SDL_WINDOW_FULLSCREEN : 0);
if (n == WIN_NORMAL) { SDL_RestoreWindow(window); }
if (n == WIN_MAXIMIZED) { SDL_MaximizeWindow(window); }
return 0;
Expand Down
62 changes: 35 additions & 27 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL.h>
#include "api/api.h"
#include "renderer.h"

Expand All @@ -15,14 +17,23 @@
SDL_Window *window;


// NOTE: WARNING: HiDPI support is NOT YET TESTED because I do not
// currently have access to such a monitor.
// NOTE: from the docs, we should handle SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED
// for supporting moving a window between monitors with different DPI values
// References:
// - https://wiki.libsdl.org/SDL3/SDL_GetDisplayContentScale
// - https://wiki.libsdl.org/SDL3/README-highdpi
static double get_scale(void) {
float dpi;
SDL_GetDisplayDPI(0, NULL, &dpi, NULL);
#if _WIN32
return dpi / 96.0;
#else
return 1.0;
#endif
// NOTE: modified when porting to SDL3 (because the previous code, calling
// SDL_GetDisplayDPI() would not compile anymore.
float val = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
if (val == 0.f)
{
fprintf(stderr, "%s\n", SDL_GetError());
return 1.0;
}
return (double)val;
}


Expand All @@ -47,16 +58,10 @@ static void get_exe_filename(char *buf, int sz) {
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_PixelFormat pxformat = SDL_GetPixelFormatForMasks(32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_Surface *surf = SDL_CreateSurfaceFrom(64, 64, pxformat, icon_rgba, 64*4);
SDL_SetWindowIcon(window, surf);
SDL_FreeSurface(surf);
SDL_DestroySurface(surf);
#endif
}

Expand All @@ -70,25 +75,28 @@ int main(int argc, char **argv) {

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
SDL_EnableScreenSaver();
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
SDL_SetEventEnabled(SDL_EVENT_DROP_FILE, true);
atexit(SDL_Quit);

#ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* Available since 2.0.8 */
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
#endif
#if SDL_VERSION_ATLEAST(2, 0, 5)
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
#endif

SDL_DisplayMode dm;
SDL_GetCurrentDisplayMode(0, &dm);
const SDL_DisplayMode* dm = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());

window = SDL_CreateWindow(
"", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, dm.w * 0.8, dm.h * 0.8,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);
window = SDL_CreateWindow("", (int)(dm->w * 0.8), (int)(dm->h * 0.8),
SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_HIDDEN);
init_window_icon();
ren_init(window);

// NOTE: added when porting to SDL3
// This call is required in SDL3 for Lite's usage (It needs TextInput events and I tested original lite to confirm that they are sent with each letter KEYDOWN)
// (It seems that it was automatically called in SDL2, or where was that call ?)
if (!SDL_StartTextInput(window))
{
#ifndef _WIN32 // because win32 apps may not have a console ?
fputs("ERROR: failed SDL_StartTextInput() - text entry may not work ! TODO: implement a fallback in Lua code, by using KEY_DOWN instead.\n", stderr);
#endif
}

lua_State *L = luaL_newstate();
luaL_openlibs(L);
Expand Down
1 change: 1 addition & 0 deletions src/rencache.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include <stdio.h>
#include "rencache.h"

Expand Down
3 changes: 2 additions & 1 deletion src/renderer.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
Expand Down Expand Up @@ -83,7 +84,7 @@ void ren_set_clip_rect(RenRect rect) {


void ren_get_size(int *x, int *y) {
SDL_Surface *surf = SDL_GetWindowSurface(window);
const SDL_Surface *surf = SDL_GetWindowSurface(window);
*x = surf->w;
*y = surf->h;
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef RENDERER_H
#define RENDERER_H

#include <SDL2/SDL.h>
#include <SDL3/SDL.h>
#include <stdint.h>

typedef struct RenImage RenImage;
Expand Down
16 changes: 0 additions & 16 deletions winlib/SDL2-2.0.10/BUGS.txt

This file was deleted.

Loading