Add some examples for the new features (#21)
* Added some examples * changed window names
This commit is contained in:
parent
21d9377822
commit
e679c38955
@ -57,9 +57,23 @@ add_library(minifb STATIC
|
|||||||
add_executable(noise
|
add_executable(noise
|
||||||
tests/noise.c
|
tests/noise.c
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(noise minifb)
|
target_link_libraries(noise minifb)
|
||||||
|
|
||||||
|
add_executable(input_events
|
||||||
|
tests/input_events.c
|
||||||
|
)
|
||||||
|
target_link_libraries(input_events minifb)
|
||||||
|
|
||||||
|
add_executable(input_events_cpp
|
||||||
|
tests/input_events_cpp.cpp
|
||||||
|
)
|
||||||
|
target_link_libraries(input_events_cpp minifb)
|
||||||
|
|
||||||
|
add_executable(multiple_windows
|
||||||
|
tests/multiple_windows.c
|
||||||
|
)
|
||||||
|
target_link_libraries(multiple_windows minifb)
|
||||||
|
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
elseif (MINGW)
|
elseif (MINGW)
|
||||||
elseif (APPLE)
|
elseif (APPLE)
|
||||||
@ -67,10 +81,34 @@ elseif (APPLE)
|
|||||||
target_link_libraries(noise "-framework QuartzCore")
|
target_link_libraries(noise "-framework QuartzCore")
|
||||||
target_link_libraries(noise "-framework Metal")
|
target_link_libraries(noise "-framework Metal")
|
||||||
target_link_libraries(noise "-framework MetalKit")
|
target_link_libraries(noise "-framework MetalKit")
|
||||||
|
|
||||||
|
target_link_libraries(input_events "-framework Cocoa")
|
||||||
|
target_link_libraries(input_events "-framework QuartzCore")
|
||||||
|
target_link_libraries(input_events "-framework Metal")
|
||||||
|
target_link_libraries(input_events "-framework MetalKit")
|
||||||
|
target_link_libraries(input_events_cpp "-framework Cocoa")
|
||||||
|
target_link_libraries(input_events_cpp "-framework QuartzCore")
|
||||||
|
target_link_libraries(input_events_cpp "-framework Metal")
|
||||||
|
target_link_libraries(input_events_cpp "-framework MetalKit")
|
||||||
|
|
||||||
|
target_link_libraries(multiple_windows "-framework Cocoa")
|
||||||
|
target_link_libraries(multiple_windows "-framework QuartzCore")
|
||||||
|
target_link_libraries(multiple_windows "-framework Metal")
|
||||||
|
target_link_libraries(multiple_windows "-framework MetalKit")
|
||||||
elseif (UNIX)
|
elseif (UNIX)
|
||||||
if(USE_WAYLAND_API)
|
if(USE_WAYLAND_API)
|
||||||
target_link_libraries(noise -lwayland-client -lwayland-cursor)
|
target_link_libraries(noise -lwayland-client -lwayland-cursor)
|
||||||
|
|
||||||
|
target_link_libraries(input_events -lwayland-client -lwayland-cursor)
|
||||||
|
target_link_libraries(input_events_cpp -lwayland-client -lwayland-cursor)
|
||||||
|
|
||||||
|
target_link_libraries(multiple_windows -lwayland-client -lwayland-cursor)
|
||||||
else()
|
else()
|
||||||
target_link_libraries(noise -lX11)
|
target_link_libraries(noise -lX11)
|
||||||
|
|
||||||
|
target_link_libraries(input_events -lX11)
|
||||||
|
target_link_libraries(input_events_cpp -lX11)
|
||||||
|
|
||||||
|
target_link_libraries(multiple_windows -lX11)
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
133
tests/input_events.c
Normal file
133
tests/input_events.c
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
#include <MiniFB.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define kUnused(var) (void) var;
|
||||||
|
|
||||||
|
#define WIDTH 800
|
||||||
|
#define HEIGHT 600
|
||||||
|
static unsigned int g_buffer[WIDTH * HEIGHT];
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void active(struct Window *window, bool isActive) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(struct Window *window, int width, int height) {
|
||||||
|
uint32_t x = 0;
|
||||||
|
uint32_t y = 0;
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
|
||||||
|
if(width > WIDTH) {
|
||||||
|
x = (width - WIDTH) >> 1;
|
||||||
|
width = WIDTH;
|
||||||
|
}
|
||||||
|
if(height > HEIGHT) {
|
||||||
|
y = (height - HEIGHT) >> 1;
|
||||||
|
height = HEIGHT;
|
||||||
|
}
|
||||||
|
mfb_set_viewport(window, x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [KeyMod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
|
||||||
|
if(key == KB_KEY_ESCAPE) {
|
||||||
|
mfb_close(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void char_input(struct Window *window, unsigned int charCode) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [KeyMod: %x]\n", window_title, button, isPressed, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_move(struct Window *window, int x, int y) {
|
||||||
|
kUnused(window);
|
||||||
|
kUnused(x);
|
||||||
|
kUnused(y);
|
||||||
|
// const char *window_title = "";
|
||||||
|
// if(window) {
|
||||||
|
// window_t(const char *) itle = mfb_get_user_data(window);
|
||||||
|
// }
|
||||||
|
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [KeyMod: %x]\n", window_title, deltaX, deltaY, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int noise, carry, seed = 0xbeef;
|
||||||
|
|
||||||
|
struct Window *window = mfb_open_ex("Input Events Test", WIDTH, HEIGHT, WF_RESIZABLE);
|
||||||
|
if (!window)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
mfb_active_callback(window, active);
|
||||||
|
mfb_resize_callback(window, resize);
|
||||||
|
mfb_keyboard_callback(window, keyboard);
|
||||||
|
mfb_char_input_callback(window, char_input);
|
||||||
|
mfb_mouse_button_callback(window, mouse_btn);
|
||||||
|
mfb_mouse_move_callback(window, mouse_move);
|
||||||
|
mfb_mouse_scroll_callback(window, mouse_scroll);
|
||||||
|
|
||||||
|
mfb_set_user_data(window, (void *) "Input Events Test");
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
UpdateState state;
|
||||||
|
|
||||||
|
for (i = 0; i < WIDTH * HEIGHT; ++i)
|
||||||
|
{
|
||||||
|
noise = seed;
|
||||||
|
noise >>= 3;
|
||||||
|
noise ^= seed;
|
||||||
|
carry = noise & 1;
|
||||||
|
noise >>= 1;
|
||||||
|
seed >>= 1;
|
||||||
|
seed |= (carry << 30);
|
||||||
|
noise &= 0xFF;
|
||||||
|
g_buffer[i] = MFB_RGB(noise, noise, noise);
|
||||||
|
}
|
||||||
|
|
||||||
|
state = mfb_update(window, g_buffer);
|
||||||
|
if (state != STATE_OK) {
|
||||||
|
window = 0x0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
138
tests/input_events_cpp.cpp
Normal file
138
tests/input_events_cpp.cpp
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
#include <MiniFB.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define kUnused(var) (void) var;
|
||||||
|
|
||||||
|
#define WIDTH 800
|
||||||
|
#define HEIGHT 600
|
||||||
|
static unsigned int g_buffer[WIDTH * HEIGHT];
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class Events {
|
||||||
|
public:
|
||||||
|
void active(struct Window *window, bool isActive) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(struct Window *window, int width, int height) {
|
||||||
|
uint32_t x = 0;
|
||||||
|
uint32_t y = 0;
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
|
||||||
|
if(width > WIDTH) {
|
||||||
|
x = (width - WIDTH) >> 1;
|
||||||
|
width = WIDTH;
|
||||||
|
}
|
||||||
|
if(height > HEIGHT) {
|
||||||
|
y = (height - HEIGHT) >> 1;
|
||||||
|
height = HEIGHT;
|
||||||
|
}
|
||||||
|
mfb_set_viewport(window, x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [KeyMod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
|
||||||
|
if(key == KB_KEY_ESCAPE) {
|
||||||
|
mfb_close(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void char_input(struct Window *window, unsigned int charCode) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [KeyMod: %x]\n", window_title, button, isPressed, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_move(struct Window *window, int x, int y) {
|
||||||
|
kUnused(window);
|
||||||
|
kUnused(x);
|
||||||
|
kUnused(y);
|
||||||
|
// const char *window_title = "";
|
||||||
|
// if(window) {
|
||||||
|
// window_t(const char *) itle = mfb_get_user_data(window);
|
||||||
|
// }
|
||||||
|
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [KeyMod: %x]\n", window_title, deltaX, deltaY, mod);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int noise, carry, seed = 0xbeef;
|
||||||
|
|
||||||
|
struct Window *window = mfb_open_ex("Input Events CPP Test", WIDTH, HEIGHT, WF_RESIZABLE);
|
||||||
|
if (!window)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
Events e;
|
||||||
|
|
||||||
|
mfb_active_callback(window, &e, &Events::active);
|
||||||
|
mfb_resize_callback(window, &e, &Events::resize);
|
||||||
|
mfb_keyboard_callback(window, &e, &Events::keyboard);
|
||||||
|
mfb_char_input_callback(window, &e, &Events::char_input);
|
||||||
|
mfb_mouse_button_callback(window, &e, &Events::mouse_btn);
|
||||||
|
mfb_mouse_move_callback(window, &e, &Events::mouse_move);
|
||||||
|
mfb_mouse_scroll_callback(window, &e, &Events::mouse_scroll);
|
||||||
|
|
||||||
|
mfb_set_user_data(window, (void *) "Input Events CPP Test");
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
UpdateState state;
|
||||||
|
|
||||||
|
for (i = 0; i < WIDTH * HEIGHT; ++i)
|
||||||
|
{
|
||||||
|
noise = seed;
|
||||||
|
noise >>= 3;
|
||||||
|
noise ^= seed;
|
||||||
|
carry = noise & 1;
|
||||||
|
noise >>= 1;
|
||||||
|
seed >>= 1;
|
||||||
|
seed |= (carry << 30);
|
||||||
|
noise &= 0xFF;
|
||||||
|
g_buffer[i] = MFB_RGB(noise, noise, noise);
|
||||||
|
}
|
||||||
|
|
||||||
|
state = mfb_update(window, g_buffer);
|
||||||
|
if (state != STATE_OK) {
|
||||||
|
window = 0x0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
186
tests/multiple_windows.c
Normal file
186
tests/multiple_windows.c
Normal file
@ -0,0 +1,186 @@
|
|||||||
|
#include <MiniFB.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define kUnused(var) (void) var;
|
||||||
|
|
||||||
|
#define WIDTH_A 800
|
||||||
|
#define HEIGHT_A 600
|
||||||
|
static unsigned int g_buffer_a[WIDTH_A * HEIGHT_A];
|
||||||
|
|
||||||
|
#define WIDTH_B 320
|
||||||
|
#define HEIGHT_B 240
|
||||||
|
static unsigned int g_buffer_b[WIDTH_B * HEIGHT_B];
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void active(struct Window *window, bool isActive) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > active: %d\n", window_title, isActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
void resize(struct Window *window, int width, int height) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stdout, "%s > resize: %d, %d\n", window_title, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > keyboard: key: %s (pressed: %d) [KeyMod: %x]\n", window_title, mfb_get_key_name(key), isPressed, mod);
|
||||||
|
if(key == KB_KEY_ESCAPE) {
|
||||||
|
mfb_close(window);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void char_input(struct Window *window, unsigned int charCode) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > charCode: %d\n", window_title, charCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > mouse_btn: button: %d (pressed: %d) [KeyMod: %x]\n", window_title, button, isPressed, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_move(struct Window *window, int x, int y) {
|
||||||
|
kUnused(window);
|
||||||
|
kUnused(x);
|
||||||
|
kUnused(y);
|
||||||
|
// const char *window_title = "";
|
||||||
|
// if(window) {
|
||||||
|
// window_t(const char *) itle = mfb_get_user_data(window);
|
||||||
|
// }
|
||||||
|
//fprintf(stdout, "%s > mouse_move: %d, %d\n", window_title, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
||||||
|
const char *window_title = "";
|
||||||
|
if(window) {
|
||||||
|
window_title = (const char *) mfb_get_user_data(window);
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%s > mouse_scroll: x: %f, y: %f [KeyMod: %x]\n", window_title, deltaX, deltaY, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int noise, carry, seed = 0xbeef;
|
||||||
|
|
||||||
|
struct Window *window_a = mfb_open_ex("Multiple Windows Test", WIDTH_A, HEIGHT_A, WF_RESIZABLE);
|
||||||
|
if (!window_a)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
mfb_active_callback(window_a, active);
|
||||||
|
mfb_resize_callback(window_a, resize);
|
||||||
|
mfb_keyboard_callback(window_a, keyboard);
|
||||||
|
mfb_char_input_callback(window_a, char_input);
|
||||||
|
mfb_mouse_button_callback(window_a, mouse_btn);
|
||||||
|
mfb_mouse_move_callback(window_a, mouse_move);
|
||||||
|
mfb_mouse_scroll_callback(window_a, mouse_scroll);
|
||||||
|
|
||||||
|
mfb_set_user_data(window_a, (void *) "Window A");
|
||||||
|
|
||||||
|
//--
|
||||||
|
struct Window *window_b = mfb_open_ex("Secondary Window", WIDTH_B, HEIGHT_B, WF_RESIZABLE);
|
||||||
|
if (!window_b)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
mfb_active_callback(window_b, active);
|
||||||
|
mfb_resize_callback(window_b, resize);
|
||||||
|
mfb_keyboard_callback(window_b, keyboard);
|
||||||
|
mfb_char_input_callback(window_b, char_input);
|
||||||
|
mfb_mouse_button_callback(window_b, mouse_btn);
|
||||||
|
mfb_mouse_move_callback(window_b, mouse_move);
|
||||||
|
mfb_mouse_scroll_callback(window_b, mouse_scroll);
|
||||||
|
|
||||||
|
mfb_set_user_data(window_b, (void *) "Window B");
|
||||||
|
|
||||||
|
// Generate pallete for plasma effect
|
||||||
|
uint32_t pallete[512];
|
||||||
|
float inc = 90.0f / 64.0f;
|
||||||
|
for(uint32_t c=0; c<64; ++c) {
|
||||||
|
int32_t col = (255.0f * sinf(c * inc * M_PI / 180.0f)) + 0.5f;
|
||||||
|
pallete[64*0 + c] = MFB_RGB(col, 0, 0);
|
||||||
|
pallete[64*1 + c] = MFB_RGB(255, col, 0);
|
||||||
|
pallete[64*2 + c] = MFB_RGB(255-col, 255, 0);
|
||||||
|
pallete[64*3 + c] = MFB_RGB(0, 255, col);
|
||||||
|
pallete[64*4 + c] = MFB_RGB(0, 255-col, 255);
|
||||||
|
pallete[64*5 + c] = MFB_RGB(col, 0, 255);
|
||||||
|
pallete[64*6 + c] = MFB_RGB(255, 0, 255-col);
|
||||||
|
pallete[64*7 + c] = MFB_RGB(255-col, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--
|
||||||
|
float time = 0;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
int i, x, y;
|
||||||
|
float dx, dy, time_x, time_y;
|
||||||
|
int index;
|
||||||
|
UpdateState state_a, state_b;
|
||||||
|
|
||||||
|
for (i = 0; i < WIDTH_A * HEIGHT_A; ++i)
|
||||||
|
{
|
||||||
|
noise = seed;
|
||||||
|
noise >>= 3;
|
||||||
|
noise ^= seed;
|
||||||
|
carry = noise & 1;
|
||||||
|
noise >>= 1;
|
||||||
|
seed >>= 1;
|
||||||
|
seed |= (carry << 30);
|
||||||
|
noise &= 0xFF;
|
||||||
|
g_buffer_a[i] = MFB_RGB(noise, noise, noise);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--
|
||||||
|
time_x = sinf(time * M_PI / 180.0f);
|
||||||
|
time_y = cosf(time * M_PI / 180.0f);
|
||||||
|
i = 0;
|
||||||
|
for(y=0; y<HEIGHT_B; ++y) {
|
||||||
|
dy = cosf((y * time_y) * M_PI / 180.0f); // [-1, 1]
|
||||||
|
for(x=0; x<WIDTH_B; ++x) {
|
||||||
|
dx = sinf((x * time_x) * M_PI / 180.0f); // [-1, 1]
|
||||||
|
|
||||||
|
index = (int) ((2.0f + dx + dy) * 0.25f * 511.0f); // [0, 511]
|
||||||
|
g_buffer_b[i++] = pallete[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
time += 0.1f;
|
||||||
|
|
||||||
|
//--
|
||||||
|
state_a = mfb_update(window_a, g_buffer_a);
|
||||||
|
if (state_a != STATE_OK) {
|
||||||
|
window_a = 0x0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--
|
||||||
|
state_b = mfb_update(window_b, g_buffer_b);
|
||||||
|
if (state_b != STATE_OK) {
|
||||||
|
window_b = 0x0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(window_a == 0x0 && window_b == 0x0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
318
tests/noise.c
318
tests/noise.c
@ -2,301 +2,26 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#define kUnused(var) (void) var;
|
#define WIDTH 800
|
||||||
|
#define HEIGHT 600
|
||||||
#define WIDTH_A 800
|
static unsigned int g_buffer[WIDTH * HEIGHT];
|
||||||
#define HEIGHT_A 600
|
|
||||||
static unsigned int g_buffer1[WIDTH_A * HEIGHT_A];
|
|
||||||
|
|
||||||
#define WIDTH_B 240
|
|
||||||
#define HEIGHT_B 120
|
|
||||||
static unsigned int g_buffer2[WIDTH_B * HEIGHT_B];
|
|
||||||
|
|
||||||
//-------------------------------------
|
|
||||||
// C interface
|
|
||||||
//-------------------------------------
|
|
||||||
void active(struct Window *window, bool isActive) {
|
|
||||||
int id = 0;
|
|
||||||
if(window) {
|
|
||||||
id = *(int *) mfb_get_user_data(window);
|
|
||||||
}
|
|
||||||
fprintf(stdout, "active %d: %d\n", id, isActive);
|
|
||||||
}
|
|
||||||
|
|
||||||
void resize(struct Window *window, int width, int height) {
|
|
||||||
uint32_t x = 0;
|
|
||||||
uint32_t y = 0;
|
|
||||||
int id = 0;
|
|
||||||
if(window) {
|
|
||||||
id = *(int *) mfb_get_user_data(window);
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stdout, "resize %d: %d, %d\n", id, width, height);
|
|
||||||
if(width > WIDTH_A) {
|
|
||||||
x = (width - WIDTH_A) >> 1;
|
|
||||||
width = WIDTH_A;
|
|
||||||
}
|
|
||||||
if(height > HEIGHT_A) {
|
|
||||||
y = (height - HEIGHT_A) >> 1;
|
|
||||||
height = HEIGHT_A;
|
|
||||||
}
|
|
||||||
mfb_set_viewport(window, x, y, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
|
||||||
int id = 0;
|
|
||||||
if(window) {
|
|
||||||
id = *(int *) mfb_get_user_data(window);
|
|
||||||
}
|
|
||||||
fprintf(stdout, "keyboard %d: key: %s (pressed: %d) [KeyMod: %x]\n", id, mfb_get_key_name(key), isPressed, mod);
|
|
||||||
if(key == KB_KEY_ESCAPE) {
|
|
||||||
mfb_close(window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void char_input(struct Window *window, unsigned int charCode) {
|
|
||||||
int id = 0;
|
|
||||||
if(window) {
|
|
||||||
id = *(int *) mfb_get_user_data(window);
|
|
||||||
}
|
|
||||||
fprintf(stdout, "charCode %d: %d\n", id, charCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
|
||||||
int id = 0;
|
|
||||||
if(window) {
|
|
||||||
id = *(int *) mfb_get_user_data(window);
|
|
||||||
}
|
|
||||||
fprintf(stdout, "mouse_btn %d: button: %d (pressed: %d) [KeyMod: %x]\n", id, button, isPressed, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_move(struct Window *window, int x, int y) {
|
|
||||||
kUnused(window);
|
|
||||||
kUnused(x);
|
|
||||||
kUnused(y);
|
|
||||||
// int id = 0;
|
|
||||||
// if(window) {
|
|
||||||
// id = *(int *) mfb_get_user_data(window);
|
|
||||||
// }
|
|
||||||
//fprintf(stdout, "mouse_move %d: %d, %d\n", id, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
|
||||||
int id = 0;
|
|
||||||
if(window) {
|
|
||||||
id = *(int *) mfb_get_user_data(window);
|
|
||||||
}
|
|
||||||
fprintf(stdout, "mouse_scroll %d: x: %f, y: %f [KeyMod: %x]\n", id, deltaX, deltaY, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
//--
|
|
||||||
void active2(struct Window *window, bool isActive) {
|
|
||||||
kUnused(window);
|
|
||||||
fprintf(stdout, "active 2: %d\n", isActive);
|
|
||||||
}
|
|
||||||
|
|
||||||
void resize2(struct Window *window, int width, int height) {
|
|
||||||
uint32_t x = 0;
|
|
||||||
uint32_t y = 0;
|
|
||||||
|
|
||||||
fprintf(stdout, "resize 2: %d, %d\n", width, height);
|
|
||||||
if(width > WIDTH_A) {
|
|
||||||
x = (width - WIDTH_A) >> 1;
|
|
||||||
width = WIDTH_A;
|
|
||||||
}
|
|
||||||
if(height > HEIGHT_A) {
|
|
||||||
y = (height - HEIGHT_A) >> 1;
|
|
||||||
height = HEIGHT_A;
|
|
||||||
}
|
|
||||||
mfb_set_viewport(window, x, y, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void keyboard2(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
|
||||||
fprintf(stdout, "keyboard 2: key: %s (pressed: %d) [KeyMod: %x]\n", mfb_get_key_name(key), isPressed, mod);
|
|
||||||
if(key == KB_KEY_ESCAPE) {
|
|
||||||
mfb_close(window);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void char_input2(struct Window *window, unsigned int charCode) {
|
|
||||||
kUnused(window);
|
|
||||||
fprintf(stdout, "charCode 2: %d\n", charCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_btn2(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
|
||||||
kUnused(window);
|
|
||||||
fprintf(stdout, "mouse_btn 2: button: %d (pressed: %d) [KeyMod: %x]\n", button, isPressed, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_move2(struct Window *window, int x, int y) {
|
|
||||||
kUnused(window);
|
|
||||||
kUnused(x);
|
|
||||||
kUnused(y);
|
|
||||||
//fprintf(stdout, "mouse_move: %d, %d\n", x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_scroll2(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
|
||||||
kUnused(window);
|
|
||||||
fprintf(stdout, "mouse_scroll 2: x: %f, y: %f [KeyMod: %x]\n", deltaX, deltaY, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------------------
|
|
||||||
// C++ interface (calling C functions)
|
|
||||||
//-------------------------------------
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
|
|
||||||
class Events {
|
|
||||||
public:
|
|
||||||
void active(struct Window *window, bool isActive) {
|
|
||||||
printf("\nEvents 1 - ");
|
|
||||||
::active(window, isActive);
|
|
||||||
}
|
|
||||||
|
|
||||||
void resize(struct Window *window, int width, int height) {
|
|
||||||
printf("Events 1 - ");
|
|
||||||
::resize(window, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
|
||||||
printf("Events 1 - ");
|
|
||||||
::keyboard(window, key, mod, isPressed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void char_input(struct Window *window, unsigned int charCode) {
|
|
||||||
printf("Events 1 - ");
|
|
||||||
::char_input(window, charCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
|
||||||
printf("Events 1 - ");
|
|
||||||
::mouse_btn(window, button, mod, isPressed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_move(struct Window *window, int x, int y) {
|
|
||||||
//printf("Events 1 - ");
|
|
||||||
::mouse_move(window, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
|
||||||
printf("Events 1 - ");
|
|
||||||
::mouse_scroll(window, mod, deltaX, deltaY);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class Events2 {
|
|
||||||
public:
|
|
||||||
void active(struct Window *window, bool isActive) {
|
|
||||||
printf("\nEvents 2 - ");
|
|
||||||
::active(window, isActive);
|
|
||||||
}
|
|
||||||
|
|
||||||
void resize(struct Window *window, int width, int height) {
|
|
||||||
printf("Events 2 - ");
|
|
||||||
::resize(window, width, height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void keyboard(struct Window *window, Key key, KeyMod mod, bool isPressed) {
|
|
||||||
printf("Events 2 - ");
|
|
||||||
::keyboard(window, key, mod, isPressed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void char_input(struct Window *window, unsigned int charCode) {
|
|
||||||
printf("Events 2 - ");
|
|
||||||
::char_input(window, charCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_btn(struct Window *window, MouseButton button, KeyMod mod, bool isPressed) {
|
|
||||||
printf("Events 2 - ");
|
|
||||||
::mouse_btn(window, button, mod, isPressed);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_move(struct Window *window, int x, int y) {
|
|
||||||
//printf("Events 2 - ");
|
|
||||||
::mouse_move(window, x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mouse_scroll(struct Window *window, KeyMod mod, float deltaX, float deltaY) {
|
|
||||||
printf("Events 2 - ");
|
|
||||||
::mouse_scroll(window, mod, deltaX, deltaY);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int noise, carry, seed = 0xbeef;
|
int noise, carry, seed = 0xbeef;
|
||||||
int id1 = 1, id2 = 2;
|
|
||||||
|
|
||||||
struct Window *window1 = mfb_open_ex("Noise Test", WIDTH_A, HEIGHT_A, WF_RESIZABLE);
|
struct Window *window = mfb_open_ex("Noise Test", WIDTH, HEIGHT, WF_RESIZABLE);
|
||||||
if (!window1)
|
if (!window)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
mfb_set_user_data(window1, &id1);
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
|
|
||||||
Events e;
|
|
||||||
|
|
||||||
mfb_active_callback(window1, &e, &Events::active);
|
|
||||||
mfb_resize_callback(window1, &e, &Events::resize);
|
|
||||||
mfb_keyboard_callback(window1, &e, &Events::keyboard);
|
|
||||||
mfb_char_input_callback(window1, &e, &Events::char_input);
|
|
||||||
mfb_mouse_button_callback(window1, &e, &Events::mouse_btn);
|
|
||||||
mfb_mouse_move_callback(window1, &e, &Events::mouse_move);
|
|
||||||
mfb_mouse_scroll_callback(window1, &e, &Events::mouse_scroll);
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
mfb_active_callback(window1, active);
|
|
||||||
mfb_resize_callback(window1, resize);
|
|
||||||
mfb_keyboard_callback(window1, keyboard);
|
|
||||||
mfb_char_input_callback(window1, char_input);
|
|
||||||
mfb_mouse_button_callback(window1, mouse_btn);
|
|
||||||
mfb_mouse_move_callback(window1, mouse_move);
|
|
||||||
mfb_mouse_scroll_callback(window1, mouse_scroll);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct Window *window2 = mfb_open_ex("Noise Test", WIDTH_B, HEIGHT_B, WF_RESIZABLE);
|
|
||||||
if (!window2)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
mfb_set_user_data(window2, &id2);
|
|
||||||
|
|
||||||
#if defined(__cplusplus)
|
|
||||||
|
|
||||||
Events2 e2;
|
|
||||||
|
|
||||||
mfb_active_callback(window2, &e2, &Events2::active);
|
|
||||||
mfb_resize_callback(window2, &e2, &Events::resize);
|
|
||||||
mfb_keyboard_callback(window2, &e2, &Events::keyboard);
|
|
||||||
mfb_char_input_callback(window2, &e2, &Events::char_input);
|
|
||||||
mfb_mouse_button_callback(window2, &e2, &Events::mouse_btn);
|
|
||||||
mfb_mouse_move_callback(window2, &e2, &Events::mouse_move);
|
|
||||||
mfb_mouse_scroll_callback(window2, &e2, &Events::mouse_scroll);
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
mfb_active_callback(window2, active2);
|
|
||||||
mfb_resize_callback(window2, resize2);
|
|
||||||
mfb_keyboard_callback(window2, keyboard2);
|
|
||||||
mfb_char_input_callback(window2, char_input2);
|
|
||||||
mfb_mouse_button_callback(window2, mouse_btn2);
|
|
||||||
mfb_mouse_move_callback(window2, mouse_move2);
|
|
||||||
mfb_mouse_scroll_callback(window2, mouse_scroll2);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
mfb_keyboard_callback(window2, 0x0);
|
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
UpdateState state1, state2;
|
UpdateState state;
|
||||||
|
|
||||||
for (i = 0; i < WIDTH_A * HEIGHT_A; ++i)
|
for (i = 0; i < WIDTH * HEIGHT; ++i)
|
||||||
{
|
{
|
||||||
noise = seed;
|
noise = seed;
|
||||||
noise >>= 3;
|
noise >>= 3;
|
||||||
@ -306,36 +31,15 @@ int main()
|
|||||||
seed >>= 1;
|
seed >>= 1;
|
||||||
seed |= (carry << 30);
|
seed |= (carry << 30);
|
||||||
noise &= 0xFF;
|
noise &= 0xFF;
|
||||||
g_buffer1[i] = MFB_RGB(noise, noise, noise);
|
g_buffer[i] = MFB_RGB(noise, noise, noise);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < WIDTH_B * HEIGHT_B; ++i)
|
state = mfb_update(window, g_buffer);
|
||||||
{
|
if (state != STATE_OK) {
|
||||||
noise = seed;
|
window = 0x0;
|
||||||
noise >>= 3;
|
|
||||||
noise ^= seed;
|
|
||||||
carry = noise & 1;
|
|
||||||
noise >>= 1;
|
|
||||||
seed >>= 1;
|
|
||||||
seed |= (carry << 30);
|
|
||||||
noise &= 0xFF;
|
|
||||||
g_buffer2[i] = MFB_RGB(noise, (~noise) & 0xff, 255 - noise);
|
|
||||||
}
|
|
||||||
|
|
||||||
state1 = mfb_update(window1, g_buffer1);
|
|
||||||
state2 = mfb_update(window2, g_buffer2);
|
|
||||||
if (state1 != STATE_OK) {
|
|
||||||
window1 = 0x0;
|
|
||||||
}
|
|
||||||
if (state2 != STATE_OK) {
|
|
||||||
window2 = 0x0;
|
|
||||||
}
|
|
||||||
if (state1 != STATE_OK && state2 != STATE_OK) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mfb_close(window1);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user