Created
December 29, 2014 22:06
-
-
Save tomassedovic/950eac1ac439d8b37946 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a repro case in Rust and a (rough) equivalent in C for this issue: | |
https://github.com/tomassedovic/tcod-rs/issues/54 | |
Download `libtcod-1.5.2-mingw32.tar.gz` from `http://roguecentral.org/doryen/libtcod/download/` and unpack `SDL.dll`, `libtcod-mingw.dll` and `terminal.png` to the same directory as the `key.rs` and `key.c` files. And you'll probably have to create a copy of `libtcod-mingw.dll` called `libtcod.dll`. | |
To build & run the Rust code: | |
rustc key.rs -o key_rs && key_rs | |
Build & run the C code: | |
mingw32-gcc key.c -ltcod -L . -o key_c && key_c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
typedef char bool; | |
typedef enum { | |
TCOD_RENDERER_GLSL, | |
TCOD_RENDERER_OPENGL, | |
TCOD_RENDERER_SDL, | |
TCOD_NB_RENDERERS, | |
} TCOD_renderer_t; | |
typedef struct { | |
int vk; | |
char c; | |
bool pressed; | |
bool lalt; | |
bool lctrl; | |
bool ralt; | |
bool rctrl; | |
bool shift; | |
} TCOD_key_t; | |
void TCOD_console_init_root(int w, int h, const char * title, bool fullscreen, TCOD_renderer_t renderer); | |
bool TCOD_console_is_window_closed(); | |
void TCOD_console_flush(); | |
TCOD_key_t TCOD_console_wait_for_keypress(bool flush); | |
int main() { | |
TCOD_console_init_root(80, 50, "tcod window", 0, 2); | |
while (TCOD_console_is_window_closed() == 0) { | |
TCOD_console_flush(); | |
TCOD_key_t key = TCOD_console_wait_for_keypress(1); | |
char* c = (char*)&key; | |
int i; | |
printf("TCOD_key_t:\n"); | |
for(i = 0; i < 12; i++) { | |
printf("%d, ", c[i]); | |
} | |
printf("\n"); | |
// NOTE: the `vk` value appears to be hidding negative four bytes from | |
// the beginning of the `TCOD_key_t` struct: | |
printf("TCOD_key_t - 4 bytes:\n"); | |
for(i = -4; i < 8; i++) { | |
printf("%d, ", c[i]); | |
} | |
printf("\n---\n"); | |
} | |
return(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate libc; | |
use libc::{c_int, c_uint, c_char, c_uchar}; | |
#[link(name = "tcod")] | |
extern "C" { | |
pub fn TCOD_console_init_root(w: c_int, h: c_int, | |
title: *const c_char, | |
fullscreen: c_uchar, | |
renderer: c_uint); | |
pub fn TCOD_console_is_window_closed() -> c_uchar; | |
pub fn TCOD_console_flush(); | |
pub fn TCOD_console_wait_for_keypress(flush: c_uchar) -> TCOD_key_t; | |
} | |
#[deriving(Show, Copy)] | |
#[repr(C)] | |
pub struct TCOD_key_t { | |
// NOTE: if you comment `vk` out, you stop getting bogus values: | |
pub vk: c_int, | |
pub c: c_char, | |
pub pressed: c_uchar, | |
pub lalt: c_uchar, | |
pub lctrl: c_uchar, | |
pub ralt: c_uchar, | |
pub rctrl: c_uchar, | |
pub shift: c_uchar, | |
} | |
fn main() { | |
unsafe { | |
"tcod window".with_c_str(|s| TCOD_console_init_root(80, 50, s, 0, 2)); | |
while TCOD_console_is_window_closed() == 0 { | |
TCOD_console_flush(); | |
let key = TCOD_console_wait_for_keypress(1); | |
// NOTE: if you comment this out, weird stuf happens (segfaults or | |
// always returning the same values for key no matter what's pressed): | |
println!("wtf?"); | |
println!("k: {}", key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment