So I'm trying to port a simple 2D game engine written in C++ that uses the windows console text buffer to draw objects using characters. I got the thing to work right up until the last char is drawn, but it crashes soon after.
It seems like there is a continued attempt to read from the text buffer even after it has gotten to the end, or at least that is what I think is going on. The example I'm following places an escape character at the end of the buffer, '\0'
. Something to do with c arrays being null-terminated or something like that, I don't really know any C but I did a little research into it. Rust doesn't seem to have that same requirements with its own arrays though. Is it because I'm assigning a c type from winapi?
I'd love it if someone could take a peek at my code and shed some light on what may be going on.
Summary
let buff_width = 2;
let buff_height = 2;
let buff_coord = wincontypes::COORD {
X: 0,
Y: 0,
};
let mut window_buffer: Vec<ctypes::wchar_t> = vec!['*' as u16; buff_width * buff_height];
// let mut window_buffer: Vec<winnt::LPCWSTR> = vec![0 as *const u16; buff_width * buff_height];
// let mut window_buffer: Vec<winnt::LPCWSTR> = vec![0 as *const u16; buff_width * buff_height];
let dwBytesWritten = 0 as *mut u32;
// window_buffer[buff_width * buff_height - 1] = '\0' as u16;
unsafe {
let hconsole = wincon::CreateConsoleScreenBuffer(winnt::GENERIC_READ | winnt::GENERIC_WRITE, 0, ptr::null(), wincon::CONSOLE_TEXTMODE_BUFFER, ntdef::NULL);
wincon::SetConsoleActiveScreenBuffer(hconsole);
//code crashes from this point on
wincon::WriteConsoleOutputCharacterW(hconsole, window_buffer.as_ptr(), 2 * 2, buff_coord, dwBytesWritten);
}
The code I'm trying to reproduce is from this video here: Code-It-Yourself! First Person Shooter (Quick and Simple C++) - YouTube