Add window icons for non-Windows systems (#206)

This commit is contained in:
biroder 2023-04-01 21:18:25 +03:00 committed by GitHub
parent f99b452073
commit 11eafb8d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 10 deletions

View File

@ -78,7 +78,7 @@ num-traits = "0.2"
open = "3.2"
paste = "1.0"
pelite = { version = ">=0.9.2", default-features = false, features = ["std"] }
sdl2 = { git = "https://github.com/doukutsu-rs/rust-sdl2.git", rev = "95bcf63768abf422527f86da41da910649b9fcc9", optional = true, features = ["unsafe_textures", "bundled", "static-link"] }
sdl2 = { git = "https://github.com/doukutsu-rs/rust-sdl2.git", rev = "95bcf63768abf422527f86da41da910649b9fcc9", optional = true, features = ["image", "unsafe_textures", "bundled", "static-link"] }
sdl2-sys = { git = "https://github.com/doukutsu-rs/rust-sdl2.git", rev = "95bcf63768abf422527f86da41da910649b9fcc9", optional = true, features = ["bundled", "static-link"] }
rc-box = "1.2.0"
serde = { version = "1", features = ["derive"] }

BIN
src/data/builtin/icon.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
src/data/builtin/icon2.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -102,6 +102,8 @@ impl BuiltinFS {
FSNode::File("builtin_font_0.png", include_bytes!("builtin/builtin_font_0.png")),
FSNode::File("builtin_font_1.png", include_bytes!("builtin/builtin_font_1.png")),
FSNode::File("gamecontrollerdb.txt", include_bytes!("builtin/gamecontrollerdb.txt")),
FSNode::File("icon.bmp", include_bytes!("builtin/icon.bmp")),
FSNode::File("icon2.bmp", include_bytes!("builtin/icon2.bmp")),
FSNode::File(
"organya-wavetable-doukutsu.bin",
include_bytes!("builtin/organya-wavetable-doukutsu.bin"),

View File

@ -1,20 +1,24 @@
use std::any::Any;
use std::cell::{RefCell, UnsafeCell};
use std::ffi::c_void;
use std::io::Read;
use std::mem;
use std::rc::Rc;
use std::sync::Arc;
use std::vec::Vec;
use glutin::event::{ElementState, Event, TouchPhase, VirtualKeyCode, WindowEvent};
use glutin::event_loop::{ControlFlow, EventLoop};
use glutin::window::WindowBuilder;
use glutin::{Api, ContextBuilder, GlProfile, GlRequest, PossiblyCurrent, WindowedContext};
use imgui::{DrawCmdParams, DrawData, DrawIdx, DrawVert};
use winit::window::Icon;
use crate::common::Rect;
use crate::framework::backend::{Backend, BackendEventLoop, BackendRenderer, BackendTexture, SpriteBatchCommand};
use crate::framework::context::Context;
use crate::framework::error::GameResult;
use crate::framework::filesystem;
use crate::framework::gl;
use crate::framework::keyboard::ScanCode;
use crate::framework::render_opengl::{GLContext, OpenGLRenderer};
@ -56,11 +60,12 @@ pub struct GlutinEventLoop {
}
impl GlutinEventLoop {
fn get_context(&self, event_loop: &EventLoop<()>) -> &mut WindowedContext<PossiblyCurrent> {
fn get_context(&self, ctx: &Context, event_loop: &EventLoop<()>) -> &mut WindowedContext<PossiblyCurrent> {
let mut refs = unsafe { &mut *self.refs.get() };
if refs.is_none() {
let mut window = WindowBuilder::new();
let windowed_context = ContextBuilder::new();
let windowed_context = windowed_context.with_gl(GlRequest::Specific(Api::OpenGl, (3, 0)));
#[cfg(target_os = "android")]
@ -79,7 +84,24 @@ impl GlutinEventLoop {
}
window = window.with_title("doukutsu-rs");
#[cfg(not(any(target_os = "windows", target_os = "android", target_os = "horizon")))]
{
let mut file = filesystem::open(&ctx, "/builtin/icon2.bmp").unwrap();
let mut buf: Vec<u8> = Vec::new();
file.read_to_end(&mut buf);
let mut img = match image::load_from_memory_with_format(buf.as_slice(), image::ImageFormat::Bmp) {
Ok(image) => image.into_rgba8(),
Err(e) => panic!("Cannot set window icon")
};
let (width, height) = img.dimensions();
let icon = Icon::from_rgba(img.into_raw(), width, height).unwrap();
window = window.with_window_icon(Some(icon));
}
let windowed_context = windowed_context.build_windowed(window, event_loop).unwrap();
let windowed_context = unsafe { windowed_context.make_current().unwrap() };
@ -154,8 +176,7 @@ impl BackendEventLoop for GlutinEventLoop {
let event_loop = EventLoop::new();
let state_ref = unsafe { &mut *game.state.get() };
let window: &'static mut WindowedContext<PossiblyCurrent> =
unsafe { std::mem::transmute(self.get_context(&event_loop)) };
unsafe { std::mem::transmute(self.get_context(&ctx, &event_loop)) };
{
let size = window.window().inner_size();
ctx.real_screen_size = (size.width, size.height);

View File

@ -2,10 +2,12 @@ use core::mem;
use std::any::Any;
use std::cell::{RefCell, UnsafeCell};
use std::ffi::c_void;
use std::io::Read;
use std::ops::Deref;
use std::ptr::{null, null_mut};
use std::rc::Rc;
use std::time::{Duration, Instant};
use std::vec::Vec;
use imgui::internal::RawWrapper;
use imgui::sys::{ImGuiKey_Backspace, ImGuiKey_Delete, ImGuiKey_Enter};
@ -16,6 +18,8 @@ use sdl2::keyboard::Scancode;
use sdl2::mouse::{Cursor, SystemCursor};
use sdl2::pixels::PixelFormatEnum;
use sdl2::render::{Texture, TextureCreator, TextureQuery, WindowCanvas};
use sdl2::rwops::RWops;
use sdl2::surface::Surface;
use sdl2::video::GLProfile;
use sdl2::video::Window;
use sdl2::video::WindowContext;
@ -168,14 +172,28 @@ impl SDL2EventLoop {
gl_attr.set_context_profile(GLProfile::Compatibility);
gl_attr.set_context_version(2, 1);
let mut window = video.window("(sr-ustukuod) yrotS evaR", size_hint.0 as _, size_hint.1 as _);
window.position_centered();
window.resizable();
let mut win_builder = video.window("(sr-ustukuod) yrotS evaR", size_hint.0 as _, size_hint.1 as _);
win_builder.position_centered();
win_builder.resizable();
#[cfg(feature = "render-opengl")]
window.opengl();
win_builder.opengl();
let window = window.build().map_err(|e| GameError::WindowError(e.to_string()))?;
let mut window = win_builder.build().map_err(|e| GameError::WindowError(e.to_string()))?;
#[cfg(not(any(target_os = "windows", target_os = "android", target_os = "horizon")))]
{
let mut file = filesystem::open(&ctx, "/builtin/icon2.bmp").unwrap();
let mut buf: Vec<u8> = Vec::new();
file.read_to_end(&mut buf)?;
let mut rwops = RWops::from_bytes(buf.as_slice()).unwrap();
let icon = Surface::load_bmp_rw(&mut rwops).unwrap();
window.set_icon(icon);
}
let opengl_available = if let Ok(v) = std::env::var("CAVESTORY_NO_OPENGL") { v != "1" } else { true };
let event_loop = SDL2EventLoop {