Skip to content

Commit

Permalink
convert startup to sdl3
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiko2k committed Dec 19, 2024
1 parent 2d409c7 commit b10beca
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 283 deletions.
143 changes: 65 additions & 78 deletions src/tauon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,11 @@
import os
import pickle
import sys
from ctypes import c_int, pointer
from ctypes import c_int, pointer, c_float, byref
from pathlib import Path

from gi.repository import GLib
from sdl2 import (
SDL_BLENDMODE_BLEND,
SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH,
SDL_HINT_RENDER_SCALE_QUALITY,
SDL_HINT_VIDEO_ALLOW_SCREENSAVER,
SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR,
SDL_INIT_VIDEO,
SDL_MESSAGEBOX_ERROR,
SDL_RENDERER_ACCELERATED,
SDL_RENDERER_PRESENTVSYNC,
SDL_WINDOW_ALLOW_HIGHDPI,
SDL_WINDOW_BORDERLESS,
SDL_WINDOW_FULLSCREEN_DESKTOP,
SDL_WINDOW_HIDDEN,
SDL_WINDOW_RESIZABLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_CreateRenderer,
SDL_CreateTextureFromSurface,
SDL_CreateWindow,
SDL_DestroyTexture,
SDL_FreeSurface,
SDL_GetError,
SDL_GetWindowSize,
SDL_GL_GetDrawableSize,
SDL_Init,
SDL_MaximizeWindow,
SDL_Rect,
SDL_RenderClear,
SDL_RenderCopy,
SDL_RenderPresent,
SDL_SetHint,
SDL_SetRenderDrawBlendMode,
SDL_SetRenderDrawColor,
SDL_SetWindowOpacity,
SDL_ShowSimpleMessageBox,
)
from sdl2.sdlimage import IMG_Load
import sdl3

install_directory: Path = Path(__file__).resolve().parent
sys.path.append(str(install_directory.parent))
Expand Down Expand Up @@ -276,11 +240,10 @@ def transfer_args_and_exit() -> None:
if sys.platform == "win32" and sys.getwindowsversion().major < 10 or Path(user_directory / "nohidpi").is_file():
allow_hidpi = False

SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, b"1")
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, b"1")
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, b"0")
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best".encode())
# SDL_SetHint(b"SDL_VIDEO_WAYLAND_ALLOW_LIBDECOR", b"0")
sdl3.SDL_SetHint(sdl3.SDL_HINT_VIDEO_ALLOW_SCREENSAVER.encode(), b"1")
sdl3.SDL_SetHint(sdl3.SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH.encode(), b"1")
sdl3.SDL_SetHint(sdl3.SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR.encode(), b"0")
# sdl3.SDL_SetHint(b"SDL_VIDEO_WAYLAND_ALLOW_LIBDECOR", b"0")

draw_border = True
w = 1120
Expand Down Expand Up @@ -347,45 +310,43 @@ def transfer_args_and_exit() -> None:
if Path(user_directory / "x11").exists():
os.environ["SDL_VIDEODRIVER"] = "x11"

SDL_Init(SDL_INIT_VIDEO)
err = SDL_GetError()

sdl3.SDL_Init(sdl3.SDL_INIT_VIDEO)
err = sdl3.SDL_GetError()
if err and "GLX" in err.decode():
logging.error(f"SDL init error: {err.decode()}")
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, b"Tauon Music Box failed to start :(",
sdl3.SDL_ShowSimpleMessageBox(
sdl3.SDL_MESSAGEBOX_ERROR, b"Tauon Music Box failed to start :(",
b"Error: " + err + b".\n If you're using Flatpak, try run `$ flatpak update`", None)
sys.exit(1)

window_title = t_title
window_title = window_title.encode("utf-8")

flags = SDL_WINDOW_RESIZABLE

if allow_hidpi:
flags |= SDL_WINDOW_ALLOW_HIGHDPI
flags = sdl3.SDL_WINDOW_RESIZABLE | sdl3.SDL_WINDOW_TRANSPARENT

if draw_border and not fs_mode:
flags |= SDL_WINDOW_BORDERLESS
flags |= sdl3.SDL_WINDOW_BORDERLESS

if fs_mode:
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP
flags |= sdl3.SDL_WINDOW_FULLSCREEN_DESKTOP

if old_window_position is None:
o_x = SDL_WINDOWPOS_UNDEFINED
o_y = SDL_WINDOWPOS_UNDEFINED
o_x = sdl3.SDL_WINDOWPOS_UNDEFINED
o_y = sdl3.SDL_WINDOWPOS_UNDEFINED
else:
o_x = old_window_position[0]
o_y = old_window_position[1]

if "--tray" in sys.argv:
flags |= SDL_WINDOW_HIDDEN
flags |= sdl3.SDL_WINDOW_HIDDEN


t_window = SDL_CreateWindow(
t_window = sdl3.SDL_CreateWindow( # todo use SDL_CreateWindowAndRenderer()
window_title,
o_x, o_y,
# o_x, o_y,
logical_size[0], logical_size[1],
flags) # | SDL_WINDOW_FULLSCREEN)
flags)

