-
sdl3 v0.14.6, garuda linux I have some raw pixels, I make SDL surface out of them. then make texture out of them, then copy it to the screen. It works fine in debug mode but shows nothing in release. Equivalent(at least I think so) code in cpp works ok. Consider the code: use std::time::Duration;
use sdl3::pixels::Color;
use sdl3::{self, event::Event, surface::Surface, sys::pixels::SDL_PixelFormat};
use sdl3::render::{FRect, Texture};
fn main() {
let sdl_ctx = sdl3::init().unwrap();
let video = sdl_ctx.video().unwrap();
let window = video.window("test", 800, 600) .position_centered() .build() .unwrap();
let mut canvas = window.into_canvas();
let pixel_format = SDL_PixelFormat::RGBA8888.try_into().unwrap();
let mut data = [
127,127,127,255 , 255,0,0,255,
255,0,0,255 , 127,127,127,255
];
let surface = Surface::from_data(&mut data, 2, 2, 2*4, pixel_format).unwrap();
let texture_creator = canvas.texture_creator();
let texture = Texture::from_surface(&surface, &texture_creator).unwrap();
let mut event_pump = sdl_ctx.event_pump().unwrap();
'main_loop: loop {
for event in event_pump.poll_iter() {
if let Event::Quit { .. } = event {
break 'main_loop;
}
}
let dst = FRect::new( 0.0,0.0, 320.0, 320.0);
canvas.set_draw_color(Color::RGBA(0, 0, 30, 255));
canvas.clear();
canvas.copy(&texture, None, Some(dst)).unwrap();
canvas.present();
std::thread::sleep(Duration::from_millis(100));
}
} If I run it in debug mode (cargo run) it does work: However if I do it CPP version which I think should do the same does work: #include <SDL3/SDL.h>
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_surface.h>
#include <cstdlib>
SDL_Window* window;
SDL_Renderer* renderer;
void init()
{
// SDL initialisation
if (! SDL_Init(SDL_INIT_VIDEO)) { abort(); }
window = SDL_CreateWindow("test window", 1024, 768, 0);
if (! window) abort();
renderer = SDL_CreateRenderer(window, nullptr);
if (! renderer) abort();
}
int main()
{
bool quit=false;
init();
unsigned char bmp[] = {
255,255,255,255, 255,0,0,255,
255,0,0,255, 255,255,255,255
};
SDL_Surface* surf = SDL_CreateSurfaceFrom(2, 2, SDL_PixelFormat::SDL_PIXELFORMAT_RGBA8888, bmp, 2*4);
if (! surf) abort();
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surf);
if (! texture) abort();
while (! quit) {
SDL_Event ev;
while (SDL_PollEvent(&ev)){
if (ev.type == SDL_EVENT_QUIT) {
quit = true;
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 30, 255);
SDL_RenderClear(renderer);
SDL_FRect dst { 0.0,0.0, 320.0, 320.0};
SDL_RenderTexture(renderer, texture, nullptr, &dst);
SDL_RenderPresent(renderer);
SDL_Delay(100);
}
SDL_Quit();
return 0;
} I tried as using system SDL, and built it from scratch, ie #sdl3 = { version = "0.14.6", features = ["build-from-source-static"] }
sdl3 = { version = "0.14.6" } So I'm not sure if it's my code buggy but it's not seen in debug(which doesn't optimize much) and in C++ version(which is unsafe to begin with and can read from where it shouldn't) or release version optimizes too much. ETA: Just for the test I've also ported C++ to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
OK, it's interesting, it seems issue in pub fn baggy_copy(canvas: &mut Canvas<Window>, texture: &Texture, src: Option<FRect>, dst: Option<FRect>) -> Result<(), String>
{
let ret = unsafe {
let renderer = canvas.deref().raw();
let mut real_dest: SDL_FRect = Default::default();
SDL_RenderTexture(
renderer,
texture.raw(),
match src {
Some(ref rect) => &rect.to_ll(),
None => std::ptr::null(),
},
match dst {
Some(ref rect) => {
real_dest = rect.to_ll();
&real_dest
//&rect.to_ll()
},
None => std::ptr::null(),
},
)
};
if !ret {
Err("meow".to_string())
} else {
Ok(())
}
} it works. Notice that in sdl2-rs It's funny that when I was writing C++ program I had the same warning: (I'll probably rewrite this post into issue for tracking purpose if it wasn't reported already, but now it's late here) |
Beta Was this translation helpful? Give feedback.
#68 Ok, found it