if not t_window:
logging.error("ERROR CREATING WINDOW!")
Expand All @@ -395,32 +356,44 @@ def transfer_args_and_exit() -> None:
logging.error(f"Size 0: {logical_size[0]}")
logging.error(f"Size 1: {logical_size[1]}")
logging.error(f"Flags: {flags}")
logging.error(f"SDL Error: {SDL_GetError()}")
logging.error(f"SDL Error: {sdl3.SDL_GetError()}")
sys.exit(1)

if maximized:
SDL_MaximizeWindow(t_window)
sdl3.SDL_MaximizeWindow(t_window)

drivers = []
i = 0
while True:
x = sdl3.SDL_GetRenderDriver(i)
i += 1
if x is None:
break
drivers.append(x)

logging.debug(f"SDL availiable drivers: {drivers}")

renderer = SDL_CreateRenderer(t_window, 0, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC)
renderer = sdl3.SDL_CreateRenderer(t_window, b"opengl") # sdl3.SDL_RENDERER_PRESENTVSYNC

if not renderer:
logging.error("ERROR CREATING RENDERER!")
sys.exit(1)

SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND)
SDL_SetWindowOpacity(t_window, window_opacity)
sdl3.SDL_SetRenderDrawBlendMode(renderer, sdl3.SDL_BLENDMODE_BLEND)
sdl3.SDL_SetWindowOpacity(t_window, window_opacity)

SDL_SetRenderDrawColor(renderer, 7, 7, 7, 255)
SDL_RenderClear(renderer)
sdl3.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0)
sdl3.SDL_RenderClear(renderer)

logging.info(f"SDL window system: {sdl3.SDL_GetCurrentVideoDriver().decode()}")

i_x = pointer(c_int(0))
i_y = pointer(c_int(0))
SDL_GL_GetDrawableSize(t_window, i_x, i_y)
sdl3.SDL_GetWindowSizeInPixels(t_window, i_x, i_y)
window_size[0] = i_x.contents.value
window_size[1] = i_y.contents.value

SDL_GetWindowSize(t_window, i_x, i_y)
sdl3.SDL_GetWindowSize(t_window, i_x, i_y)
logical_size[0] = i_x.contents.value
logical_size[1] = i_y.contents.value

Expand All @@ -434,18 +407,26 @@ def transfer_args_and_exit() -> None:
img_path = img_path2
del img_path2

raw_image = IMG_Load(str(img_path).encode())
sdl_texture = SDL_CreateTextureFromSurface(renderer, raw_image)
w = raw_image.contents.w
h = raw_image.contents.h
rect = SDL_Rect(window_size[0] // 2 - w // 2, window_size[1] // 2 - h // 2, w, h)
sdl3.SDL_SetRenderDrawColor(renderer, 7, 7, 7, 255)
sdl3.SDL_RenderFillRect(renderer, None)

SDL_RenderCopy(renderer, sdl_texture, None, rect)
raw_image = sdl3.IMG_Load(str(img_path).encode())
texture = sdl3.SDL_CreateTextureFromSurface(renderer, raw_image)
i_x = c_float(0.0)
i_y = c_float(0.0)
sdl3.SDL_GetTextureSize(texture, byref(i_x), byref(i_y))
w = i_x.value
h = i_y.value
rect = sdl3.SDL_FRect(window_size[0] // 2 - w // 2, window_size[1] // 2 - h // 2, w, h)
sdl3.SDL_RenderTexture(renderer, texture, None, rect)

SDL_RenderPresent(renderer)

SDL_FreeSurface(raw_image)
SDL_DestroyTexture(sdl_texture)


sdl3.SDL_RenderPresent(renderer)

sdl3.SDL_DestroySurface(raw_image)
sdl3.SDL_DestroyTexture(texture)

holder = t_bootstrap.holder
holder.t_window = t_window
Expand Down Expand Up @@ -474,13 +455,19 @@ def transfer_args_and_exit() -> None:
holder.log = log

del raw_image
del sdl_texture
del texture
del w
del h
del rect
del flags
del img_path


# todo temporary, remove me
import time
time.sleep(3)
exit()

from tauon.t_modules import t_main

# if pyinstaller_mode or sys.platform == "darwin" or install_mode:
Expand Down
28 changes: 1 addition & 27 deletions src/tauon/t_modules/t_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,7 @@
from typing import TYPE_CHECKING

from PIL import Image
from sdl2 import (
SDL_BLENDFACTOR_ONE,
SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SDL_BLENDOPERATION_ADD,
SDL_FLIP_VERTICAL,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_BGR24,
SDL_PIXELFORMAT_RGB888,
SDL_ComposeCustomBlendMode,
SDL_CreateRGBSurfaceWithFormatFrom,
SDL_CreateTextureFromSurface,
SDL_DestroyTexture,
SDL_FreeSurface,
SDL_MapRGB,
SDL_QueryTexture,
SDL_Rect,
SDL_RenderCopy,
SDL_RenderCopyEx,
SDL_RenderDrawLine,
SDL_RenderFillRect,
SDL_RenderReadPixels,
SDL_SetColorKey,
SDL_SetRenderDrawColor,
SDL_SetTextureBlendMode,
rw_from_object,
)
from sdl2.sdlimage import IMG_Load_RW
import sdl3

from tauon.t_modules.t_extra import Timer, alpha_blend, coll_rect

Expand Down
Loading

0 comments on commit b10beca

Please sign in to comment.